From f034081650c10f0ddc822ee784bc98c66aedf87a Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Thu, 14 Jan 2016 13:49:17 +0530 Subject: [PATCH 1/3] Document the input image type --- skimage/filters/thresholding.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index 272641ae..de33593e 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -97,7 +97,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,6 +118,10 @@ def threshold_otsu(image, nbins=256): >>> image = camera() >>> thresh = threshold_otsu(image) >>> binary = image <= thresh + + Notes + ----- + The input image must be Grayscale. """ hist, bin_centers = histogram(image.ravel(), nbins) hist = hist.astype(float) From 296438d08ed52cf42b7d7cf5d56bf15356fb9a27 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Thu, 14 Jan 2016 17:51:46 +0530 Subject: [PATCH 2/3] Raise a warning if the image is not grayscale --- skimage/filters/thresholding.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index de33593e..2362254b 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -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, @@ -123,6 +124,11 @@ def threshold_otsu(image, nbins=256): ----- 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 {} looks like an RGB image" + warnings.warn(msg.format(image.shape)) + hist, bin_centers = histogram(image.ravel(), nbins) hist = hist.astype(float) From a2d75de8348ff89d68bf69afd034cbf89d4d22b9 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Fri, 15 Jan 2016 10:16:26 +0530 Subject: [PATCH 3/3] Add expected_warnings to the tests using threshold_otsu --- skimage/filters/tests/test_thresholding.py | 7 +++++-- skimage/filters/thresholding.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/skimage/filters/tests/test_thresholding.py b/skimage/filters/tests/test_thresholding.py index a47b6f20..fdcbf34b 100644 --- a/skimage/filters/tests/test_thresholding.py +++ b/skimage/filters/tests/test_thresholding.py @@ -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()) diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index 2362254b..f6a4ee1e 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -122,11 +122,11 @@ def threshold_otsu(image, nbins=256): Notes ----- - The input image must be Grayscale. + 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 {} looks like an RGB image" + "grayscale images; image shape {0} looks like an RGB image" warnings.warn(msg.format(image.shape)) hist, bin_centers = histogram(image.ravel(), nbins)