RGB to monochrome conversion

China☆狼群 提交于 2019-11-30 06:31:20

问题


How do I convert the RGB values of a pixel to a single monochrome value?


回答1:


I found one possible solution in the Color FAQ. The luminance component Y (from the CIE XYZ system) captures what is most perceived by humans as color in one channel. So, use those coefficients:

mono = (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b);



回答2:


This MSDN article uses (0.299 * color.R + 0.587 * color.G + 0.114 * color.B);

This Wikipedia article uses (0.3* color.R + 0.59 * color.G + 0.11 * color.B);




回答3:


This recent scientific article compares the state-of-the-art in converting color photographs to grayscale, including the simple luminance formula and more complex techniques.




回答4:


This depends on what your motivations are. If you just want to turn an arbitrary image to grayscale and have it look pretty good, the conversions in other answers to this question will do.

If you are converting color photographs to black and white, the process can be both very complicated and subjective, requiring specific tweaking for each image. For an idea what might be involved, take a look at this tutorial from Adobe for Photoshop.

Replicating this in code would be fairly involved, and would still require user intervention to get the resulting image aesthetically "perfect" (whatever that means!).




回答5:


As mentioned also, a grayscale translation (note that monochromatic images need not to be in grayscale) from an RGB-triplet is subject to taste.

For example, you could cheat, extract only the blue component, by simply throwing the red and green components away, and copying the blue value in their stead. Another simple and generally ok solution would be to take the average of the pixel's RGB-triplet and use that value in all three components.

The fact that there's a considerable market for professional and not-very-cheap-at-all-no-sirree grayscale/monochrome converter plugins for Photoshop alone, tells that the conversion is just as simple or complex as you wish.




回答6:


The logic behind converting any RGB based picture to monochrome can is not a trivial linear transformation. In my opinion such a problem is better addressed by "Color Segmentation" techniques. You could achieve "Color segmentation" by k-means clustering.

See reference example from MathWorks site.

https://www.mathworks.com/examples/image/mw/images-ex71219044-color-based-segmentation-using-k-means-clustering

Original picture in colours.

After converting to monochrome using k-means clustering

How does this work?

Collect all pixel values from entire image. From an image which is W pixels wide and H pixels high, you will get W *H color values. Now, using k-means algorithm create 2 clusters (or bins) and throw the colours into the appropriate "bins". The 2 clusters represent your black and white shades.

Youtube video demonstrating image segmentation using k-means? https://www.youtube.com/watch?v=yR7k19YBqiw

Challenges with this method

The k-means clustering algorithm is susceptible to outliers. A few random pixels with a color whose RGB distance is far away from the rest of the crowd could easily skew the centroids to produce unexpected results.



来源:https://stackoverflow.com/questions/14330/rgb-to-monochrome-conversion

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