Main Content

imbinarize

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

Description

example

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

example

BW= imbinarize(I,method)creates a binary image from imageIusing the thresholding method specified bymethod:'global'or'adaptive'.

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

example

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

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 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 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 axes object with title Original Image contains an object of type image.

Convert the image to a binary image using adaptive thresholding. Use theForegroundPolarityparameter 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 axes object with title Binary Version of Image contains an object of type image.

加载3 d灰度强度data into the workspace.

loadmristack; V = mristack;

View the 3-D volume.

figure slice(double(V),size(V,2)/2,size(V,1)/2,size(V,3)/2) colormapgrayshadinginterp

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) colormapgrayshadinginterp

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.imbinarizeexpects pixel values of data typedoubleandsingleto be in the range [0, 1]. You can use therescalefunction to adjust pixel values to the expected range.

Note

imbinarizeinterprets 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 usingrgb2gray.

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. Seegraythreshfor more information about Otsu’s method.

'adaptive'

Calculate locally adaptive image threshold chosen using local first-order image statistics around each pixel. Seeadaptthreshfor details. If the image containsInfs ors, the behavior ofimbinarizefor the'adaptive'method is undefined. Propagation ofInfs ors might not be localized to the neighborhood aroundInfandpixels.

Data Types:char|string

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

  • IfTis a numeric scalar, thenimbinarizeinterpretsTas a global image threshold. Usegraythreshorotsuthreshto compute a global image threshold.

  • IfTis a numeric array, thenimbinarizeinterpretsTas a locally adaptive threshold. Useadaptthreshto 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 asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseName在报价。

Example:BW = imbinarize(I,'adaptive','Sensitivity',0.4);

Sensitivity factor for adaptive thresholding, specified as the comma-separated pair consisting of'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 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 the comma-separated pair consisting of'ForegroundPolarity'and 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 asI.

Data Types:logical

Tips

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

Algorithms

The'adaptive'method binarizes the image using a locally adaptive threshold.imbinarizecomputes 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 as2*floor(size(I)/16)+1). To use a different first order local statistic or a different neighborhood size, seeadaptthresh.

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