Global histogram threshold using Otsu's method
a global threshold
T = otsuthresh(
T from histogram counts,
counts, using Otsu's method . Otsu's method chooses a
threshold that minimizes the intraclass variance of the thresholded black and white
pixels. The global threshold
T can be used with
imbinarize to convert a grayscale
image to a binary image.
Compute Threshold from Image Histogram and Binarize Image
Read image into the workspace.
I = imread('coins.png');
Calculate a 16-bin histogram for the image.
[counts,x] = imhist(I,16); stem(x,counts)
Compute a global threshold using the histogram counts.
T = otsuthresh(counts);
Create a binary image using the computed threshold and display the image.
BW = imbinarize(I,T); figure imshow(BW)
counts — Histogram counts
vector of nonnegative numbers
Histogram counts, specified as a vector of nonnegative numbers.
T — Global threshold
Global threshold, returned as a numeric scalar in the range [0, 1].
EM — Effectiveness metric
Effectiveness metric of the threshold, returned as a numeric scalar in the range [0, 1]. The lower bound is attainable only by histogram counts with all data in a single non-zero bin. The upper bound is attainable only by histogram counts with two non-zero bins.
 Otsu, N., "A Threshold Selection Method from Gray-Level Histograms." IEEE Transactions on Systems, Man, and Cybernetics. Vol. 9, No. 1, 1979, pp. 62–66.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
otsuthresh supports the generation of C
code (requires MATLAB®
Coder™). For more information, see Code Generation for Image Processing.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
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.
Version HistoryIntroduced in R2016a
R2021b: Support for thread-based environments
otsuthresh now supports thread-based