Exemplo n.º 1
0
def test_empty_selem():
    # check that min, max and mean returns zeros if structuring element is
    # empty

    image = np.zeros((5, 5), dtype=np.uint16)
    out = np.zeros_like(image)
    mask = np.ones_like(image, dtype=np.uint8)
    res = np.zeros_like(image)
    image[2, 2] = 255
    image[2, 3] = 128
    image[1, 2] = 16

    elem = np.array([[0, 0, 0], [0, 0, 0]], dtype=np.uint8)

    rank.mean(image=image, selem=elem, out=out, mask=mask,
              shift_x=0, shift_y=0)
    assert_equal(res, out)
    rank.geometric_mean(image=image, selem=elem, out=out, mask=mask,
                        shift_x=0, shift_y=0)
    assert_equal(res, out)
    rank.minimum(image=image, selem=elem, out=out, mask=mask,
                 shift_x=0, shift_y=0)
    assert_equal(res, out)
    rank.maximum(image=image, selem=elem, out=out, mask=mask,
                 shift_x=0, shift_y=0)
    assert_equal(res, out)
Exemplo n.º 2
0
    def test_empty_selem(self):
        # check that min, max and mean returns zeros if structuring element is
        # empty

        image = np.zeros((5, 5), dtype=np.uint16)
        out = np.zeros_like(image)
        mask = np.ones_like(image, dtype=np.uint8)
        res = np.zeros_like(image)
        image[2, 2] = 255
        image[2, 3] = 128
        image[1, 2] = 16

        elem = np.array([[0, 0, 0], [0, 0, 0]], dtype=np.uint8)

        rank.mean(image=image, selem=elem, out=out, mask=mask,
                  shift_x=0, shift_y=0)
        assert_equal(res, out)
        rank.geometric_mean(image=image, selem=elem, out=out, mask=mask,
                            shift_x=0, shift_y=0)
        assert_equal(res, out)
        rank.minimum(image=image, selem=elem, out=out, mask=mask,
                     shift_x=0, shift_y=0)
        assert_equal(res, out)
        rank.maximum(image=image, selem=elem, out=out, mask=mask,
                     shift_x=0, shift_y=0)
        assert_equal(res, out)
Exemplo n.º 3
0
    def test_selem_dtypes(self):

        image = np.zeros((5, 5), dtype=np.uint8)
        out = np.zeros_like(image)
        mask = np.ones_like(image, dtype=np.uint8)
        image[2, 2] = 255
        image[2, 3] = 128
        image[1, 2] = 16

        for dtype in (bool, np.uint8, np.uint16, np.int32, np.int64,
                      np.float32, np.float64):
            elem = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=dtype)
            rank.mean(image=image,
                      selem=elem,
                      out=out,
                      mask=mask,
                      shift_x=0,
                      shift_y=0)
            assert_equal(image, out)
            rank.geometric_mean(image=image,
                                selem=elem,
                                out=out,
                                mask=mask,
                                shift_x=0,
                                shift_y=0)
            assert_equal(image, out)
            rank.mean_percentile(image=image,
                                 selem=elem,
                                 out=out,
                                 mask=mask,
                                 shift_x=0,
                                 shift_y=0)
            assert_equal(image, out)
Exemplo n.º 4
0
        def check_all():
            selem = morphology.disk(1)
            refs = np.load(
                os.path.join(skimage.data_dir, "rank_filter_tests.npz"))

            assert_equal(refs["autolevel"], rank.autolevel(self.image, selem))
            assert_equal(refs["autolevel_percentile"],
                         rank.autolevel_percentile(self.image, selem))
            assert_equal(refs["bottomhat"], rank.bottomhat(self.image, selem))
            assert_equal(refs["equalize"], rank.equalize(self.image, selem))
            assert_equal(refs["gradient"], rank.gradient(self.image, selem))
            assert_equal(refs["gradient_percentile"],
                         rank.gradient_percentile(self.image, selem))
            assert_equal(refs["maximum"], rank.maximum(self.image, selem))
            assert_equal(refs["mean"], rank.mean(self.image, selem))
            assert_equal(refs["geometric_mean"],
                         rank.geometric_mean(self.image, selem)),
            assert_equal(refs["mean_percentile"],
                         rank.mean_percentile(self.image, selem))
            assert_equal(refs["mean_bilateral"],
                         rank.mean_bilateral(self.image, selem))
            assert_equal(refs["subtract_mean"],
                         rank.subtract_mean(self.image, selem))
            assert_equal(refs["subtract_mean_percentile"],
                         rank.subtract_mean_percentile(self.image, selem))
            assert_equal(refs["median"], rank.median(self.image, selem))
            assert_equal(refs["minimum"], rank.minimum(self.image, selem))
            assert_equal(refs["modal"], rank.modal(self.image, selem))
            assert_equal(refs["enhance_contrast"],
                         rank.enhance_contrast(self.image, selem))
            assert_equal(refs["enhance_contrast_percentile"],
                         rank.enhance_contrast_percentile(self.image, selem))
            assert_equal(refs["pop"], rank.pop(self.image, selem))
            assert_equal(refs["pop_percentile"],
                         rank.pop_percentile(self.image, selem))
            assert_equal(refs["pop_bilateral"],
                         rank.pop_bilateral(self.image, selem))
            assert_equal(refs["sum"], rank.sum(self.image, selem))
            assert_equal(refs["sum_bilateral"],
                         rank.sum_bilateral(self.image, selem))
            assert_equal(refs["sum_percentile"],
                         rank.sum_percentile(self.image, selem))
            assert_equal(refs["threshold"], rank.threshold(self.image, selem))
            assert_equal(refs["threshold_percentile"],
                         rank.threshold_percentile(self.image, selem))
            assert_equal(refs["tophat"], rank.tophat(self.image, selem))
            assert_equal(refs["noise_filter"],
                         rank.noise_filter(self.image, selem))
            assert_equal(refs["entropy"], rank.entropy(self.image, selem))
            assert_equal(refs["otsu"], rank.otsu(self.image, selem))
            assert_equal(refs["percentile"],
                         rank.percentile(self.image, selem))
            assert_equal(refs["windowed_histogram"],
                         rank.windowed_histogram(self.image, selem))
Exemplo n.º 5
0
def test_selem_dtypes():

    image = np.zeros((5, 5), dtype=np.uint8)
    out = np.zeros_like(image)
    mask = np.ones_like(image, dtype=np.uint8)
    image[2, 2] = 255
    image[2, 3] = 128
    image[1, 2] = 16

    for dtype in (np.uint8, np.uint16, np.int32, np.int64,
                  np.float32, np.float64):
        elem = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=dtype)
        rank.mean(image=image, selem=elem, out=out, mask=mask,
                  shift_x=0, shift_y=0)
        assert_equal(image, out)
        rank.geometric_mean(image=image, selem=elem, out=out, mask=mask,
                            shift_x=0, shift_y=0)
        assert_equal(image, out)
        rank.mean_percentile(image=image, selem=elem, out=out, mask=mask,
                             shift_x=0, shift_y=0)
        assert_equal(image, out)
Exemplo n.º 6
0
    def test_smallest_selem16(self):
        # check that min, max and mean returns identity if structuring element
        # contains only central pixel

        image = np.zeros((5, 5), dtype=np.uint16)
        out = np.zeros_like(image)
        mask = np.ones_like(image, dtype=np.uint8)
        image[2, 2] = 255
        image[2, 3] = 128
        image[1, 2] = 16

        elem = np.array([[1]], dtype=np.uint8)
        rank.mean(image=image,
                  selem=elem,
                  out=out,
                  mask=mask,
                  shift_x=0,
                  shift_y=0)
        assert_equal(image, out)
        rank.geometric_mean(image=image,
                            selem=elem,
                            out=out,
                            mask=mask,
                            shift_x=0,
                            shift_y=0)
        assert_equal(image, out)
        rank.minimum(image=image,
                     selem=elem,
                     out=out,
                     mask=mask,
                     shift_x=0,
                     shift_y=0)
        assert_equal(image, out)
        rank.maximum(image=image,
                     selem=elem,
                     out=out,
                     mask=mask,
                     shift_x=0,
                     shift_y=0)
        assert_equal(image, out)
Exemplo n.º 7
0
    def test_trivial_footprint8(self):
        # check that min, max and mean returns identity if footprint
        # contains only central pixel

        image = np.zeros((5, 5), dtype=np.uint8)
        out = np.zeros_like(image)
        mask = np.ones_like(image, dtype=np.uint8)
        image[2, 2] = 255
        image[2, 3] = 128
        image[1, 2] = 16

        elem = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.uint8)
        rank.mean(image=image,
                  footprint=elem,
                  out=out,
                  mask=mask,
                  shift_x=0,
                  shift_y=0)
        assert_equal(image, out)
        rank.geometric_mean(image=image,
                            footprint=elem,
                            out=out,
                            mask=mask,
                            shift_x=0,
                            shift_y=0)
        assert_equal(image, out)
        rank.minimum(image=image,
                     footprint=elem,
                     out=out,
                     mask=mask,
                     shift_x=0,
                     shift_y=0)
        assert_equal(image, out)
        rank.maximum(image=image,
                     footprint=elem,
                     out=out,
                     mask=mask,
                     shift_x=0,
                     shift_y=0)
        assert_equal(image, out)
Exemplo n.º 8
0
def test_smallest_selem16():
    # check that min, max and mean returns identity if structuring element
    # contains only central pixel

    image = np.zeros((5, 5), dtype=np.uint16)
    out = np.zeros_like(image)
    mask = np.ones_like(image, dtype=np.uint8)
    image[2, 2] = 255
    image[2, 3] = 128
    image[1, 2] = 16

    elem = np.array([[1]], dtype=np.uint8)
    rank.mean(image=image, selem=elem, out=out, mask=mask,
              shift_x=0, shift_y=0)
    assert_equal(image, out)
    rank.geometric_mean(image=image, selem=elem, out=out, mask=mask,
                        shift_x=0, shift_y=0)
    assert_equal(image, out)
    rank.minimum(image=image, selem=elem, out=out, mask=mask,
                 shift_x=0, shift_y=0)
    assert_equal(image, out)
    rank.maximum(image=image, selem=elem, out=out, mask=mask,
                 shift_x=0, shift_y=0)
    assert_equal(image, out)
Exemplo n.º 9
0
def test_random_sizes():
    # make sure the size is not a problem

    elem = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8)
    for m, n in np.random.random_integers(1, 100, size=(10, 2)):
        mask = np.ones((m, n), dtype=np.uint8)

        image8 = np.ones((m, n), dtype=np.uint8)
        out8 = np.empty_like(image8)
        rank.mean(image=image8, selem=elem, mask=mask, out=out8,
                  shift_x=0, shift_y=0)
        assert_equal(image8.shape, out8.shape)
        rank.mean(image=image8, selem=elem, mask=mask, out=out8,
                  shift_x=+1, shift_y=+1)
        assert_equal(image8.shape, out8.shape)
        
        rank.geometric_mean(image=image8, selem=elem, mask=mask, out=out8,
                            shift_x=0, shift_y=0)
        assert_equal(image8.shape, out8.shape)
        rank.geometric_mean(image=image8, selem=elem, mask=mask, out=out8,
                            shift_x=+1, shift_y=+1)
        assert_equal(image8.shape, out8.shape)

        image16 = np.ones((m, n), dtype=np.uint16)
        out16 = np.empty_like(image8, dtype=np.uint16)
        rank.mean(image=image16, selem=elem, mask=mask, out=out16,
                  shift_x=0, shift_y=0)
        assert_equal(image16.shape, out16.shape)
        rank.mean(image=image16, selem=elem, mask=mask, out=out16,
                  shift_x=+1, shift_y=+1)
        assert_equal(image16.shape, out16.shape)

        rank.geometric_mean(image=image16, selem=elem, mask=mask, out=out16,
                            shift_x=0, shift_y=0)
        assert_equal(image16.shape, out16.shape)
        rank.geometric_mean(image=image16, selem=elem, mask=mask, out=out16,
                            shift_x=+1, shift_y=+1)
        assert_equal(image16.shape, out16.shape)

        rank.mean_percentile(image=image16, mask=mask, out=out16,
                             selem=elem, shift_x=0, shift_y=0, p0=.1, p1=.9)
        assert_equal(image16.shape, out16.shape)
        rank.mean_percentile(image=image16, mask=mask, out=out16,
                             selem=elem, shift_x=+1, shift_y=+1, p0=.1, p1=.9)
        assert_equal(image16.shape, out16.shape)
Exemplo n.º 10
0
    def test_random_sizes(self):
        # make sure the size is not a problem

        elem = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8)
        for m, n in np.random.randint(1, 101, size=(10, 2)):
            mask = np.ones((m, n), dtype=np.uint8)

            image8 = np.ones((m, n), dtype=np.uint8)
            out8 = np.empty_like(image8)
            rank.mean(image=image8, selem=elem, mask=mask, out=out8,
                      shift_x=0, shift_y=0)
            assert_equal(image8.shape, out8.shape)
            rank.mean(image=image8, selem=elem, mask=mask, out=out8,
                      shift_x=+1, shift_y=+1)
            assert_equal(image8.shape, out8.shape)

            rank.geometric_mean(image=image8, selem=elem, mask=mask, out=out8,
                                shift_x=0, shift_y=0)
            assert_equal(image8.shape, out8.shape)
            rank.geometric_mean(image=image8, selem=elem, mask=mask, out=out8,
                                shift_x=+1, shift_y=+1)
            assert_equal(image8.shape, out8.shape)

            image16 = np.ones((m, n), dtype=np.uint16)
            out16 = np.empty_like(image8, dtype=np.uint16)
            rank.mean(image=image16, selem=elem, mask=mask, out=out16,
                      shift_x=0, shift_y=0)
            assert_equal(image16.shape, out16.shape)
            rank.mean(image=image16, selem=elem, mask=mask, out=out16,
                      shift_x=+1, shift_y=+1)
            assert_equal(image16.shape, out16.shape)

            rank.geometric_mean(image=image16, selem=elem, mask=mask, out=out16,
                                shift_x=0, shift_y=0)
            assert_equal(image16.shape, out16.shape)
            rank.geometric_mean(image=image16, selem=elem, mask=mask, out=out16,
                                shift_x=+1, shift_y=+1)
            assert_equal(image16.shape, out16.shape)

            rank.mean_percentile(image=image16, mask=mask, out=out16,
                                 selem=elem, shift_x=0, shift_y=0, p0=.1, p1=.9)
            assert_equal(image16.shape, out16.shape)
            rank.mean_percentile(image=image16, mask=mask, out=out16,
                                 selem=elem, shift_x=+1, shift_y=+1, p0=.1, p1=.9)
            assert_equal(image16.shape, out16.shape)
Exemplo n.º 11
0
    def calculate_features(imfile):
        #these features are based on validation results that showed these four
        #features were the most important out of a set of ~30 features
        #for the classification of "good" and "bad" patches. although
        #restricting ourselves to only these 4 features may result in
        #slightly lower prediction accuracy, the time it takes to calculate
        #30 features on ~1 million images is excessive
        #call .compute() for dask array element
        image = io.imread(imfile.compute())

        features = []
        #first lbp stdev
        features.append(local_binary_pattern(image, 8, 8).std())

        #second median of geo. mean
        features.append(np.median(geometric_mean(image, square(11))))

        #third stdev of entropy
        features.append(entropy(image, square(7)).std())

        #fourth mean of the canny filter
        features.append(canny(image, sigma=1).mean())
        return features
Exemplo n.º 12
0
def check_all():
    np.random.seed(0)
    image = np.random.rand(25, 25)
    selem = morphology.disk(1)
    refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz"))

    assert_equal(refs["autolevel"],
                 rank.autolevel(image, selem))
    assert_equal(refs["autolevel_percentile"],
                 rank.autolevel_percentile(image, selem))
    assert_equal(refs["bottomhat"],
                 rank.bottomhat(image, selem))
    assert_equal(refs["equalize"],
                 rank.equalize(image, selem))
    assert_equal(refs["gradient"],
                 rank.gradient(image, selem))
    assert_equal(refs["gradient_percentile"],
                 rank.gradient_percentile(image, selem))
    assert_equal(refs["maximum"],
                 rank.maximum(image, selem))
    assert_equal(refs["mean"],
                 rank.mean(image, selem))
    assert_equal(refs["geometric_mean"],
                 rank.geometric_mean(image, selem)),
    assert_equal(refs["mean_percentile"],
                 rank.mean_percentile(image, selem))
    assert_equal(refs["mean_bilateral"],
                 rank.mean_bilateral(image, selem))
    assert_equal(refs["subtract_mean"],
                 rank.subtract_mean(image, selem))
    assert_equal(refs["subtract_mean_percentile"],
                 rank.subtract_mean_percentile(image, selem))
    assert_equal(refs["median"],
                 rank.median(image, selem))
    assert_equal(refs["minimum"],
                 rank.minimum(image, selem))
    assert_equal(refs["modal"],
                 rank.modal(image, selem))
    assert_equal(refs["enhance_contrast"],
                 rank.enhance_contrast(image, selem))
    assert_equal(refs["enhance_contrast_percentile"],
                 rank.enhance_contrast_percentile(image, selem))
    assert_equal(refs["pop"],
                 rank.pop(image, selem))
    assert_equal(refs["pop_percentile"],
                 rank.pop_percentile(image, selem))
    assert_equal(refs["pop_bilateral"],
                 rank.pop_bilateral(image, selem))
    assert_equal(refs["sum"],
                 rank.sum(image, selem))
    assert_equal(refs["sum_bilateral"],
                 rank.sum_bilateral(image, selem))
    assert_equal(refs["sum_percentile"],
                 rank.sum_percentile(image, selem))
    assert_equal(refs["threshold"],
                 rank.threshold(image, selem))
    assert_equal(refs["threshold_percentile"],
                 rank.threshold_percentile(image, selem))
    assert_equal(refs["tophat"],
                 rank.tophat(image, selem))
    assert_equal(refs["noise_filter"],
                 rank.noise_filter(image, selem))
    assert_equal(refs["entropy"],
                 rank.entropy(image, selem))
    assert_equal(refs["otsu"],
                 rank.otsu(image, selem))
    assert_equal(refs["percentile"],
                 rank.percentile(image, selem))
    assert_equal(refs["windowed_histogram"],
                 rank.windowed_histogram(image, selem))