本文介绍了如何从色彩校正增益获得色温的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出相机拍摄的照片的色温.

  final CameraCaptureSession.CaptureCallback PreviewSSession = new CameraCaptureSession.CaptureCallback(){@Overridepublic void onCaptureStarted(@NonNull CameraCaptureSession会话,@ NonNull CaptureRequest请求,很长的时间戳,很长的frameNumber){super.onCaptureStarted(会话,请求,时间戳,frameNumber);}@Override公共无效onCaptureCompleted(@NonNull CameraCaptureSession会话,@ NonNull CaptureRequest请求,TotalCaptureResult结果){super.onCaptureCompleted(会话,请求,结果);RggbChannelVector rggbChannelVector = result.get(CaptureResult.COLOR_CORRECTION_GAINS);getColorTemperature(rggbChannelVector);startCamera();}};私人无效getColorTemperature(RggbChannelVector rggbChannelVector){//rggbChannelVector.getRed()= 2.192929//rggbChannelVector.getGreenEven()= 1.0//rggbChannelVector.getGreenOdd()= 1.0//rggbChannelVector.getBlue()= 1.832323} 

iOS似乎有一种容易使用的方法来进行此操作


根据RGB值计算色温

根据SO帖子,可以使用以下公式轻松计算色温

1.找出CIE三刺激值(XYZ),如下所示:

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

2.计算归一化色度值:

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

3.通过以下方式计算CCT值:

  CCT = 449n3 + 3525n2 + 6823.3n + 5520.33其中n =(x-0.3320)/(0.1858-y) 

合并公式(来自RGB的CCT)

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

Android

使用Java实施相同的方程式.

注意:参考文件

计算色温和照度使用TAOS TCS3414CS数字色彩传感器


在其他平台上的类似实现

PHP-SO Post

Python-SO Post

注意:

从RGB转换为色温的问题是大约有1600万种RGB颜色,但是这些颜色中只有很小一部分实际上对应于色温.

例如绿色与任何温度都不对应-由于人脑对光的感知方式,绿色是不可能的.记住上面的演示实际上只是一个近似值,从理论上讲,可以查找与给定颜色关联的温度,但不适用于大多数颜色.

为什么绿色不包括在内?阅读:为什么没有紫色或绿色星星?


许多解释来自其他网站,

希望一切都满足您的需求!

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).

解决方案

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

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


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 Illuminanceusing 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 exampleA 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!

这篇关于如何从色彩校正增益获得色温的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:49