Minor cleanups

* PEP8 clean ups (space after commas, docstring spacing, prefer parentheses over line continuation)
* Simplify creation of test images.
* Use `plt.subplots` instead of state machine
* Minor wording modifications
This commit is contained in:
Tony S Yu
2013-04-25 22:47:22 -05:00
parent 18839cdf08
commit 5c3c75cd09
3 changed files with 63 additions and 66 deletions
+8 -11
View File
@@ -1,23 +1,20 @@
import matplotlib
import matplotlib.pyplot as plt
from skimage.data import camera
from skimage.filter import roberts,sobel
from skimage.filter import roberts, sobel
image = camera()
edge_roberts = roberts(image)
edge_sobel = sobel(image)
plt.figure(figsize=(8, 2.5))
plt.subplot(1, 2, 1)
plt.imshow(edge_roberts, cmap=plt.cm.gray)
plt.title('Roberts Edge Detection')
plt.axis('off')
fig, (ax0, ax1) = plt.subplots(ncols=2)
plt.subplot(1, 2, 2)
plt.imshow(edge_sobel, cmap=plt.cm.gray)
plt.title('Sobel Edge Detection')
plt.axis('off')
ax0.imshow(edge_roberts, cmap=plt.cm.gray)
ax0.set_title('Roberts Edge Detection')
ax0.axis('off')
ax1.imshow(edge_sobel, cmap=plt.cm.gray)
ax1.set_title('Sobel Edge Detection')
ax1.axis('off')
plt.show()
+7 -5
View File
@@ -341,7 +341,7 @@ def vprewitt(image, mask=None):
def roberts(image, mask=None):
"""Find the edge magnitude using Roberts' Cross Operator.
"""Find the edge magnitude using Roberts' cross operator.
Parameters
----------
@@ -357,14 +357,14 @@ def roberts(image, mask=None):
output : ndarray
The Roberts' Cross edge map.
"""
return np.sqrt(roberts_positive_diagonal(image, mask)**2 +\
return np.sqrt(roberts_positive_diagonal(image, mask)**2 +
roberts_negative_diagonal(image, mask)**2)
def roberts_positive_diagonal(image, mask=None):
"""Find the cross edges of an image using the Roberts' Cross operator.
"""Find the cross edges of an image using Roberts' cross operator.
The kernel is applied to the input image, to produce separate measurements
The kernel is applied to the input image to produce separate measurements
of the gradient component one orientation.
Parameters
@@ -399,8 +399,10 @@ def roberts_positive_diagonal(image, mask=None):
def roberts_negative_diagonal(image, mask=None):
"""Find the cross edges of an image using the Roberts' Cross operator.
The kernel is applied to the input image, to produce separate measurements
The kernel is applied to the input image to produce separate measurements
of the gradient component one orientation.
Parameters
----------
image : 2-D array
+48 -50
View File
@@ -4,43 +4,41 @@ from numpy.testing import assert_array_almost_equal as assert_close
import skimage.filter as F
from skimage.filter.edges import _mask_filter_result
def test_roberts_zeros():
"""Roberts' on an array of all zeros"""
"""Roberts' filter on an array of all zeros."""
result = F.roberts(np.zeros((10, 10)), np.ones((10, 10), bool))
assert (np.all(result == 0))
def test_roberts_diagonal1():
"""Roberts' on an edge should be a one diagonal"""
"""Roberts' filter on a diagonal edge should be a diagonal line."""
image = np.tri(10, 10, 0)
expected = ~(np.tri(10, 10, -1).astype(bool) | \
expected = ~(np.tri(10, 10, -1).astype(bool) |
np.tri(10, 10, -2).astype(bool).transpose())
expected = _mask_filter_result(expected,None)
expected = _mask_filter_result(expected, None)
result = F.roberts(image).astype(bool)
assert_close(result,expected)
assert_close(result, expected)
def test_roberts_diagonal2():
"""Roberts' on an edge should be a other diagonal"""
diagonal = np.tri(10, 10, 0,dtype=int)
rev_diagonal = np.rot90(diagonal.transpose(),1)
image = (rev_diagonal > 0).astype(float)
expected = ~np.rot90((np.tri(10, 10, -1).astype(bool) | \
np.tri(10, 10, -2).astype(bool).transpose()),1)
expected = _mask_filter_result(expected,None)
"""Roberts' filter on a diagonal edge should be a diagonal line."""
image = np.rot90(np.tri(10, 10, 0), 3)
expected = ~np.rot90(np.tri(10, 10, -1).astype(bool) |
np.tri(10, 10, -2).astype(bool).transpose())
expected = _mask_filter_result(expected, None)
result = F.roberts(image).astype(bool)
assert_close(result,expected)
assert_close(result, expected)
def test_sobel_zeros():
"""Sobel on an array of all zeros"""
"""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_sobel_mask():
"""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))
@@ -48,7 +46,7 @@ def test_sobel_mask():
def test_sobel_horizontal():
"""Sobel on an edge should be a horizontal line"""
"""Sobel on a horizontal edge should be a horizontal line."""
i, j = np.mgrid[-5:6, -5:6]
image = (i >= 0).astype(float)
result = F.sobel(image)
@@ -59,7 +57,7 @@ def test_sobel_horizontal():
def test_sobel_vertical():
"""Sobel on a vertical edge should be a vertical line"""
"""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)
@@ -69,13 +67,13 @@ def test_sobel_vertical():
def test_hsobel_zeros():
"""Horizontal sobel on an array of all zeros"""
"""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_hsobel_mask():
"""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))
@@ -83,7 +81,7 @@ def test_hsobel_mask():
def test_hsobel_horizontal():
"""Horizontal Sobel on an edge should be a horizontal line"""
"""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)
@@ -94,7 +92,7 @@ def test_hsobel_horizontal():
def test_hsobel_vertical():
"""Horizontal Sobel on a vertical edge should be zero"""
"""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)
@@ -102,13 +100,13 @@ def test_hsobel_vertical():
def test_vsobel_zeros():
"""Vertical sobel on an array of all zeros"""
"""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_vsobel_mask():
"""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))
@@ -116,7 +114,7 @@ def test_vsobel_mask():
def test_vsobel_vertical():
"""Vertical Sobel on an edge should be a vertical line"""
"""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)
@@ -127,7 +125,7 @@ def test_vsobel_vertical():
def test_vsobel_horizontal():
"""vertical Sobel on a horizontal edge should be zero"""
"""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)
@@ -136,13 +134,13 @@ def test_vsobel_horizontal():
def test_scharr_zeros():
"""Scharr on an array of all zeros"""
"""Scharr on an array of all zeros."""
result = F.scharr(np.zeros((10, 10)), np.ones((10, 10), bool))
assert (np.all(result == 0))
def test_scharr_mask():
"""Scharr on a masked array should be zero"""
"""Scharr on a masked array should be zero."""
np.random.seed(0)
result = F.scharr(np.random.uniform(size=(10, 10)),
np.zeros((10, 10), bool))
@@ -150,7 +148,7 @@ def test_scharr_mask():
def test_scharr_horizontal():
"""Scharr on an edge should be a horizontal line"""
"""Scharr on an edge should be a horizontal line."""
i, j = np.mgrid[-5:6, -5:6]
image = (i >= 0).astype(float)
result = F.scharr(image)
@@ -161,7 +159,7 @@ def test_scharr_horizontal():
def test_scharr_vertical():
"""Scharr on a vertical edge should be a vertical line"""
"""Scharr on a vertical edge should be a vertical line."""
i, j = np.mgrid[-5:6, -5:6]
image = (j >= 0).astype(float)
result = F.scharr(image)
@@ -171,13 +169,13 @@ def test_scharr_vertical():
def test_hscharr_zeros():
"""Horizontal Scharr on an array of all zeros"""
"""Horizontal Scharr on an array of all zeros."""
result = F.hscharr(np.zeros((10, 10)), np.ones((10, 10), bool))
assert (np.all(result == 0))
def test_hscharr_mask():
"""Horizontal Scharr on a masked array should be zero"""
"""Horizontal Scharr on a masked array should be zero."""
np.random.seed(0)
result = F.hscharr(np.random.uniform(size=(10, 10)),
np.zeros((10, 10), bool))
@@ -185,7 +183,7 @@ def test_hscharr_mask():
def test_hscharr_horizontal():
"""Horizontal Scharr on an edge should be a horizontal line"""
"""Horizontal Scharr on an edge should be a horizontal line."""
i, j = np.mgrid[-5:6, -5:6]
image = (i >= 0).astype(float)
result = F.hscharr(image)
@@ -196,7 +194,7 @@ def test_hscharr_horizontal():
def test_hscharr_vertical():
"""Horizontal Scharr on a vertical edge should be zero"""
"""Horizontal Scharr on a vertical edge should be zero."""
i, j = np.mgrid[-5:6, -5:6]
image = (j >= 0).astype(float)
result = F.hscharr(image)
@@ -204,13 +202,13 @@ def test_hscharr_vertical():
def test_vscharr_zeros():
"""Vertical Scharr on an array of all zeros"""
"""Vertical Scharr on an array of all zeros."""
result = F.vscharr(np.zeros((10, 10)), np.ones((10, 10), bool))
assert (np.all(result == 0))
def test_vscharr_mask():
"""Vertical Scharr on a masked array should be zero"""
"""Vertical Scharr on a masked array should be zero."""
np.random.seed(0)
result = F.vscharr(np.random.uniform(size=(10, 10)),
np.zeros((10, 10), bool))
@@ -218,7 +216,7 @@ def test_vscharr_mask():
def test_vscharr_vertical():
"""Vertical Scharr on an edge should be a vertical line"""
"""Vertical Scharr on an edge should be a vertical line."""
i, j = np.mgrid[-5:6, -5:6]
image = (j >= 0).astype(float)
result = F.vscharr(image)
@@ -229,7 +227,7 @@ def test_vscharr_vertical():
def test_vscharr_horizontal():
"""vertical Scharr on a horizontal edge should be zero"""
"""vertical Scharr on a horizontal edge should be zero."""
i, j = np.mgrid[-5:6, -5:6]
image = (i >= 0).astype(float)
result = F.vscharr(image)
@@ -238,13 +236,13 @@ def test_vscharr_horizontal():
def test_prewitt_zeros():
"""Prewitt on an array of all zeros"""
"""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_prewitt_mask():
"""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))
@@ -253,7 +251,7 @@ def test_prewitt_mask():
def test_prewitt_horizontal():
"""Prewitt on an edge should be a horizontal line"""
"""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)
@@ -265,7 +263,7 @@ def test_prewitt_horizontal():
def test_prewitt_vertical():
"""Prewitt on a vertical edge should be a vertical line"""
"""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)
@@ -276,13 +274,13 @@ def test_prewitt_vertical():
def test_hprewitt_zeros():
"""Horizontal prewitt on an array of all zeros"""
"""Horizontal prewitt 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_hprewitt_mask():
"""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))
@@ -291,7 +289,7 @@ def test_hprewitt_mask():
def test_hprewitt_horizontal():
"""Horizontal prewitt on an edge should be a horizontal line"""
"""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)
@@ -303,7 +301,7 @@ def test_hprewitt_horizontal():
def test_hprewitt_vertical():
"""Horizontal prewitt on a vertical edge should be zero"""
"""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)
@@ -312,13 +310,13 @@ def test_hprewitt_vertical():
def test_vprewitt_zeros():
"""Vertical prewitt on an array of all zeros"""
"""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_vprewitt_mask():
"""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))
@@ -326,7 +324,7 @@ def test_vprewitt_mask():
def test_vprewitt_vertical():
"""Vertical prewitt on an edge should be a vertical line"""
"""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)
@@ -338,7 +336,7 @@ def test_vprewitt_vertical():
def test_vprewitt_horizontal():
"""Vertical prewitt on a horizontal edge should be zero"""
"""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)