From 0b0cb6a1693556ac364409d099dd0294d57f6119 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Sun, 28 Jul 2013 17:21:25 +0530 Subject: [PATCH 01/11] Adding Octagon structural element --- skimage/morphology/__init__.py | 4 +- skimage/morphology/selem.py | 105 +++++++++++++++++++++++---------- 2 files changed, 78 insertions(+), 31 deletions(-) diff --git a/skimage/morphology/__init__.py b/skimage/morphology/__init__.py index c8fafe6f..38357f02 100644 --- a/skimage/morphology/__init__.py +++ b/skimage/morphology/__init__.py @@ -4,7 +4,8 @@ from .grey import (erosion, dilation, opening, closing, white_tophat, black_tophat, greyscale_erode, greyscale_dilate, greyscale_open, greyscale_close, greyscale_white_top_hat, greyscale_black_top_hat) -from .selem import square, rectangle, diamond, disk, cube, octahedron, ball +from .selem import (square, rectangle, diamond, disk, cube, octahedron, ball, + octagon) from .ccomp import label from .watershed import watershed, is_local_maximum from ._skeletonize import skeletonize, medial_axis @@ -36,6 +37,7 @@ __all__ = ['binary_erosion', 'cube', 'octahedron', 'ball', + 'octagon', 'label', 'watershed', 'is_local_maximum', diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index 2cccae22..f2cba44b 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -4,6 +4,7 @@ """ import numpy as np +from . import convex_hull_image def square(width, dtype=np.uint8): @@ -15,18 +16,18 @@ def square(width, dtype=np.uint8): Parameters ---------- width : int - The width and height of the square + The width and height of the square Other Parameters ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - A structuring element consisting only of ones, i.e. every - pixel belongs to the neighborhood. + A structuring element consisting only of ones, i.e. every + pixel belongs to the neighborhood. """ return np.ones((width, width), dtype=dtype) @@ -41,21 +42,20 @@ def rectangle(width, height, dtype=np.uint8): Parameters ---------- width : int - The width of the rectangle - + The width of the rectangle height : int - The height of the rectangle + The height of the rectangle Other Parameters ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - A structuring element consisting only of ones, i.e. every - pixel belongs to the neighborhood. + A structuring element consisting only of ones, i.e. every + pixel belongs to the neighborhood. """ return np.ones((width, height), dtype=dtype) @@ -71,17 +71,19 @@ def diamond(radius, dtype=np.uint8): Parameters ---------- radius : int - The radius of the diamond-shaped structuring element. + The radius of the diamond-shaped structuring element. + Other Parameters + ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - The structuring element where elements of the neighborhood - are 1 and 0 otherwise. + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. """ half = radius (I, J) = np.meshgrid(range(0, radius * 2 + 1), range(0, radius * 2 + 1)) @@ -98,16 +100,18 @@ def disk(radius, dtype=np.uint8): Parameters ---------- radius : int - The radius of the disk-shaped structuring element. + The radius of the disk-shaped structuring element. + Other Parameters + ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - The structuring element where elements of the neighborhood - are 1 and 0 otherwise. + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. """ L = np.linspace(-radius, radius, 2 * radius + 1) (X, Y) = np.meshgrid(L, L) @@ -125,18 +129,18 @@ def cube(width, dtype=np.uint8): Parameters ---------- width : int - The width, height and depth of the cube + The width, height and depth of the cube Other Parameters ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - A structuring element consisting only of ones, i.e. every - pixel belongs to the neighborhood. + A structuring element consisting only of ones, i.e. every + pixel belongs to the neighborhood. """ return np.ones((width, width, width), dtype=dtype) @@ -153,17 +157,19 @@ def octahedron(radius, dtype=np.uint8): Parameters ---------- radius : int - The radius of the octahedron-shaped structuring element. + The radius of the octahedron-shaped structuring element. + Other Parameters + ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - The structuring element where elements of the neighborhood - are 1 and 0 otherwise. + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. """ # note that in contrast to diamond(), this method allows non-integer radii n = 2 * radius + 1 @@ -184,16 +190,18 @@ def ball(radius, dtype=np.uint8): Parameters ---------- radius : int - The radius of the ball-shaped structuring element. + The radius of the ball-shaped structuring element. + Other Parameters + ---------------- dtype : data-type - The data type of the structuring element. + The data type of the structuring element. Returns ------- selem : ndarray - The structuring element where elements of the neighborhood - are 1 and 0 otherwise. + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. """ n = 2 * radius + 1 Z, Y, X = np.mgrid[ -radius:radius:n*1j, @@ -201,3 +209,40 @@ def ball(radius, dtype=np.uint8): -radius:radius:n*1j] s = X**2 + Y**2 + Z**2 return np.array(s <= radius * radius, dtype=dtype) + + +def octagon(m, n, dtype=np.uint8): + """ + Generates a octagon shaped structuring element with a given size of + horizontal and vertical sides and a given height of slanted sides. + The slanted sides are 45 or 135 degrees to the horizontal axis. + + Parameters + ---------- + m : int + The size of the horizontal and vertical sides. + n : int + The vertical height of the slanted sides. + + Other Parameters + ---------------- + dtype : data-type + The data type of the structuring element. + + Returns + ------- + selem : ndarray + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. + """ + selem = np.zeros((m + 2*n, m + 2*n)) + selem[0, n] = 1 + selem[n, 0] = 1 + selem[0, m + n -1] = 1 + selem[m + n - 1, 0] = 1 + selem[-1, n] = 1 + selem[n, -1] = 1 + selem[-1, m + n - 1] = 1 + selem[m + n - 1, -1] = 1 + selem = convex_hull_image(selem).astype(dtype) + return selem From f4332ce488f16897a4d9db5a84dcbb7bcbdb4b4a Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Sun, 28 Jul 2013 18:41:19 +0530 Subject: [PATCH 02/11] Removing circular import --- skimage/morphology/selem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index f2cba44b..8d907d88 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -4,7 +4,6 @@ """ import numpy as np -from . import convex_hull_image def square(width, dtype=np.uint8): @@ -235,6 +234,7 @@ def octagon(m, n, dtype=np.uint8): The structuring element where elements of the neighborhood are 1 and 0 otherwise. """ + from . import convex_hull_image selem = np.zeros((m + 2*n, m + 2*n)) selem[0, n] = 1 selem[n, 0] = 1 From 8a062617d0669cc421b1ec272c4873e6d476a81c Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 30 Jul 2013 03:22:45 +0530 Subject: [PATCH 03/11] Correcting grammatical typo --- skimage/morphology/selem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index 8d907d88..d403a27a 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -212,7 +212,7 @@ def ball(radius, dtype=np.uint8): def octagon(m, n, dtype=np.uint8): """ - Generates a octagon shaped structuring element with a given size of + Generates an octagon shaped structuring element with a given size of horizontal and vertical sides and a given height of slanted sides. The slanted sides are 45 or 135 degrees to the horizontal axis. From 93819c74bbe5698b43ac633fe363afeb0b3f06b3 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 30 Jul 2013 03:28:58 +0530 Subject: [PATCH 04/11] Making the doc more explicit --- skimage/morphology/selem.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index d403a27a..2c41e393 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -213,15 +213,16 @@ def ball(radius, dtype=np.uint8): def octagon(m, n, dtype=np.uint8): """ Generates an octagon shaped structuring element with a given size of - horizontal and vertical sides and a given height of slanted sides. - The slanted sides are 45 or 135 degrees to the horizontal axis. + horizontal and vertical sides and a given height or width of slanted + sides. The slanted sides are 45 or 135 degrees to the horizontal axis + and hence the widths and heights are equal. Parameters ---------- m : int The size of the horizontal and vertical sides. n : int - The vertical height of the slanted sides. + The height or width of the slanted sides. Other Parameters ---------------- From ac6f28225313ae2105d027864b5eb1fce23bd425 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Mon, 5 Aug 2013 10:04:58 +0530 Subject: [PATCH 05/11] Added star structural element --- skimage/morphology/__init__.py | 2 +- skimage/morphology/selem.py | 48 ++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/skimage/morphology/__init__.py b/skimage/morphology/__init__.py index 38357f02..f01fc3ed 100644 --- a/skimage/morphology/__init__.py +++ b/skimage/morphology/__init__.py @@ -5,7 +5,7 @@ from .grey import (erosion, dilation, opening, closing, white_tophat, greyscale_open, greyscale_close, greyscale_white_top_hat, greyscale_black_top_hat) from .selem import (square, rectangle, diamond, disk, cube, octahedron, ball, - octagon) + octagon, star) from .ccomp import label from .watershed import watershed, is_local_maximum from ._skeletonize import skeletonize, medial_axis diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index 2c41e393..9d466e84 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -232,8 +232,9 @@ def octagon(m, n, dtype=np.uint8): Returns ------- selem : ndarray - The structuring element where elements of the neighborhood - are 1 and 0 otherwise. + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. + """ from . import convex_hull_image selem = np.zeros((m + 2*n, m + 2*n)) @@ -247,3 +248,46 @@ def octagon(m, n, dtype=np.uint8): selem[m + n - 1, -1] = 1 selem = convex_hull_image(selem).astype(dtype) return selem + + +def star(a, dtype=np.uint8): + """ + Generates a star shaped structuring element that is an overlap of square + of size `2*a + 1` with its 45 degree rotated version. The slanted sides + are 45 or 135 degrees to the horizontal axis. + + Parameters + ---------- + a : int + Parameter deciding the size of the star structural element. The side + of the square array returned is `2*a + 1 + 2*floor(a / 2)`. + + Other Parameters + ---------------- + dtype : data-type + The data type of the structuring element. + + Returns + ------- + selem : ndarray + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. + + """ + from . import convex_hull_image + if a == 1: + bfilter = np.zeros((3, 3)) + bfilter[:] = 1 + return bfilter + m = 2 * a + 1 + n = a / 2 + selem_square = np.zeros((m + 2 * n, m + 2 * n), dtype=np.uint8) + selem_square[n: m + n, n: m + n] = 1 + selem_triangle = np.zeros((m + 2 * n, m + 2 * n), dtype=np.uint8) + selem_triangle[(m + 2 * n - 1) / 2, 0] = 1 + selem_triangle[(m + 1) / 2, n - 1] = 1 + selem_triangle[(m + 4 * n - 3) / 2, n - 1] = 1 + selem_triangle = convex_hull_image(selem_triangle).astype(int) + selem_triangle += (selem_triangle[:, ::-1] + selem_triangle.T + + selem_triangle.T[::-1, :]) + return selem_square + selem_triangle From 060a4dd2733b2701ec4eb29cf34c469889c4ea98 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 6 Aug 2013 03:02:58 +0530 Subject: [PATCH 06/11] Minor doc changes --- skimage/morphology/selem.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index 9d466e84..8b63a798 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -252,9 +252,9 @@ def octagon(m, n, dtype=np.uint8): def star(a, dtype=np.uint8): """ - Generates a star shaped structuring element that is an overlap of square - of size `2*a + 1` with its 45 degree rotated version. The slanted sides - are 45 or 135 degrees to the horizontal axis. + Generates a star shaped structuring element that has 8 vertices and is an + overlap of square of size `2*a + 1` with its 45 degree rotated version. + The slanted sides are 45 or 135 degrees to the horizontal axis. Parameters ---------- From 67165363bfd3f8aaeed67a609137cd4bae91486e Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 6 Aug 2013 03:38:52 +0530 Subject: [PATCH 07/11] Test for Octagon --- skimage/morphology/tests/test_selem.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/skimage/morphology/tests/test_selem.py b/skimage/morphology/tests/test_selem.py index 9a8ca895..945a9ede 100644 --- a/skimage/morphology/tests/test_selem.py +++ b/skimage/morphology/tests/test_selem.py @@ -66,6 +66,21 @@ class TestSElem(): def test_selem_octahedron(self): self.strel_worker_3d("diamond-matlab-output.npz", selem.octahedron) + def test_selem_octagon(self): + expected_mask = array([[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], dtype=uint8) + actual_mask = selem.octagon(5, 3) + assert_equal(expected_mask, actual_mask) + if __name__ == '__main__': np.testing.run_module_suite() From 55d7eaa87d07d32b46ba7f2b34255bd9896bfaab Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 6 Aug 2013 03:48:06 +0530 Subject: [PATCH 08/11] Improving the implementation of star --- skimage/morphology/selem.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index 8b63a798..f85b3a95 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -276,18 +276,17 @@ def star(a, dtype=np.uint8): """ from . import convex_hull_image if a == 1: - bfilter = np.zeros((3, 3)) + bfilter = np.zeros((3, 3), dtype) bfilter[:] = 1 return bfilter m = 2 * a + 1 n = a / 2 - selem_square = np.zeros((m + 2 * n, m + 2 * n), dtype=np.uint8) + selem_square = np.zeros((m + 2 * n, m + 2 * n)) selem_square[n: m + n, n: m + n] = 1 - selem_triangle = np.zeros((m + 2 * n, m + 2 * n), dtype=np.uint8) - selem_triangle[(m + 2 * n - 1) / 2, 0] = 1 - selem_triangle[(m + 1) / 2, n - 1] = 1 - selem_triangle[(m + 4 * n - 3) / 2, n - 1] = 1 - selem_triangle = convex_hull_image(selem_triangle).astype(int) - selem_triangle += (selem_triangle[:, ::-1] + selem_triangle.T + - selem_triangle.T[::-1, :]) - return selem_square + selem_triangle + c = (m + 2 * n - 1) / 2 + selem_rotated = np.zeros((m + 2 * n, m + 2 * n)) + selem_rotated[0, c] = selem_rotated[-1, c] = selem_rotated[c, 0] = selem_rotated[c, -1] = 1 + selem_rotated = convex_hull_image(selem_rotated).astype(int) + selem = selem_square + selem_rotated + selem[selem > 0] = 1 + return selem.astype(dtype) From 8569d73d571a1685937f4464033fbffe92c5132d Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 6 Aug 2013 04:13:27 +0530 Subject: [PATCH 09/11] Adding namespace --- skimage/morphology/tests/test_selem.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/skimage/morphology/tests/test_selem.py b/skimage/morphology/tests/test_selem.py index 945a9ede..311f5a2e 100644 --- a/skimage/morphology/tests/test_selem.py +++ b/skimage/morphology/tests/test_selem.py @@ -81,6 +81,23 @@ class TestSElem(): actual_mask = selem.octagon(5, 3) assert_equal(expected_mask, actual_mask) + def test_selem_star(self): + expected_mask = array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]], dtype=uint8) + actual_mask = selem.star(4) + assert_equal(expected_mask, actual_mask) + if __name__ == '__main__': np.testing.run_module_suite() From 6455d9b6e9b6226c4bb35bbc2addf69aa2bee5a4 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 6 Aug 2013 04:14:40 +0530 Subject: [PATCH 10/11] Adding test for star --- skimage/morphology/tests/test_selem.py | 48 +++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/skimage/morphology/tests/test_selem.py b/skimage/morphology/tests/test_selem.py index 311f5a2e..a49cd026 100644 --- a/skimage/morphology/tests/test_selem.py +++ b/skimage/morphology/tests/test_selem.py @@ -67,34 +67,34 @@ class TestSElem(): self.strel_worker_3d("diamond-matlab-output.npz", selem.octahedron) def test_selem_octagon(self): - expected_mask = array([[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], dtype=uint8) + expected_mask = np.array([[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], dtype=np.uint8) actual_mask = selem.octagon(5, 3) assert_equal(expected_mask, actual_mask) def test_selem_star(self): - expected_mask = array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]], dtype=uint8) + expected_mask = np.array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]], dtype=np.uint8) actual_mask = selem.star(4) assert_equal(expected_mask, actual_mask) From 6e8dcd3ddd150b8eb26b054f3b6279fc85728b44 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Wed, 7 Aug 2013 12:12:23 +0530 Subject: [PATCH 11/11] Adding tests for smallest selem --- skimage/morphology/tests/test_selem.py | 66 +++++++++++++++----------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/skimage/morphology/tests/test_selem.py b/skimage/morphology/tests/test_selem.py index a49cd026..7895fa58 100644 --- a/skimage/morphology/tests/test_selem.py +++ b/skimage/morphology/tests/test_selem.py @@ -67,36 +67,46 @@ class TestSElem(): self.strel_worker_3d("diamond-matlab-output.npz", selem.octahedron) def test_selem_octagon(self): - expected_mask = np.array([[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], dtype=np.uint8) - actual_mask = selem.octagon(5, 3) - assert_equal(expected_mask, actual_mask) + expected_mask1 = np.array([[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], dtype=np.uint8) + actual_mask1 = selem.octagon(5, 3) + expected_mask2 = np.array([[0, 1, 0], + [1, 1, 1], + [0, 1, 0]], dtype=np.uint8) + actual_mask2 = selem.octagon(1, 1) + assert_equal(expected_mask1, actual_mask1) + assert_equal(expected_mask2, actual_mask2) def test_selem_star(self): - expected_mask = np.array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], - [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]], dtype=np.uint8) - actual_mask = selem.star(4) - assert_equal(expected_mask, actual_mask) + expected_mask1 = np.array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]], dtype=np.uint8) + actual_mask1 = selem.star(4) + expected_mask2 = np.array([[1, 1, 1], + [1, 1, 1], + [1, 1, 1]], dtype=np.uint8) + actual_mask2 = selem.star(1) + assert_equal(expected_mask1, actual_mask1) + assert_equal(expected_mask2, actual_mask2) if __name__ == '__main__':