Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image/color: are the coefficients for grayModel correct? #16251

Closed
lgg opened this issue Jul 2, 2016 · 8 comments
Closed

image/color: are the coefficients for grayModel correct? #16251

lgg opened this issue Jul 2, 2016 · 8 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@lgg
Copy link

lgg commented Jul 2, 2016

https://github.com/golang/go/blob/master/src/image/color/color.go#L249

For now there are coefficients for NTSC, not sRGB.

See sRGB table: http://www.brucelindbloom.com/index.html?WorkingSpaceInfo.html
Or point 9 here: http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html

@bradfitz bradfitz changed the title Are you sure that coefficients for grayModel() in color.go are right? image/color: are the coefficients for grayModel correct? Jul 2, 2016
@lgg
Copy link
Author

lgg commented Jul 3, 2016

The formula using to calculate relative luminance (or brightness):
Y = 0.299 R + 0.587 G + 0.114

But it is correct in NTSC (1953) color space only. This formula is deprecated and legacy.
Actual color space sRGB significantly different: chromacity diagram.

Correct formula is:
Y = 0.2126 R + 0.7152 G + 0.0722 B

The proof of numbers
Source — Bruce Lindbloom site
See also — Charles Poynton Color FAQ

A lot of work or refactoring is not required. We can simply replace this coefficients.

@nigeltao nigeltao added this to the Go1.8 milestone Jul 7, 2016
@nigeltao
Copy link
Contributor

nigeltao commented Jul 7, 2016

We're currently in code freeze for the upcoming Go 1.7 release. I'll take a look at it for Go 1.8.

@bradfitz
Copy link
Contributor

Ping @nigeltao.

@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 7, 2016
@rsc
Copy link
Contributor

rsc commented Oct 19, 2016

ping @nigeltao, @robpike

@robpike
Copy link
Contributor

robpike commented Oct 19, 2016

It's fine with me to change to sRGB normalization but I'm sure it will break something.

@nigeltao
Copy link
Contributor

Note that the NTSC formula is also used by https://github.com/golang/go/blob/master/src/image/color/ycbcr.go#L10 based on the JFIF specification.

Changing the grayModel function to use the sRGB coefficients instead of the NTSC coefficients would break the nice property that, in theory, converting an image.RGBA to an image.YCbCr and then taking only the Y channel is equivalent to converting an image.RGBA directly to an image.Gray. In practice, the two use slightly different formulae (the ycbcr.go one uses a shift instead of a divide), but we could harmonize them.

We could conceivably change the ycbcr.go formulae to do something compatible with the sRGB coefficients, but that has larger knock-on effects, and we are then also disregarding the JFIF spec.

I appreciate that there are arguments for sRGB over NTSC, but I think this is a situation where everything has trade-offs, and there being multiple competing standards, instead of there being a single 'correct' answer. I'm happy with the trade-off that the image/color package makes.

You are of course free to write your own function that takes an image.RGBA and produces an image.Gray based on the sRGB coefficients.

Sorry for the delay in replying.

@gopherbot
Copy link

CL https://golang.org/cl/31538 mentions this issue.

gopherbot pushed a commit that referenced this issue Oct 20, 2016
This makes grayModel and gray16Model in color.go use the exact same
formula as RGBToYCbCr in ycbcr.go. They were the same formula in theory,
but in practice the color.go versions used a divide by 1000 and the
ycbcr.go versions used a (presumably faster) shift by 16.

This implies the nice property that converting an image.RGBA to an
image.YCbCr and then taking only the Y channel is equivalent to
converting an image.RGBA directly to an image.Gray.

The difference between the two formulae is non-zero, but small:
https://play.golang.org/p/qG7oe-eqHI

Updates #16251

Change-Id: I288ecb957fd6eceb9626410bd1a8084d2e4f8198
Reviewed-on: https://go-review.googlesource.com/31538
Reviewed-by: Rob Pike <r@golang.org>
@nigeltao
Copy link
Contributor

That commit harmonized the two NTSC implementations. As for replacing NTSC with sRGB coefficients, as I said, I'm not convinced that we should do that.

@golang golang locked and limited conversation to collaborators Oct 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants