Merge pull request #1876 from OrkoHunter/threshold_otsu_grayscale

Show warnings for input image type in threshold_otsu
This commit is contained in:
Juan Nunez-Iglesias
2016-01-18 10:38:17 +11:00
2 changed files with 16 additions and 3 deletions
+5 -2
View File
@@ -3,6 +3,7 @@ from numpy.testing import assert_equal, assert_almost_equal
import skimage
from skimage import data
from skimage._shared._warnings import expected_warnings
from skimage.filters.thresholding import (threshold_adaptive,
threshold_otsu,
threshold_li,
@@ -158,11 +159,13 @@ def test_otsu_coins_image_as_float():
def test_otsu_lena_image():
img = skimage.img_as_ubyte(data.lena())
assert 140 < threshold_otsu(img) < 142
with expected_warnings(['grayscale']):
assert 140 < threshold_otsu(img) < 142
def test_otsu_astro_image():
img = skimage.img_as_ubyte(data.astronaut())
assert 109 < threshold_otsu(img) < 111
with expected_warnings(['grayscale']):
assert 109 < threshold_otsu(img) < 111
def test_li_camera_image():
camera = skimage.img_as_ubyte(data.camera())
+11 -1
View File
@@ -8,6 +8,7 @@ import numpy as np
from scipy import ndimage as ndi
from ..exposure import histogram
from .._shared.utils import assert_nD
import warnings
def threshold_adaptive(image, block_size, method='gaussian', offset=0,
@@ -97,7 +98,7 @@ def threshold_otsu(image, nbins=256):
Parameters
----------
image : array
Input image.
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
@@ -118,7 +119,16 @@ def threshold_otsu(image, nbins=256):
>>> image = camera()
>>> thresh = threshold_otsu(image)
>>> binary = image <= thresh
Notes
-----
The input image must be grayscale.
"""
if image.shape[-1] in (3, 4):
msg = "threshold_otsu is expected to work correctly only for " \
"grayscale images; image shape {0} looks like an RGB image"
warnings.warn(msg.format(image.shape))
hist, bin_centers = histogram(image.ravel(), nbins)
hist = hist.astype(float)