opencv 学习之 图像色偏、清晰度、亮度检测

方法一
计算图片在灰度图上的均值和方差
当存在亮度异常时,均值会偏离均值点(可以假设为128),方差也会偏小;通过计算灰度图的均值和方差,评估图像是否存在过曝光或曝光不足

int light(string imgName)
{
    //Mat 转 IplImage
    Mat M= imread(imgName);
    IplImage *image = &IplImage(M);
    IplImage * gray = cvCreateImage(cvGetSize(image), image->depth, 1);
    //转为灰度图片
    cvCvtColor(image, gray, CV_BGR2GRAY);
    double sum = 0;
    double avg = 0;
    CvScalar scalar;
    int ls[256];
    for (int i = 0; i<256; i )
        ls[i] = 0;
    for (int i = 0; i<gray->height; i )
    {
        for (int j = 0; j<gray->width; j )
        {
            scalar = cvGet2D(gray, i, j);
            sum = (scalar.val[0] - 128);
            int x = (int)scalar.val[0];
            ls[x] ;
        }
    }
    avg = sum / (gray->height * gray->width);
    double total = 0;
    double mean = 0;
    for (int i = 0; i<256; i )
    {
        total = abs(i - 128 - avg)* ls[i];
    }
    mean = total / (gray->height * gray->width);
    double cast = abs(avg / mean);
    cout << imgName << "亮度异常值:" << cast << endl;
    if (cast>1)
    {
        if (avg > 0)
        {
            cout << "亮度异常 过亮" << avg << endl;
            return 1;
        }
        else {
            cout << "亮度异常 过暗" << avg << endl;
            return -1;
        }
    }
    else
    {
        cout << "normal" << endl;
        return 0;
    }
}

方法二

 

在这里插入图片描述
根据hsl中l=(max(R,G,B) min(R,G,B))/2
用cvAvg分别计算R,G,B三通道平均值,最大与最小的平均值作为亮度

int light2(string imgName)
{
    Mat img,gray;
    img = imread(imgName);
    IplImage *img1 = &IplImage(img);
    CvScalar cs;
    const char * c = imgName.c_str();
    IplImage *src1;
    src1 = cvLoadImage(c);

    cs = cvAvg(src1);
    double max1=max(cs.val[0],cs.val[1]), min1=min(cs.val[0],cs.val[1]);
    cout <<"R"<<cs.val[0] << endl;
    cout <<"G"<< cs.val[1] << endl;
    cout <<"B"<< cs.val[2] << endl;
    cout << "亮度为" << (max(max1,cs.val[2]) min(min1,cs.val[2]))/2 << endl;
    return 0;
}

  

方法三

转为灰度图片,用cvAvg计算像素平均值作为亮度

int light(string imgName)
{
    Mat img;
    img = imread(imgName,0);
    Scalar scalar = mean(img);
    return scalar.val[0];
}

测试

int main()
{
string img1Name,img2Name;
    img1Name = "brightKurt.jpg";
    img2Name = "darkKurt.jpg";
    cout << "图片1 brightKurt" << endl;
    cout << "方法1" << endl;
    light(img1Name);
    cout << "方法2" << endl;
    light2(img1Name);
    cout << "方法3" << endl;
    light3(img1Name);
    cout << endl << endl<< "图片2 darkKurt" << endl;
    cout << "方法1" << endl;
    light(img2Name);
    cout << "方法2" << endl;
    light2(img2Name);
    cout << "方法3" << endl;
    light3(img2Name);
    Mat img1, img2;
    img1 = imread(img1Name);
    img2 = imread(img2Name);
    imshow("img1 bright Kurt", img1);
    imshow("img2 dark Kurt", img2);
    
    waitKey(0);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值