Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

imbinarize

Binarize image by thresholding

Syntax

BW = imbinarize(I)
BW = imbinarize(I,method)
BW = imbinarize(I,T)
BW = imbinarize(I,'adaptive',Name,Value)

Description

example

BW = imbinarize(I) creates a binary image from 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. imbinarize uses a 256-bin image histogram to compute Otsu's threshold. To use a different histogram, see otsuthresh. BW is the output binary image.

example

BW = imbinarize(I,method) creates a binary image from image I using the thresholding method specified by method.

BW = imbinarize(I,T) creates a binary image from image I using the threshold value T.

example

BW = imbinarize(I,'adaptive',Name,Value) creates a binary image from image I using 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')

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

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

Input Arguments

collapse all

Input image, specified as a real, nonsparse, 2-D matrix.

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

Method used to binarize image, specified as one of the following values (names can be abbreviated).

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 may not be localized to the neighborhood around Inf and NaN pixels.

Data Types: char

Threshold, specified as a scalar luminance value or as a matrix of luminance values. If T is a scalar luminance value, imbinarize interprets it as a global image threshold. If T is a matrix of luminance values, imbinarize interprets it as a locally adaptive threshold. T must have a value between 0 and 1. If T is a matrix, it must be of the same size as I. Use the functions graythresh, otsuthresh, or adaptthresh to compute T.

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

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

collapse all

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.

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

Determine which pixels are considered foreground pixels, specified as either of the following values:

Values

Meaning

'bright'

The foreground is brighter than the background. This is the default.

'dark'

The foreground is darker than the background

Data Types: char

Output Arguments

collapse all

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

Tips

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

  • To produce a binary image from an RGB image, first convert the image to a grayscale intensity image using rgb2gray.

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

Extended Capabilities

Introduced in R2016a

Was this topic helpful?