opencv中图像失焦检测

  • 失焦的图片和对焦准确的图片最大的区别就是正常图片轮廓明显,而失焦图片几乎没有较大像素值之间的变化
  • 对图像的横向,以及纵向,分别做差分,累计差分可以用来作为判断是否失焦的参考
  • 两个函数,一个简单粗暴直接根据差分值判断是否失焦,适合确定样本类型的情况,另外一种,需要进一步判断
//简单设定阈值判断是否失焦
bool focusDetect(Mat& img){

    clock_t start, end;
    start = clock();
    int diff = 0;
    int diff_thre = 20;
    int diff_sum_thre = 1000;
    for (int i = img.rows / 10; i < img.rows; i += img.rows / 10){
        uchar* ptrow = img.ptr<uchar>(i);
        for (int j = 0; j < img.cols - 1; j++){
            if (abs(ptrow[j + 1] - ptrow[j])>diff_thre)
                diff += abs(ptrow[j + 1] - ptrow[j]);
        }
        cout << diff << endl;
    }
    end = clock();
    cout << "time=" << end - start << endl;

    bool res = true;
    if (diff < diff_sum_thre) {
        cout << "the focus might be wrong!" << endl;
        res = false;
    }

    return res;
}

//返回一个与焦距是否对焦成功的一个比例因子
double focus_measure_GRAT(Mat Image)
{
    double threshold = 0;
    double temp = 0;
    double totalsum = 0;
    int totalnum = 0;

    for (int i=0; i<Image.rows; i++)
    {
        uchar* Image_ptr = Image.ptr<uchar>(i);
        uchar* Image_ptr_1 = Image.ptr<uchar>(i+1);
        for (int j=0; j<Image.cols; j++)
        {
            temp = max(abs(Image_ptr_1[j]-Image_ptr[j]), abs(Image_ptr[j+1]-Image_ptr[j]));
            totalsum += temp;
            totalnum += 1;
        }
    }

    double FM = totalsum/totalnum;

    return FM;
}
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值