Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Color Temperature from Color Correction Gain

I am trying to find out the Color Temperature of a Photo captured by the Camera.

final CameraCaptureSession.CaptureCallback previewSSession = new CameraCaptureSession.CaptureCallback() {
    @Override
    public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
        super.onCaptureStarted(session, request, timestamp, frameNumber);
    }

    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);
        RggbChannelVector rggbChannelVector = result.get(CaptureResult.COLOR_CORRECTION_GAINS);
        getColorTemperature(rggbChannelVector);

        startCamera();
    }
};

private void getColorTemperature(RggbChannelVector rggbChannelVector) {
    //rggbChannelVector.getRed() = 2.192929
    //rggbChannelVector.getGreenEven() = 1.0
    //rggbChannelVector.getGreenOdd() = 1.0
    //rggbChannelVector.getBlue() = 1.832323
}

iOS seems to have a readily available method to do that temperatureAndTintValues

While searching for something similar(in Java or any other language which I can adopt), almost all such methods expect a RGB value with [0, 255] range.

There are few methods to convert XYZ to CCT(Correlated Color Temperature) but even to get the XYZ value correct I need RGB values with in [0, 255]

As you can see the values from COLOR_CORRECTION_GAINS are >1 i.e greater than 255 which is not unusual because its a gain and iOS returns similar values(greater than 1).

like image 548
Archie.bpgc Avatar asked Aug 01 '17 09:08

Archie.bpgc


People also ask

How is color temperature determined?

The color temperature is indicated by the “27” or “2700K”, which is a warm color on the Kelvin scale. If the bulb had “841” printed on it, then it would be an 800 series and “41” or “4100K” which is a cool (whiter) color on the Kelvin scale. Low CCT lights start from red, orange, then going to yellow.

What is color correction temperature?

The particular color of a white light source can be simplified into a correlated color temperature (CCT). The higher the CCT, the bluer the light appears. Sunlight at 5600 K, for example, appears much bluer than tungsten light at 3200 K.

What is color corrected temperature of a lamp?

Color temperature (Correlated Color Temperature, or CCT, in lighting tech jargon) is essentially a gauge of how yellow or blue the color of light emitted from a light bulb appears. It's measured in the Kelvin unit and is most commonly found between 2200 Kelvin degrees and 6500 Kelvin degrees.


1 Answers

Since you have mentioned about apple provided method for achieving the same.

I m starting with Apple documentation on the method

From Apple Documentation

Apple documentation regarding temperatureAndTintValues is as follows

Converts device-specific white balance RGB gain values to device-independent temperature and tint values.

Reference : Documentation by Apple

Same functionality we can implement in android also by following the below methods.

Find out the RGB components in position

int x = (int)event.getX();
int y = (int)event.getY();
int pixel = bitmap.getPixel(x,y);

int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel); 

Correlated Color Temperature (CCT) which is measured in degrees Kelvin (K) on a scale from 1,000 to 10,000.

Following image shows the relation between CCT and some colors enter image description here


Calculating Color Temperature from RGB value

According to the SO Post The Color Temperature can be easily calculated using the following formulas

1. Find out 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=449n3+3525n2+6823.3n+5520.33

where n=(x−0.3320)/(0.1858−y)

Consolidated Formula (CCT From RGB)

CCT=449n3+3525n2+6823.3n+5520.33
where n=((0.23881)R+(0.25499)G+(−0.58291)B)/((0.11109)R+(−0.85406)G+(0.52289)B)

Android

Implement the same equation using java.

Note: Reference paper

Calculating Color Temperature and Illuminance using the TAOS TCS3414CS Digital Color Sensor


Similar implementations in other platforms

PHP - SO Post

Python - SO Post

Note:

The problem with converting from RGB to color tempperature is that there are approximately 16 million RGB colors, but only a very tiny subset of those colors actually correspond to a color temperature.

For example A green color doesn't correspond to any temperature - it's impossible due to how the human brain perceives light. Remembering that the demo above is really just an approximation, it would be theoretically possible to look up the temperature associated with a given color, but it wouldn't work for most colors.

Why Green is excluded? Read : Why Are There No Purple or Green Stars?


Many of the explanations are taken from other sites,

Hope everything sum up to your need!

like image 188
Saranjith Avatar answered Sep 20 '22 11:09

Saranjith