Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What grayscale conversion algorithm does OpenCV cvtColor() use?

Tags:

When converting an image in OpenCV from color to grayscale, what conversion algorithm is used? I tried to look this up in the source code on GitHub, but I did not have any success.

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.72 G + 0.07 B. 

Here is an example of some conversion algorithms: http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/

like image 957
Steve Avatar asked Oct 04 '13 12:10

Steve


1 Answers

The color to grayscale algorithm is stated in the cvtColor() documentation. (search for RGB2GRAY). The formula used is the same as for CCIR 601:

Y = 0.299 R + 0.587 G + 0.114 B

The luminosity formula you gave is for ITU-R Recommendation BT. 709. If you want that you can specify CV_RGB2XYZ (e.g.) in the third parameter to cvtColor() then extract the Y channel.

You can get OpenCV to to do the "lightness" method you described by doing a CV_RGB2HLS conversion then extract the L channel. I don't think that OpenCV has a conversion for the "average" method, but if you explore the documentation you will see that there are a few other possibilities.

like image 90
Bull Avatar answered Dec 08 '22 17:12

Bull