image: algorithm to convert to grayscale

191 views
Skip to first unread message

Rodolfo Carvalho

unread,
Sep 28, 2016, 6:21:24 PM9/28/16
to golang-nuts
Hello,

I'm not an image processing expert, but was trying to write a small program to convert images to grayscale, just for the fun of it [1].

First I managed to get something working with image/draw, then I discovered about github.com/disintegration/gift through an old post here.

I realized the output images had a different luminance, what led me to dig a bit deeper to see how the implementations differed.


image/color [2]:

    y := (299*r + 587*g + 114*b + 500) / 1000


gift [3]:

    y := 0.299*px.R + 0.587*px.G + 0.114*px.B


The initially funny numbers, weights, match those described in Wikipedia [4].
I went ahead and compared to what Matlab [5] and Octave [6] do and found a similar formula.


I did not understand why image/color adds an extra 0.5 (500/1000) to y.
Could anybody give me a clue?






Thanks,

Rodolfo Carvalho

Patrick Smith

unread,
Sep 28, 2016, 7:51:41 PM9/28/16
to Rodolfo Carvalho, golang-nuts
Looks like it's just rounding to the nearest integer.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Jones

unread,
Sep 28, 2016, 11:23:45 PM9/28/16
to Patrick Smith, Rodolfo Carvalho, golang-nuts

Yes, certainly this is round to nearest for the luminance computation, and that is a good thing.

 

As far as the equation itself, this is a color science topic with many types of answers—engineering answers that cite various specifications, scientific answers that study human perception, and others. The logic of it is that the typical human eye is much more sensitive to green light, then reds, and then blue least of all. Basically this is just 6x, 3x and 1x the sensitivity.

 

However, the color responses in the eye are spectral—across a range of frequencies—not centered at just the one particular red, green, and blue wavelength of a particular display device, which is why there are so many engineering answers—one overall best fit per color triad for the original NTSC phosphor colors, for CRT computer monitors, etc.

 

That said, NONE OF THEM ARE VERY GOOD for common purposes.

 

Here is a survey:

http://cseweb.ucsd.edu/~ckanan/publications/Kanan_Cottrell_PloS_ONE_2012.pdf

 

And interesting approaches…

http://www.cs.northwestern.edu/~ago820/color2gray/color2gray.pdf

https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-649.pdf

What I tend to use is the perceptually pleasing, unofficial, non-standard, clever solution by Darel Rex Finley

y  :=  math.Sqrt(0 .299*r*r + 0.587*g*g + 0.114*b*b)

 

You can see how well it works by looking at the child and building images on his web page. See how long it takes you to see what’s happening in the bottom row… 

 

Michael

 

 

--

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

--

You received this message because you are subscribed to the Google Groups "golang-nuts" group.

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Micky

unread,
Sep 29, 2016, 12:13:35 AM9/29/16
to Rodolfo Carvalho, golang-nuts
On Thu, Sep 29, 2016 at 3:19 AM, Rodolfo Carvalho <rhcar...@gmail.com> wrote:
gift [3]:
    y := 0.299*px.R + 0.587*px.G + 0.114*px.B

I did not understand why image/color adds an extra 0.5 (500/1000) to y.
Could anybody give me a clue?

To directly answer your question:

GIFT also adds 0.5 to round off.

Wanna know where? Try to make up a call stack:

gift.Grayscale() > returns Filter > is colorFilter > has a callback fn > returns pixel > contains float32s

pixel is a struct containing float32s
Filter is an interface, made of Draw() and Bounds()
colorFilter implements Filter

So, it eventually boils down to: [1]

*colorFilter.Draw() > parallelize() > newPixelSetter().SetPixel() > f32u16()

color.Color uses uint32s while gift.pixel uses float32s. I guess gift uses floats for cosmetic reasons. But they both round off while applying the grayscale filter.


Rodolfo Carvalho

unread,
Sep 29, 2016, 5:44:47 AM9/29/16
to golang-nuts
Thanks everyone for the answers and links.

I computed a pixel-to-pixel difference and indeed what I was getting was -1/+1 differences in the gray values, due to rounding.

Next free time I'll try the other formulas.


Thank you again,

Rodolfo Carvalho
Reply all
Reply to author
Forward
0 new messages