RGB to monochrome conversion

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

18.08.2008 08:27:32
I think you mean greyscale. Monochrome means an image is in one hue, and is still colorful.
golopot 17.06.2017 02:04:55

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);
18.08.2008 08:27:43
You have to LINEARIZE the sRGB values before you can apply the coefficients, and then re-apply gamma.
Myndex 20.06.2019 03:21:19

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);

8.10.2015 01:07:23
I think these are both wrong for this application as they apply to gamma-corrected RGB values.
Jason Sundram 22.11.2011 23:22:35
These are actually the same
user877329 10.12.2015 07:54:29

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!).

18.08.2008 19:38:49

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.

21.08.2017 04:37:50

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.

25.08.2008 08:30:01

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.


Original picture in colours.

Picture with colours

After converting to monochrome using k-means clustering Picture 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.

10.07.2018 20:14:06