Main Content

graythresh

R2026b

Global image threshold using Otsu's method

Description

thresh = graythresh(I) calculates a global threshold thresh from grayscale image I, using Otsu's method [1]. Otsu's method chooses a threshold that minimizes the intraclass variance of the thresholded black and white pixels.

graythresh calculates the global threshold by binning the image data into a 256-bin image histogram. By default, the graythresh function creates the histogram assuming that the image data spans the full range of the data type.

You can use the global threshold thresh with the imbinarize function to convert a grayscale image to a binary image.

example

thresh = graythresh(I,HistogramRangeMode=histogramRangeMode) calculates the threshold based on a histogram whose range is specified by histogramRangeMode. Specify histogramRangeMode as "data-range" when the image data has a small dynamic range compared to the data type.

[thresh,em] = graythresh(I) also returns the effectiveness metric, em.

Examples

collapse all

Read a grayscale image into the workspace.

I = imread('coins.png');

Calculate a threshold using graythresh. The threshold is normalized to the range [0, 1].

level = graythresh(I)
level = 
0.4941

Convert the image into a binary image using the threshold.

BW = imbinarize(I,level);

Display the original image next to the binary image.

imshowpair(I,BW,'montage')

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

Input Arguments

collapse all

Grayscale image, specified as a numeric array of any dimensionality. The graythresh 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.

Data Types: single | double | int16 | uint8 | uint16

Since R2026b

Histogram range mode, specified as one of these values.

Value

Meaning

"type-range"

The histogram spans the range of the data type.

"data-range"

The histogram spans the range of the image data. Specify this value when the image data has a small dynamic range compared to the data type.

Data Types: char | string

Output Arguments

collapse all

Global threshold, returned as a number in the range [0, 1].

Data Types: double

Effectiveness metric of the threshold, returned as a number in the range [0, 1]. The lower bound is attainable only by images having a single gray level, and the upper bound is attainable only by two-valued images.

Data Types: double

Tips

  • The imbinarize function can also calculate a global threshold for binary images using Otsu’s method, and apply the threshold to binary images in one step. However, imbinarize does not calculate an effectiveness metric. If you want to know the effectiveness metric, use graythresh before calling imbinarize.

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.

Extended Capabilities

expand all

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a

expand all