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
Find Threshold and Segment Bright Rice Grains from Dark Background
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')
Find Threshold and Segment Dark Text from Bright Background
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)
Calculate Threshold for 3-D Volume
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
2-D numeric matrix | 3-D numeric array
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
'bright' (default) |
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
'mean' (default) |
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.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
adaptthreshsupports the generation of C code (requires MATLAB® Coder™). Note that if you choose the generic
MATLAB Host Computertarget platform,
adaptthreshgenerates 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 Types of Code Generation Support in Image Processing Toolbox.
Statisticarguments must be compile-time constants.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.