diff --git a/skimage/filter/_thresholding.pyx b/skimage/filter/_thresholding.pyx index 772aef78..498580da 100644 --- a/skimage/filter/_thresholding.pyx +++ b/skimage/filter/_thresholding.pyx @@ -9,14 +9,14 @@ cimport cython def _threshold_adaptive(np.ndarray[np.double_t, ndim=2] image, int block_size, method, double offset, mode, param): cdef int r, c - cdef np.ndarray[np.float64_t, ndim=2] thres_image + cdef np.ndarray[np.double_t, ndim=2] thres_image if method == 'generic': thres_image = scipy.ndimage.generic_filter(image, param, block_size, mode=mode) elif method == 'gaussian': if param is None: - # automatically determine sigme which covers > 99% of distribution + # automatically determine sigma which covers > 99% of distribution sigma = (block_size - 1) / 6.0 thres_image = scipy.ndimage.gaussian_filter(image, sigma, mode=mode) elif method == 'mean': diff --git a/skimage/filter/thresholding.py b/skimage/filter/thresholding.py index b26fcf0d..77c6bf9d 100644 --- a/skimage/filter/thresholding.py +++ b/skimage/filter/thresholding.py @@ -21,30 +21,47 @@ def threshold_adaptive(image, block_size, method='gaussian', offset=0, image : NxM ndarray Input image. block_size : int - uneven size of pixel neighborhood which is used to calculate the - threshold value (e.g. 3, 5, 7, ..., 21, ...) + Uneven 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. By default the 'gaussian' - method is used. + Method used to determine adaptive threshold fpr local neighbourhood in + weighted mean image. + * 'generic': use custom function (see `param` parameter) + * 'gaussian': apply gaussian filter (see `param` parameter for custom + sigma value) + * 'mean': apply arithmetic mean filter + * 'median' apply median rank filter + By default the 'gaussian' method is used. offset : float, optional - constant subtracted from weighted mean of neighborhood to calculate + Constant subtracted from weighted mean of neighborhood to calculate the local threshold value. Default offset is 0. - mode : {'reflect','constant','nearest','mirror', 'wrap'}, optional + mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional The mode parameter determines how the array borders are handled, where - cval is the value when mode is equal to 'constant'. Default is 'reflect' + cval is the value when mode is equal to 'constant'. + Default is 'reflect'. param : {int, function}, optional - either specify sigma for 'gaussian' method or function object for - 'generic' method. + Either specify sigma for 'gaussian' method or function object for + 'generic' method. This functions takes the flat array of local + neighbourhood as a single argument and returns the calculated threshold + for the centre pixel. Returns ------- threshold : NxM ndarray - thresholded binary image + Thresholded binary image References ---------- http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations .html?highlight=threshold#adaptivethreshold + + Examples + -------- + >>> from skimage.data import camera + >>> image = camera() + >>> binary_image1 = threshold_adaptive(image, 15, 'mean') + >>> func = lambda arr: arr.mean() + >>> binary_image2 = threshold_adaptive(image, 15, 'generic', param=func) """ # not using img_as_float because offset parameter wouldn't work image = image.astype('double')