RGB to monochrome conversion
Asked Answered
A

6

22

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

Antoine answered 18/8, 2008 at 8:27 Comment(2)
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.Asir
I think you mean greyscale. Monochrome means an image is in one hue, and is still colorful.Reflex
A
27

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);
Antoine answered 18/8, 2008 at 8:27 Comment(2)
You have to LINEARIZE the sRGB values before you can apply the coefficients, and then re-apply gamma.Crape
The quick, down and dirty way to make this work for typical sRGB images is to convert the 8 bit R G B to float 0-1, and then raise to the exp of 2.2, and THEN apply these coefficients: ` Y = pow(color.r, 2.2) * 0.2126 + pow(color.g, 2.2) * 0.7152 + pow(color.b, 2.2) * 0.0722; `Crape
P
6

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

Pusher answered 18/8, 2008 at 8:40 Comment(2)
I think these are both wrong for this application as they apply to gamma-corrected RGB values.Hornswoggle
These are actually the sameLizalizabeth
C
5

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

Clericals answered 18/8, 2008 at 19:38 Comment(0)
J
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.

Jugular answered 25/8, 2008 at 8:30 Comment(1)
The best answer here -> adding the formulas you mentioned would have been more eye catching. I use Simple (R+G+B) / 3 .... does the trick and its reasonably fast (especially when working on emb systems).Unbroken
E
1

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.

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.

Eventful answered 10/7, 2018 at 9:50 Comment(0)
C
1

Just to point out in the self-selected answer, you have to LINEARIZE the sRGB values before you can apply the coefficients. This means removing the transfer curve.

To remove the power curve, divide the 8 bit R G and B channels by 255.0, then either use the sRGB piecewise transform, which is recommended for image procesing, OR you can cheat and raise each channel to the power of 2.2.

Only after linearizing can you apply the coefficients shown, (which also are not exactly correct in the selected answer).

The standard is 0.2126 0.7152 and 0.0722. Multiply each channel by its coefficient and sum them together for Y, the luminance. Then re-apply the gamma to Y and multiply by 255, then copy to all three channels, and boom you have a greyscale (monochrome) image.

Here it is all at once in one simple line:

// Andy's Easy Greyscale in one line.
// Send it sR sG sB channels as 8 bit ints, and
// it returns three channels sRgrey sGgrey sBgrey
// as 8 bit ints that display glorious grey.


  sRgrey = sGgrey = sBgrey = Math.min(Math.pow((Math.pow(sR/255.0,2.2)*0.2126+Math.pow(sG/255.0,2.2)*0.7152+Math.pow(sB/255.0,2.2)*0.0722),0.454545)*255),255);

And that's it. Unless you have to parse hex strings....

Crape answered 15/11, 2021 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.