Main Content

imbinarize

Binarize 2-D grayscale image or 3-D volume by thresholding

Description

BW = imbinarize(I) creates a binary image from 2-D or 3-D grayscale image I by replacing all values above a globally determined threshold with 1s and setting all other values to 0s. By default, imbinarize uses Otsu's method, which chooses the threshold value to minimize the intraclass variance of the thresholded black and white pixels [1]. imbinarize uses a 256-bin image histogram to compute Otsu's threshold. To use a different histogram, see otsuthresh.

example

BW = imbinarize(I,method) creates a binary image from image I using the thresholding method specified by method: "global" or "adaptive".

example

BW = imbinarize(I,T) creates a binary image from image I using the threshold value T. T can be a global image threshold, specified as a scalar luminance value, or a locally adaptive threshold, specified as a matrix of luminance values.

BW = imbinarize(I,"adaptive",Name=Value) creates a binary image from image I using name-value arguments to control aspects of adaptive thresholding.

example

Examples

collapse all

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.

figure
imshowpair(I,BW,'montage')

Figure contains an axes object. The hidden axes object contains an object of type image.

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.

figure
imshowpair(I,BW,'montage')

Figure contains an axes object. The hidden axes object contains an object of type image.

Read a grayscale image into the workspace and display it.

I = imread('printedtext.png');
figure
imshow(I)
title('Original Image')

Figure contains an axes object. The hidden axes object with title Original Image contains an object of type 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')

Figure contains an axes object. The hidden axes object with title Binary Version of Image contains an object of type 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

Figure contains an axes object. The axes object contains 3 objects of type surface.

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

Figure contains an axes object. The axes object contains 3 objects of type surface.

Input Arguments

collapse all

Input image, specified as a 2-D grayscale image or a 3-D grayscale volume. The imbinarize function expects images of data type double and single to have values in the range [0, 1]. If I has values outside the range [0, 1], then you can rescale values to the expected range by using the rescale function.

Note

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 im2gray.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

Method used to binarize image, specified as one of the following values.

Values

Meaning

"global"

Calculate global image threshold using Otsu's method. See graythresh for more information about Otsu’s method.

"adaptive"

Calculate locally adaptive image threshold chosen using local first-order image statistics around each pixel. See adaptthresh for details. If the image contains Infs or NaNs, the behavior of imbinarize for the "adaptive" method is undefined. Propagation of Infs or NaNs might not be localized to the neighborhood around Inf and NaN pixels.

Data Types: char | string

Threshold luminance value, specified as a numeric scalar or numeric array with values in the range [0, 1].

  • If T is a numeric scalar, then imbinarize interprets T as a global image threshold. Use graythresh or otsuthresh to compute a global image threshold.

  • If T is a numeric array, then imbinarize interprets T as a locally adaptive threshold. Use adaptthresh to compute a locally adaptive threshold.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: BW = imbinarize(I,"adaptive",Sensitivity=0.4); specifies the sensitivity factor as 0.4.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: BW = imbinarize(I,"adaptive","Sensitivity",0.4); specifies the sensitivity factor as 0.4.

Sensitivity factor for adaptive thresholding, specified as a number in the range [0, 1]. A high sensitivity value leads to thresholding more pixels as foreground, at the risk of including some background pixels.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Determine which pixels are considered foreground pixels for adaptive thresholding, specified as one of the following values.

Value

Meaning

"bright"

The foreground is brighter than the background.

"dark"

The foreground is darker than the background

Data Types: char | string

Output Arguments

collapse all

Output binary image, returned as a logical matrix or logical array of the same size as I.

Data Types: logical

Tips

  • To produce a binary image from an indexed image, first convert the image to a grayscale image using ind2gray.

Algorithms

The "adaptive" method binarizes the image using a locally adaptive threshold. 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 [2]. The "adaptive" 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 adaptthresh.

References

[1] 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.

[2] Bradley, D., G. Roth, "Adapting Thresholding Using the Integral Image," Journal of Graphics Tools. Vol. 12, No. 2, 2007, pp.13–21.

Extended Capabilities

Version History

Introduced in R2016a