Camera2 API 设置自定义白平衡和温度颜色

Posted

技术标签:

【中文标题】Camera2 API 设置自定义白平衡和温度颜色【英文标题】:Camera2 API Set Custom White Balance & Temperature Color 【发布时间】:2016-05-28 02:55:19 【问题描述】:

我正在尝试在我的相机应用中为白平衡和温度颜色设置自定义值。我正在使用 camera2 API,我正在尝试不同的方法来设置这个值。我从 excel 文件中找到了一种方法,可以从 100 到 100.000 之间的白平衡值中获取正确的 RGB 温度矩阵 [红、绿、蓝]。

我将此方法附加到 Seekbar 并且它工作正常,当我尝试聚焦白色时出现我的问题,然后它变成粉红色。任何一种光在屏幕上看起来都像是一个粉红色的手电筒。

我以这种方式设置值:

mPreviewRequestBuilder.set(CaptureRequest.COLOR_CORRECTION_MODE, CaptureRequest.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX);

RggbChannelVector rggb = getTemperatureVector(seekBackSelectedTemperature);

mPreviewRequestBuilder.set(CaptureRequest.COLOR_CORRECTION_GAINS, myRggbChannelVector);

另外,我获取矩阵的方法是这样的:

 public static RggbChannelVector getTemperatureVector (int WhiteBalanceValue)


    float InsertTemperature = WhiteBalanceValue;
    float temperature = InsertTemperature / 100;
    float red;
    float green;
    float blue;

    //Calculate red

    if (temperature <= 66)
        red = 255;
    else 
        red = temperature - 60;
        red = (float) (329.698727446 * (Math.pow((double) red, -0.1332047592)));
        if (red < 0)
            red = 0;
        if (red > 255)
            red = 255;
    


    //Calculate green
    if (temperature <= 66) 
        green = temperature;
        green = (float) (99.4708025861 * Math.log(green) - 161.1195681661);
        if (green < 0)
            green = 0;
        if (green > 255)
            green = 255;
     else
        green = temperature - 60;
    green = (float) (288.1221695283 * (Math.pow((double) red, -0.0755148492)));
    if (green < 0)
        green = 0;
    if (green > 255)
        green = 255;


    //calculate blue
    if (temperature >= 66)
        blue = 255;
    else if (temperature <= 19)
        blue = 0;
    else 
        blue = temperature - 10;
        blue = (float) (138.5177312231 * Math.log(blue) - 305.0447927307);
        if (blue < 0)
            blue = 0;
        if (blue > 255)
            blue = 255;
    
    RggbChannelVector finalTemperatureValue = new RggbChannelVector(red/255,(green/255)/2,(green/255)/2,blue/255);
    return finalTemperatureValue;

可能是因为我的CaptureRequest的方法不正确,但是我没有找到修复的方法。

【问题讨论】:

最好将链接指向该算法:link 【参考方案1】:

将模板更改为 Still_capture 或 Manual Template 并使用下一个方法后它工作:

 captureBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_OFF);
        // adjust color correction using seekbar's params
        captureBuilder.set(CaptureRequest.COLOR_CORRECTION_MODE, CaptureRequest.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX);
        captureBuilder.set(CaptureRequest.COLOR_CORRECTION_GAINS, CameraCapabilities.colorTemperature(Integer.parseInt(awbMode)));

public static RggbChannelVector colorTemperature(int whiteBalance) 
    float temperature = whiteBalance / 100;
    float red;
    float green;
    float blue;

    //Calculate red
    if (temperature <= 66)
        red = 255;
    else 
        red = temperature - 60;
        red = (float) (329.698727446 * (Math.pow((double) red, -0.1332047592)));
        if (red < 0)
            red = 0;
        if (red > 255)
            red = 255;
    


    //Calculate green
    if (temperature <= 66) 
        green = temperature;
        green = (float) (99.4708025861 * Math.log(green) - 161.1195681661);
        if (green < 0)
            green = 0;
        if (green > 255)
            green = 255;
     else 
        green = temperature - 60;
        green = (float) (288.1221695283 * (Math.pow((double) green, -0.0755148492)));
        if (green < 0)
            green = 0;
        if (green > 255)
            green = 255;
    

    //calculate blue
    if (temperature >= 66)
        blue = 255;
    else if (temperature <= 19)
        blue = 0;
    else 
        blue = temperature - 10;
        blue = (float) (138.5177312231 * Math.log(blue) - 305.0447927307);
        if (blue < 0)
            blue = 0;
        if (blue > 255)
            blue = 255;
    

    Log.v(TAG, "red=" + red + ", green=" + green + ", blue=" + blue);
    return new RggbChannelVector((red / 255) * 2, (green / 255), (green / 255), (blue / 255) * 2);

【讨论】:

非常感谢您分享您的代码。我正在寻找手动调整 WB,而您的代码为我节省了几个小时。 放轻松,伙计!在摄影论坛中找到它以将其添加到 android 是一件很痛苦的事情。希望对你有很大帮助;) 我猜想用camera2 API,opencv的很多功能都处理好了【参考方案2】:

另一种可能且更简单的解决方案; 因子必须在 0 到 100 之间:

private static RggbChannelVector computeTemperature(final int factor)

    return new RggbChannelVector(0.635f + (0.0208333f * factor), 1.0f, 1.0f, 3.7420394f + (-0.0287829f * factor));

【讨论】:

以上是关于Camera2 API 设置自定义白平衡和温度颜色的主要内容,如果未能解决你的问题,请参考以下文章

CANON SDK 将自定义白平衡设置为保存的 JPG 图像

Android Camera2 API 设置自定义亮度、对比度、伽玛

白平衡是什么?怎么理解白平衡?

camera摄像原理之三:色温和自动白平衡

白平衡调整的步骤

如何调节白平衡