Function File: T = adaptthresh (img)
Function File: T = adaptthresh (img, sensitivity)
Function File: T = adaptthresh (…, name, value, …)

Compute local threshold value for each pixel using local mean intensity in the neighborhood of each pixel.

Parameters:

img

Input image, specified as a 2-D grayscale image. Image type can be int8, uint8, int16, uint16, int32, uint32, single or double.

sensitivity

Sensitivity factor, specified as a scalar in the range [0, 1]. Higher values make the threshold more sensitive, resulting in more foreground pixels. Default value = 0.5

name, value

Additional options as name-value pairs:

Statistic

Method used to compute the threshold. Valid options are:

"mean"

Computes the threshold based on the local mean intensity. This method assumes that the foreground and background have different mean intensities.

"median"

Computes the threshold using the local median intensity. This method is more robust to noise and outliers compared to the mean method.

"gaussian"

Uses a weighted average where weights are determined by a Gaussian window. This method smooths the local intensity variations and can be more effective in noisy images.

NeighborhoodSize

Size of the local neighborhood, specified as a positive scalar or a 2-element vector [m n]. m & n should be odd integers. By default, the window size is determined based on the size of the input image. For an image of size MxN, the default window size is computed as:

m = 2 * floor (M / 16) + 1
n = 2 * floor (N / 16) + 1

This ensures the window size is odd and appropriately scaled relative to the image dimensions.

ForegroundPolarity

Defines object polarity:

  • "bright" (default): Assumes bright foreground on dark background
  • "dark": Assumes dark foreground on bright background

Outputs

T

Thresholds image, where each pixel present the normalized intensity. The values of T are in the range [0, 1]

Example

I = imread ("example.png");
T = adaptthresh (I, 0.5, "Statistic", "mean", "NeighborhoodSize", [15 15]);
BW = imbinarize (I, T);
imshow (BW);

See also: imbinarize, graythresh, imboxfilt, medfilt2, imgaussfilt.

Package: image