diff --git a/TODO.txt b/TODO.txt index db7ffe48..29d1ebac 100644 --- a/TODO.txt +++ b/TODO.txt @@ -6,6 +6,7 @@ Version 0.10 * Remove {`ratio`, `sigma`} deprecation warnings of `skimage.segmentation.slic` * Change default mode of random_walker segmentation to 'cg_mg' > 'cg' > 'bf', depending on which optional dependencies are available. +* Remove deprecated `out` parameter of `skimage.morphology.binary_*` Version 0.9 ----------- diff --git a/skimage/morphology/binary.py b/skimage/morphology/binary.py index e2e0f20b..11a14d49 100644 --- a/skimage/morphology/binary.py +++ b/skimage/morphology/binary.py @@ -1,7 +1,39 @@ +import warnings import numpy as np from scipy import ndimage +def _convolve(image, selem, out, cval): + + # determine the smallest integer dtype which does not overflow + selem = selem != 0 + selem_sum = np.sum(selem) + if selem_sum < 2 ** 8: + out_dtype = np.uint8 + else: + out_dtype = np.intp + + if out is None: + out = np.zeros_like(image, dtype=out_dtype) + else: + warnings.warn('Parameter `out` is deprecated and it does not equal ' + 'the output image if the sum of the structuring element ' + 'overflows the dtype of `out`.') + iinfo = np.iinfo(out.dtype) + if iinfo.max - iinfo.min < selem_sum: + raise ValueError('Sum of structuring (=%d) element results in ' + 'overflow for dtype of `out`. You must raise the ' + 'bit-depth.') + + conv = ndimage.convolve(image > 0, selem, output=out, + mode='constant', cval=cval) + + if conv is not None: + out = conv + + return out, selem_sum + + def binary_erosion(image, selem, out=None): """Return fast binary morphological erosion of an image. @@ -29,11 +61,9 @@ def binary_erosion(image, selem, out=None): """ - conv = ndimage.convolve(image > 0, selem, output=out, - mode='constant', cval=1) - if conv is not None: - out = conv - return np.equal(out, np.sum(selem), out=out) + out, selem_sum = _convolve(image, selem, out, 1) + return np.array(np.equal(out, selem_sum, out=out), dtype=np.bool, + copy=False) def binary_dilation(image, selem, out=None): @@ -64,11 +94,8 @@ def binary_dilation(image, selem, out=None): """ - conv = ndimage.convolve(image > 0, selem, output=out, - mode='constant', cval=0) - if conv is not None: - out = conv - return np.not_equal(out, 0, out=out) + out, _ = _convolve(image, selem, out, 0) + return np.array(np.not_equal(out, 0, out=out), dtype=np.bool, copy=False) def binary_opening(image, selem, out=None): diff --git a/skimage/morphology/tests/test_binary.py b/skimage/morphology/tests/test_binary.py new file mode 100644 index 00000000..5f831669 --- /dev/null +++ b/skimage/morphology/tests/test_binary.py @@ -0,0 +1,70 @@ +import numpy as np +from numpy import testing + +from skimage import data, color +from skimage.util import img_as_bool +from skimage.morphology import binary, grey, selem + + +lena = color.rgb2gray(data.lena()) +bw_lena = lena > 100 + + +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_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_selem_overflow_exception(): + strel = np.ones((17, 17), dtype=np.uint8) + img = np.zeros((20, 20)) + img[2:19, 2:19] = 1 + out = np.zeros((20, 20), dtype=np.uint8) + testing.assert_raises(ValueError, binary.binary_erosion, img, strel, out) + out = np.zeros((20, 20), dtype=np.uint16) + binary_res = binary.binary_erosion(img, strel, out=out) + grey_res = img_as_bool(grey.erosion(img, 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_dilation(): + strel = selem.square(3) + binary_res = binary.binary_dilation(bw_lena, strel) + grey_res = img_as_bool(grey.dilation(bw_lena, strel)) + testing.assert_array_equal(binary_res, grey_res) + + +def test_binary_closing(): + strel = selem.square(3) + binary_res = binary.binary_closing(bw_lena, strel) + grey_res = img_as_bool(grey.closing(bw_lena, strel)) + testing.assert_array_equal(binary_res, grey_res) + + +def test_binary_opening(): + strel = selem.square(3) + binary_res = binary.binary_opening(bw_lena, strel) + grey_res = img_as_bool(grey.opening(bw_lena, strel)) + testing.assert_array_equal(binary_res, grey_res) + + +if __name__ == '__main__': + testing.run_module_suite() diff --git a/skimage/morphology/tests/test_grey.py b/skimage/morphology/tests/test_grey.py index 244ec566..e2a3928d 100644 --- a/skimage/morphology/tests/test_grey.py +++ b/skimage/morphology/tests/test_grey.py @@ -6,7 +6,7 @@ from numpy import testing import skimage from skimage import data_dir from skimage.util import img_as_bool -from skimage.morphology import binary, grey, selem +from skimage.morphology import grey, selem lena = np.load(os.path.join(data_dir, 'lena_GRAY_U8.npy')) @@ -155,40 +155,5 @@ class TestDTypes(): self._test_image(image) -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_dilation(): - strel = selem.square(3) - binary_res = binary.binary_dilation(bw_lena, strel) - grey_res = img_as_bool(grey.dilation(bw_lena, strel)) - testing.assert_array_equal(binary_res, grey_res) - - -def test_binary_closing(): - strel = selem.square(3) - binary_res = binary.binary_closing(bw_lena, strel) - grey_res = img_as_bool(grey.closing(bw_lena, strel)) - testing.assert_array_equal(binary_res, grey_res) - - -def test_binary_opening(): - strel = selem.square(3) - binary_res = binary.binary_opening(bw_lena, strel) - grey_res = img_as_bool(grey.opening(bw_lena, strel)) - testing.assert_array_equal(binary_res, grey_res) - - if __name__ == '__main__': testing.run_module_suite()