18

How can I calculate the (black body equivalent) color temperature of the light source illuminating a given image? Below are screenshots of Adobe Lightroom manipulating color temperature and a shift in the RGB Histograms. Given RGB components of the image, how do I go about calculating it? I should expect a single value - The black body equivalent temperature of the illumination source, right?

4600K 23810K

Royi
  • 19,608
  • 4
  • 197
  • 238
Lord Loh.
  • 491
  • 1
  • 4
  • 18
  • I'm not sure that makes sense. Color temperature is a property of light, like the lamps used to illuminate a scene while the image is being taken. I don't know if images themselves have a color temperature. Like, what's the color temperature of a plain green image? Green isn't even in the blackbody spectrum. Was it produced by a dark green object illuminated by a high temperature source, or a light green object illuminated by a low temperature source? – endolith May 02 '13 at 15:28
  • The last part of my question - " I should expect a single value - The black body equivalent temperature of the illumination source, right?" Have I been unclear? If so, please feel free to suggest edits. If you have a green surface, it shall look different depending on the illumination source. I am trying to see the green and compute the (black body equivalent) temperature of the illumination source - How do Photoshop and Light Room do it?. Even a plain green object shall not appear as - #00FF00 (rgb hex). – Lord Loh. May 02 '13 at 18:32
  • @endolith - I made some changes. Do you think the question is clearer now? – Lord Loh. May 02 '13 at 18:38
  • 1
    Ok, that makes more sense, but I'm still not sure it's possible. Can't two shades of green illuminated by different color temperature sources produce identical output images? So how can you tell the illuminant's color temperature without knowing the properties of the object and the camera? – endolith May 02 '13 at 19:32
  • @endolith - Can it? I am not sure. I think it might. Cameras have built in White balance control. But most images have multiple shades and this seems to be analyzed with histograms - stochastically. So there might be a confidence interval of being at a particular temperature. Besides, cameras and pictures have some meta data too. I have a feeling that the equation for this is expert drive - someone (expert) calibrated it to give a number. Just like C or F - Who decided that freezing point of water shall be set to 0C or 32F? – Lord Loh. May 02 '13 at 19:45
  • My rep is too low to comment so I have to say I completely agree with Martin Kallmans comment re the issues with Sam Maloneys answer. No offense, Sam, hence I didn't vote you down. :-) The paper provided gives an example for the formula with the following inputs: - R=231, G=260, B=95 Umm - green of 260? This is, as Martin says, hardware specific and hence otherwise completely useless to the rest of us. Also, the paper doesn't mention which RGB colour-space it uses - mainly because it's read directly from a custom sensor. Moreover, take the supplied RGB -> XYZ formula and plug in a basic red of – RowanPD May 20 '17 at 06:52

1 Answers1

12

This paper (pdf download/updated link) gives the following formulae for calculating Correlated Color Temperature (CCT). They do not explicitly say (or I missed it), but their example leads me to infer that they are assuming RGB values in the range of 0-255.

1. Convert the RGB values to CIE tristimulus values (XYZ) as follows:

$$X = (-0.14282)(R) + (1.54924)(G) + (-0.95641)(B)$$ $$Y = (-0.32466)(R) + (1.57837)(G) + (-0.73191)(B) = Illuminance$$ $$Z = (-0.68202)(R) + (0.77073)(G) + (0.56332)(B)$$

2. Calculate the normalized chromaticity values:

$$x = X/(X+Y+Z)$$ $$y = Y/(X+Y+Z)$$

3. Compute the CCT value from:

$$CCT = 449n^3 + 3525n^2 + 6823.3n + 5520.33$$

$$\text{where }n = (x − 0.3320) / (0.1858 − y)$$

Which can be combined to form the following equation:

$$CCT = 449n^3 + 3525n^2 + 6823.3n + 5520.33$$ $$\text{where }n = ((0.23881)R+(0.25499)G+(-0.58291)B) / ((0.11109)R+(-0.85406)G+(0.52289)B)$$

I am not sure about applying this to an image, but if you just want a single, generalizing number, than you could perhaps use some sort of averaging? Either find an acceptable "average" RGB value for the image (eg. the centroid) and use that to calculate a temperature or (a much more computationally expensive option) calculate the temperature for each pixel in the image and take the average of those results.

Also, bear in mind that CCT is only an approximate metric for most colors, since only a single curve in the color space actually represents color which can be obtained from a real world black body radiator. Thus for all other colors, the calculated color temperature is simply an approximation of the black body temperature it most closely represents. Thus, for some colors (especially greens) it can actually be a somewhat meaningless value, at least in a physical sense. This is illustrated well in the following image (from the wikipedia article on color temperature).

The black line in the image represents the Planckian Locus of colors which could actually be produced by block body radiation. The smaller crossing lines represent the isotherms of the CCT approximation nearby.

Also, since your question specifically references Adobe Lightroom, I found this while searching around:

The sliders [in Adobe Lightroom] adjust not the black body temp of the light, but the compensation applied to the image to compensate for the black body temp of the light. This goes the other way round.

So bear in mind that the color temperature you see on the Lightroom slider will not be the same as those calculated from the above formulae.

lennon310
  • 3,590
  • 19
  • 24
  • 27
Sam Maloney
  • 810
  • 7
  • 25
  • Please be advised that the RGB-XYZ transform used above appears to be specific to the colour space used by a certain hardware. It is most certainly not correct for sRGB, which is what is most probably being used in OP's case. Refer to the correct transforms in the sRGB standard – awdz9nld Dec 26 '14 at 20:05
  • Note also that the Y component in CIE 1931 denotes luminance, as opposed to illuminance – awdz9nld Dec 26 '14 at 22:39
  • I updated the broken link; as @awdz9nld suspected, the paper is intended for "Calculating Color Temperature and Illuminance using the TAOS TCS3414CS Digital Color Sensor" which is a specialized (and outdated) proprietary camera component. – ashleedawg Aug 23 '21 at 04:04
  • I've calculated some colors according to your formulae, and as I see in some cases it works, while in some other cases - such as red (255 0 0) and blue (0 0 255) - it gives wrong answer: - white (255 255 255): n = 0.4049, CCT = 8890.77 K -> seems to be correct - yellow (255 255 0): n = -0.6646, CCT = 2410.65 K -> seems to be correct - green (0 255 0): n = -0.2986, CCT = 3785.42 K -> seems to be correct - cyan (0 255 255): n = 0.9902, CCT = 16168.7 K -> seems to be correct - magenta (255 0 255): n = -0.5428, CCT = 2783.54 K -> seems to be correct – Tamas Jan 10 '15 at 15:58
  • Continuation of @Tamas's comment: however:

    red (255 0 0): n = 2.1497, CCT = 40938.6 K -> seems to be wrong

    blue (0 0 255): n = -1.1148, CCT = 1672.45 K -> seems to be wrong

    – Peter K. Aug 23 '21 at 15:05