Binarize 2-D grayscale image or 3-D volume by thresholding
BW = imbinarize(I)
BW = imbinarize(I,method)
BW = imbinarize(I,T)
BW = imbinarize(I,'adaptive',Name,Value)
binary image from 2-D or 3-D grayscale image
BW = imbinarize(
I by replacing all
values above a globally determined threshold with
1s and setting
all other values to
0s. By default,
uses Otsu's method, which chooses the threshold value to minimize the intraclass
variance of the thresholded black and white pixels .
imbinarize uses a 256-bin image histogram to compute Otsu's
threshold. To use a different histogram, see
Read grayscale image into the workspace.
I = imread('coins.png');
Convert the image into a binary image.
BW = imbinarize(I);
Display the original image next to the binary version.
Read grayscale image into workspace.
I = imread('rice.png');
Convert grayscale image to binary image.
BW = imbinarize(I, 'adaptive');
Display original image along side binary version.
Read a grayscale image into the workspace and display it.
I = imread('printedtext.png'); figure imshow(I) title('Original Image')
Convert the image to a binary image using adaptive thresholding. Use the
ForegroundPolarity parameter to indicate that the foreground is darker than the background.
BW = imbinarize(I,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
Display the binary version of the image.
figure imshow(BW) title('Binary Version of Image')
Load 3-D grayscale intensity data into the workspace.
load mristack; V = mristack;
View the 3-D volume.
figure slice(double(V),size(V,2)/2,size(V,1)/2,size(V,3)/2) colormap gray shading interp
Convert the intensity volume into a 3-D binary volume.
J = imbinarize(V);
View the 3-D binary volume.
figure slice(double(J),size(J,2)/2,size(J,1)/2,size(J,3)/2) colormap gray shading interp
I— Input image
Input image, specified as a 2-D grayscale image or a 3-D grayscale volume.
imbinarize expects pixel values of data type
single to be in the
range [0, 1]. You can use the
rescale function to adjust pixel values to the expected
imbinarize interprets an RGB image as a volumetric
grayscale image and does not binarize each channel separately. To
produce a binary image from an RGB image, first convert the image to a
grayscale image using
method— Method used to binarize image
Method used to binarize image, specified as one of the following values.
Calculate global image threshold using Otsu's method.
Calculate locally adaptive image threshold chosen using local first-order image statistics
around each pixel. See
Threshold luminance value, specified as a numeric scalar or numeric array with values in the range [0, 1].
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
BW = imbinarize(I,'adaptive','Sensitivity',0.4);
'Sensitivity'— Sensitivity factor for adaptive thresholding
0.50(default) | number in the range [0, 1]
Sensitivity factor for adaptive thresholding, specified as the comma-separated pair consisting
'Sensitivity' and a number in the range [0, 1]. A high sensitivity
value leads to thresholding more pixels as foreground, at the risk of including some
'ForegroundPolarity'— Determine which pixels are considered foreground pixels
Determine which pixels are considered foreground pixels for adaptive thresholding, specified
as the comma-separated pair consisting of
'ForegroundPolarity' and one of the following
The foreground is brighter than the background.
The foreground is darker than the background
To produce a binary image from an indexed image, first convert the image to a
grayscale image using
'adaptive' method binarizes the image using a locally adaptive
imbinarize computes a threshold for each pixel using the local
mean intensity around the neighborhood of the pixel. This technique is also called Bradley's
method . The
method also uses a neighborhood size of approximately 1/8th of the size of the image (computed as
2*floor(size(I)/16)+1). To use a different first order local statistic or a
different neighborhood size, see
 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.
 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:
imbinarize supports the generation of C
code (requires MATLAB®
Coder™). Note that if you choose the generic
MATLAB Host Computer
imbinarize 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.
When generating code, all character vector input arguments must be compile-time constants.