Detailed Calculation of RGB to Graylevel by Mathematica

青春壹個敷衍的年華 提交于 2019-12-10 19:59:01

问题


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?


回答1:


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




回答2:


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]


来源:https://stackoverflow.com/questions/27812393/detailed-calculation-of-rgb-to-graylevel-by-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!