Best threshold for converting grayscale to black and white

大城市里の小女人 提交于 2019-12-04 19:33:20

问题


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!


回答1:


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.




回答2:


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.




回答3:


The K-Means Clustering Method works great if you do the following:

  1. Partitioning the image into Sub Blocks.
  2. Apply The K-Means Clustering on each sub block. The result is a binary image (Let's assume what you wanted is '1' and the rest '0').
  3. Make step 2 again, this time on overlapping blocks.
  4. Apply 'AND' operator on the sub images (For Overlapping Sub Blocks).

It's really easy to do in Matlab.
If needed, I can share the code.




回答4:


What are your criteria for a "good" threshold? You might want to start with the average grayscale intensity of the image...




回答5:


I would think that the threshold would depend upon the average darkness (or distribution of colors) upon each image independently. If you go with an arbitrary value, then you'll end up losing a lot of data if the image started out pretty washed out.

Also, you can emulate some of the grayscales by sparsely populating an area with black and white. 50% gray is an every-other checkerboard, 75% you color in half the remaining white squares, 25% you invert black and white, etc.

I don't think there's a fixed answer for this question without considering each image individually.




回答6:


Threshold-based halftoning usually results in a lot of information loss. Depending on the purpose, you may want to consider dithering.

I like the look of the Stucki filter, since it's sharp and preserves detail. Here's a C# project that implements the algorithm. You could download the source if you were interested.



来源:https://stackoverflow.com/questions/1871852/best-threshold-for-converting-grayscale-to-black-and-white

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!