def test_discontiguous_out_array(): image = np.array([[5, 6, 2], [7, 2, 2], [3, 5, 1]], np.uint8) out_array_big = np.zeros((5, 5), np.uint8) out_array = out_array_big[::2, ::2] expected_dilation = np.array( [[7, 0, 6, 0, 6], [0, 0, 0, 0, 0], [7, 0, 7, 0, 2], [0, 0, 0, 0, 0], [7, 0, 5, 0, 5]], np.uint8) expected_erosion = np.array( [[5, 0, 2, 0, 2], [0, 0, 0, 0, 0], [2, 0, 2, 0, 1], [0, 0, 0, 0, 0], [3, 0, 1, 0, 1]], np.uint8) grey.dilation(image, out=out_array) assert_array_equal(out_array_big, expected_dilation) grey.erosion(image, out=out_array) testing.assert_array_equal(out_array_big, expected_erosion)
def find_peaks_in_subspace(self, subspace, t_abs, prominence, min_dist): """ Retrieve locations with prominent local maxima from one part of the accumulator """ de = dilation(subspace + 1, ) / erosion(subspace + 1) p = peak_local_max(subspace, threshold_abs=t_abs, min_distance=min_dist, exclude_border=False) r, c = p[:, 0], p[:, 1] v = subspace[r, c] valid = de[r, c] > prominence peaks = np.empty([np.sum(valid), 2], np.float64) values = v[valid] # Subpixel correction of the peak s = np.pad(subspace, ((1, 1), (1, 1)), constant_values=((0, 0), (0, 0))) for i, pi in enumerate(p[valid]): neighborhood = s[pi[0]:pi[0] + 3, pi[1]:pi[1] + 3] neighborhood = neighborhood / np.sum(neighborhood) weight_r = np.tile(np.array([-1.0, 0, 1]), (3, 1)).T + pi[0] weight_c = np.tile(np.array([-1.0, 0, 1]), (3, 1)) + pi[1] peaks[i, 1] = np.sum(neighborhood * weight_r).astype(float) peaks[i, 0] = np.sum(neighborhood * weight_c).astype(float) return peaks, values
def test_selem_overflow(): strel = np.ones((17, 17), dtype=np.uint8) img = np.zeros((20, 20), dtype=bool) img[2:19, 2:19] = True binary_res = binary.binary_erosion(img, strel) grey_res = img_as_bool(grey.erosion(img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_uint16(): im16, eroded16, dilated16, opened16, closed16 = ( map(img_as_uint, [im, eroded, dilated, opened, closed])) np.testing.assert_allclose(grey.erosion(im16), eroded16) np.testing.assert_allclose(grey.dilation(im16), dilated16) np.testing.assert_allclose(grey.opening(im16), opened16) np.testing.assert_allclose(grey.closing(im16), closed16)
def test_selem_overflow(): strel = np.ones((17, 17), dtype=np.uint8) img = np.zeros((20, 20)) img[2:19, 2:19] = 1 binary_res = binary.binary_erosion(img, strel) grey_res = img_as_bool(grey.erosion(img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_uint16(): with expected_warnings(['Possible precision loss']): im16, eroded16, dilated16, opened16, closed16 = (map( img_as_uint, [im, eroded, dilated, opened, closed])) np.testing.assert_allclose(grey.erosion(im16), eroded16) np.testing.assert_allclose(grey.dilation(im16), dilated16) np.testing.assert_allclose(grey.opening(im16), opened16) np.testing.assert_allclose(grey.closing(im16), closed16)
def test_selem_overflow(): strel = np.ones((17, 17), dtype=np.uint8) img = np.zeros((20, 20)) img[2:19, 2:19] = 1 binary_res = binary.binary_erosion(img, strel) with expected_warnings(['precision loss']): grey_res = img_as_bool(grey.erosion(img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_uint16(): with expected_warnings(['Possible precision loss']): im16, eroded16, dilated16, opened16, closed16 = ( map(img_as_uint, [im, eroded, dilated, opened, closed])) np.testing.assert_allclose(grey.erosion(im16), eroded16) np.testing.assert_allclose(grey.dilation(im16), dilated16) np.testing.assert_allclose(grey.opening(im16), opened16) np.testing.assert_allclose(grey.closing(im16), closed16)
def test_discontiguous_out_array(): image = np.array([[5, 6, 2], [7, 2, 2], [3, 5, 1]], np.uint8) out_array_big = np.zeros((5, 5), np.uint8) out_array = out_array_big[::2, ::2] expected_dilation = np.array([[7, 0, 6, 0, 6], [0, 0, 0, 0, 0], [7, 0, 7, 0, 2], [0, 0, 0, 0, 0], [7, 0, 5, 0, 5]], np.uint8) expected_erosion = np.array([[5, 0, 2, 0, 2], [0, 0, 0, 0, 0], [2, 0, 2, 0, 1], [0, 0, 0, 0, 0], [3, 0, 1, 0, 1]], np.uint8) grey.dilation(image, out=out_array) testing.assert_array_equal(out_array_big, expected_dilation) grey.erosion(image, out=out_array) testing.assert_array_equal(out_array_big, expected_erosion)
def test_compare_with_grey_erosion(): # compare the result of maximum filter with erode image = (np.random.rand(100, 100) * 256).astype(np.uint8) out = np.empty_like(image) mask = np.ones(image.shape, dtype=np.uint8) for r in range(3, 20, 2): elem = np.ones((r, r), dtype=np.uint8) rank.minimum(image=image, selem=elem, out=out, mask=mask) cm = grey.erosion(image=image, selem=elem) assert_equal(out, cm)
def test_float(): np.testing.assert_allclose(grey.erosion(im), eroded) np.testing.assert_allclose(grey.dilation(im), dilated) np.testing.assert_allclose(grey.opening(im), opened) np.testing.assert_allclose(grey.closing(im), closed)
def test_dilate_erode_symmetry(self): for s in self.selems: c = grey.erosion(self.black_pixel, s) d = grey.dilation(self.white_pixel, s) assert np.all(c == (255 - d))
def test_1d_erosion(): image = np.array([1, 2, 3, 2, 1]) expected = np.array([1, 1, 2, 1, 1]) eroded = grey.erosion(image) testing.assert_array_equal(eroded, expected)
def test_binary_erosion(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_img, strel) with expected_warnings(['precision loss']): grey_res = img_as_bool(grey.erosion(bw_img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_binary_erosion(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_img, strel) grey_res = img_as_bool(grey.erosion(bw_img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_non_square_image(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_img[:100, :200], strel) grey_res = img_as_bool(grey.erosion(bw_img[:100, :200], strel)) testing.assert_array_equal(binary_res, grey_res)
def test_non_square_image(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_lena[:100, :200], strel) grey_res = img_as_bool(grey.erosion(bw_lena[:100, :200], strel)) testing.assert_array_equal(binary_res, grey_res)
def test_binary_erosion(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_lena, strel) grey_res = img_as_bool(grey.erosion(bw_lena, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_binary_erosion(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_lena, strel) grey_res = grey.erosion(bw_lena, strel) testing.assert_array_equal(binary_res, grey_res)