Helpful Posts Helpful Posts:  0
Results 1 to 19 of 19

Thread: Color Math

  1. #1

    Join Date
    Feb 2011
    Location
    Italy
    Posts
    18
    Real Name
    Jenner

    Color Math

    Hi all,
    I'm here again asking a strange question. :P

    I would like to know the formulas that convert the R, G and B values to Luminance... what's the math behind that?
    I would like to better understand what is represented in the histogram in terms of contributions given by the three components.

    Pointing me to an online document that explains this math is also welcome.

    Thanks.
    Jenner

    P.S.: I hope this is not off-topic here.

  2. #2
    tbob's Avatar
    Join Date
    Jan 2011
    Location
    Dawson Creek British Columbia Canada
    Posts
    2,193
    Real Name
    Trevor Reeves

    Re: Color Math

    Your best bet here is to get a program called 123 di by Vincent Bockaert. Reasonably inexpensive and covers the physics and math quite well.

  3. #3
    Moderator Dave Humphries's Avatar
    Join Date
    May 2008
    Location
    Windsor, Berks, UK
    Posts
    16,295
    Real Name
    Dave Humphries :)

    Re: Color Math

    Quote Originally Posted by ntx View Post
    Hi all,
    I'm here again asking a strange question. :P

    I would like to know the formulas that convert the R, G and B values to Luminance... what's the math behind that?
    I would like to better understand what is represented in the histogram in terms of contributions given by the three components.
    Hi Jenner,

    In the TV industry, the accepted ratios of 'contribution' to the luminance from the three colour tubes/sensors were 0.3R, 0.59G and 0.11B, or put another way 30% Red + 59% Green and 11% Blue.

    Is that what you were asking?

  4. #4

    Join Date
    Feb 2011
    Location
    Italy
    Posts
    18
    Real Name
    Jenner

    Re: Color Math

    Quote Originally Posted by Dave Humphries View Post
    In the TV industry, the accepted ratios of 'contribution' to the luminance from the three colour tubes/sensors were 0.3R, 0.59G and 0.11B, or put another way 30% Red + 59% Green and 11% Blue.

    Is that what you were asking?
    Sort of... so that means pixels with a great intensity in the Blue (near clipping for example) will produce a very little "shift" of the peaks in the luminance histogram in my camera?

    Bye
    Jenner

  5. #5
    arith's Avatar
    Join Date
    Sep 2009
    Location
    Burton on Trent, UK
    Posts
    4,789
    Real Name
    Steve

    Re: Color Math

    I've never considered this question so I looked in Paint Shop, and fiddled around a bit. V might stand for Value, so moving this from 0 to 100 makes me suspect, luminence is a scalar multiplier when thinking of (r,g,b) as a vector.

    At greatest luminence at least one of the ordered triple is 255, and x/100(r,g,b) gives the required luminence. x belongs to [0,100].

    I might be wrong since I suffered a brain injury causing just this sort of thing to be difficult for me; and looking at saturation is a bit too hard for me at the moment.

    But observe at least one of (r,g,b) is fixed at 255, they all reach 255 at x=0, at x=100 at least one of the values is 0 (unless it is white), and the other one increases at a linear rate, apparently.

    Suppose we have a colour (0,255,46), when x=100, at x=50 we have (127,255,150) where the third entry is 0.5*255+0.5*46, looks like (100-x)/100*255 + x/100*(value of a coordinate which is less than 255) where at least one of the coordinates is zero at 100% saturation=x.

    doesn't work for black
    Last edited by arith; 16th March 2011 at 05:33 PM.

  6. #6

    Join Date
    Feb 2011
    Location
    Cheshire, UK
    Posts
    45
    Real Name
    Paul

    Re: Color Math

    As Dave says, luminance is a weighted sum of the RGB values. The ratios depend on which colour space you are using (for sRGB luminance is 22%R + 72%G + 7.2%B, for AdobeRGB it is 30%R + 63%G + 7.5%B). Whether the camera actually uses these ratios or not to calculate the luminance histogram is another matter.
    The histogram displayed by the camera shows you the values from the JPEG preview image and not the actual raw values - so the contrast, saturation and white balance settings also influence the result.

  7. #7
    arith's Avatar
    Join Date
    Sep 2009
    Location
    Burton on Trent, UK
    Posts
    4,789
    Real Name
    Steve

    Re: Color Math

    Quote Originally Posted by PaulMiller View Post
    As Dave says, luminance is a weighted sum of the RGB values. The ratios depend on which colour space you are using (for sRGB luminance is 22%R + 72%G + 7.2%B, for AdobeRGB it is 30%R + 63%G + 7.5%B). Whether the camera actually uses these ratios or not to calculate the luminance histogram is another matter.
    The histogram displayed by the camera shows you the values from the JPEG preview image and not the actual raw values - so the contrast, saturation and white balance settings also influence the result.
    I do not think that is luminance; because then it would be fixed for every photo. I think that is demosaicing.

  8. #8
    Peter Ryan's Avatar
    Join Date
    Aug 2009
    Location
    Melbourne, Australia
    Posts
    1,971
    Real Name
    Peter

    Re: Color Math

    Interesting question Jenner. As soon as you said 'maths' I was lost

    CiC has come through again. Thanks for the input guys - wouldn't it be a terrible day if you woke up and didn't learn something....

  9. #9
    Sonic4Spuds's Avatar
    Join Date
    Nov 2010
    Location
    Superior Wisconsin, USA
    Posts
    252
    Real Name
    Will

    Re: Color Math

    Steve,

    It is an accepted fact that green in human sight contributes the most to what we observe as the luminance. The values that Dave and Paul spit out are used in the formulas for calculating luminance. The real question is weather the camera is calculating a real luminance or just gamma corrected number values, and that is what I can't tell you.

    -Sonic

  10. #10

    Join Date
    Feb 2011
    Location
    Cheshire, UK
    Posts
    45
    Real Name
    Paul

    Re: Color Math

    Jenner,
    I missed your reply to Dave while I was writing my first answer (too busy going off looking for colour transform matrices for different colour spaces ).
    If you are looking at a single histogram on your camera, then it is likely to be an RGB histogram. With an RGB histogram, the graph shows the distribution of all the numbers in the image, ignoring which channel they come from - so a peak at the right end tells you that something is clipping, but not which channel. Each channel contributes equally to the result. There is a CiC tutorial that explains this.
    Also, remember that the camera histogram shows the JPEG preview image, so clipping in the histogram doesn't necessarily mean that the channels in the raw file are clipped.

    Steve,
    What I was trying to explain is something like the Channel Mixer in Photoshop - you generate a B&W (luminance) image by mixing the R,G,B channels in different proportions.

    - Paul

  11. #11
    arith's Avatar
    Join Date
    Sep 2009
    Location
    Burton on Trent, UK
    Posts
    4,789
    Real Name
    Steve

    Re: Color Math

    ok I misread the question; if you compare the green histogram with the luminence you should detect a similarity.


    Color Math

    Color Math
    Last edited by arith; 17th March 2011 at 11:27 AM.

  12. #12

    Join Date
    Feb 2011
    Location
    Italy
    Posts
    18
    Real Name
    Jenner

    Re: Color Math

    @PaulMiller: So that means the camera histogram isn't based on the Value nor Luminosity... correct? Thank for the link to the CiC tutorial: very useful!
    According to the tutorial, every channel affects the histogram for only a third of it's real value: if you have a peak in the blue but not in red and green, it will be "scaled" by one third, is this right?

    Back to my original question: cause the histogram miss where the values come from, I suppose there's no way to say how to maximize the "editability" of my pixel. With "editability" I mean the amound of useful data I can count on when I go in post-production.
    The problem raised when I noted that even by exposing to the right, I don't always end up with good editable pixels. So I asked for the color math to calculate which values (luminance... but as we see it's not luminance) can give the maximum amount of editable data (for extension: the maximum available dynamics).

    I have to say that the more I go in depth in those concepts the less camera features seems designed for (digital-)photographers! ;D

    Thank you all for your replies!

    Bye
    Jenner

  13. #13

    Join Date
    Feb 2011
    Location
    Italy
    Posts
    18
    Real Name
    Jenner

    Re: Color Math

    After reading the CiC tutorial I made some test... which never get what expected! :P
    In GIMP I set a test color: 10,10,250 (R,G,B).
    Using the formula to calculate Luminance ( L = R * 0.30 + G * 0.59 + B * 0.11) I got 36.4 as a result.
    If I convert the test color to grayscale using "Luminance" method I get a 27,27,27 gray (I expect a 36,36,36 or a 37,37,37).
    I find also hard to calculate where my test color should be placed in the histogram: in GIMP, reading the Value histogram, my test color is placed at 250. Does this mean that, at least in GIMP histogram, it places the colors in the histogram by using the higher component value? I noted that converting the color from RGB to HSV I get coherent result (V channel in plain 250,250,250 gray).

    Did I made some mistake? I so what's wrong?

    Thanks
    Jenner

  14. #14

    Join Date
    Feb 2011
    Location
    Cheshire, UK
    Posts
    45
    Real Name
    Paul

    Re: Color Math

    The GIMP is open source, so after a bit of poking around in the code...

    Quote Originally Posted by ntx View Post
    In GIMP I set a test color: 10,10,250 (R,G,B).
    Using the formula to calculate Luminance ( L = R * 0.30 + G * 0.59 + B * 0.11) I got 36.4 as a result.
    If I convert the test color to grayscale using "Luminance" method I get a 27,27,27 gray (I expect a 36,36,36 or a 37,37,37).
    The GIMP uses the newer BT.709 luminance formula (as used in HDTV and sRGB) but without gamma correction, so:

    grey = 0.2126 * r + 0.7152 * g + 0.0722 * b

    which gives 27 for your RGB values.

    Quote Originally Posted by ntx View Post
    I find also hard to calculate where my test color should be placed in the histogram: in GIMP, reading the Value histogram, my test color is placed at 250. Does this mean that, at least in GIMP histogram, it places the colors in the histogram by using the higher component value? I noted that converting the color from RGB to HSV I get coherent result (V channel in plain 250,250,250 gray).
    The Value histogram in the GIMP is the V from HSV, which is the maximum of RGB, as you have observed.

  15. #15

    Join Date
    Feb 2011
    Location
    Italy
    Posts
    18
    Real Name
    Jenner

    Re: Color Math

    @PaulMiller: Great! Thank you Paul! Now I have to find what's displayed in the histogram of my camera (Pentax K200D)... is it just the sum of the three channel histograms? If so, a pixel of my test color would raise the count by one in the 250 column and by 2 in the 10 column, is it right?

    Thank you for all the stuff I'm learning!

    Bye
    Jenner

  16. #16

    Join Date
    Feb 2011
    Location
    Cheshire, UK
    Posts
    45
    Real Name
    Paul

    Re: Color Math

    Quote Originally Posted by ntx View Post
    Now I have to find what's displayed in the histogram of my camera (Pentax K200D)... is it just the sum of the three channel histograms? If so, a pixel of my test color would raise the count by one in the 250 column and by 2 in the 10 column, is it right?
    If I'm right about the camera histogram being an RGB one, then yes.

    I've just checked the manual for the K200D online, and you can display separate R,G,B histograms for each colour channel (page 26) - this is much more useful if you are looking for clipping.

  17. #17

    Join Date
    Feb 2011
    Location
    Italy
    Posts
    18
    Real Name
    Jenner

    Re: Color Math

    Quote Originally Posted by PaulMiller View Post
    If I'm right about the camera histogram being an RGB one, then yes.

    I've just checked the manual for the K200D online, and you can display separate R,G,B histograms for each colour channel (page 26) - this is much more useful if you are looking for clipping.
    Yes, I always use the separated histograms but I want to understand what's showed in the main histogram. The manual say it shows "brightness" which stands for... what? Some say "brightness" it's just a kind of luminance calculation (I never figured there was so many kind of luminance out there).

    Bye
    Jenner

  18. #18
    Moderator Dave Humphries's Avatar
    Join Date
    May 2008
    Location
    Windsor, Berks, UK
    Posts
    16,295
    Real Name
    Dave Humphries :)

    Re: Color Math

    Quote Originally Posted by ntx View Post
    Yes, I always use the separated histograms but I want to understand what's showed in the main histogram. The manual say it shows "brightness" which stands for... what? Some say "brightness" it's just a kind of luminance calculation (I never figured there was so many kind of luminance out there).

    Bye
    Jenner
    Hi Jenner,

    Try this;
    (I did it indoors under (a compact fluorescent lit) scene with about 50% of scene being a red plastic bag, some white paint)
    Set WB to Tungsten, take a picture of a scene.
    Set WB to cloudy, take another identically framed picture.
    Review Lum, R, G, B histograms for each.
    See how the peaks on the Luma one vary between shots?
    The scene didn't change, the light didn't change, but ....

    Must dash,

  19. #19

    Join Date
    Feb 2011
    Location
    Cheshire, UK
    Posts
    45
    Real Name
    Paul

    Re: Color Math

    Jenner,

    Dave's experiment is a good idea. I would try something that is evenly lit (to try to keep the histogram as peaky as possible), blue and fills the frame as much as possible - the reason being that the blue channel has the smallest weight in Luma based histograms and the difference between that and RGB should be more obvious.

    Or, if you can persuade the camera to display histograms for JPEG images you have put on the memory card you could repeat your GIMP experiments on the camera. ( why do open source projects have to have 'amusing' names?)

    Or, you could try Pentax customer support - they may be able to shed some light on what the manual means by "Brightness".

    - Paul

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •