2

I am doing a bit of research for a small project I intend to make and that included determining the brightness of a pixel. First idea that came to my mind was to just compute brightness = (R + G + B)/3 but then I stumbled upon this answer which had very different and specific equations. Why wouldn't averaging RGB values work?

Eyad
  • 31
  • 3

1 Answers1

5

Our eyes are more sensitive to green light, so 255 in the green channel leads to a larger perception of brightness than 255 in the red or blue channel.

The International Commission on Illumination (CIE) created two standard color spaces in 1931 (CIE 1931 XYZ and CIE 1931 RGB color space), derived from perceptual experiments performed during the 1920's. These experiments and color spaces are the basis of all colorimetry today*.

In the CIE 1931 RGB color space, the luminance equation Y, is defined as:

Y = ( 0.17697 R + 0.81240 G + 0.01063 B ) / 0.17697

However, the definition of Y from RGB depends on the primaries used for R, G and B (e.g. the three pure colors emitted by an LCD monitor) and the color of the white produced by their addition. For example, for a standard D65 white point and the primaries from Rec. 709 and sRGB, the conversion from RGB to Y is as follows:

Y = 0.2126 R + 0.7152 G + 0.0722 B

This equation weights green about ten times more strongly than blue. This matches what is known about color perception, and explains for example why, on a computer screen, green text on a black background is quite OK to read, whereas blue text on the same background is much harder to read.

If you want to learn more about different color spaces, and how to convert between them, you should read Poynton's Color FAQ.


*A lot has been learned about color perception since, and new color spaces such as CIELAB and CIELUV have been published, but CIE XYZ is still the foundation for them.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • So if I am writing a program and I take the average of RGB values as an indicator of brightness, that's correct? What I understood is that these values determine brightness to our eyes, not to the computer. – Eyad Nov 10 '18 at 02:27
  • The entire concept of brightness exists in humans, not in computers. It is a perceptual quality. If you want to measure how a computer perceives something, you'll need to do the work to measure it yourself and figure out how it's perceived by the computer. But if you're doing anything that's going to translate the RGB into a brightness as determined by a human, you'll want to use the formulas you found. I can tell you that professional photo and video editing apps use formulas like those in the linked question. – user1118321 Nov 10 '18 at 02:53
  • The idea I have is that my application will determine the brightness of a pixel and replace it with a character. I'll have pre-set ranges of brightness that map differently to characters, so no human perception of the picture or the brightness is actually going to happen. It's all inside the computer and it just outputs a .txt file after it's done with all the characters replacing each pixel depending on that pixels "brightness". – Eyad Nov 10 '18 at 02:56
  • 1
    @Eyad be aware that calculating the brightness of a character requires gamma correction - a checkerboard of 50% white 50% black looks like (186,186,186) not (128,128,128). If it's not obvious that green (0,255,0) is lighter than blue (0,0,255) then maybe it's better to compare yellow (255,255,0) to magenta (255,0,255). – Mark Ransom Nov 10 '18 at 03:51
  • 1
    I would argue that you are dealing with human perception. You're trying to convert a human-perceived brightness into a character that has the same brightness. You want the text file to look (i.e. be humanly perceived as) roughly the same brightness in the corresponding areas as the image. You should absolutely use one of the formulas mentioned here for that. – user1118321 Nov 10 '18 at 04:33
  • @KelSolaar: Thanks for your edits. I have undone some of them I don't agree with. For example, conversion of sRGB to XYZ requires a gamma correction, as sRGB is a non-linear encoding of RGB. I'd rather leave sRGB out of this answer altogether, it's a complication that doesn't address OP's question. – Cris Luengo Nov 12 '18 at 15:58
  • @CrisLuengo: This is incorrect, conversion to sRGB does not necessarily require applying the sRGB OETF, all the RGB colourspaces are intrinsically linear, as a matter of fact, CIE RGB does not specify an OETF or an EOTF. sRGB is not a "non-linear encoding of RGB", it is one of the dozens of possible RGB colourspaces that you specify by their primaries, whitepoint and CCTFs. Whether you use non-linearly encoded data is totally dependent on your application and where you are in the color management chain. We have been storing linear sRGB data for over 15 years in the VFX industry for example. – Kel Solaar Nov 12 '18 at 18:44
  • I would suggest you do some reading of our blog post here: https://www.colour-science.org/posts/the-importance-of-terminology-and-srgb-uncertainty/ – Kel Solaar Nov 12 '18 at 18:44
  • @KelSolaar: Thanks for the link. It says "When one talks about sRGB alone, it should be safe to assume that he is referring to the sRGB colourspace as per IEC 61966-2-1:1999 Standard, not just cherry picked components." [The standard abstract](https://webstore.iec.ch/preview/info_iec61966-2-1%7Bed1.0%7Db.pdf) says "The three major factors of this RGB space are the colorimetric RGB definition, the simple exponent value of 2,2, and the well-defined viewing condition [...]". I think if you use a linear version of sRGB, you are cherry-picking components. :) – Cris Luengo Nov 12 '18 at 19:00
  • My point was that you said "requires", this is what I'm saying is incorrect especially when after you say "as sRGB is a non-linear encoding of RGB." which is confusing at best. RGB is technically a colour model and sRGB is one of the many colour spaces of that colour model. Saying what you say implies that there is only a single one and not many of them. – Kel Solaar Nov 12 '18 at 19:17
  • @KelSolaar: I see you point. Thank you for clarifying it. I have edited the sentence in question. Does that sound better to you? – Cris Luengo Nov 12 '18 at 20:00