ENH: use heapq to select the best match

This commit is contained in:
François Boulogne
2013-10-12 14:09:51 +02:00
parent 7e970b18cd
commit debd4d54d6
3 changed files with 35 additions and 24 deletions
+8 -2
View File
@@ -3,6 +3,7 @@
#cython: nonecheck=False
#cython: wraparound=False
import numpy as np
import heapq
cimport numpy as cnp
cimport cython
@@ -131,8 +132,12 @@ def hough_ellipse(cnp.ndarray img, int threshold=4, double accuracy=1,
>>> img = np.zeros((25, 25), dtype=int)
>>> rr, cc = draw.ellipse_perimeter(10, 10, 6, 8)
>>> img[cc, rr] = 1
>>> result = hough_ellipse(img, threshold=8)
>>> result = hough_ellipse(img, threshold=4)
>>> # extract the highest accumulator
>>> heapq.nlargest(1, result)
[(10, 10.0, 10.0, 8.0, 6.0, 0.0)]
>>> # To sort them all
>>> results = [heappop(results) for i in range(len(results))]
Notes
-----
@@ -217,7 +222,8 @@ def hough_ellipse(cnp.ndarray img, int threshold=4, double accuracy=1,
elif angle < - np.pi:
angle = angle + np.pi / 2.
b = sqrt(bin_edges[hist.argmax()])
results.append((hist_max, # Accumulator
heapq.heappush(results,
(hist_max, # Accumulator
x0,
y0,
a,
+19 -15
View File
@@ -1,4 +1,5 @@
import numpy as np
import heapq
from numpy.testing import (assert_almost_equal,
assert_equal,
)
@@ -157,11 +158,12 @@ def test_hough_ellipse_zero_angle():
rr, cc = ellipse_perimeter(y0, x0, b, a)
img[rr, cc] = 1
result = tf.hough_ellipse(img, threshold=9)
assert_equal(result[0][1], x0)
assert_equal(result[0][2], y0)
assert_almost_equal(result[0][3], b, decimal=1)
assert_almost_equal(result[0][4], a, decimal=1)
assert_equal(result[0][5], angle)
best = heapq.nlargest(1, result)[0]
assert_equal(best[1], x0)
assert_equal(best[2], y0)
assert_almost_equal(best[3], b, decimal=1)
assert_almost_equal(best[4], a, decimal=1)
assert_equal(best[5], angle)
def test_hough_ellipse_non_zero_angle():
@@ -174,11 +176,12 @@ def test_hough_ellipse_non_zero_angle():
rr, cc = ellipse_perimeter(y0, x0, b, a, orientation=angle)
img[rr, cc] = 1
result = tf.hough_ellipse(img, threshold=15, accuracy=3)
assert_almost_equal(result[0][1] / 100., x0 / 100., decimal=1)
assert_almost_equal(result[0][2] / 100., y0 / 100., decimal=1)
assert_almost_equal(result[0][3] / 10., b / 10., decimal=1)
assert_almost_equal(result[0][4] / 100., a / 100., decimal=1)
assert_almost_equal(result[0][5], angle, decimal=1)
best = heapq.nlargest(1, result)[0]
assert_almost_equal(best[1] / 100., x0 / 100., decimal=1)
assert_almost_equal(best[2] / 100., y0 / 100., decimal=1)
assert_almost_equal(best[3] / 10., b / 10., decimal=1)
assert_almost_equal(best[4] / 100., a / 100., decimal=1)
assert_almost_equal(best[5], angle, decimal=1)
def test_hough_ellipse_non_zero_angle2():
@@ -191,11 +194,12 @@ def test_hough_ellipse_non_zero_angle2():
rr, cc = ellipse_perimeter(y0, x0, b, a, orientation=angle)
img[rr, cc] = 1
result = tf.hough_ellipse(img, threshold=15, accuracy=3)
assert_almost_equal(result[0][1] / 100., x0 / 100., decimal=1)
assert_almost_equal(result[0][2] / 100., y0 / 100., decimal=1)
assert_almost_equal(result[0][3] / 100., a / 100., decimal=1)
assert_almost_equal(result[0][3] / 100., b / 100., decimal=1)
assert_almost_equal(result[0][5], angle, decimal=1)
best = heapq.nlargest(1, result)[0]
assert_almost_equal(best[1] / 100., x0 / 100., decimal=1)
assert_almost_equal(best[2] / 100., y0 / 100., decimal=1)
assert_almost_equal(best[3] / 100., a / 100., decimal=1)
assert_almost_equal(best[3] / 100., b / 100., decimal=1)
assert_almost_equal(best[5], angle, decimal=1)
if __name__ == "__main__":