Otsu thresholding — image binarization

HBY coding academic
4 min readJan 19, 2019

In image processing and analysis, we sometimes need a method to separate two related data, for example, background and foreground, land and river. In this article, I’ll introduce a data-driven way that can adaptively find the optimal threshold to distinguish two-class data — Otsu thresholding. This method can be applied in image segmentation and image binarization, and the latter is the main topic in this article.

+ Short introduction

Otsu’s method is an adaptive thresholding way for binarization in image processing. It can find the optimal threshold value of the input image by going through all possible threshold values (from 0 to 255).

+ More details

In the following three sections, I’ll explain how Otsu thresholding works and why it does that.

Overview of Otsu method: find the optimal threshold value so that within-class variance Vw is minimum (or between-class variance Vb is maximum).

  1. create a histogram for the input image

We assume that the input image is grayscale. As for the RGB input image, it should be converted into a grayscale image first.

In Otsu thresholding, statistical data of an image is used. First, we talk about “Histogram”. It is a representation of the distribution of the data. And how to create the histogram for an input image? Very straightforward, take a look at the following example 👇, to calculate the number of every pixel value in one image and this statistical result is the so-called histogram.

Note that the range of the pixel value is from 0 to 255. For convenience, we assume that the pixel value is not greater than 5 and the pixel value is not related to the color shown in the example.

Image by HBY coding academic | Histogram for the image

2. Within-class variance

Use the same histogram as an example. If we choose the threshold value T=2, then the image is separated into two classes, which are Class 1 (pixel value<=2) and Class 2 (pixel value>2). We can say that these two classes represent the background and foreground of the input image respectively. (Class 2 can be the background if the foreground is darker than the background)

Now, let’s talk about “Variance”. From the following mathematical equation 👇, it can be explained as the distribution of the data. The higher the value of “Variance”, the more dispersed the data is.

Image by HBY coding academic | Variance; Xi is the pixel value, μ is the mean, and N is the number of pixels in one image

In terms of the within-class variance (Vw, for short), the lower the value of Vw is, the less dispersed the data in each class is (background and foreground). As the result, to get the optimal threshold value is to find the minimum value of Vw.

Here is the example to calculate Vw.

Image by HBY coding academic | An example to compete Vw separated by a threshold T

3. Between-class variance

In the previous section, we mentioned “within-class variance”. Here, we then talk about “between-class variance” (Vb, for short). Vb is the variance between two classes. And, to get the suitable threshold value is to find the maximum value of Vb. I’ll provide two ways to explain.

  • Way 1:

There is another technical term “total variance” (Vt, for short), which is the total variance in one image. Obviously, Vt-Vw=Vb because the variance is either in classes or between classes. Vt is held fixed in one image, and Vw is minimum so that Vb should be maximum.

  • Way 2:

We know that there are two classes, which are the background and foreground of the image. Consider if Vb is minimum. It means that the dispersity between the two classes is the lowest (it is not a good result). As the result, Vb should be maximum.

Here is the example to calculate Vb.

Image by HBY coding academic | An example to compete Vb separated by a threshold T

+ Conclusion

Otsu’s method is adaptive thresholding for image binarization. From pixel value 0 to 255, find the optimal threshold value by calculating and evaluating their between-class variance (or within-class variance). There are the input image and resultant image shown below. If you are interested in the math secrets behind Otsu thresholding, the article provides a complete introduction.

Image by HBY coding academic | (left) Input image (right) The resultant image

--

--

HBY coding academic

Image Processing/Game Designing/Informative and Interesting