Merge pull request #873 from jmetz/convex_hull_image_qhull_precision_fix

QH6228 Bug fix - remove offset from coords

Fixes issue #871.
This commit is contained in:
Juan Nunez-Iglesias
2014-02-02 05:05:58 -08:00
2 changed files with 26 additions and 0 deletions
+7
View File
@@ -54,6 +54,10 @@ def convex_hull_image(image):
raise ImportError('Could not import scipy.spatial, only available in '
'scipy >= 0.9.')
# Subtract offset
offset = coords.mean(axis=0)
coords -= offset
# Find the convex hull
chull = Delaunay(coords).convex_hull
v = coords[np.unique(chull)]
@@ -63,6 +67,9 @@ def convex_hull_image(image):
angles = np.arctan2(v_centred[:, 0], v_centred[:, 1])
v = v[np.argsort(angles)]
# Add back offset
v += offset
# For each pixel coordinate, check whether that pixel
# lies inside the convex hull
mask = grid_points_inside_poly(image.shape[:2], v)
@@ -32,6 +32,25 @@ def test_basic():
assert_array_equal(convex_hull_image(image), expected)
@skipif(not scipy_spatial)
def test_qhull_offset_example():
nonzeros = (([1367, 1368, 1368, 1368, 1369, 1369, 1369, 1369, 1369, 1370, 1370,
1370, 1370, 1370, 1370, 1370, 1371, 1371, 1371, 1371, 1371, 1371,
1371, 1371, 1371, 1372, 1372, 1372, 1372, 1372, 1372, 1372, 1372,
1372, 1373, 1373, 1373, 1373, 1373, 1373, 1373, 1373, 1373, 1374,
1374, 1374, 1374, 1374, 1374, 1374, 1375, 1375, 1375, 1375, 1375,
1376, 1376, 1376, 1377]),
([151, 150, 151, 152, 149, 150, 151, 152, 153, 148, 149, 150, 151,
152, 153, 154, 147, 148, 149, 150, 151, 152, 153, 154, 155, 146,
147, 148, 149, 150, 151, 152, 153, 154, 146, 147, 148, 149, 150,
151, 152, 153, 154, 147, 148, 149, 150, 151, 152, 153, 148, 149,
150, 151, 152, 149, 150, 151, 150]))
image = np.zeros((1392, 1040), dtype=bool)
image[nonzeros] = True
expected = image.copy()
assert_array_equal(convex_hull_image(image), expected)
@skipif(not scipy_spatial)
def test_pathological_qhull_example():
image = np.array(