Histogram thresholding#

Binary thresholding#

A very simple method of segmentation consists in associating with each pixel of the image \(f\) a binary number which depends on the intensity of the pixels and on a threshold (French: seuil) \(T\):

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

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 (Fig. 52).

../_images/binarization.svg

Fig. 52 Example of binarization for two thresholds.#

As you can see, the segmentation depends on the value of \(T\). Therefore, the histogram is very useful to choose the threshold! As an example, Fig. 53 gives the histogram of the image, with the chosen thresholds.

../_images/binarization-histogram.svg

Fig. 53 Histogram of Fig. 52 with the two thresholds.#

It would be useful to have an automatic process to define the threshold, whatever the image to segment. Otsu’s method is the most famous automatic method for histogram thresholding.

Otsu’s method#

Binarization divides the histogram of the images in two groups, namely class 0 and class 1, as illustrated Fig. 54.

../_images/otsu-histogram.svg

Fig. 54 An histogram and a threshold \(T\).#

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

\[ \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,\dots,L-1\}\) and \(h\) the histogram, the variables are defined as below.

Class 1

\(\displaystyle w_0(T) = \sum_{i = 0}^{T} h(i)\)

\(\displaystyle \mu_0(T) = \frac{1}{w_0(T)} \sum_{i = 0}^{T} i h(i)\)

\(\displaystyle \sigma^2_0(T) = \frac{1}{w_0(T)} \sum_{i = 0}^{T}(i-\mu_0(T))^2 h(i)\)

Class 2

\(\displaystyle w_1(T) = \sum_{i = T+1}^{L-1} h(i)\)

\(\displaystyle \mu_1(T) = \frac{1}{w_1(T)} \sum_{i = T+1}^{L-1} i h(i)\)

\(\displaystyle \sigma^2_1(T) = \frac{1}{w_1(T)} \sum_{i = T+1}^{L-1}(i-\mu_1(T))^2 h(i)\)

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

An example of Otsu’s thresholding is given Fig. 55.

../_images/otsu.svg

Fig. 55 Result of Otsu’s segmentation.#

Multiple thresholds#

An image can be segmented in more than two classes by defining or computing several thresholds (see Fig. 56). 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!

../_images/multiple-thresholds.svg

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