Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Histogram thresholding is a very basic method for segmenting grayscale images based on the pixel intensities.

Binary thresholding

Binary thresholding consists in associating each pixel of the image ff to a binary number which depends on the intensity of the pixels and on a threshold (French: seuil) TT:

g(m,n)={1if f(m,n)⩾T,0if f(m,n)<Tg(m,n) = \begin{cases} 1 & \text{if}\, f(m,n)\geqslant T, \\ 0 & \text{if}\, f(m,n)<T \end{cases}

This method is called “binarization” (French: binarisation). It gives a segmentation into two classes, depending on the intensity of the pixels of a grayscale image (Figure 1).

Example of binarization for two thresholds.

Figure 1:Example of binarization for two thresholds.

Because the segmentation depends on the value of TT, the histogram can be very useful. As an example, Figure 2 gives the histogram of the previous image, with the chosen thresholds.

Histogram of  with the two thresholds.

Figure 2:Histogram of Figure 1 with the two thresholds.

It would be useful to have an automatic process to define the threshold. Several methods exist but Otsu’s method is the most famous automatic method for histogram thresholding.

Otsu’s method

Binarization divides the image histogram in two groups, namely class 0 and class 1, as illustrated Figure 3.

An histogram and a threshold T.

Figure 3:An histogram and a threshold TT.

Each group has a number of pixel wi(T)w_i(T) with mean μi(T)\mu_i(T) and variance σi2(T)\sigma_i^2(T), where i∈{0,1}i\in\{0,1\} is the group index. Otsu’s method [Otsu 1979] computes the threshold TT which minimizes the intra-class variance σw2(T)\sigma_w^2(T) (also known as the within-class variance), defined as the weighted mean of the variances of each class:

σw2(T)=w0(T)σ02(T)+w1(T)σ12(T).\sigma_w^2(T) = w_0(T)\sigma_0^2(T) + w_1(T)\sigma_1^2(T).

Considering the intensities to lie in {0,…,L−1}\{0,\dots,L-1\} and hh being the histogram, we have:

The algorithm to determine the value of TT that minimize σw2(T)\sigma_w^2(T) is simple: the intra-class variance σw2(T)\sigma_w^2(T) is calculated for all the thresholds T={0,…,L−1}T=\{0,\dots,L-1\}, and the one that gives the lowest value for σw2(T)\sigma_w^2(T) is returned.

An example of Otsu’s thresholding is given Figure 4.

Result of Otsu’s segmentation.

Figure 4:Result of Otsu’s segmentation.

Multiple thresholds

An image can be segmented in more than two classes by defining several thresholds (Figure 5). In particular, Otsu’s method can be extended to several thresholds, but the computational complexity (hence the computation time) increases greatly with the number of classes!

Several thresholds are applied to an image to get several classes (shown in colors).

Figure 5:Several thresholds are applied to an image to get several classes (shown in colors).