Opencv图像偏色检测

**

1. 偏色检测公式

**

图像的偏色不仅与图像色度的平均值有直接关系,还与图像的色度分布特性有关。如果在 a - b色度坐标平面上的二维直方图中色度分布基本上为单峰值,或者分布较为集中,而色度平均值又较大时,一般都存在偏色,而且色度平均值越大,偏色越严重。因此引入等效圆的概念,采用图像平均色度D和色度中心距M的比值,即偏色因子K来衡量图像的偏色程度。其计算方法如下式:
Opencv图像偏色检测_第1张图片
Opencv图像偏色检测_第2张图片

以上摘自论文《基于图像分析的偏色检测及颜色校正方法》——徐晓昭,蔡轶珩等。
但是在实际应用中,公式(3)去掉平方可更好的指示图像是否偏色:
Opencv图像偏色检测_第3张图片
**

2.RGB颜色空间转Lab颜色空间

**
颜色转换原理见:颜色空间系列2: RGB和CIELAB颜色空间的转换及优化算法
利用opencv实现代码为:

void RGB2LAB(Mat& rgb, Mat& Lab)
{
    Mat XYZ(rgb.size(), rgb.type());
    Mat_<Vec3b>::iterator begainRGB = rgb.begin<Vec3b>();
    Mat_<Vec3b>::iterator endRGB = rgb.end<Vec3b>();
    Mat_<Vec3b>::iterator begainXYZ = XYZ.begin<Vec3b>();
    int shift = 22;
    for (; begainRGB != endRGB; begainRGB++, begainXYZ++)
    {
        (*begainXYZ)[0] = ((*begainRGB)[0] * 199049 + (*begainRGB)[1] * 394494 + (*begainRGB)[2] * 455033 + 524288) >> (shift-2);
        (*begainXYZ)[1] = ((*begainRGB)[0] * 75675 + (*begainRGB)[1] * 749900 + (*begainRGB)[2] * 223002 + 524288) >> (shift-2);
        (*begainXYZ)[2] = ((*begainRGB)[0] * 915161 + (*begainRGB)[1] * 114795 + (*begainRGB)[2] * 18621 + 524288) >> (shift-2);
    }

    int LabTab[1024];
    for (int i = 0; i < 1024; i++)
    {
        if (i>9)
            LabTab[i] = (int)(pow((float)i / 1020, 1.0F / 3) * (1 << shift) + 0.5);
        else
            LabTab[i] = (int)((29 * 29.0 * i / (6 * 6 * 3 * 1020) + 4.0 / 29) * (1 << shift) + 0.5);
    }
    const int ScaleLC = (int)(16 * 2.55 * (1 << shift) + 0.5);
    const int ScaleLT = (int)(116 * 2.55 + 0.5);
    const int HalfShiftValue = 524288;
    begainXYZ = XYZ.begin<Vec3b>();
    Mat_<Vec3b>::iterator endXYZ = XYZ.end<Vec3b>();
    Lab.create(rgb.size(),rgb.type());
    Mat_<Vec3b>::iterator begainLab = Lab.begin<Vec3b>();
    for (; begainXYZ != endXYZ; begainXYZ++, begainLab++)
    {
        int X = LabTab[(*begainXYZ)[0]];
        int Y = LabTab[(*begainXYZ)[1]];
        int Z = LabTab[(*begainXYZ)[2]];
        int L = ((ScaleLT * Y - ScaleLC + HalfShiftValue) >> shift);
        int A = ((500 * (X - Y) + HalfShiftValue) >> shift) + 128;
        int B = ((200 * (Y - Z) + HalfShiftValue) >> shift) + 128;
        (*begainLab)[0] = L;
        (*begainLab)[1] = A;
        (*begainLab)[2] = B;
    }
}

3.偏色检测算法实现

根据偏色检测公式,opencv实现过程为:

float colorCheck(const Mat& imgLab)
{
    Mat_<Vec3b>::const_iterator begainIt = imgLab.begin<Vec3b>();
    Mat_<Vec3b>::const_iterator endIt = imgLab.end<Vec3b>();
    float aSum = 0;
    float bSum = 0;
    for (; begainIt != endIt; begainIt++)
    {
        aSum += (*begainIt)[1];
        bSum += (*begainIt)[2];
    }
    int MN = imgLab.cols*imgLab.rows;
    double Da = aSum / MN - 128; // 必须归一化到[-128,,127]范围内 
    double Db = bSum / MN - 128;

    //平均色度
    double D = sqrt(Da*Da+Db*Db);

    begainIt = imgLab.begin<Vec3b>();
    double Ma = 0;
    double Mb = 0;
    for (; begainIt != endIt; begainIt++)
    {
        Ma += abs((*begainIt)[1]-128 - Da);
        Mb += abs((*begainIt)[2]-128 - Db);
    }
    Ma = Ma / MN;
    Mb = Mb / MN;
    //色度中心距
    double M = sqrt(Ma*Ma + Mb*Mb);
    //偏色因子
    float K = (float)(D / M);
    return K;
}

综合来说,k值不大于1.5我们可以认为其整体图像偏色的可能性不大,当然这个值取多少可能还是需要和实际情况结合的。

你可能感兴趣的:(opencv,偏色检测)