Add scharr edge filter

This commit is contained in:
Johannes Schönberger
2012-10-12 19:10:46 +02:00
parent 635e75bb7d
commit f22a014ffa
3 changed files with 228 additions and 9 deletions
+120
View File
@@ -9,6 +9,7 @@ def test_sobel_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"""
np.random.seed(0)
@@ -16,6 +17,7 @@ def test_sobel_mask():
np.zeros((10, 10), bool))
assert (np.all(result == 0))
def test_sobel_horizontal():
"""Sobel on an edge should be a horizontal line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -26,6 +28,7 @@ def test_sobel_horizontal():
assert (np.all(result[i == 0] == 1))
assert (np.all(result[np.abs(i) > 1] == 0))
def test_sobel_vertical():
"""Sobel on a vertical edge should be a vertical line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -41,6 +44,7 @@ def test_hsobel_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"""
np.random.seed(0)
@@ -48,6 +52,7 @@ def test_hsobel_mask():
np.zeros((10, 10), bool))
assert (np.all(result == 0))
def test_hsobel_horizontal():
"""Horizontal Sobel on an edge should be a horizontal line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -58,6 +63,7 @@ def test_hsobel_horizontal():
assert (np.all(result[i == 0] == 1))
assert (np.all(result[np.abs(i) > 1] == 0))
def test_hsobel_vertical():
"""Horizontal Sobel on a vertical edge should be zero"""
i, j = np.mgrid[-5:6, -5:6]
@@ -71,6 +77,7 @@ def test_vsobel_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"""
np.random.seed(0)
@@ -78,6 +85,7 @@ def test_vsobel_mask():
np.zeros((10, 10), bool))
assert (np.all(result == 0))
def test_vsobel_vertical():
"""Vertical Sobel on an edge should be a vertical line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -88,6 +96,7 @@ def test_vsobel_vertical():
assert (np.all(result[j == 0] == 1))
assert (np.all(result[np.abs(j) > 1] == 0))
def test_vsobel_horizontal():
"""vertical Sobel on a horizontal edge should be zero"""
i, j = np.mgrid[-5:6, -5:6]
@@ -97,11 +106,114 @@ def test_vsobel_horizontal():
assert (np.all(np.abs(result) < eps))
def test_scharr_zeros():
"""Sobel 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():
"""Sobel 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))
assert (np.all(result == 0))
def test_scharr_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.scharr(image)
# Fudge the eroded points
i[np.abs(j) == 5] = 10000
assert (np.all(result[i == 0] == 1))
assert (np.all(result[np.abs(i) > 1] == 0))
def test_scharr_vertical():
"""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.scharr(image)
j[np.abs(i) == 5] = 10000
assert (np.all(result[j == 0] == 1))
assert (np.all(result[np.abs(j) > 1] == 0))
def test_hscharr_zeros():
"""Horizontal sobel 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 Sobel 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))
assert (np.all(result == 0))
def test_hscharr_horizontal():
"""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.hscharr(image)
# Fudge the eroded points
i[np.abs(j) == 5] = 10000
assert (np.all(result[i == 0] == 1))
assert (np.all(result[np.abs(i) > 1] == 0))
def test_hscharr_vertical():
"""Horizontal Sobel on a vertical edge should be zero"""
i, j = np.mgrid[-5:6, -5:6]
image = (j >= 0).astype(float)
result = F.hscharr(image)
assert (np.all(result == 0))
def test_vscharr_zeros():
"""Vertical sobel 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 Sobel 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))
assert (np.all(result == 0))
def test_vscharr_vertical():
"""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.vscharr(image)
# Fudge the eroded points
j[np.abs(i) == 5] = 10000
assert (np.all(result[j == 0] == 1))
assert (np.all(result[np.abs(j) > 1] == 0))
def test_vscharr_horizontal():
"""vertical Sobel on a horizontal edge should be zero"""
i, j = np.mgrid[-5:6, -5:6]
image = (i >= 0).astype(float)
result = F.vscharr(image)
eps = .000001
assert (np.all(np.abs(result) < eps))
def test_prewitt_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"""
np.random.seed(0)
@@ -110,6 +222,7 @@ def test_prewitt_mask():
eps = .000001
assert (np.all(np.abs(result) < eps))
def test_prewitt_horizontal():
"""Prewitt on an edge should be a horizontal line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -121,6 +234,7 @@ def test_prewitt_horizontal():
assert (np.all(result[i == 0] == 1))
assert (np.all(np.abs(result[np.abs(i) > 1]) < eps))
def test_prewitt_vertical():
"""Prewitt on a vertical edge should be a vertical line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -137,6 +251,7 @@ def test_hprewitt_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"""
np.random.seed(0)
@@ -145,6 +260,7 @@ def test_hprewitt_mask():
eps = .000001
assert (np.all(np.abs(result) < eps))
def test_hprewitt_horizontal():
"""Horizontal prewitt on an edge should be a horizontal line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -156,6 +272,7 @@ def test_hprewitt_horizontal():
assert (np.all(result[i == 0] == 1))
assert (np.all(np.abs(result[np.abs(i) > 1]) < eps))
def test_hprewitt_vertical():
"""Horizontal prewitt on a vertical edge should be zero"""
i, j = np.mgrid[-5:6, -5:6]
@@ -170,6 +287,7 @@ def test_vprewitt_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"""
np.random.seed(0)
@@ -177,6 +295,7 @@ def test_vprewitt_mask():
np.zeros((10, 10), bool))
assert (np.all(result == 0))
def test_vprewitt_vertical():
"""Vertical prewitt on an edge should be a vertical line"""
i, j = np.mgrid[-5:6, -5:6]
@@ -188,6 +307,7 @@ def test_vprewitt_vertical():
eps = .000001
assert (np.all(np.abs(result[np.abs(j) > 1]) < eps))
def test_vprewitt_horizontal():
"""Vertical prewitt on a horizontal edge should be zero"""
i, j = np.mgrid[-5:6, -5:6]