Change binary morphology functions to output numpy.bool dtype

This commit is contained in:
Nelson Brown
2014-07-13 19:59:43 -07:00
parent b677c400df
commit a1519a015e
2 changed files with 42 additions and 2 deletions
+2 -2
View File
@@ -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)
+40
View File
@@ -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()