views:

1729

answers:

5

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

+4  A: 

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);
Ashwin
+2  A: 

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

Mark Cidade
+4  A: 

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

Carl Russmann
+2  A: 

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.

palm3D
+1  A: 

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.

Henrik Paul