Sitemap

Otsu’s method

4 min readMar 29, 2019

--

Recently, I’m learning some classic computer vision methods from the notes, and I find it a good habit to take some notes, so here I am.
This is my first time to write an article at Medium. So there may be some mistakes, and I will be really happy that you point out the mistakes.
In this article, I will show you the mathematical details of Otsu’s method, and I will show you an example, you can view the Python code here although it’s a really easy example.

Introduction

Otsu’s method can be used to the reduction of a grayscale image to a binary image. It assumes that the image contains two classes of pixels following bi-modal histogram (foreground pixels and background pixels). The key is that this method can generate a threshold based on the histogram of an image.
As the histogram figure shows, it finds the threshold t that minimizes the weighted sum of within-group variances (we will discuss it later, note that it refers to variations caused by differences within individual groups ) for the two groups that result from separating the gray tones at value t.

So, how can we find the proper t ? Before we start, let’s catch up some knowledge of probability theory.

Algorithm details

The variance is defined as:

That is all we need. :)

And the within-group variance is defined as:

where weights ω0 and ω1 are the possibilities of each group (Grp0’s intensity is lesser than t ), σ0 and σ1 are the variances of each group. Suppose we have a normalized histogram P with L bins. The mathematical definition:

So, now, we know how to calculate the within-group variance, it seems we are ready, but there some tricks to simplify the algorithm, so it can run faster.
Here comes the between-group variance, contrary to within-group variance, it refers to the variations caused by differences between groups. Between-group variance is the difference between the variance of the total histogram distribution and within-group variance:

So minimizing within-group variance is equal to maximizing the between-group variance.
Now let’s have a look at the formula of between-group variance:

Following is how we get this formula:

Get Minghao Ning’s stories in your inbox

Join Medium for free to get updates from this writer.

Keep in mind that:

if we set t=L, we can get the E(X).
According to the definition of E(x²) and E(X) we can easily find:

and

So,

Press enter or click to view image in full size

Now, all we need to do is only to get two arrays of the ω0 and E(X0), then we can get the between-group variance. Finally, we use argmax to get the proper threshold t.

Code

img_hist = np.histogram(img_flattened,bins=255)
brightness_list = img_hist[1][1:].astype(int) #ignore the first element
hist_counts = img_hist[0]
normed_hist_counts = hist_counts/hist_counts.sum()
weighted_prob = brightness_list*normed_hist_counts
omiga_arr = normed_hist_counts.cumsum()
Ex_arr = weighted_prob.cumsum()
def calculate_between_group_variance(threshold):
eps = 1e-10 #avoid divide by zero
omiga0 = omiga_arr[threshold-1]
Ex0 = Ex_arr[threshold-1]
between_group_variance = omiga0*(Ex0-EX)**2/(1-omiga0+eps)
return between_group_variance

inter_class_variance_arr = np.array(list(map(calculate_between_group_variance,brightness_list)))

Example:

I got the origin grayscale image from wiki,

Press enter or click to view image in full size

and the histogram is shown as follow(note that I have set count number of 255 as the number of 254, because the sky is overexposed):

the between_group_variance is:

and the final binary map is:

Press enter or click to view image in full size

That is all! Thanks for your time! If you have any question, feel free to contact me. :)

--

--

Minghao Ning
Minghao Ning

Written by Minghao Ning

The top is still spinning perfectly, as if it will never topple.