Binarize image by thresholding
BW = imbinarize(I)
BW = imbinarize(I,method)
BW = imbinarize(I,T)
BW = imbinarize(I,'adaptive',Name,Value)
a binary image from image
BW = imbinarize(
I by replacing all
values above a globally determined threshold with
and setting all other values to
0s. By default,
Otsu's method, which chooses the threshold value to minimize the intraclass
variance of the thresholded black and white pixels.
a 256-bin image histogram to compute Otsu's threshold. To use a different
the output binary image.
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')
I— Input image
Input image, specified as a real, nonsparse, 2-D matrix.
method— Method used to binarize image
Method used to binarize image, specified as one of the following values (names can be abbreviated).
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, specified as a scalar luminance value or as a matrix
of luminance values. If
T is a scalar luminance
imbinarize interprets it as a global image
T is a matrix of luminance values,
it as a locally adaptive threshold.
T must have
a value between
a matrix, it must be of the same size as
adaptthresh to compute
Specify optional comma-separated pairs of
Name is the argument
Value is the corresponding
Name must appear
inside single 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) | value in the range
Sensitivity factor for adaptive thresholding, specified as a
value in the range
[0 1]. A high sensitivity value
leads to thresholding more pixels as foreground, at the risk of including
some background pixels.
'ForegroundPolarity'— Determine which pixels are considered foreground pixels
Determine which pixels are considered foreground pixels, specified as either of the following values:
The foreground is brighter than the background. This is the default.
The foreground is darker than the background
'adaptive' method binarizes the image
using a locally adaptive threshold.
a threshold for each pixel using the local mean intensity around the
neighborhood of the pixel. (This technique is also called Bradley's
'adaptive' method also uses a neighborhood
size of approximately 1/8th of the size of the image (computed as
To use a different first order local statistic or a different neighborhood
Usage notes and limitations:
This function supports the generation of C code using MATLAB® Coder™.
Note that if you choose the generic
MATLAB Host Computer target
platform, the function 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 Understanding Code Generation with Image Processing Toolbox.
When generating code, all character vector input arguments must be compile-time constants.