 # How to calculate/measure the color temperature of an image

Given an image, is there a way I can calculate the temperature in Kelvin?
It can be the average of the temperature from all the color shown in the image.

By color temperature I mean Correlated Color Temperature (CCT). It is also described here.

Hi,

Using the formula on the page you referenced, you can use this:

``````
/*Macro to get CCT from RGB image according to https://dsp.stackexchange.com/questions/8949/how-to-calculate-the-color-temperature-tint-of-the-colors-in-an-image/8968#8968
Beware!  This is an approximation: pure blue and red images do not give appropriate values.
*/

Title=getTitle();
run("Duplicate...", "yes");
rename("Colour");

run("Split Channels");

selectWindow("Colour (blue)");
getStatistics(area, mean, min, max, std, histogram);
BlueMean=mean;
close("Colour (blue)");

selectWindow("Colour (green)");
getStatistics(area, mean, min, max, std, histogram);
GreenMean=mean;
close("Colour (green)");

selectWindow("Colour (red)");
getStatistics(area, mean, min, max, std, histogram);
RedMean=mean;
close("Colour (red)");

//Convert RGB to CIE tristimulus values

X=(-0.14282*RedMean)+(1.54924*GreenMean)+(-0.95641*BlueMean);
Y=(-0.32466*RedMean)+(1.57837*GreenMean)+(-0.73191*BlueMean);
Z=(-0.68202*RedMean)+(0.77073*GreenMean)+(0.56332*BlueMean);

//Calculate Normalized Chromacity Values
Normx=X/(X+Y+Z);
Normy=Y/(X+Y+Z);

//compute CCT values
n=(Normx-0.3320)/(0.1858-Normy);
CCT=449*(Math.pow(n,3))+3525*(Math.pow(n,2))+6823.3*n+5520.33;

print("The average RGB values of "+Title+" (R:"+RedMean+"; G:"+ GreenMean+"; B"+ BlueMean+") give CCT value of "+CCT);

``````

The formula may need changing as on that very page, Tamas indicates that pure red and pure blue values give weird results.

Sincerely,

Matthieu

1 Like