Suppose you have a color image:
and you want to convert it to a grayscale image.
At first, you might think you you can simply add up the red, green, and blue color values, and divide by 3:
Gray = (Red + Green + Blue)/3
If you do that, this is what you’ll get:
That doesn’t look very good. The yellow and green colors in particular are obviously too dark.
So, perhaps you decide to search the internet for grayscale conversion formulas, and the one you see a lot is:
Gray = 0.299×Red + 0.587×Green + 0.114×Blue
Using that formula, this is what you’ll get:
That’s clearly an improvement. Though if you look closely, the bright green, and perhaps some of the other colors, seem to be a little too dark.
The problem is that the popular formula given above is incorrect. The coefficients might be optimal in a certain sense, but there is no set of coefficients that that will give correct results. For correct results, you need a more complicated formula: first convert to a linear colorspace, then use different weights:
Gray = 0.2126×Red + 0.7152×Green + 0.0722×Bluethen convert back to your display’s colorspace. The result:
What does this mean in practice? It means that, in most cases, a good approximation of the correct formula is:
Gray = (0.2126×Red2.2 + 0.7152×Green2.2 + 0.0722×Blue2.2)1/2.2
As usual, there’s a performance penalty for doing it the right way. Taking numbers to powers like this is much slower than simple multiplication and addition. If your convert-to-grayscale algorithm needs to run as quickly as possible, while still giving good results, you may have to use techniques such as lookup tables to improve the speed.
Note that while this formula produces arguably the best possible results from an objective technical perspective, that doesn’t mean its results always look the best subjectively. Other formulas have been devised that may give better-looking results for specific images.