Thresholding is a very basic operation in image processing. And, a good algorithm always begins with a good basis! Otsu thresholding is a simple yet effective global automatic thresholding method for binarizing grayscale images such as foregrounds and backgrounds.
In image processing, Otsu’s thresholding method (1979) is used for automatic binarization level decision, based on the shape of the histogram. The algorithm assumes that the image is composed of two basic classes: Foreground and Background. It then computes an optimal threshold value that minimizes the weighted within class variances of these two classes. It is mathematically proven that minimizing the within class variance is same as maximizing the between class variance.
Otsu threshold is used in many applications from medical imaging to low level computer vision. It has many advantages and assumptions.
- Speed: Because Otsu threshold operates on histograms (which are integer or float arrays of length 256), it’s quite fast.
- Ease of coding: Approximately 80 lines of very easy stuff.
- Assumption of uniform illumination.
- Histogram should be bimodal (hence the image).
- It doesn’t use any object structure or spatial coherence.
- The non-local version assumes uniform statistics.
Where q1 and q2 represent the estimate of class probabilities defined as:
and sigmas are the individual class variances defined as:
and the class means:
Here, P represents the image histogram. The problem of minimizing within class variance can be expressed as a maximization problem of the between class variance. It can be written as a difference of total variance and within class variance:
Finally, this expression can safely be maximized and the solution is t that is maximizing .
Now, let's take a look at Otsu's thresholding from a more algorithmic point of view. Here are the steps of the algorithm:
For each potential threshold T, we:
- Separate the pixels into two clusters according to the threshold.
- Find the mean of each cluster.
- Square the difference between the means.
- Multiply by the number of pixels in one cluster times the number in the other.
If we incorporate a little math into that simple step-wise algorithm, such an explanation evolves:
- Compute histogram and probabilities of each intensity level.
- Set up initial qi(0) and μi(0).
- Step through all possible thresholds maximum intensity.
- Update qi and μi.
- Compute .
- Desired threshold corresponds to the maximum.
Since Otsu operates over the histograms, it's very wise to analyze the image histogram and decision of threshold level. I think this simple image will be enough to summarize the story (the threshold value is marked by the red arrow):
Using the Code
It's pretty simple to use the code. Otsu Thresholding works on grayscale images. So firstly, we have to convert our image into a gray scale one. After that, we can obtain our Otsu threshold value by solving for maximal t. Finally, we threshold our image using this t value. In short, the usage looks like this:
Bitmap temp = (Bitmap)org.Clone();
int otsuThreshold= ot.getOtsuThreshold((Bitmap)temp);
textBox1.Text = otsuThreshold.ToString();
pictureBox1.Image = temp;
If we look inside the
getOtsuThreshold function, it looks like this:
public int getOtsuThreshold(Bitmap bmp)
float vet=new float;
int hist=new int;
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
for (k = 1; k != 255; k++)
p1 = Px(0, k, hist);
p2 = Px(k + 1, 255, hist);
p12 = p1 * p2;
if (p12 == 0)
p12 = 1;
float diff=(Mx(0, k, hist) * p2) - (Mx(k + 1, 255, hist) * p1);
vet[k] = (float)diff * diff / p12;
t = (byte)findMax(vet, 256);
For more information on how to efficiently process images in C#, please refer to my older articles such as "Image Processing Basics in C#".
Points of Interest
Otsu thresholding doesn't claim to be the best automatic thresholding ever, but there are many applicable uses in computer vision and medical imaging. It's a first step to get non-parametrized, adaptive algorithms. Addition to that, there may be faster implementations available (such as recursive algorithms).
Also, Otsu threshold can be extended to a multi-level thresholding which could result in segmentation. Such research is present in the literature. This may be a further article. Please check:
- 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.
Currently, also an MSc. student in Technical University of Munich, I develop practical application in computer vision for more than 5 years. I design real-time solutions to industrial and practical vision problems, both 3D and 2D. Very interested in developing algorithms in C relating math and vision.
Please visit Gravi's web page (www.gravi.com.tr) and my page (www.tbirdal.me) to learn more about what we develop.
I admire performance in algorithms.
"Great minds never die, they just tend to infinity..."