From 78f16f4264a90595d44e1401e53049636bebe834 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 26 Sep 2011 17:21:03 +0200 Subject: [PATCH 1/6] pep8 --- scikits/image/filter/tests/test_canny.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/scikits/image/filter/tests/test_canny.py b/scikits/image/filter/tests/test_canny.py index a92a576e..f7002773 100644 --- a/scikits/image/filter/tests/test_canny.py +++ b/scikits/image/filter/tests/test_canny.py @@ -11,21 +11,21 @@ class TestCanny(unittest.TestCase): def test_00_01_zeros_mask(self): '''Test that the Canny filter finds no points in a masked image''' - result = (F.canny(np.random.uniform(size=(20,20)), 4,0,0, - np.zeros((20,20),bool))) + result = (F.canny(np.random.uniform(size=(20,20)), 4, 0, 0, + np.zeros((20, 20), bool))) self.assertFalse(np.any(result)) def test_01_01_circle(self): '''Test that the Canny filter finds the outlines of a circle''' - i,j = np.mgrid[-200:200,-200:200].astype(float) / 200 - c = np.abs(np.sqrt(i*i+j*j) - .5) < .02 - result = F.canny(c.astype(float), 4, 0, 0,np.ones(c.shape,bool)) + i, j = np.mgrid[-200:200, -200:200].astype(float) / 200 + c = np.abs(np.sqrt( i * i + j * j) - .5) < .02 + result = F.canny(c.astype(float), 4, 0, 0, np.ones(c.shape, bool)) # # erode and dilate the circle to get rings that should contain the # outlines # cd = binary_dilation(c, iterations=3) - ce = binary_erosion(c,iterations=3) + ce = binary_erosion(c, iterations=3) cde = np.logical_and(cd, np.logical_not(ce)) self.assertTrue(np.all(cde[result])) # @@ -41,16 +41,16 @@ class TestCanny(unittest.TestCase): def test_01_02_circle_with_noise(self): '''Test that the Canny filter finds the circle outlines in a noisy image''' np.random.seed(0) - i,j = np.mgrid[-200:200,-200:200].astype(float) / 200 - c = np.abs(np.sqrt(i*i+j*j) - .5) < .02 - cf = c.astype(float) * .5 + np.random.uniform(size=c.shape)*.5 - result = F.canny(cf, 4, .1, .2,np.ones(c.shape,bool)) + i,j = np.mgrid[-200:200, -200:200].astype(float) / 200 + c = np.abs(np.sqrt( i * i + j * j) - .5) < .02 + cf = c.astype(float) * .5 + np.random.uniform(size=c.shape) * .5 + result = F.canny(cf, 4, .1, .2, np.ones(c.shape, bool)) # # erode and dilate the circle to get rings that should contain the # outlines # cd = binary_dilation(c, iterations=4) - ce = binary_erosion(c,iterations=4) + ce = binary_erosion(c, iterations=4) cde = np.logical_and(cd, np.logical_not(ce)) self.assertTrue(np.all(cde[result])) point_count = np.sum(result) From 604296bb51272a79680fe08b1bb5b893ae2f11fe Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 26 Sep 2011 17:21:25 +0200 Subject: [PATCH 2/6] removed dead link form canny help string --- scikits/image/filter/canny.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scikits/image/filter/canny.py b/scikits/image/filter/canny.py index 49db7343..341d5b77 100644 --- a/scikits/image/filter/canny.py +++ b/scikits/image/filter/canny.py @@ -80,10 +80,8 @@ def canny(image, sigma, low_threshold, high_threshold, mask=None): ----------- Canny, J., A Computational Approach To Edge Detection, IEEE Trans. Pattern Analysis and Machine Intelligence, 8:679-714, 1986 - - William Green's Canny tutorial - http://www.pages.drexel.edu/~weg22/can_tut.html ''' + # # The steps involved: # From 24d771d3335a2e54261bdbdbffcb301648c09bac Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 26 Sep 2011 17:21:58 +0200 Subject: [PATCH 3/6] check if input for canny is 2d array, test for error raise. --- scikits/image/filter/canny.py | 4 ++++ scikits/image/filter/tests/test_canny.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/scikits/image/filter/canny.py b/scikits/image/filter/canny.py index 341d5b77..dd3b5608 100644 --- a/scikits/image/filter/canny.py +++ b/scikits/image/filter/canny.py @@ -111,6 +111,10 @@ def canny(image, sigma, low_threshold, high_threshold, mask=None): # mask by one and then mask the output. We also mask out the border points # because who knows what lies beyond the edge of the image? # + + if image.ndim!=2: + raise TypeError("The input 'image' must be a two dimensional array.") + if mask is None: mask = np.ones(image.shape, dtype=bool) fsmooth = lambda x: gaussian_filter(x, sigma, mode='constant') diff --git a/scikits/image/filter/tests/test_canny.py b/scikits/image/filter/tests/test_canny.py index f7002773..ddadc790 100644 --- a/scikits/image/filter/tests/test_canny.py +++ b/scikits/image/filter/tests/test_canny.py @@ -56,3 +56,6 @@ class TestCanny(unittest.TestCase): point_count = np.sum(result) self.assertTrue(point_count > 1200) self.assertTrue(point_count < 1600) + + def test_image_shape(self): + self.assertRaises(TypeError,F.canny,np.zeros((20, 20, 20)), 4, 0, 0) From fcf4c41bd6223e67c4469851b8cdae18f56b59c5 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 26 Sep 2011 17:22:13 +0200 Subject: [PATCH 4/6] somewhat sensible default values for canny edges --- scikits/image/filter/canny.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scikits/image/filter/canny.py b/scikits/image/filter/canny.py index dd3b5608..e660d284 100644 --- a/scikits/image/filter/canny.py +++ b/scikits/image/filter/canny.py @@ -50,7 +50,7 @@ def smooth_with_function_and_mask(image, function, mask): return output_image -def canny(image, sigma, low_threshold, high_threshold, mask=None): +def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): '''Edge filter an image using the Canny algorithm. Parameters From 8e33b17b125a39f1a7123a143438eca874104d47 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 26 Sep 2011 19:29:44 +0200 Subject: [PATCH 5/6] pep8, removed unused import and variables --- scikits/image/filter/canny.py | 35 ++++++++++++------------ scikits/image/filter/tests/test_canny.py | 30 ++++++++++---------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/scikits/image/filter/canny.py b/scikits/image/filter/canny.py index e660d284..f99213e3 100644 --- a/scikits/image/filter/canny.py +++ b/scikits/image/filter/canny.py @@ -14,7 +14,7 @@ Original author: Lee Kamentsky import numpy as np import scipy.ndimage as ndi -from scipy.ndimage import (gaussian_filter, convolve, +from scipy.ndimage import (gaussian_filter, generate_binary_structure, binary_erosion, label) @@ -25,10 +25,10 @@ def smooth_with_function_and_mask(image, function, mask): ---------- image : array The image to smooth - + function : callable A function that takes an image and returns a smoothed image - + mask : array Mask with 1's for significant pixels, 0 for masked pixels @@ -37,11 +37,11 @@ def smooth_with_function_and_mask(image, function, mask): This function calculates the fractional contribution of masked pixels by applying the function to the mask (which gets you the fraction of the pixel data that's due to significant points). We then mask the image - and apply the function. The resulting values will be lower by the bleed-over - fraction, so you can recalibrate by dividing by the function on the mask - to recover the effect of smoothing from just the significant pixels. + and apply the function. The resulting values will be lower by the + bleed-over fraction, so you can recalibrate by dividing by the function + on the mask to recover the effect of smoothing from just the significant + pixels. """ - not_mask = np.logical_not(mask) bleed_over = function(mask.astype(float)) masked_image = np.zeros(image.shape, image.dtype) masked_image[mask] = image[mask] @@ -58,10 +58,10 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): image : array_like, dtype=float The greyscale input image to detect edges on; should be normalized to 0.0 to 1.0. - + sigma : float The standard deviation of the Gaussian filter - + low_threshold : float The lower bound for hysterisis thresholding (linking edges) @@ -112,7 +112,7 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): # because who knows what lies beyond the edge of the image? # - if image.ndim!=2: + if image.ndim != 2: raise TypeError("The input 'image' must be a two dimensional array.") if mask is None: @@ -138,7 +138,7 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): # Assign each point to have a normal of 0-45 degrees, 45-90 degrees, # 90-135 degrees and 135-180 degrees. # - local_maxima = np.zeros(image.shape,bool) + local_maxima = np.zeros(image.shape, bool) #----- 0 to 45 degrees ------ pts_plus = (isobel >= 0) & (jsobel >= 0) & (abs_isobel >= abs_jsobel) pts_minus = (isobel <= 0) & (jsobel <= 0) & (abs_isobel >= abs_jsobel) @@ -187,7 +187,6 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): c1 = magnitude[:, :-1][pts[:, 1:]] c2 = magnitude[1:, :-1][pts[:-1, 1:]] c_minus = c2 * w + c1 * (1.0 - w) <= m - cc = np.logical_and(c_plus,c_minus) local_maxima[pts] = c_plus & c_minus #----- 135 to 180 degrees ------ # Mix anti-diagonal and anti-horizontal @@ -202,7 +201,7 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): w = abs_jsobel[pts] / abs_isobel[pts] c_plus = c2 * w + c1 * (1 - w) <= m c1 = magnitude[1:, :][pts[:-1, :]] - c2 = magnitude[1:,:-1][pts[:-1,1:]] + c2 = magnitude[1:, :-1][pts[:-1, 1:]] c_minus = c2 * w + c1 * (1 - w) <= m local_maxima[pts] = c_plus & c_minus # @@ -214,14 +213,14 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): # Segment the low-mask, then only keep low-segments that have # some high_mask component in them # - labels,count = label(low_mask, np.ndarray((3, 3),bool)) + labels, count = label(low_mask, np.ndarray((3, 3), bool)) if count == 0: return low_mask - - sums = (np.array(ndi.sum(high_mask,labels, - np.arange(count,dtype=np.int32) + 1), + + sums = (np.array(ndi.sum(high_mask, labels, + np.arange(count, dtype=np.int32) + 1), copy=False, ndmin=1)) - good_label = np.zeros((count + 1,),bool) + good_label = np.zeros((count + 1,), bool) good_label[1:] = sums > 0 output_mask = good_label[labels] return output_mask diff --git a/scikits/image/filter/tests/test_canny.py b/scikits/image/filter/tests/test_canny.py index ddadc790..fd5ac67d 100644 --- a/scikits/image/filter/tests/test_canny.py +++ b/scikits/image/filter/tests/test_canny.py @@ -3,22 +3,23 @@ import numpy as np from scipy.ndimage import binary_dilation, binary_erosion import scikits.image.filter as F + class TestCanny(unittest.TestCase): def test_00_00_zeros(self): '''Test that the Canny filter finds no points for a blank field''' - result = F.canny(np.zeros((20,20)), 4, 0, 0, np.ones((20,20),bool)) + result = F.canny(np.zeros((20, 20)), 4, 0, 0, np.ones((20, 20), bool)) self.assertFalse(np.any(result)) - + def test_00_01_zeros_mask(self): '''Test that the Canny filter finds no points in a masked image''' - result = (F.canny(np.random.uniform(size=(20,20)), 4, 0, 0, + result = (F.canny(np.random.uniform(size=(20, 20)), 4, 0, 0, np.zeros((20, 20), bool))) self.assertFalse(np.any(result)) - + def test_01_01_circle(self): '''Test that the Canny filter finds the outlines of a circle''' i, j = np.mgrid[-200:200, -200:200].astype(float) / 200 - c = np.abs(np.sqrt( i * i + j * j) - .5) < .02 + c = np.abs(np.sqrt(i * i + j * j) - .5) < .02 result = F.canny(c.astype(float), 4, 0, 0, np.ones(c.shape, bool)) # # erode and dilate the circle to get rings that should contain the @@ -30,19 +31,20 @@ class TestCanny(unittest.TestCase): self.assertTrue(np.all(cde[result])) # # The circle has a radius of 100. There are two rings here, one - # for the inside edge and one for the outside. So that's 100 * 2 * 2 * 3 - # for those places where pi is still 3. The edge contains both pixels - # if there's a tie, so we bump the count a little. - # + # for the inside edge and one for the outside. So that's + # 100 * 2 * 2 * 3 for those places where pi is still 3. + # The edge contains both pixels if there's a tie, so we + # bump the count a little. point_count = np.sum(result) self.assertTrue(point_count > 1200) self.assertTrue(point_count < 1600) - + def test_01_02_circle_with_noise(self): - '''Test that the Canny filter finds the circle outlines in a noisy image''' + '''Test that the Canny filter finds the circle outlines + in a noisy image''' np.random.seed(0) - i,j = np.mgrid[-200:200, -200:200].astype(float) / 200 - c = np.abs(np.sqrt( i * i + j * j) - .5) < .02 + i, j = np.mgrid[-200:200, -200:200].astype(float) / 200 + c = np.abs(np.sqrt(i * i + j * j) - .5) < .02 cf = c.astype(float) * .5 + np.random.uniform(size=c.shape) * .5 result = F.canny(cf, 4, .1, .2, np.ones(c.shape, bool)) # @@ -58,4 +60,4 @@ class TestCanny(unittest.TestCase): self.assertTrue(point_count < 1600) def test_image_shape(self): - self.assertRaises(TypeError,F.canny,np.zeros((20, 20, 20)), 4, 0, 0) + self.assertRaises(TypeError, F.canny, np.zeros((20, 20, 20)), 4, 0, 0) From 089485ff93cbfa3e8bc6a710c87efa92e850921a Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Mon, 26 Sep 2011 19:48:25 +0200 Subject: [PATCH 6/6] new canny edge tutorial location --- scikits/image/filter/canny.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scikits/image/filter/canny.py b/scikits/image/filter/canny.py index f99213e3..75e9abbe 100644 --- a/scikits/image/filter/canny.py +++ b/scikits/image/filter/canny.py @@ -80,6 +80,9 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): ----------- Canny, J., A Computational Approach To Edge Detection, IEEE Trans. Pattern Analysis and Machine Intelligence, 8:679-714, 1986 + + William Green' Canny tutorial + http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/can_tut.html ''' #