|
|
|
@@ -2,13 +2,13 @@ import numpy as np
|
|
|
|
|
from numpy.testing import (assert_array_almost_equal as assert_close,
|
|
|
|
|
assert_, assert_allclose)
|
|
|
|
|
|
|
|
|
|
import skimage.filters as F
|
|
|
|
|
from skimage import filters
|
|
|
|
|
from skimage.filters.edges import _mask_filter_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_roberts_zeros():
|
|
|
|
|
"""Roberts' filter on an array of all zeros."""
|
|
|
|
|
result = F.roberts(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.roberts(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert (np.all(result == 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -18,7 +18,7 @@ def test_roberts_diagonal1():
|
|
|
|
|
expected = ~(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)
|
|
|
|
|
result = filters.roberts(image).astype(bool)
|
|
|
|
|
assert_close(result, expected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -28,21 +28,21 @@ def test_roberts_diagonal2():
|
|
|
|
|
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)
|
|
|
|
|
result = filters.roberts(image).astype(bool)
|
|
|
|
|
assert_close(result, expected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sobel_zeros():
|
|
|
|
|
"""Sobel on an array of all zeros."""
|
|
|
|
|
result = F.sobel(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.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)
|
|
|
|
|
result = F.sobel(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
result = filters.sobel(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert (np.all(result == 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -50,7 +50,7 @@ def test_sobel_horizontal():
|
|
|
|
|
"""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) * np.sqrt(2)
|
|
|
|
|
result = filters.sobel(image) * np.sqrt(2)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
i[np.abs(j) == 5] = 10000
|
|
|
|
|
assert_allclose(result[i == 0], 1)
|
|
|
|
@@ -61,7 +61,7 @@ def test_sobel_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.sobel(image) * np.sqrt(2)
|
|
|
|
|
result = filters.sobel(image) * np.sqrt(2)
|
|
|
|
|
j[np.abs(i) == 5] = 10000
|
|
|
|
|
assert (np.all(result[j == 0] == 1))
|
|
|
|
|
assert (np.all(result[np.abs(j) > 1] == 0))
|
|
|
|
@@ -69,15 +69,15 @@ def test_sobel_vertical():
|
|
|
|
|
|
|
|
|
|
def test_hsobel_zeros():
|
|
|
|
|
"""Horizontal sobel on an array of all zeros."""
|
|
|
|
|
result = F.hsobel(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.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)
|
|
|
|
|
result = F.hsobel(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
result = filters.hsobel(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert (np.all(result == 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -85,7 +85,7 @@ def test_hsobel_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.hsobel(image)
|
|
|
|
|
result = filters.hsobel(image)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
i[np.abs(j) == 5] = 10000
|
|
|
|
|
assert (np.all(result[i == 0] == 1))
|
|
|
|
@@ -96,20 +96,20 @@ def test_hsobel_vertical():
|
|
|
|
|
"""Horizontal Sobel on a vertical edge should be zero."""
|
|
|
|
|
i, j = np.mgrid[-5:6, -5:6]
|
|
|
|
|
image = (j >= 0).astype(float) * np.sqrt(2)
|
|
|
|
|
result = F.hsobel(image)
|
|
|
|
|
result = filters.hsobel(image)
|
|
|
|
|
assert_allclose(result, 0, atol=1e-10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vsobel_zeros():
|
|
|
|
|
"""Vertical sobel on an array of all zeros."""
|
|
|
|
|
result = F.vsobel(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.vsobel(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vsobel_mask():
|
|
|
|
|
"""Vertical Sobel on a masked array should be zero."""
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
result = F.vsobel(np.random.uniform(size=(10, 10)),
|
|
|
|
|
result = filters.vsobel(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
@@ -118,7 +118,7 @@ def test_vsobel_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.vsobel(image)
|
|
|
|
|
result = filters.vsobel(image)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
j[np.abs(i) == 5] = 10000
|
|
|
|
|
assert (np.all(result[j == 0] == 1))
|
|
|
|
@@ -129,21 +129,21 @@ def test_vsobel_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.vsobel(image)
|
|
|
|
|
result = filters.vsobel(image)
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_scharr_zeros():
|
|
|
|
|
"""Scharr on an array of all zeros."""
|
|
|
|
|
result = F.scharr(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.scharr(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert (np.all(result < 1e-16))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_scharr_mask():
|
|
|
|
|
"""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))
|
|
|
|
|
result = filters.scharr(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -151,7 +151,7 @@ def test_scharr_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.scharr(image) * np.sqrt(2)
|
|
|
|
|
result = filters.scharr(image) * np.sqrt(2)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
i[np.abs(j) == 5] = 10000
|
|
|
|
|
assert_allclose(result[i == 0], 1)
|
|
|
|
@@ -162,7 +162,7 @@ def test_scharr_vertical():
|
|
|
|
|
"""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) * np.sqrt(2)
|
|
|
|
|
result = filters.scharr(image) * np.sqrt(2)
|
|
|
|
|
j[np.abs(i) == 5] = 10000
|
|
|
|
|
assert_allclose(result[j == 0], 1)
|
|
|
|
|
assert (np.all(result[np.abs(j) > 1] == 0))
|
|
|
|
@@ -170,15 +170,15 @@ def test_scharr_vertical():
|
|
|
|
|
|
|
|
|
|
def test_hscharr_zeros():
|
|
|
|
|
"""Horizontal Scharr on an array of all zeros."""
|
|
|
|
|
result = F.hscharr(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.hscharr(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_hscharr_mask():
|
|
|
|
|
"""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))
|
|
|
|
|
result = filters.hscharr(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -186,7 +186,7 @@ def test_hscharr_horizontal():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.hscharr(image)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
i[np.abs(j) == 5] = 10000
|
|
|
|
|
assert (np.all(result[i == 0] == 1))
|
|
|
|
@@ -197,21 +197,21 @@ def test_hscharr_vertical():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.hscharr(image)
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vscharr_zeros():
|
|
|
|
|
"""Vertical Scharr on an array of all zeros."""
|
|
|
|
|
result = F.vscharr(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.vscharr(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vscharr_mask():
|
|
|
|
|
"""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))
|
|
|
|
|
result = filters.vscharr(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -219,7 +219,7 @@ def test_vscharr_vertical():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.vscharr(image)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
j[np.abs(i) == 5] = 10000
|
|
|
|
|
assert (np.all(result[j == 0] == 1))
|
|
|
|
@@ -230,21 +230,21 @@ def test_vscharr_horizontal():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.vscharr(image)
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prewitt_zeros():
|
|
|
|
|
"""Prewitt on an array of all zeros."""
|
|
|
|
|
result = F.prewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.prewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prewitt_mask():
|
|
|
|
|
"""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 = filters.prewitt(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(np.abs(result), 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -252,7 +252,7 @@ def test_prewitt_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.prewitt(image) * np.sqrt(2)
|
|
|
|
|
result = filters.prewitt(image) * np.sqrt(2)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
i[np.abs(j) == 5] = 10000
|
|
|
|
|
assert (np.all(result[i == 0] == 1))
|
|
|
|
@@ -263,7 +263,7 @@ def test_prewitt_vertical():
|
|
|
|
|
"""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) * np.sqrt(2)
|
|
|
|
|
result = filters.prewitt(image) * np.sqrt(2)
|
|
|
|
|
j[np.abs(i) == 5] = 10000
|
|
|
|
|
assert_allclose(result[j == 0], 1)
|
|
|
|
|
assert_allclose(result[np.abs(j) > 1], 0, atol=1e-10)
|
|
|
|
@@ -271,14 +271,14 @@ def test_prewitt_vertical():
|
|
|
|
|
|
|
|
|
|
def test_hprewitt_zeros():
|
|
|
|
|
"""Horizontal prewitt on an array of all zeros."""
|
|
|
|
|
result = F.hprewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.hprewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_hprewitt_mask():
|
|
|
|
|
"""Horizontal prewitt on a masked array should be zero."""
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
result = F.hprewitt(np.random.uniform(size=(10, 10)),
|
|
|
|
|
result = filters.hprewitt(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
@@ -287,7 +287,7 @@ def test_hprewitt_horizontal():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.hprewitt(image)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
i[np.abs(j) == 5] = 10000
|
|
|
|
|
assert (np.all(result[i == 0] == 1))
|
|
|
|
@@ -298,21 +298,21 @@ def test_hprewitt_vertical():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.hprewitt(image)
|
|
|
|
|
assert_allclose(result, 0, atol=1e-10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vprewitt_zeros():
|
|
|
|
|
"""Vertical prewitt on an array of all zeros."""
|
|
|
|
|
result = F.vprewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
result = filters.vprewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vprewitt_mask():
|
|
|
|
|
"""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 = filters.vprewitt(np.random.uniform(size=(10, 10)),
|
|
|
|
|
np.zeros((10, 10), bool))
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -320,7 +320,7 @@ def test_vprewitt_vertical():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.vprewitt(image)
|
|
|
|
|
# Fudge the eroded points
|
|
|
|
|
j[np.abs(i) == 5] = 10000
|
|
|
|
|
assert (np.all(result[j == 0] == 1))
|
|
|
|
@@ -331,7 +331,7 @@ def test_vprewitt_horizontal():
|
|
|
|
|
"""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)
|
|
|
|
|
result = filters.vprewitt(image)
|
|
|
|
|
assert_allclose(result, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -347,7 +347,7 @@ def test_horizontal_mask_line():
|
|
|
|
|
expected[1:-1, 1:-1] = 0.2 # constant gradient for most of image,
|
|
|
|
|
expected[4:7, 1:-1] = 0 # but line and neighbors masked
|
|
|
|
|
|
|
|
|
|
for grad_func in (F.hprewitt, F.hsobel, F.hscharr):
|
|
|
|
|
for grad_func in (filters.hprewitt, filters.hsobel, filters.hscharr):
|
|
|
|
|
result = grad_func(vgrad, mask)
|
|
|
|
|
yield assert_close, result, expected
|
|
|
|
|
|
|
|
|
@@ -364,7 +364,7 @@ def test_vertical_mask_line():
|
|
|
|
|
expected[1:-1, 1:-1] = 0.2 # constant gradient for most of image,
|
|
|
|
|
expected[1:-1, 4:7] = 0 # but line and neighbors masked
|
|
|
|
|
|
|
|
|
|
for grad_func in (F.vprewitt, F.vsobel, F.vscharr):
|
|
|
|
|
for grad_func in (filters.vprewitt, filters.vsobel, filters.vscharr):
|
|
|
|
|
result = grad_func(hgrad, mask)
|
|
|
|
|
yield assert_close, result, expected
|
|
|
|
|
|
|
|
|
@@ -372,7 +372,8 @@ def test_vertical_mask_line():
|
|
|
|
|
def test_range():
|
|
|
|
|
"""Output of edge detection should be in [0, 1]"""
|
|
|
|
|
image = np.random.random((100, 100))
|
|
|
|
|
for detector in (F.sobel, F.scharr, F.prewitt, F.roberts):
|
|
|
|
|
for detector in (filters.sobel, filters.scharr,
|
|
|
|
|
filters.prewitt, filters.roberts):
|
|
|
|
|
out = detector(image)
|
|
|
|
|
assert_(out.min() >= 0,
|
|
|
|
|
"Minimum of `{0}` is smaller than zero".format(
|
|
|
|
|