diff --git a/skimage/morphology/binary.py b/skimage/morphology/binary.py index 5324b0f5..69d3eb08 100644 --- a/skimage/morphology/binary.py +++ b/skimage/morphology/binary.py @@ -49,7 +49,7 @@ def binary_erosion(image, selem=None, out=None): ndimage.convolve(binary, selem, mode='constant', cval=1, output=conv) if out is None: - out = conv + out = np.empty_like(conv, dtype=np.bool) return np.equal(conv, selem_sum, out=out) @@ -98,7 +98,7 @@ def binary_dilation(image, selem=None, out=None): ndimage.convolve(binary, selem, mode='constant', cval=0, output=conv) if out is None: - out = conv + out = np.empty_like(conv, dtype=np.bool) return np.not_equal(conv, 0, out=out) diff --git a/skimage/morphology/tests/test_binary.py b/skimage/morphology/tests/test_binary.py index c0c4c682..e0db7416 100644 --- a/skimage/morphology/tests/test_binary.py +++ b/skimage/morphology/tests/test_binary.py @@ -110,5 +110,45 @@ def test_3d_fallback_cube_selem(): new_image = function(image, cube) yield testing.assert_array_equal, new_image, image +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 + image[3:-3, 3:-3, 3:-3] = 2**15 + image[4, 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) + if __name__ == '__main__': testing.run_module_suite()