{ } Raw JSON

bundles / skimage 0.26.1rc0.dev0+git20260530.b607368ff / skimage / filters / thresholding / threshold_local

function

skimage.filters.thresholding:threshold_local

source: /dev/scikit-image/src/skimage/filters/thresholding.py :174

Signature

def   threshold_local ( image block_size = 3 method = gaussian offset = 0 mode = reflect param = None cval = 0 )

Summary

Compute a threshold mask image based on local pixel neighborhood.

Extended Summary

Also known as adaptive or dynamic thresholding. The threshold value is the weighted mean for the local neighborhood of a pixel subtracted by a constant. Alternatively the threshold can be determined dynamically by a given function, using the 'generic' method.

Parameters

image : (M, N[, ...]) ndarray

Grayscale input image.

block_size : int or Sequence of int

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 neighborhood 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 the local threshold value. Default offset is 0.

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'.

param : int or Callable, optional

Either specify sigma for 'gaussian' method or function object for 'generic' method. This functions takes the flat array of local neighborhood as a single argument and returns the calculated threshold for the centre pixel.

cval : float, optional

Value to fill past edges of input if mode is 'constant'.

Returns

threshold : (M, N[, ...]) ndarray

Threshold image. All pixels in the input image higher than the corresponding pixel in the threshold image are considered foreground.

Examples

from skimage.data import camera
image = camera()[:50, :50]
binary_image1 = image > threshold_local(image, 15, 'mean')
func = lambda arr: arr.mean()
binary_image2 = image > threshold_local(image, 15, 'generic',
                                        param=func)

Aliases

  • skimage.filters.threshold_local