add unittest for hough circle

This commit is contained in:
François Boulogne
2013-02-09 20:00:41 +01:00
parent a997195881
commit 12c1bf8883
@@ -4,6 +4,7 @@ from numpy.testing import *
import skimage.transform as tf
import skimage.transform.hough_transform as ht
from skimage.transform import probabilistic_hough
from skimage.draw import circle_perimeter
def append_desc(func, description):
@@ -14,8 +15,6 @@ def append_desc(func, description):
return func
from skimage.transform import *
def test_hough():
# Generate a test image
@@ -23,7 +22,7 @@ def test_hough():
for i in range(25, 75):
img[100 - i, i] = 1
out, angles, d = tf.hough(img)
out, angles, d = tf.hough_line(img)
y, x = np.where(out == out.max())
dist = d[y[0]]
@@ -37,7 +36,7 @@ def test_hough_angles():
img = np.zeros((10, 10))
img[0, 0] = 1
out, angles, d = tf.hough(img, np.linspace(0, 360, 10))
out, angles, d = tf.hough_line(img, np.linspace(0, 360, 10))
assert_equal(len(angles), 10)
@@ -76,7 +75,7 @@ def test_hough_peaks_dist():
img = np.zeros((100, 100), dtype=np.bool_)
img[:, 30] = True
img[:, 40] = True
hspace, angles, dists = tf.hough(img)
hspace, angles, dists = tf.hough_line(img)
assert len(tf.hough_peaks(hspace, angles, dists, min_distance=5)[0]) == 2
assert len(tf.hough_peaks(hspace, angles, dists, min_distance=15)[0]) == 1
@@ -86,17 +85,17 @@ def test_hough_peaks_angle():
img[:, 0] = True
img[0, :] = True
hspace, angles, dists = tf.hough(img)
hspace, angles, dists = tf.hough_line(img)
assert len(tf.hough_peaks(hspace, angles, dists, min_angle=45)[0]) == 2
assert len(tf.hough_peaks(hspace, angles, dists, min_angle=90)[0]) == 1
theta = np.linspace(0, np.pi, 100)
hspace, angles, dists = tf.hough(img, theta)
hspace, angles, dists = tf.hough_line(img, theta)
assert len(tf.hough_peaks(hspace, angles, dists, min_angle=45)[0]) == 2
assert len(tf.hough_peaks(hspace, angles, dists, min_angle=90)[0]) == 1
theta = np.linspace(np.pi / 3, 4. / 3 * np.pi, 100)
hspace, angles, dists = tf.hough(img, theta)
hspace, angles, dists = tf.hough_line(img, theta)
assert len(tf.hough_peaks(hspace, angles, dists, min_angle=45)[0]) == 2
assert len(tf.hough_peaks(hspace, angles, dists, min_angle=90)[0]) == 1
@@ -105,10 +104,25 @@ def test_hough_peaks_num():
img = np.zeros((100, 100), dtype=np.bool_)
img[:, 30] = True
img[:, 40] = True
hspace, angles, dists = tf.hough(img)
hspace, angles, dists = tf.hough_line(img)
assert len(tf.hough_peaks(hspace, angles, dists, min_distance=0,
min_angle=0, num_peaks=1)[0]) == 1
def test_houghcircle():
# Prepare picture
img = np.zeros((100, 100), dtype=int)
radius = 20
x_0, y_0 = (50, 50)
x, y = circle_perimeter(y_0, x_0, radius)
img[y, x] = 1
out = tf.hough_circle(img, np.array([radius]))
y, x = np.where(out[0] == out[0].max())
# Offset for x_0, y_0
assert_equal(x[0], x_0 + radius)
assert_equal(y[0], y_0 + radius)
if __name__ == "__main__":
run_module_suite()