From 12fabff65fa745fa0da6e698b60796850c673f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Wed, 1 Jun 2016 09:17:36 +0200 Subject: [PATCH] Some corrections --- .../segmentation/plot_thresholding.py | 14 ++--- skimage/filters/__init__.py | 2 +- skimage/filters/thresholding.py | 53 +++++++++---------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/doc/examples/segmentation/plot_thresholding.py b/doc/examples/segmentation/plot_thresholding.py index 55abf889..8b1cae13 100644 --- a/doc/examples/segmentation/plot_thresholding.py +++ b/doc/examples/segmentation/plot_thresholding.py @@ -7,11 +7,11 @@ Thresholding is used to create a binary image from a grayscale image [1]_. Thresholding algorithms can be separated in two categories: -* Histogram-based. The histogram of the pixel intensity is used and -assumptions may be made on the properties of this histogram (e.g. bimodal). +- Histogram-based. The histogram of the pixel intensity is used and + assumptions may be made on the properties of this histogram (e.g. bimodal). +- Local. To process a pixel, only the neighboring pixels are used. + These algorithms often require more computation time. -* Local. To process a pixel, only the neighboring pixels are used. -These algorithms often require more computation time. Scikit-image includes a function to test thresholding algorithms provided in the library. Therefore, in a glance, you can select the best algorithm @@ -30,9 +30,9 @@ img = page() # Here, we specify a radius for local thresholding algorithm. # If it is not specified, only global algorithms are called. -fig, ax = thresholding.mosaic_threshold(img, radius=20, - figsize=(10,8), verbose=False) -fig.show() +fig, ax = thresholding.try_all_threshold(img, radius=20, + figsize=(10,8), verbose=False) +plt.show() """ diff --git a/skimage/filters/__init__.py b/skimage/filters/__init__.py index be22469f..9ae67a5c 100644 --- a/skimage/filters/__init__.py +++ b/skimage/filters/__init__.py @@ -44,7 +44,7 @@ __all__ = ['inverse', 'rank_order', 'gabor_kernel', 'gabor', - 'mosaic_threshold', + 'try_all_threshold', 'threshold_adaptive', 'threshold_otsu', 'threshold_yen', diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index 1df0042a..eb21ffbb 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -1,11 +1,3 @@ -__all__ = ['mosaic_threshold', - 'threshold_adaptive', - 'threshold_otsu', - 'threshold_yen', - 'threshold_isodata', - 'threshold_li', - 'threshold_minimum', ] - import math import numpy as np from scipy import ndimage as ndi @@ -18,7 +10,16 @@ from ..morphology import disk from ..filters.rank import otsu -def _mosaic(image, methods=None, figsize=None, num_cols=2, verbose=True): +__all__ = ['try_all_threshold', + 'threshold_adaptive', + 'threshold_otsu', + 'threshold_yen', + 'threshold_isodata', + 'threshold_li', + 'threshold_minimum', ] + + +def _try_all(image, methods=None, figsize=None, num_cols=2, verbose=True): """Returns a figure comparing the outputs of different methods. Parameters @@ -26,14 +27,14 @@ def _mosaic(image, methods=None, figsize=None, num_cols=2, verbose=True): image : (N, M) ndarray Input image. methods : dict, optional - Names and associated functions of the algorithms. - The functions must return an image. + Names and associated functions. + Functions must take and return an image. figsize : tuple, optional Figure size (in inches). num_cols : int, optional Number of columns. verbose : bool, optional - Print the function name for each method. + Print function name for each method. Returns ------- @@ -41,7 +42,7 @@ def _mosaic(image, methods=None, figsize=None, num_cols=2, verbose=True): Matplotlib figure and axes. """ num_rows = math.ceil((len(methods) + 1) / 2.) - num_rows = int(num_rows) # Python 2.7 support + num_rows = int(num_rows) # Python 2.7 support fig, ax = plt.subplots(num_rows, num_cols, figsize=figsize, sharex=True, sharey=True, subplot_kw={'adjustable': 'box-forced'}) @@ -62,11 +63,10 @@ def _mosaic(image, methods=None, figsize=None, num_cols=2, verbose=True): a.axis('off') fig.tight_layout() - #plt.close() return fig, ax -def mosaic_threshold(image, radius=None, figsize=(8, 5), verbose=True): +def try_all_threshold(image, radius=None, figsize=(8, 5), verbose=True): """Returns a figure comparing the outputs of different thresholding methods. Parameters @@ -75,11 +75,11 @@ def mosaic_threshold(image, radius=None, figsize=(8, 5), verbose=True): Input image. radius : int, optinal Lengthscale used for local methods. - If None, local methods are not called. + If None, local methods are ignored. figsize : tuple, optional Figure size (in inches). verbose : bool, optional - Print the function name for each method. + Print function name for each method. Returns ------- @@ -100,23 +100,22 @@ def mosaic_threshold(image, radius=None, figsize=(8, 5), verbose=True): Example ------- >>> from skimage.data import text - >>> fig, ax = mosaic_threshold(text(), radius=20, - ... figsize=(10, 6), verbose=None) + >>> fig, ax = try_all_threshold(text(), radius=20, + ... figsize=(10, 6), verbose=False) """ def include_selem(func, *args, **kwargs): """ - A wrapper function to embed a threshold range. + A wrapper function to embed a threshold range for local algorithms. """ def wrapper(im): return func(im, *args, **kwargs) try: wrapper.__orifunc__ = func.__orifunc__ except AttributeError: - wrapper.__orifunc__ = func.__module__ + '.' + func.__name__ + wrapper.__orifunc__ = func.__module__ + '.' + func.__name__ return wrapper - def thresh(func): """ A wrapper function to return a thresholded image. @@ -126,7 +125,7 @@ def mosaic_threshold(image, radius=None, figsize=(8, 5), verbose=True): try: wrapper.__orifunc__ = func.__orifunc__ except AttributeError: - wrapper.__orifunc__ = func.__module__ + '.' + func.__name__ + wrapper.__orifunc__ = func.__module__ + '.' + func.__name__ return wrapper # Global algorithms. @@ -142,12 +141,12 @@ def mosaic_threshold(image, radius=None, figsize=(8, 5), verbose=True): methods['Local Otsu'] = thresh(local_otsu) block_size = 2 * int(radius) + 1 - adaptive_threshold = include_selem(threshold_adaptive, block_size, offset=10) + adaptive_threshold = include_selem(threshold_adaptive, block_size, + offset=10) methods['Adaptive threshold'] = adaptive_threshold - - return _mosaic(image, figsize=figsize, - methods=methods, verbose=verbose) + return _try_all(image, figsize=figsize, + methods=methods, verbose=verbose) __all__ = ['threshold_adaptive', 'threshold_otsu',