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.


Global image threshold using Otsu's method


level = graythresh(I)
[level,EM] = graythresh(I)



level = graythresh(I) computes a global threshold, level, that can be used to convert an intensity image to a binary image with imbinarize. The graythresh function uses Otsu's method, which chooses the threshold to minimize the intraclass variance of the black and white pixels [1].

Multidimensional arrays are converted automatically to 2-D arrays using reshape. The graythresh function ignores any nonzero imaginary part of I.

[level,EM] = graythresh(I) returns the effectiveness metric, EM, as the second output argument. The effectiveness metric is a value in the range [0, 1] that indicates the effectiveness of the thresholding of the input image. The lower bound is attainable only by images having a single gray level, and the upper bound is attainable only by two-valued images.


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.


Input Arguments

collapse all

Intensity image, specified as a nonsparse 2-D array.

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

Output Arguments

collapse all

Global threshold, returned as a positive scalar. level is a normalized intensity value in the range [0, 1].

Data Types: double

Effectiveness metric, returned as a positive scalar.

Data Types: double


  • By default, the function imbinarize creates a binary image using a threshold obtained using Otsu's method. This default threshold is identical to the threshold returned by graythresh. However, imbinarize only returns the binary image. If you want to know the level or the effectiveness metric, use graythresh before calling imbinarize.


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

Introduced before R2006a

Was this topic helpful?