From 7602d8eed5108698fe28268e6981886e5767fa34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sat, 27 Apr 2013 14:56:02 +0200 Subject: [PATCH 1/4] Pre-build edge filter weights and use views --- skimage/filter/edges.py | 58 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/skimage/filter/edges.py b/skimage/filter/edges.py index d06b2efa..329433f3 100644 --- a/skimage/filter/edges.py +++ b/skimage/filter/edges.py @@ -16,6 +16,26 @@ from scipy.ndimage import convolve, binary_erosion, generate_binary_structure EROSION_SELEM = generate_binary_structure(2, 2) +HSOBEL_WEIGHTS = np.array([[ 1, 2, 1], + [ 0, 0, 0], + [-1,-2,-1]]) / 4.0 +VSOBEL_WEIGHTS = HSOBEL_WEIGHTS.T + +HSCHARR_WEIGHTS = np.array([[ 3, 10, 3], + [ 0, 0, 0], + [-3, -10, -3]]) / 16.0 +VSCHARR_WEIGHTS = HSCHARR_WEIGHTS.T + +HPREWITT_WEIGHTS = np.array([[ 1, 1, 1], + [ 0, 0, 0], + [-1,-1,-1]]) / 3.0 +VPREWITT_WEIGHTS = HPREWITT_WEIGHTS.T + +ROBERTS_PD_WEIGHTS = np.array([[ 1, 0], + [ 0, -1]], dtype=np.double) +ROBERTS_ND_WEIGHTS = np.array([[0, 1], + [-1, 0]], dtype=np.double) + def _mask_filter_result(result, mask): """Return result after masking. @@ -91,10 +111,7 @@ def hsobel(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[ 1, 2, 1], - [ 0, 0, 0], - [-1,-2,-1]]).astype(float) / 4.0)) + result = np.abs(convolve(image, HSOBEL_WEIGHTS)) return _mask_filter_result(result, mask) @@ -126,10 +143,7 @@ def vsobel(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[1, 0, -1], - [2, 0, -2], - [1, 0, -1]]).astype(float) / 4.0)) + result = np.abs(convolve(image, VSOBEL_WEIGHTS)) return _mask_filter_result(result, mask) @@ -198,10 +212,7 @@ def hscharr(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[ 3, 10, 3], - [ 0, 0, 0], - [-3, -10, -3]]).astype(float) / 16.0)) + result = np.abs(convolve(image, HSCHARR_WEIGHTS)) return _mask_filter_result(result, mask) @@ -238,10 +249,7 @@ def vscharr(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[ 3, 0, -3], - [10, 0, -10], - [ 3, 0, -3]]).astype(float) / 16.0)) + result = np.abs(convolve(image, VSCHARR_WEIGHTS)) return _mask_filter_result(result, mask) @@ -298,10 +306,7 @@ def hprewitt(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[ 1, 1, 1], - [ 0, 0, 0], - [-1,-1,-1]]).astype(float) / 3.0)) + result = np.abs(convolve(image, HPREWITT_WEIGHTS)) return _mask_filter_result(result, mask) @@ -333,10 +338,7 @@ def vprewitt(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[1, 0, -1], - [1, 0, -1], - [1, 0, -1]]).astype(float) / 3.0)) + result = np.abs(convolve(image, VPREWITT_WEIGHTS)) return _mask_filter_result(result, mask) @@ -391,9 +393,7 @@ def roberts_positive_diagonal(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[ 1, 0], - [ 0, -1]]).astype(float))) + result = np.abs(convolve(image, ROBERTS_PD_WEIGHTS)) return _mask_filter_result(result, mask) @@ -427,7 +427,5 @@ def roberts_negative_diagonal(image, mask=None): """ image = img_as_float(image) - result = np.abs(convolve(image, - np.array([[0, 1], - [-1, 0]]).astype(float))) + result = np.abs(convolve(image, ROBERTS_ND_WEIGHTS)) return _mask_filter_result(result, mask) From c8fc92be82f9055d4c421425f0ca9cfa6a99f5f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sat, 27 Apr 2013 14:56:41 +0200 Subject: [PATCH 2/4] Remove outdated author in doc string --- skimage/filter/edges.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/skimage/filter/edges.py b/skimage/filter/edges.py index 329433f3..46d2907e 100644 --- a/skimage/filter/edges.py +++ b/skimage/filter/edges.py @@ -1,14 +1,3 @@ -"""edges.py - Edge filters - -Sobel and Prewitt filters originally part of CellProfiler, code licensed under -both GPL and BSD licenses. -Website: http://www.cellprofiler.org -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 from skimage import img_as_float from scipy.ndimage import convolve, binary_erosion, generate_binary_structure From cd78e81de3af9f360f92cbacca3c16d916629945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sat, 27 Apr 2013 15:15:46 +0200 Subject: [PATCH 3/4] Fix output array dimensions in doc string --- skimage/filter/edges.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/skimage/filter/edges.py b/skimage/filter/edges.py index 46d2907e..cfc0b15c 100644 --- a/skimage/filter/edges.py +++ b/skimage/filter/edges.py @@ -57,7 +57,7 @@ def sobel(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Sobel edge map. Notes @@ -86,7 +86,7 @@ def hsobel(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Sobel edge map. Notes @@ -118,7 +118,7 @@ def vsobel(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Sobel edge map. Notes @@ -150,7 +150,7 @@ def scharr(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Scharr edge map. Notes @@ -182,7 +182,7 @@ def hscharr(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Scharr edge map. Notes @@ -219,7 +219,7 @@ def vscharr(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Scharr edge map. Notes @@ -256,7 +256,7 @@ def prewitt(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Prewitt edge map. Notes @@ -281,7 +281,7 @@ def hprewitt(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Prewitt edge map. Notes @@ -313,7 +313,7 @@ def vprewitt(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Prewitt edge map. Notes @@ -345,7 +345,7 @@ def roberts(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Roberts' Cross edge map. """ return np.sqrt(roberts_positive_diagonal(image, mask)**2 + @@ -369,7 +369,7 @@ def roberts_positive_diagonal(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Robert's edge map. Notes @@ -403,7 +403,7 @@ def roberts_negative_diagonal(image, mask=None): Returns ------- - output : ndarray + output : 2-D array The Robert's edge map. Notes From cec8516f69c693b67d659a151b993189d873d6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 6 May 2013 12:11:09 +0200 Subject: [PATCH 4/4] Readd Cellprofiler license notes to edges.py --- skimage/filter/edges.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/skimage/filter/edges.py b/skimage/filter/edges.py index cfc0b15c..7a70f00b 100644 --- a/skimage/filter/edges.py +++ b/skimage/filter/edges.py @@ -1,3 +1,14 @@ +""" + +Sobel and Prewitt filters originally part of CellProfiler, code licensed under +both GPL and BSD licenses. +Website: http://www.cellprofiler.org +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 from skimage import img_as_float from scipy.ndimage import convolve, binary_erosion, generate_binary_structure