diff --git a/skimage/morphology/misc.py b/skimage/morphology/misc.py index 181fe5a4..94aadf7a 100644 --- a/skimage/morphology/misc.py +++ b/skimage/morphology/misc.py @@ -8,15 +8,16 @@ funcs = ('erosion', 'dilation', 'opening', 'closing') skimage2ndimage = {x: 'grey_' + x for x in funcs} # These function names are the same in ndimage. -funcs = ('binary_erosion', 'binary_dilation', 'binary_opening', +funcs = ('binary_erosion', 'binary_dilation', 'binary_opening', 'binary_closing', 'black_tophat', 'white_tophat') skimage2ndimage.update({x: x for x in funcs}) def default_fallback(func): """Decorator to fall back on ndimage for images with more than 2 dimensions + Decorator also provides a default structuring element, `selem`, with the appropriate dimensionality if none is specified. - + Parameters ---------- func : function @@ -29,7 +30,7 @@ def default_fallback(func): If the image dimentionality is greater than 2D, the ndimage function is returned, otherwise skimage function is used. """ - + def func_out(image, selem=None, out=None, **kwargs): # Default structure element if selem is None: diff --git a/skimage/morphology/tests/test_binary.py b/skimage/morphology/tests/test_binary.py index e0db7416..a8284103 100644 --- a/skimage/morphology/tests/test_binary.py +++ b/skimage/morphology/tests/test_binary.py @@ -110,26 +110,42 @@ def test_3d_fallback_cube_selem(): new_image = function(image, cube) yield testing.assert_array_equal, new_image, image +def test_2d_ndimage_equivalence(): + image = np.zeros((9, 9), np.uint16) + image[2:-2, 2:-2] = 2**14 + image[3:-3, 3:-3] = 2**15 + image[4, 4] = 2**16-1 + + bin_opened = binary.binary_opening(image) + bin_closed = binary.binary_closing(image) + + selem = ndimage.generate_binary_structure(2, 1) + ndimage_opened = ndimage.binary_opening(image, structure=selem) + ndimage_closed = ndimage.binary_closing(image, structure=selem) + + testing.assert_array_equal(bin_opened, ndimage_opened) + testing.assert_array_equal(bin_closed, ndimage_closed) + def test_binary_output_2d(): image = np.zeros((9, 9), np.uint16) image[2:-2, 2:-2] = 2**14 image[3:-3, 3:-3] = 2**15 image[4, 4] = 2**16-1 - + bin_opened = binary.binary_opening(image) bin_closed = binary.binary_closing(image) - + int_opened = np.empty_like(image, dtype=np.uint8) int_closed = np.empty_like(image, dtype=np.uint8) binary.binary_opening(image, out=int_opened) binary.binary_closing(image, out=int_closed) - + testing.assert_equal(bin_opened.dtype, np.bool) testing.assert_equal(bin_closed.dtype, np.bool) - + testing.assert_equal(int_opened.dtype, np.uint8) testing.assert_equal(int_closed.dtype, np.uint8) - + def test_binary_output_3d(): image = np.zeros((9, 9, 9), np.uint16) image[2:-2, 2:-2, 2:-2] = 2**14 @@ -138,17 +154,17 @@ def test_binary_output_3d(): bin_opened = binary.binary_opening(image) bin_closed = binary.binary_closing(image) - + int_opened = np.empty_like(image, dtype=np.uint8) int_closed = np.empty_like(image, dtype=np.uint8) binary.binary_opening(image, out=int_opened) binary.binary_closing(image, out=int_closed) - + testing.assert_equal(bin_opened.dtype, np.bool) testing.assert_equal(bin_closed.dtype, np.bool) - + testing.assert_equal(int_opened.dtype, np.uint8) - testing.assert_equal(int_closed.dtype, np.uint8) + testing.assert_equal(int_closed.dtype, np.uint8) if __name__ == '__main__': testing.run_module_suite() diff --git a/skimage/morphology/tests/test_grey.py b/skimage/morphology/tests/test_grey.py index f9a7305d..9f8e95b3 100644 --- a/skimage/morphology/tests/test_grey.py +++ b/skimage/morphology/tests/test_grey.py @@ -175,7 +175,7 @@ def test_3d_fallback_white_tophat(): footprint = ndimage.generate_binary_structure(3,1) image_expected = ndimage.white_tophat(image,footprint=footprint) testing.assert_array_equal(new_image, image_expected) - + def test_3d_fallback_black_tophat(): image = np.ones((7, 7, 7), dtype=bool) image[2, 2:4, 2:4] = 0 @@ -186,6 +186,22 @@ def test_3d_fallback_black_tophat(): image_expected = ndimage.black_tophat(image,footprint=footprint) testing.assert_array_equal(new_image, image_expected) +def test_2d_ndimage_equivalence(): + image = np.zeros((9, 9), np.uint16) + image[2:-2, 2:-2] = 2**14 + image[3:-3, 3:-3] = 2**15 + image[4, 4] = 2**16-1 + + opened = grey.opening(image) + closed = grey.closing(image) + + selem = ndimage.generate_binary_structure(2, 1) + ndimage_opened = ndimage.grey_opening(image, structure=selem) + ndimage_closed = ndimage.grey_closing(image, structure=selem) + + testing.assert_array_equal(opened, ndimage_opened) + testing.assert_array_equal(closed, ndimage_closed) + class TestDTypes(): def setUp(self):