diff --git a/skimage/filters/tests/test_thresholding.py b/skimage/filters/tests/test_thresholding.py index 16682610..4c14b8b6 100644 --- a/skimage/filters/tests/test_thresholding.py +++ b/skimage/filters/tests/test_thresholding.py @@ -1,5 +1,7 @@ import numpy as np -from numpy.testing import assert_equal, assert_almost_equal +from numpy.testing import (assert_equal, + assert_almost_equal, + assert_raises) import skimage from skimage import data @@ -162,6 +164,11 @@ def test_otsu_astro_image(): with expected_warnings(['grayscale']): assert 109 < threshold_otsu(img) < 111 + +def test_otsu_one_color_image(): + img = np.ones((10, 10), dtype=np.uint8) + assert_raises(TypeError, threshold_otsu, img) + def test_li_camera_image(): camera = skimage.img_as_ubyte(data.camera()) assert 63 < threshold_li(camera) < 65 diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index f6a4ee1e..7f4aa7de 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -129,6 +129,12 @@ def threshold_otsu(image, nbins=256): "grayscale images; image shape {0} looks like an RGB image" warnings.warn(msg.format(image.shape)) + # Check if the image is multi-colored or not + if image.min() == image.max(): + raise TypeError("threshold_otsu is expected to work with images " \ + "having more than one color. The input image seems " \ + "to have just one color {0}.".format(image.min())) + hist, bin_centers = histogram(image.ravel(), nbins) hist = hist.astype(float)