From 41afd05d0a5d13b75595d6e042792b53a822921d Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Mon, 25 Jan 2016 19:31:23 +0530 Subject: [PATCH 1/2] Modify threshold_adaptive 1. Improve docstring 2. Raise error for even block_size 3. Test for the ValueError --- skimage/filters/tests/test_thresholding.py | 9 ++++++++- skimage/filters/thresholding.py | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/skimage/filters/tests/test_thresholding.py b/skimage/filters/tests/test_thresholding.py index 16682610..d0582211 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 @@ -196,6 +198,11 @@ def test_yen_coins_image_as_float(): assert 0.43 < threshold_yen(coins) < 0.44 +def test_adaptive_even_block_size_error(): + img = data.camera() + assert_raises(ValueError, threshold_adaptive, img, block_size=4) + + def test_isodata_camera_image(): camera = skimage.img_as_ubyte(data.camera()) diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index f6a4ee1e..6ad80fde 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -25,7 +25,7 @@ def threshold_adaptive(image, block_size, method='gaussian', offset=0, image : (N, M) ndarray Input image. block_size : int - Uneven size of pixel neighborhood which is used to calculate the + Odd size of pixel neighborhood which is used to calculate the threshold value (e.g. 3, 5, 7, ..., 21, ...). method : {'generic', 'gaussian', 'mean', 'median'}, optional Method used to determine adaptive threshold for local neighbourhood in @@ -68,6 +68,9 @@ def threshold_adaptive(image, block_size, method='gaussian', offset=0, >>> func = lambda arr: arr.mean() >>> binary_image2 = threshold_adaptive(image, 15, 'generic', param=func) """ + if block_size % 2 == 0: + raise ValueError("The kwarg ``block_size`` must be odd! Given " + "``block_size`` {0} is even.".format(block_size)) assert_nD(image, 2) thresh_image = np.zeros(image.shape, 'double') if method == 'generic': From df18b53aedb22284d14bbfabe145632c43bb6845 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 26 Jan 2016 01:59:18 +0530 Subject: [PATCH 2/2] Correct example for threshold_adaptive --- doc/examples/segmentation/plot_threshold_adaptive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/segmentation/plot_threshold_adaptive.py b/doc/examples/segmentation/plot_threshold_adaptive.py index 9bf7b0c6..6f473abb 100644 --- a/doc/examples/segmentation/plot_threshold_adaptive.py +++ b/doc/examples/segmentation/plot_threshold_adaptive.py @@ -26,7 +26,7 @@ image = data.page() global_thresh = threshold_otsu(image) binary_global = image > global_thresh -block_size = 40 +block_size = 35 binary_adaptive = threshold_adaptive(image, block_size, offset=10) fig, axes = plt.subplots(nrows=3, figsize=(7, 8))