diff --git a/skimage/morphology/convex_hull.py b/skimage/morphology/convex_hull.py index bb9ca734..31b92a47 100644 --- a/skimage/morphology/convex_hull.py +++ b/skimage/morphology/convex_hull.py @@ -1,11 +1,12 @@ -__all__ = ['convex_hull_image', 'convex_hull_object'] - +"""Convex Hull.""" import numpy as np from ..measure._pnpoly import grid_points_in_poly from ._convex_hull import possible_hull from ..measure._label import label from ..util import unique_rows +__all__ = ['convex_hull_image', 'convex_hull_object'] + try: from scipy.spatial import Delaunay except ImportError: @@ -33,6 +34,8 @@ def convex_hull_image(image): .. [1] http://blogs.mathworks.com/steve/2011/10/04/binary-image-convex-hull-algorithm-notes/ """ + if image.ndim > 2: + raise ValueError("Input must be a 2D image") if Delaunay is None: raise ImportError("Could not import scipy.spatial.Delaunay, " @@ -85,7 +88,7 @@ def convex_hull_object(image, neighbors=8): Parameters ---------- - image : ndarray + image : (M, N) array Binary input image. neighbors : {4, 8}, int Whether to use 4- or 8-connectivity. @@ -104,6 +107,8 @@ def convex_hull_object(image, neighbors=8): convex_hull_image separately on each object. """ + if image.ndim > 2: + raise ValueError("Input must be a 2D image") if neighbors != 4 and neighbors != 8: raise ValueError('Neighbors must be either 4 or 8.') diff --git a/skimage/morphology/tests/test_convex_hull.py b/skimage/morphology/tests/test_convex_hull.py index c0cc954e..1a41b6c8 100644 --- a/skimage/morphology/tests/test_convex_hull.py +++ b/skimage/morphology/tests/test_convex_hull.py @@ -31,20 +31,24 @@ def test_basic(): assert_array_equal(convex_hull_image(image), expected) + # Test that an error is raised on passing a 3D image: + image3d = np.empty((5, 5, 5)) + assert_raises(ValueError, convex_hull_image, image3d) + @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])) + 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() @@ -139,6 +143,9 @@ def test_object(): assert_raises(ValueError, convex_hull_object, image, 7) + # Test that an error is raised on passing a 3D image: + image3d = np.empty((5, 5, 5)) + assert_raises(ValueError, convex_hull_object, image3d) if __name__ == "__main__": np.testing.run_module_suite()