Detailed Calculation of RGB to Graylevel by Mathem

2019-07-07 04:50发布

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?

2条回答
贼婆χ
2楼-- · 2019-07-07 05:21

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

查看更多
男人必须洒脱
3楼-- · 2019-07-07 05:35

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]
查看更多
登录 后发表回答