help multithresh
MULTITHRESH Multi-level image thresholds using Otsu's method.
THRESH = MULTITHRESH(A) computes a single threshold for image A using
Otsu's method and returns it in THRESH. THRESH can be used to convert A
into a two-level image using IMQUANTIZE.
THRESH = MULTITHRESH(A, N) computes N thresholds for image A using the
Otsu's method and returns them in THRESH. THRESH is a 1xN vector which
can be used to convert A into an image with (N+1) discrete levels using
IMQUANTIZE. The maximum value allowed for N is 20.
[THRESH, METRIC] = MULTITHRESH(A,...) returns the effectiveness metric
as the second output argument. METRIC is in the range [0 1] and a
higher value indicates greater effectiveness of the thresholds in
separating the input image into N+1 regions based on Otsu's objective
criterion.
Class Support
-------------
The input image A is an array of one of the following classes: uint8,
uint16, int16, single, or double. It must be nonsparse. N is a positive
integer between 1 and 20, and can be any numeric class. THRESH is a 1xN
numeric vector of the same class as A. METRIC is a scalar of class
double.
Notes
-----
1. A can be an array of any dimension. MULTITHRESH finds the thresholds
based on the aggregate histogram of the entire array. An RGB image
is considered a 3D numeric array and the thresholds are computed
for the combined data from all the three color planes.
2. MULTITHRESH uses the range of the input A [min(A(:)) max(A(:))] as
the limits for computing image histogram which is used in subsequent
computations. Any NaNs in A are ignored in computation. Any Infs and
-Infs in A are counted in the first and last bin of the histogram,
respectively.
3. For N > 2, MULTITHRESH uses search-based optimization of Otsu's
criterion to find the thresholds. The search-based optimization
guarantees only locally optimal results. Since the chance of
converging to local optimum increases with N, it is preferable to
use smaller values of N, typically N < 10.
4. For degenerate inputs where the number of unique values in A is less
than or equal to N, there is no viable solution using Otsu's method.
For such inputs, MULTITHRESH returns THRESH such that it includes
all the unique values from A and possibly some extra values that are
chosen arbitrarily.
5. The thresholds (THRESH) returned by MULTITHRESH are in the same
range as the input image A. This is unlike GRAYTHRESH which returns
a normalized threshold in the range [0, 1].
Reference
---------
N. Otsu, "A Threshold Selection Method from Gray-Level Histograms," IEEE
Transactions on Systems, Man, and Cybernetics, Vol. 9, No. 1, pp. 62-66,
1979.
Example
---------
% This example computes multiple thresholds for an image using
% MULTITHRESH and applies those thresholds to the image using IMQUANTIZE
% to get segment labels.
I = imread('circlesBrightDark.png');
imshow(I, [])
title('Original Image');
% Compute the thresholds
thresh = multithresh(I,2);
% Apply the thresholds to obtain segmented image
seg_I = imquantize(I,thresh);
% Show the various segments in the segmented image in color
RGB = label2rgb(seg_I);
figure, imshow(RGB)
title('Segmented Image');
See also GRAYTHRESH, IMBINARIZE, IMQUANTIZE, RGB2IND.
Documentation for multithresh
doc multithresh