Programming

Three algorithms for converting color to grayscale

steloflute 2013. 12. 2. 23:30

http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/

 

 

Three algorithms for converting color to grayscale

How do you convert a color image to grayscale? If each color pixel is described by a triple (R, G, B) of intensities for red, green, and blue, how do you map that to a single number giving a grayscale value? The GIMP image software has three algorithms.

The lightness method averages the most prominent and least prominent colors: (max(R, G, B) + min(R, G, B)) / 2.

The average method simply averages the values: (R + G + B) / 3.

The luminosity method is a more sophisticated version of the average method. It also averages the values, but it forms a weighted average to account for human perception. We’re more sensitive to green than other colors, so green is weighted most heavily. The formula for luminosity is 0.21 R + 0.71 G + 0.07 B.

The example sunflower images below come from the GIMP documentation.

Original image color photo of sunflower
Lightness sunflower converted to grayscale using lightness algorithm
Average sunflower converted to grayscale using average algorithm
Luminosity sunflower converted to grayscale using luminosity algorithm

The lightness method tends to reduce contrast. The luminosity method works best overall and is the default method used if you ask GIMP to change an image from RGB to grayscale from the Image -> Mode menu. However, some images look better using one of the other algorithms. And sometimes the three methods produce very similar results.