结合灰度世界和完美反射的颜色校正方法

1.结合灰度世界和完美反射的颜色校正方法原理

引用块内容
这里写图片描述

《基于图像分析的偏色检测及颜色校正方法》

2.opencv实现

ColorCalibrate.h

#ifndef COLORCALIBRATE_H
#define COLORRALIBRATE_H
#include <opencv.hpp>
using namespace cv;

class ColorCalibrate
{
private:
    float ur, vr;//校正系数
    float ub, vb;
    Mat src;
public:
    ColorCalibrate(const Mat& img);
    void clcCalibateCoefficient();
    Mat getCalibratePlane();
};


#endif

ColorCalibrate.cpp

#include"ColorCalibrate.h"

ColorCalibrate::ColorCalibrate(const Mat& img)
{
    src = img;
    ur = 0;
    vr = 0;
    vb = 0;
    ub = 0;
}
void ColorCalibrate::clcCalibateCoefficient()
{
    if (src.isContinuous())
    {
        src.reshape(1, src.cols*src.rows);
    }
    double sumSquareB = 0, sumValB = 0;
    double maxSquareB = 0, maxValB = 0;
    double sumSquareR = 0, sumValR = 0;
    double maxSquareR = 0, maxValR = 0;
    double sumValG = 0;
    double maxValG = 0;
    for (int rowCount = 0; rowCount < src.rows; rowCount++)
    {
        uchar* rowPt = src.ptr<uchar>(rowCount);
        for (int colCount = 0; colCount < src.cols*src.channels(); colCount += 3)
        {
            maxValB = maxValB < rowPt[colCount] ? rowPt[colCount] : maxValB;
            sumValB += rowPt[colCount];
            sumSquareB += rowPt[colCount] * rowPt[colCount];

            maxValG = maxValG < rowPt[colCount+1] ? rowPt[colCount+1] : maxValG;
            sumValG += rowPt[colCount+1];

            maxValR = maxValR < rowPt[colCount+2] ? rowPt[colCount+2] : maxValR;
            sumValR += rowPt[colCount+2];
            sumSquareR += rowPt[colCount+2] * rowPt[colCount+2];
        }
    }
    maxSquareB = maxValB*maxValB;
    maxSquareR = maxValB*maxValR;

    vb = (float)((sumValG*maxSquareB-sumSquareB*maxValG)/(sumValB*maxSquareB - sumSquareB*maxValB));
    ub = (float)((sumValG - sumValB*vb) / sumSquareB);
    vr = (float)((sumValG*maxSquareR - sumSquareR*maxValG) / (sumValR*maxSquareR - sumSquareR*maxValR));
    ur = (float)((sumValG - sumValR*vr) / sumSquareR);
}
Mat ColorCalibrate::getCalibratePlane()
{
    if (ur == 0 || vr == 0 || vb == 0 || vb == 0)
        clcCalibateCoefficient();
    if (src.isContinuous())
    {
        src.reshape(1, src.cols*src.rows);
    }
    Mat cali(src.size(),src.type());
    for (int rowCount = 0; rowCount < src.rows; rowCount++)
    {
        uchar* rowPt = src.ptr<uchar>(rowCount);
        uchar* caliPt = cali.ptr<uchar>(rowCount);
        for (int colCount = 0; colCount < src.cols*src.channels(); colCount += 3)
        {
            caliPt[colCount] = ub*rowPt[colCount] * rowPt[colCount] + vb*rowPt[colCount];
            caliPt[colCount + 1] = rowPt[colCount + 1];
            caliPt[colCount+2] = ur*rowPt[colCount+2] * rowPt[colCount+2] + vr*rowPt[colCount+2];
        }
    }
    return cali;
}

3.结果分析
这里写图片描述
原图
这里写图片描述
校正后图像

这里写图片描述

从实验结果看,只有图像偏蓝的时候效果比较好,比单纯的灰度世界法和完美反射法都好,但是其他情况下会失效。

可能是代码实现有问题。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值