From b1d99be7352bbaa4e036858e32c27943a6998dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 11 Sep 2012 07:41:53 +0200 Subject: [PATCH 1/3] Fix bug in grey erosion and dilation --- skimage/morphology/cmorph.pyx | 6 +++--- skimage/morphology/tests/test_grey.py | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/skimage/morphology/cmorph.pyx b/skimage/morphology/cmorph.pyx index fb1e3af1..9b8b3a27 100644 --- a/skimage/morphology/cmorph.pyx +++ b/skimage/morphology/cmorph.pyx @@ -51,7 +51,7 @@ def dilate(np.ndarray[np.uint8_t, ndim=2] image, rr = r + sr[s] cc = c + sc[s] if 0 <= rr < rows and 0 <= cc < cols: - value = image_data[rr * rows + cc] + value = image_data[rr * cols + cc] if value > local_max: local_max = value @@ -85,7 +85,7 @@ def erode(np.ndarray[np.uint8_t, ndim=2] image, cdef np.uint8_t* out_data = out.data cdef np.uint8_t* image_data = image.data - cdef int r, c, rr, cc, s, value, local_max + cdef int r, c, rr, cc, s, value, local_min cdef int selem_num = np.sum(selem != 0) cdef int* sr = malloc(selem_num * sizeof(int)) @@ -106,7 +106,7 @@ def erode(np.ndarray[np.uint8_t, ndim=2] image, rr = r + sr[s] cc = c + sc[s] if 0 <= rr < rows and 0 <= cc < cols: - value = image_data[rr * rows + cc] + value = image_data[rr * cols + cc] if value < local_min: local_min = value diff --git a/skimage/morphology/tests/test_grey.py b/skimage/morphology/tests/test_grey.py index 6003425f..6d2b75e8 100644 --- a/skimage/morphology/tests/test_grey.py +++ b/skimage/morphology/tests/test_grey.py @@ -5,6 +5,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 @@ -157,28 +158,28 @@ class TestDTypes(): def test_binary_erosion(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_lena, strel) - grey_res = grey.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 = grey.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 = grey.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 = grey.opening(bw_lena, strel) + grey_res = img_as_bool(grey.opening(bw_lena, strel)) testing.assert_array_equal(binary_res, grey_res) From f478b6a397e2efc481844c52133c2e909243257c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 11 Sep 2012 20:11:55 +0200 Subject: [PATCH 2/3] Fix bug in binary dilation and test case --- skimage/morphology/binary.py | 2 +- skimage/morphology/tests/test_grey.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/morphology/binary.py b/skimage/morphology/binary.py index ffd79482..e2e0f20b 100644 --- a/skimage/morphology/binary.py +++ b/skimage/morphology/binary.py @@ -65,7 +65,7 @@ def binary_dilation(image, selem, out=None): """ conv = ndimage.convolve(image > 0, selem, output=out, - mode='constant', cval=1) + mode='constant', cval=0) if conv is not None: out = conv return np.not_equal(out, 0, out=out) diff --git a/skimage/morphology/tests/test_grey.py b/skimage/morphology/tests/test_grey.py index 6d2b75e8..31a33321 100644 --- a/skimage/morphology/tests/test_grey.py +++ b/skimage/morphology/tests/test_grey.py @@ -10,7 +10,7 @@ from skimage.morphology import binary, grey, selem lena = np.load(os.path.join(data_dir, 'lena_GRAY_U8.npy')) -bw_lena = lena > 0.4 +bw_lena = lena > 100 class TestMorphology(): From 56b147678943dbd5a1e480f253b74f7864aff4f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 11 Sep 2012 20:27:18 +0200 Subject: [PATCH 3/3] Add test case for non-square images --- skimage/morphology/tests/test_grey.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/skimage/morphology/tests/test_grey.py b/skimage/morphology/tests/test_grey.py index 31a33321..244ec566 100644 --- a/skimage/morphology/tests/test_grey.py +++ b/skimage/morphology/tests/test_grey.py @@ -155,6 +155,13 @@ 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)