Adaptive image threshold using local first-order statistics
locally adaptive threshold for 2-D grayscale image or 3-D grayscale volume
T = adaptthresh(
adaptthresh function chooses
the threshold based on the local mean intensity (first-order statistics) in the
neighborhood of each pixel. The threshold
T can be used with
imbinarize function to convert the grayscale image to a
Read image into the workspace.
I = imread('rice.png');
adaptthresh to determine threshold to use in binarization operation.
T = adaptthresh(I, 0.4);
Convert image to binary image, specifying the threshold value.
BW = imbinarize(I,T);
Display the original image with the binary version, side-by-side.
figure imshowpair(I, BW, 'montage')
Read image into the workspace.
I = imread('printedtext.png');
adaptthresh compute adaptive threshold and display the local threshold image. This represents an estimate of average background illumination.
T = adaptthresh(I,0.4,'ForegroundPolarity','dark'); figure imshow(T)
Binarize image using locally adaptive threshold
BW = imbinarize(I,T); figure imshow(BW)
Load 3-D volume into the workspace.
load mristack; V = mristack;
Display the data.
figure slice(double(V),size(V,2)/2,size(V,1)/2,size(V,3)/2) colormap gray shading interp
Calculate the threshold.
J = adaptthresh(V,'neigh',[3 3 3],'Fore','bright');
Display the threshold.
figure slice(double(J),size(J,2)/2,size(J,1)/2,size(J,3)/2) colormap gray shading interp
I— Grayscale image or volume
Grayscale image or volume, specified as a 2-D numeric matrix or 3-D numeric array.
If the image contains
the behavior of
adaptthresh is undefined. Propagation of
NaNs might not be
localized to the neighborhood around
sensitivity— Determine which pixels get thresholded as foreground pixels
0.5(default) | number in the range [0, 1]
Determine which pixels get thresholded as foreground pixels, specified as a number in the range [0, 1]. High sensitivity values lead to thresholding more pixels as foreground, at the risk of including some background pixels.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
T = adaptthresh(I,0.4,'ForegroundPolarity','dark');
'NeighborhoodSize'— Size of neighborhood used to compute local statistic around each pixel
2*floor(size(I)/16)+1(default) | positive odd integer | 2-element vector of positive odd integers
Size of neighborhood used to compute local statistic around each pixel, specified as a positive odd integer or a 2-element vector of positive odd integers.
'ForegroundPolarity'— Determine which pixels are considered foreground pixels
Determine which pixels are considered foreground pixels, specified using one of the following:
The foreground is brighter than the background.
The foreground is darker than the background
'Statistic'— Statistic used to compute local threshold
Statistic used to compute local threshold at each pixel, specified as one of the following:
The local mean intensity in the neighborhood. This technique is also called Bradley’s method .
The local median in the neighborhood. Computation of this statistic can be slow. Consider using a smaller neighborhood size to obtain faster results.
The Gaussian weighted mean in the neighborhood.
 Bradley, D., G. Roth, "Adapting Thresholding Using the Integral Image," Journal of Graphics Tools. Vol. 12, No. 2, 2007, pp.13–21.
Usage notes and limitations:
adaptthresh supports the generation of C
code (requires MATLAB®
Coder™). Note that if you choose the generic
MATLAB Host Computer
adaptthresh generates code that uses a precompiled,
platform-specific shared library. Use of a shared library preserves performance optimizations
but limits the target platforms for which code can be generated. For more information, see Code Generation Using a Shared Library.
must be compile-time constants.