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)) diff --git a/skimage/filters/tests/test_thresholding.py b/skimage/filters/tests/test_thresholding.py index 4c14b8b6..03557caf 100644 --- a/skimage/filters/tests/test_thresholding.py +++ b/skimage/filters/tests/test_thresholding.py @@ -203,6 +203,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 7f4aa7de..943041b8 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':