diff --git a/skimage/morphology/convex_hull.py b/skimage/morphology/convex_hull.py index 1a26b842..4d54498d 100644 --- a/skimage/morphology/convex_hull.py +++ b/skimage/morphology/convex_hull.py @@ -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) diff --git a/skimage/morphology/tests/test_convex_hull.py b/skimage/morphology/tests/test_convex_hull.py index ee3b6bfa..67850cf2 100644 --- a/skimage/morphology/tests/test_convex_hull.py +++ b/skimage/morphology/tests/test_convex_hull.py @@ -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(