根据灰度直方图调整图象亮度

两种办法:

1. 计算两张图象灰度图的平均亮度,然后根据亮度差异调整

            double scaleBack=cv::mean(roiBack)[0];
            double scaleIn=cv::mean(roiIn)[0];

            roiIn.convertTo(roiIn,roiIn.type(),scaleBack/scaleIn,0);

2.根据灰度图的两个峰值的位置,将一张图的峰值移动到另一张图的峰值位置

计算直方图峰值位置

vector<int> getPeak(Mat img,int thres)
{
    MatND hist;
    int histSize[] = { 256 };
    float range[]={0,256};
    const float* ranges[]={range};
    calcHist(&img,1,0,Mat(),hist,1,histSize,ranges,true,false);
    //MatND hist=smooth(hist0,10);
    //因为任何一个图像的某个像素的总个数,都有可能会有很多,会超出所定义的图像的尺寸,针对这种情况,先对个数进行范围的限制
    //先用 minMaxLoc函数来得到计算直方图后的像素的最大个数
    double g_dHistMaxValue;
    int maxLocation;
    Point maxLoc;
    minMaxLoc(hist, 0, &g_dHistMaxValue, 0, &maxLoc);
    if(maxLoc.y<=2)
    {
        g_dHistMaxValue=0;
        maxLocation=-1;
        for(int i=5;i<histSize[0];i++)
        {
            if(hist.at<float>(i)>hist.at<float>(i-1)&&hist.at<float>(i)>hist.at<float>(i+1)
                    &&hist.at<float>(i)>hist.at<float>(i-2)&&hist.at<float>(i)>hist.at<float>(i+2))
            {
                if(g_dHistMaxValue<hist.at<float>(i))
                {
                    g_dHistMaxValue=hist.at<float>(i);
                    maxLocation=i;
                }
            }
        }
    }
    else
    {
        maxLocation=maxLoc.y;
    }
    int secPeakLocation;
    int max=g_dHistMaxValue;
    int secMax=0;
    for(int i=2;i<histSize[0];i++)
    {
        cout<<hist.at<float>(i)<<" ";
        if(hist.at<float>(i)>hist.at<float>(i-1)&&hist.at<float>(i)>hist.at<float>(i+1)
                &&hist.at<float>(i)>hist.at<float>(i-2)&&hist.at<float>(i)>hist.at<float>(i+2))
        {
            if(secMax<hist.at<float>(i)&&hist.at<float>(i)<max&&abs(maxLocation-i)>thres)
            {
                secMax=hist.at<float>(i);
                secPeakLocation=i;
            }
        }
    }
    cout<<endl;
    cout<<"value:"<<secMax<<","<<max<<endl;
    cout<<"loc:"<<secPeakLocation<<","<<maxLocation<<endl;
    vector<int> loc;
    loc.push_back(secPeakLocation);
    loc.push_back(maxLocation);
    return loc;
}

调整亮度

            vector<int> backPeak=getPeak(roiBack,40);
            vector<int> inPeak=getPeak(roiIn,20);
            double alpha=(double)(backPeak[0]-backPeak[1])/(double)(inPeak[0]-inPeak[1]);
            double beta=(double)(inPeak[0]*backPeak[1]-inPeak[1]*backPeak[0])/(double)(inPeak[0]-inPeak[1]);
            cout<<"alpha: "<<alpha<<" beta: "<<beta<<endl;
            roiIn.convertTo(roiIn,roiIn.type(),alpha,beta);

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值