Restore @ahojnnes's overflow test. Correctly assign out argument.

This commit is contained in:
Stefan van der Walt
2013-10-12 19:44:14 +02:00
parent 9ff4316cbb
commit 9329d0ad56
2 changed files with 21 additions and 6 deletions
+12 -6
View File
@@ -32,13 +32,16 @@ def binary_erosion(image, selem, out=None):
selem = (selem != 0)
selem_sum = np.sum(selem)
if selem_sum > 255:
if selem_sum <= 255:
binary = (image != 0).view(np.uint8)
else:
binary = (image != 0).astype(np.intp)
out = ndimage.convolve(binary, selem, mode='constant', cval=1)
return np.equal(out, selem_sum, out=out)
conv = ndimage.convolve(binary, selem, mode='constant', cval=1)
if out is None:
out = np.zeros_like(binary, dtype=bool)
return np.equal(conv, selem_sum, out=out)
def binary_dilation(image, selem, out=None):
@@ -69,13 +72,16 @@ def binary_dilation(image, selem, out=None):
"""
selem = (selem != 0)
if np.sum(selem) > 255:
if np.sum(selem) <= 255:
binary = (image != 0).view(np.uint8)
else:
binary = (image != 0).astype(np.intp)
out = ndimage.convolve(binary, selem, mode='constant', cval=0)
return np.not_equal(out, 0, out=out)
conv = ndimage.convolve(binary, selem, mode='constant', cval=0)
if out is None:
out = np.zeros_like(binary, dtype=bool)
return np.not_equal(conv, 0, out=out)
def binary_opening(image, selem, out=None):
+9
View File
@@ -45,5 +45,14 @@ def test_binary_opening():
testing.assert_array_equal(binary_res, grey_res)
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)
if __name__ == '__main__':
testing.run_module_suite()