From 94dc84f5b719c4114325a2e93f4ef8a476f76c38 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Fri, 28 Jun 2013 18:34:50 -0400 Subject: [PATCH 1/5] Clarified that you can pass images of any type into Canny filter. If low and high thresholds are not specified by user, default to 10 and 20 % (respectively) of range of image's dtype. --- skimage/filter/_canny.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/skimage/filter/_canny.py b/skimage/filter/_canny.py index 904919be..cd676eaf 100644 --- a/skimage/filter/_canny.py +++ b/skimage/filter/_canny.py @@ -1,4 +1,4 @@ -'''canny.py - Canny Edge detector +"""canny.py - Canny Edge detector Reference: Canny, J., A Computational Approach To Edge Detection, IEEE Trans. Pattern Analysis and Machine Intelligence, 8:679-714, 1986 @@ -9,13 +9,13 @@ Copyright (c) 2003-2009 Massachusetts Institute of Technology Copyright (c) 2009-2011 Broad Institute All rights reserved. Original author: Lee Kamentsky - -''' +""" import numpy as np import scipy.ndimage as ndi from scipy.ndimage import (gaussian_filter, generate_binary_structure, binary_erosion, label) +from skimage import dtype_limits def smooth_with_function_and_mask(image, function, mask): @@ -30,7 +30,7 @@ def smooth_with_function_and_mask(image, function, mask): A function that takes an image and returns a smoothed image mask : array - Mask with 1's for significant pixels, 0 for masked pixels + Mask with 1's for significant pixels, 0's for masked pixels. Notes ------ @@ -50,26 +50,27 @@ def smooth_with_function_and_mask(image, function, mask): return output_image -def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): - '''Edge filter an image using the Canny algorithm. +def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None): + """Edge filter an image using the Canny algorithm. Parameters ----------- - image : array_like, dtype=float - The greyscale input image to detect edges on; should be normalized to - 0.0 to 1.0. + image : array_like, dtype=float or int + Greyscale input image to detect edges on; can be of any dtype. sigma : float - The standard deviation of the Gaussian filter + Standard deviation of the Gaussian filter. low_threshold : float - The lower bound for hysterisis thresholding (linking edges) + Lower bound for hysteresis thresholding (linking edges). + If none is provided, low_threshold is set to 10%. high_threshold : float - The upper bound for hysterisis thresholding (linking edges) + Upper bound for hysteresis thresholding (linking edges). + If none is provided, high_threshold is set to 20%. mask : array, dtype=bool, optional - An optional mask to limit the application of Canny to a certain area. + Mask to limit the application of Canny to a certain area. Returns ------- @@ -107,7 +108,7 @@ 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 + William Green's Canny tutorial http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/can_tut.html Examples @@ -121,7 +122,7 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): >>> edges1 = filter.canny(im) >>> # Increase the smoothing for better results >>> edges2 = filter.canny(im, sigma=3) - ''' + """ # # The steps involved: @@ -154,7 +155,13 @@ def canny(image, sigma=1., low_threshold=.1, high_threshold=.2, mask=None): # if image.ndim != 2: - raise TypeError("The input 'image' must be a two dimensional array.") + raise TypeError("The input 'image' must be a two-dimensional array.") + + if low_threshold is None: + low_threshold = 0.1 * dtype_limits(image)[0] + + if high_threshold is None: + high_threshold = 0.2 * dtype_limits(image)[1] if mask is None: mask = np.ones(image.shape, dtype=bool) From 8f40e05aab3850bcba6ff3f92c4b532cef52fb2a Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Fri, 28 Jun 2013 18:51:33 -0400 Subject: [PATCH 2/5] Fixed relative threshold: you consider 10% and 20% of the max for range of corresponding dtype. --- skimage/filter/_canny.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/filter/_canny.py b/skimage/filter/_canny.py index cd676eaf..13731ac6 100644 --- a/skimage/filter/_canny.py +++ b/skimage/filter/_canny.py @@ -158,7 +158,7 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None): raise TypeError("The input 'image' must be a two-dimensional array.") if low_threshold is None: - low_threshold = 0.1 * dtype_limits(image)[0] + low_threshold = 0.1 * dtype_limits(image)[1] if high_threshold is None: high_threshold = 0.2 * dtype_limits(image)[1] From ee7f710e554020f74d33d84f2090d1468de1c21a Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Fri, 28 Jun 2013 19:24:28 -0400 Subject: [PATCH 3/5] Improved style. --- skimage/filter/_canny.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/skimage/filter/_canny.py b/skimage/filter/_canny.py index 13731ac6..cbac5054 100644 --- a/skimage/filter/_canny.py +++ b/skimage/filter/_canny.py @@ -1,4 +1,5 @@ -"""canny.py - Canny Edge detector +""" +canny.py - Canny Edge detector Reference: Canny, J., A Computational Approach To Edge Detection, IEEE Trans. Pattern Analysis and Machine Intelligence, 8:679-714, 1986 @@ -24,11 +25,9 @@ def smooth_with_function_and_mask(image, function, mask): Parameters ---------- image : array - The image to smooth - + Image you want to smooth. function : callable - A function that takes an image and returns a smoothed image - + A function that does image smoothing. mask : array Mask with 1's for significant pixels, 0's for masked pixels. @@ -55,20 +54,16 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None): Parameters ----------- - image : array_like, dtype=float or int + image : two-dimensional array Greyscale input image to detect edges on; can be of any dtype. - sigma : float Standard deviation of the Gaussian filter. - low_threshold : float Lower bound for hysteresis thresholding (linking edges). If none is provided, low_threshold is set to 10%. - high_threshold : float Upper bound for hysteresis thresholding (linking edges). If none is provided, high_threshold is set to 20%. - mask : array, dtype=bool, optional Mask to limit the application of Canny to a certain area. From 5b3081ccc6e2f03c3837679c3f332bb28032e085 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Sat, 29 Jun 2013 18:29:55 -0400 Subject: [PATCH 4/5] Always follow the style guide :) --- skimage/filter/_canny.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/skimage/filter/_canny.py b/skimage/filter/_canny.py index cbac5054..1c5e5c19 100644 --- a/skimage/filter/_canny.py +++ b/skimage/filter/_canny.py @@ -25,11 +25,11 @@ def smooth_with_function_and_mask(image, function, mask): Parameters ---------- image : array - Image you want to smooth. + Image you want to smooth. function : callable - A function that does image smoothing. + A function that does image smoothing. mask : array - Mask with 1's for significant pixels, 0's for masked pixels. + Mask with 1's for significant pixels, 0's for masked pixels. Notes ------ @@ -55,22 +55,22 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None): Parameters ----------- image : two-dimensional array - Greyscale input image to detect edges on; can be of any dtype. + Greyscale input image to detect edges on; can be of any dtype. sigma : float - Standard deviation of the Gaussian filter. + Standard deviation of the Gaussian filter. low_threshold : float - Lower bound for hysteresis thresholding (linking edges). - If none is provided, low_threshold is set to 10%. + Lower bound for hysteresis thresholding (linking edges). + If None, low_threshold is set to 10%. high_threshold : float - Upper bound for hysteresis thresholding (linking edges). - If none is provided, high_threshold is set to 20%. + Upper bound for hysteresis thresholding (linking edges). + If None, high_threshold is set to 20%. mask : array, dtype=bool, optional - Mask to limit the application of Canny to a certain area. + Mask to limit the application of Canny to a certain area. Returns ------- output : array (image) - The binary edge map. + The binary edge map. See also -------- @@ -112,7 +112,7 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None): >>> # Generate noisy image of a square >>> im = np.zeros((256, 256)) >>> im[64:-64, 64:-64] = 1 - >>> im += 0.2*np.random.random(im.shape) + >>> im += 0.2 * np.random.random(im.shape) >>> # First trial with the Canny filter, with the default smoothing >>> edges1 = filter.canny(im) >>> # Increase the smoothing for better results From 75b3fcd4dde6b20765e707dbba2eee0a6f82225c Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Sat, 29 Jun 2013 18:34:41 -0400 Subject: [PATCH 5/5] Included Tony's edits. --- skimage/filter/_canny.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skimage/filter/_canny.py b/skimage/filter/_canny.py index 1c5e5c19..185bfd40 100644 --- a/skimage/filter/_canny.py +++ b/skimage/filter/_canny.py @@ -54,22 +54,22 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None): Parameters ----------- - image : two-dimensional array + image : 2D array Greyscale input image to detect edges on; can be of any dtype. sigma : float Standard deviation of the Gaussian filter. low_threshold : float Lower bound for hysteresis thresholding (linking edges). - If None, low_threshold is set to 10%. + If None, low_threshold is set to 10% of dtype's max. high_threshold : float Upper bound for hysteresis thresholding (linking edges). - If None, high_threshold is set to 20%. + If None, high_threshold is set to 20% of dtype's max. mask : array, dtype=bool, optional Mask to limit the application of Canny to a certain area. Returns ------- - output : array (image) + output : 2D array (image) The binary edge map. See also