adaptthresh
Adaptive image threshold using local first-order statistics
Description
calculates a
locally adaptive threshold for 2-D grayscale image or 3-D grayscale volume
T
= adaptthresh(I
)I
. The 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
the imbinarize
function to convert the grayscale image to a
binary image.
calculates a locally adaptive threshold using a sensitivity factor towards
thresholding more pixels as foreground.T
= adaptthresh(I
,sensitivity
)
calculates a locally adaptive threshold using name-value pairs to control aspects of
the thresholding.T
= adaptthresh(___,Name=Value
)
Examples
Find Threshold and Segment Bright Rice Grains from Dark Background
Read image into the workspace.
I = imread('rice.png');
Use 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');
Using 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
Input Arguments
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.
The
adaptthresh
function expects images of data type
double
and single
to have values in the range [0, 1].
If I
has values outside the range [0, 1], then you can rescale values to
the expected range by using the rescale
function.
If the image contains Inf
or NaN
values, the behavior of adaptthresh
is undefined.
Propagation of Inf
or NaN
values might
not be localized to the neighborhood around Inf
or
NaN
pixels.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
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.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: T = adaptthresh(I,0.4,ForegroundPolarity="dark");
specifies that the foreground is darker than the background.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: T = adaptthresh(I,0.4,"ForegroundPolarity","dark");
specifies that
the foreground is darker than the background.
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.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
ForegroundPolarity
— Determine which pixels are considered foreground pixels
"bright"
(default) | "dark"
Determine which pixels are considered foreground pixels, specified using one of the following:
Value | Meaning |
---|---|
| The foreground is brighter than the background. |
| The foreground is darker than the background. |
Data Types: char
| string
Statistic
— Statistic used to compute local threshold
"mean"
(default) | "median"
| "gaussian"
Statistic used to compute local threshold at each pixel, specified as one of the following:
Value | Meaning |
---|---|
| The local mean intensity in the neighborhood. This technique is also called Bradley’s method [1]. |
| 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. |
Data Types: char
| string
Output Arguments
T
— Normalized intensity values
numeric matrix | numeric array
Normalized intensity values, returned as a numeric matrix or numeric array of the same size as
the input image or volume, I
. Values are clipped to the
range [0, 1].
Data Types: double
References
[1] Bradley, D., G. Roth, "Adapting Thresholding Using the Integral Image," Journal of Graphics Tools. Vol. 12, No. 2, 2007, pp.13–21.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
adaptthresh
supports the generation of C code (requires MATLAB® Coder™). Note that if you choose the genericMATLAB Host Computer
target platform,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 Types of Code Generation Support in Image Processing Toolbox.The
ForegroundPolarity
andStatistic
arguments must be compile-time constants.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
The ForegroundPolarity
and Statistic
arguments must be compile-time constants.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced in R2016aR2022b: Generate CUDA code using GPU Coder
adaptthresh
now supports the generation of
optimized CUDA® code (requires GPU Coder™).
R2021b: Support for thread-based environments
adaptthresh
now supports thread-based
environments.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)