(Not recommended) Convert image to binary image, based on threshold
im2bw is not recommended. Use
imbinarize instead. For more information, see Compatibility Considerations.
converts the grayscale image
BW = im2bw(
I to binary image
BW, by replacing all pixels in the input image with
luminance greater than
level with the value
1 (white) and replacing all other pixels with the value
This range is relative to the signal levels possible for the image's class.
level value of
to an intensity value halfway between the minimum and maximum value of the
Convert an Indexed Image To a Binary Image
load trees BW = im2bw(X,map,0.4); imshow(X,map), figure, imshow(BW)
I — 2-D grayscale image
m-by-n numeric matrix
2-D grayscale image, specified as an m-by-n numeric matrix.
X — 2-D indexed image
m-by-n numeric matrix
2-D indexed image, specified as an m-by-n numeric matrix.
cmap — Colormap
c-by-3 numeric matrix
Colormap associated with indexed image
RGB — 2-D RGB image
m-by-n-by-3 numeric matrix
2-D RGB image, specified as an m-by-n-by-3 numeric matrix.
level — Luminance threshold
0.5 (default) | number in the range [0, 1]
Luminance threshold, specified as a number in the range [0, 1]. To compute
level, you can use the
BW — Binary image
m-by-n logical matrix
Binary image, returned as an m-by-n logical matrix.
If the input image is not a grayscale image,
im2bw converts the
input image to grayscale using
rgb2gray, and then converts this
grayscale image to binary by thresholding.
Version HistoryIntroduced before R2006a
im2bw is not recommended
The default luminance threshold of
im2bw is not optimal for
most images. If you want to use a threshold appropriate for your image, you must
compute the level using
graythresh before calling
In R2016a, the
imbinarize function was introduced.
This function computes the luminance threshold and performs binarization in one
imbinarize has additional benefits, such as the ability to
perform adaptive thresholding when the image has nonuniform shading. For more
information, see Image Binarization - New 2016a Functions.
The table shows some typical usages of
im2bw and how to update
your code to use
BW = im2bw(I);
BW = imbinarize(I,0.5);
thresh = graythresh(I); BW = im2bw(I,thresh);
BW = imbinarize(I);
There are no plans to remove
im2bw at this time.