3

I tried

ColorConvert[img, "Grayscale"]

to convert the RGB to Graylevel. I am wondering the detailed calculation by mathematica..

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

or something else?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Cici
  • 65
  • 6
  • related , tho not mathematica specific. http://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity – agentp Jan 07 '15 at 12:30

2 Answers2

2

We can obtain the exact values used by mathematica by making up a 3 pixel image with pure red,green,blue and converting it:

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

{0.299, 0.587, 0.114}

Note these are the "Rec. 601 luna coefficients" per http://en.wikipedia.org/wiki/Luma_%28video%29

Test this on a real image:

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

True

agentp
  • 6,849
  • 2
  • 19
  • 37
1

See How can I convert colors to grayscale? for insight into how the calculation might be being done.

The two images produced by the commands below are a close match, but you could figure out the exact scaling vector with a short program making multiple comparisons. A further test on a different image would ascertain whether ColorConvert uses the same vector all the time, or whether the image is analysed before conversion for an optimal grayscale appearance.

ColorConvert[img, "GrayScale"]

ImageApply[{0.35, 0.45, 0.2}.# &, img]
Community
  • 1
  • 1
Chris Degnen
  • 8,443
  • 2
  • 23
  • 40