Detailed RGB calculation for Graylevel by Mathematica

I tried

ColorConvert[img, "Grayscale"]

      

to convert RGB to Grailevel . I am interested in a detailed calculation in mathematics ..

Gray level= square(R^2+G^2+B^2)?

      

or something else?

+3


source to share


2 answers


We can get the exact values ​​used by the math by creating a 3-pixel image with pure red, green, blue and transforming it:

 lvec = First@
         ImageData[
           ColorConvert[Image[{{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}}], 
             "GrayScale"]]

      

{0.299, 0.587, 0.114}

Note that these are "moon 601 rates." for http://en.wikipedia.org/wiki/Luma_%28video%29



Check it out on the real image:

 lena = ExampleData[{"TestImage", "Lena"}];
 lenag = ColorConvert[lena, "GrayScale"];
 ImageData@ImageApply[ lvec.# & , lena ] == ImageData@lenag

      

True

+2


source


See How to convert colors to grayscale? to understand how the calculation can be done.

The two images produced by the commands below are close, but you can find the exact scaling vector with a short program that does a few comparisons. Another test on another image will find out if ColorConvert

the same vector is used all the time, or if the image is analyzed for optimal grayscale display before being converted.



ColorConvert[img, "GrayScale"]

ImageApply[{0.35, 0.45, 0.2}.# &, img]

      

+1


source







All Articles