Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best threshold for converting grayscale to black and white

What's the best way to automatically figure out the best threshold for converting a grayscale image to black and white? I can figure out pretty good threshold values by hand, but I would like to automate choosing the threshold value.

Edit: I've been reading a bit about this problem, and by looking at the histogram for the image can help, e.g. if the image has a bi-modal histogram then choosing a threshold between the modes seems to be reasonably. However, for multi-modal, or flat histograms, it appears more complicated. So I think I have some more reading to do. Thanks to everyone who replied!

like image 752
David Parr Avatar asked Dec 09 '09 05:12

David Parr


2 Answers

0.5 usually ends up loosing a lot of information unless the original image is extremely bright. In fact, any absolute threshold will mess up one kind of images or another.

A better method would be to make a histogram of luminosities and choose a threshold near the mode. This should work better on most images than any absolute threshold.

like image 176
KJ Saxena Avatar answered Oct 05 '22 18:10

KJ Saxena


I would look into an adaptive thresholding algorithm. One such, which is not very hard to implement is Otsus method.

It works by assuming that you have foreground pixels and background pixels and attempts to find the best separation of them.

like image 44
Hannes Ovrén Avatar answered Oct 05 '22 19:10

Hannes Ovrén