From f3dd496d1f87ef380ee9a0e86869f331638d4025 Mon Sep 17 00:00:00 2001 From: Pieter Holtzhausen Date: Mon, 18 Apr 2011 12:07:27 +0200 Subject: [PATCH] Fixed PEP8 and added numpy documentation. --- scikits/image/filter/edges.py | 156 ++++++++++++++------ scikits/image/filter/tests/test_edges.py | 174 +++++++++++------------ scikits/image/version.py | 3 +- 3 files changed, 199 insertions(+), 134 deletions(-) diff --git a/scikits/image/filter/edges.py b/scikits/image/filter/edges.py index dae11cf9..d4fbc5ef 100644 --- a/scikits/image/filter/edges.py +++ b/scikits/image/filter/edges.py @@ -1,4 +1,4 @@ -'''edges.py - Sobel edge filter +"""edges.py - Sobel edge filter Originally part of CellProfiler, code licensed under both GPL and BSD licenses. Website: http://www.cellprofiler.org @@ -7,124 +7,190 @@ Copyright (c) 2009-2011 Broad Institute All rights reserved. Original author: Lee Kamentsky -''' +""" import numpy as np from scipy.ndimage import convolve, binary_erosion, generate_binary_structure def sobel(image, mask=None): - '''Calculate the absolute magnitude Sobel to find the edges - - image - image to process - mask - mask of relevant points + """Calculate the absolute magnitude Sobel to find the edges. + + Parameters + ---------- + image : array_like, dtype=float + Image to process + mask : array_like, dtype=bool, optional + An optional mask to limit the application to a certain area + Returns + ------- + output : ndarray + The Sobel edge map. + + Notes + ----- Take the square root of the sum of the squares of the horizontal and vertical Sobels to get a magnitude that's somewhat insensitive to direction. Note that scipy's Sobel returns a directional Sobel which isn't useful for edge detection in its raw form. - ''' + """ return np.sqrt(hsobel(image,mask)**2 + vsobel(image,mask)**2) def hsobel(image, mask=None): - '''Find the horizontal edges of an image using the Sobel transform + """Find the horizontal edges of an image using the Sobel transform - image - image to process - mask - mask of relevant points + Parameters + ---------- + image : array_like, dtype=float + Image to process + mask : array_like, dtype=bool, optional + An optional mask to limit the application to a certain area. + Returns + ------- + output : ndarray + The Sobel edge map. + + Notes + ----- We use the following kernel and return the absolute value of the result at each point: 1 2 1 0 0 0 -1 -2 -1 - ''' - if mask == None: + """ + if mask is None: mask = np.ones(image.shape, bool) big_mask = binary_erosion(mask, - generate_binary_structure(2,2), + generate_binary_structure(2, 2), border_value = 0) result = np.abs(convolve(image, np.array([[ 1, 2, 1], [ 0, 0, 0], - [-1,-2,-1]]).astype(float)/4.0)) - result[big_mask==False] = 0 + [-1,-2,-1]]).astype(float) / 4.0)) + result[big_mask == False] = 0 return result def vsobel(image, mask=None): - '''Find the vertical edges of an image using the Sobel transform + """Find the vertical edges of an image using the Sobel transform. - image - image to process - mask - mask of relevant points + Parameters + ---------- + image : array_like, dtype=float + Image to process + mask : array_like, dtype=bool, optional + An optional mask to limit the application to a certain area + Returns + ------- + output : ndarray + The Sobel edge map. + + Notes + ----- We use the following kernel and return the absolute value of the result at each point: 1 0 -1 2 0 -2 1 0 -1 - ''' - if mask == None: + """ + if mask is None: mask = np.ones(image.shape, bool) big_mask = binary_erosion(mask, - generate_binary_structure(2,2), + generate_binary_structure(2, 2), border_value = 0) result = np.abs(convolve(image, np.array([[ 1, 0,-1], [ 2, 0,-2], - [ 1, 0,-1]]).astype(float)/4.0)) - result[big_mask==False] = 0 + [ 1, 0,-1]]).astype(float) / 4.0)) + result[big_mask == False] = 0 return result def prewitt(image, mask=None): - '''Find the edge magnitude using the Prewitt transform + """Find the edge magnitude using the Prewitt transform. - image - image to process - mask - mask of relevant points + Parameters + ---------- + image : array_like, dtype=float + Image to process + mask : array_like, dtype=bool, optional + An optional mask to limit the application to a certain area + Returns + ------- + output : ndarray + The Prewitt edge map. + + Notes + ----- Return the square root of the sum of squares of the horizontal and vertical Prewitt transforms. - ''' - return np.sqrt(hprewitt(image,mask)**2 + vprewitt(image,mask)**2) + """ + return np.sqrt(hprewitt(image, mask) ** 2 + vprewitt(image, mask) ** 2) def hprewitt(image, mask=None): - '''Find the horizontal edges of an image using the Prewitt transform + """Find the horizontal edges of an image using the Prewitt transform. - image - image to process - mask - mask of relevant points + Parameters + ---------- + image : array_like, dtype=float + Image to process + mask : array_like, dtype=bool, optional + An optional mask to limit the application to a certain area + Returns + ------- + output : ndarray + The Prewitt edge map. + + Notes + ----- We use the following kernel and return the absolute value of the result at each point: 1 1 1 0 0 0 -1 -1 -1 - ''' - if mask == None: + """ + if mask is None: mask = np.ones(image.shape, bool) big_mask = binary_erosion(mask, - generate_binary_structure(2,2), + generate_binary_structure(2, 2), border_value = 0) result = np.abs(convolve(image, np.array([[ 1, 1, 1], [ 0, 0, 0], - [-1,-1,-1]]).astype(float)/3.0)) - result[big_mask==False] = 0 + [-1,-1,-1]]).astype(float) / 3.0)) + result[big_mask == False] = 0 return result def vprewitt(image, mask=None): - '''Find the vertical edges of an image using the Prewitt transform + """Find the vertical edges of an image using the Prewitt transform. - image - image to process - mask - mask of relevant points + Parameters + ---------- + image : array_like, dtype=float + Image to process + mask : array_like, dtype=bool, optional + An optional mask to limit the application to a certain area + Returns + ------- + output : ndarray + The Prewitt edge map. + + Notes + ----- We use the following kernel and return the absolute value of the result at each point: 1 0 -1 1 0 -1 1 0 -1 - ''' - if mask == None: + """ + if mask is None: mask = np.ones(image.shape, bool) big_mask = binary_erosion(mask, - generate_binary_structure(2,2), - border_value = 0) + generate_binary_structure(2, 2), + border_value=0) result = np.abs(convolve(image, np.array([[ 1, 0,-1], [ 1, 0,-1], - [ 1, 0,-1]]).astype(float)/3.0)) - result[big_mask==False] = 0 + [ 1, 0,-1]]).astype(float) / 3.0)) + result[big_mask == False] = 0 return result diff --git a/scikits/image/filter/tests/test_edges.py b/scikits/image/filter/tests/test_edges.py index 015d0a7e..1ce479f0 100644 --- a/scikits/image/filter/tests/test_edges.py +++ b/scikits/image/filter/tests/test_edges.py @@ -5,193 +5,193 @@ import scikits.image.filter as F class TestSobel(): def test_00_00_zeros(self): - '''Sobel on an array of all zeros''' - result = F.sobel(np.zeros((10,10)), np.ones((10,10),bool)) - assert (np.all(result==0)) + """Sobel on an array of all zeros""" + result = F.sobel(np.zeros((10, 10)), np.ones((10, 10), bool)) + assert (np.all(result == 0)) def test_00_01_mask(self): - '''Sobel on a masked array should be zero''' + """Sobel on a masked array should be zero""" np.random.seed(0) - result = F.sobel(np.random.uniform(size=(10,10)), - np.zeros((10,10),bool)) + result = F.sobel(np.random.uniform(size=(10, 10)), + np.zeros((10, 10), bool)) assert (np.all(result == 0)) def test_01_01_horizontal(self): - '''Sobel on an edge should be a horizontal line''' - i,j = np.mgrid[-5:6,-5:6] - image = (i>=0).astype(float) + """Sobel on an edge should be a horizontal line""" + i, j = np.mgrid[-5:6, -5:6] + image = (i >= 0).astype(float) result = F.sobel(image) # Fudge the eroded points - i[np.abs(j)==5] = 10000 - assert (np.all(result[i==0] == 1)) + i[np.abs(j) == 5] = 10000 + assert (np.all(result[i == 0] == 1)) assert (np.all(result[np.abs(i) > 1] == 0)) def test_01_02_vertical(self): - '''Sobel on a vertical edge should be a vertical line''' - i,j = np.mgrid[-5:6,-5:6] - image = (j>=0).astype(float) + """Sobel on a vertical edge should be a vertical line""" + i,j = np.mgrid[-5:6, -5:6] + image = (j >= 0).astype(float) result = F.sobel(image) - j[np.abs(i)==5] = 10000 - assert (np.all(result[j==0] == 1)) + j[np.abs(i) == 5] = 10000 + assert (np.all(result[j == 0] == 1)) assert (np.all(result[np.abs(j) > 1] == 0)) class TestHSobel(): def test_00_00_zeros(self): - '''Horizontal sobel on an array of all zeros''' - result = F.hsobel(np.zeros((10,10)), np.ones((10,10),bool)) - assert (np.all(result==0)) + """Horizontal sobel on an array of all zeros""" + result = F.hsobel(np.zeros((10, 10)), np.ones((10, 10), bool)) + assert (np.all(result == 0)) def test_00_01_mask(self): - '''Horizontal Sobel on a masked array should be zero''' + """Horizontal Sobel on a masked array should be zero""" np.random.seed(0) - result = F.hsobel(np.random.uniform(size=(10,10)), - np.zeros((10,10),bool)) + result = F.hsobel(np.random.uniform(size=(10, 10)), + np.zeros((10, 10), bool)) assert (np.all(result == 0)) def test_01_01_horizontal(self): - '''Horizontal Sobel on an edge should be a horizontal line''' - i,j = np.mgrid[-5:6,-5:6] - image = (i>=0).astype(float) + """Horizontal Sobel on an edge should be a horizontal line""" + i,j = np.mgrid[-5:6, -5:6] + image = (i >= 0).astype(float) result = F.hsobel(image) # Fudge the eroded points - i[np.abs(j)==5] = 10000 - assert (np.all(result[i==0] == 1)) + i[np.abs(j) == 5] = 10000 + assert (np.all(result[i == 0] == 1)) assert (np.all(result[np.abs(i) > 1] == 0)) def test_01_02_vertical(self): - '''Horizontal Sobel on a vertical edge should be zero''' - i,j = np.mgrid[-5:6,-5:6] - image = (j>=0).astype(float) + """Horizontal Sobel on a vertical edge should be zero""" + i,j = np.mgrid[-5:6, -5:6] + image = (j >= 0).astype(float) result = F.hsobel(image) assert (np.all(result == 0)) class TestVSobel(): def test_00_00_zeros(self): - '''Vertical sobel on an array of all zeros''' - result = F.vsobel(np.zeros((10,10)), np.ones((10,10),bool)) - assert (np.all(result==0)) + """Vertical sobel on an array of all zeros""" + result = F.vsobel(np.zeros((10, 10)), np.ones((10, 10), bool)) + assert (np.all(result == 0)) def test_00_01_mask(self): - '''Vertical Sobel on a masked array should be zero''' + """Vertical Sobel on a masked array should be zero""" np.random.seed(0) - result = F.vsobel(np.random.uniform(size=(10,10)), - np.zeros((10,10),bool)) + result = F.vsobel(np.random.uniform(size=(10, 10)), + np.zeros((10, 10), bool)) assert (np.all(result == 0)) def test_01_01_vertical(self): - '''Vertical Sobel on an edge should be a vertical line''' - i,j = np.mgrid[-5:6,-5:6] - image = (j>=0).astype(float) + """Vertical Sobel on an edge should be a vertical line""" + i,j = np.mgrid[-5:6, -5:6] + image = (j >= 0).astype(float) result = F.vsobel(image) # Fudge the eroded points - j[np.abs(i)==5] = 10000 - assert (np.all(result[j==0] == 1)) + j[np.abs(i) == 5] = 10000 + assert (np.all(result[j == 0] == 1)) assert (np.all(result[np.abs(j) > 1] == 0)) def test_01_02_horizontal(self): - '''vertical Sobel on a horizontal edge should be zero''' - i,j = np.mgrid[-5:6,-5:6] - image = (i>=0).astype(float) + """vertical Sobel on a horizontal edge should be zero""" + i,j = np.mgrid[-5:6, -5:6] + image = (i >= 0).astype(float) result = F.vsobel(image) eps = .000001 assert (np.all(np.abs(result) < eps)) class TestPrewitt(): def test_00_00_zeros(self): - '''Prewitt on an array of all zeros''' - result = F.prewitt(np.zeros((10,10)), np.ones((10,10),bool)) - assert (np.all(result==0)) + """Prewitt on an array of all zeros""" + result = F.prewitt(np.zeros((10, 10)), np.ones((10, 10), bool)) + assert (np.all(result == 0)) def test_00_01_mask(self): - '''Prewitt on a masked array should be zero''' + """Prewitt on a masked array should be zero""" np.random.seed(0) - result = F.prewitt(np.random.uniform(size=(10,10)), - np.zeros((10,10),bool)) + result = F.prewitt(np.random.uniform(size=(10, 10)), + np.zeros((10, 10), bool)) eps = .000001 assert (np.all(np.abs(result) < eps)) def test_01_01_horizontal(self): - '''Prewitt on an edge should be a horizontal line''' - i,j = np.mgrid[-5:6,-5:6] - image = (i>=0).astype(float) + """Prewitt on an edge should be a horizontal line""" + i,j = np.mgrid[-5:6, -5:6] + image = (i >= 0).astype(float) result = F.prewitt(image) # Fudge the eroded points - i[np.abs(j)==5] = 10000 + i[np.abs(j) == 5] = 10000 eps = .000001 - assert (np.all(result[i==0] == 1)) + assert (np.all(result[i == 0] == 1)) assert (np.all(np.abs(result[np.abs(i) > 1]) < eps)) def test_01_02_vertical(self): - '''Prewitt on a vertical edge should be a vertical line''' - i,j = np.mgrid[-5:6,-5:6] - image = (j>=0).astype(float) + """Prewitt on a vertical edge should be a vertical line""" + i,j = np.mgrid[-5:6, -5:6] + image = (j >= 0).astype(float) result = F.prewitt(image) eps = .000001 j[np.abs(i)==5] = 10000 - assert (np.all(result[j==0] == 1)) + assert (np.all(result[j == 0] == 1)) assert (np.all(np.abs(result[np.abs(j) > 1]) < eps)) class TestHPrewitt(): def test_00_00_zeros(self): - '''Horizontal sobel on an array of all zeros''' - result = F.hprewitt(np.zeros((10,10)), np.ones((10,10),bool)) - assert (np.all(result==0)) + """Horizontal sobel on an array of all zeros""" + result = F.hprewitt(np.zeros((10, 10)), np.ones((10, 10), bool)) + assert (np.all(result == 0)) def test_00_01_mask(self): - '''Horizontal prewitt on a masked array should be zero''' + """Horizontal prewitt on a masked array should be zero""" np.random.seed(0) - result = F.hprewitt(np.random.uniform(size=(10,10)), - np.zeros((10,10),bool)) + result = F.hprewitt(np.random.uniform(size=(10, 10)), + np.zeros((10, 10), bool)) eps = .000001 assert (np.all(np.abs(result) < eps)) def test_01_01_horizontal(self): - '''Horizontal prewitt on an edge should be a horizontal line''' - i,j = np.mgrid[-5:6,-5:6] - image = (i>=0).astype(float) + """Horizontal prewitt on an edge should be a horizontal line""" + i,j = np.mgrid[-5:6, -5:6] + image = (i >= 0).astype(float) result = F.hprewitt(image) # Fudge the eroded points - i[np.abs(j)==5] = 10000 + i[np.abs(j) == 5] = 10000 eps = .000001 - assert (np.all(result[i==0] == 1)) + assert (np.all(result[i == 0] == 1)) assert (np.all(np.abs(result[np.abs(i) > 1]) < eps)) def test_01_02_vertical(self): - '''Horizontal prewitt on a vertical edge should be zero''' - i,j = np.mgrid[-5:6,-5:6] - image = (j>=0).astype(float) + """Horizontal prewitt on a vertical edge should be zero""" + i,j = np.mgrid[-5:6, -5:6] + image = (j >= 0).astype(float) result = F.hprewitt(image) eps = .000001 assert (np.all(np.abs(result) < eps)) class TestVPrewitt(): def test_00_00_zeros(self): - '''Vertical prewitt on an array of all zeros''' - result = F.vprewitt(np.zeros((10,10)), np.ones((10,10),bool)) - assert (np.all(result==0)) + """Vertical prewitt on an array of all zeros""" + result = F.vprewitt(np.zeros((10, 10)), np.ones((10, 10), bool)) + assert (np.all(result == 0)) def test_00_01_mask(self): - '''Vertical prewitt on a masked array should be zero''' + """Vertical prewitt on a masked array should be zero""" np.random.seed(0) - result = F.vprewitt(np.random.uniform(size=(10,10)), - np.zeros((10,10),bool)) + result = F.vprewitt(np.random.uniform(size=(10, 10)), + np.zeros((10, 10), bool)) assert (np.all(result == 0)) def test_01_01_vertical(self): - '''Vertical prewitt on an edge should be a vertical line''' - i,j = np.mgrid[-5:6,-5:6] - image = (j>=0).astype(float) + """Vertical prewitt on an edge should be a vertical line""" + i,j = np.mgrid[-5:6, -5:6] + image = (j >= 0).astype(float) result = F.vprewitt(image) # Fudge the eroded points - j[np.abs(i)==5] = 10000 - assert (np.all(result[j==0] == 1)) + j[np.abs(i) == 5] = 10000 + assert (np.all(result[j == 0] == 1)) eps = .000001 assert (np.all(np.abs(result[np.abs(j) > 1]) < eps)) def test_01_02_horizontal(self): - '''vertical prewitt on a horizontal edge should be zero''' - i,j = np.mgrid[-5:6,-5:6] - image = (i>=0).astype(float) + """Vertical prewitt on a horizontal edge should be zero""" + i,j = np.mgrid[-5:6, -5:6] + image = (i >= 0).astype(float) result = F.vprewitt(image) eps = .000001 assert (np.all(np.abs(result) < eps)) diff --git a/scikits/image/version.py b/scikits/image/version.py index f30761c7..4fadcd3a 100644 --- a/scikits/image/version.py +++ b/scikits/image/version.py @@ -1,2 +1 @@ -# THIS FILE IS GENERATED FROM THE SCIKITS.IMAGE SETUP.PY -version='0.3dev' +version='unbuilt-dev'