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(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(i);
        uchar* Image_ptr_1 = Image.ptr(i+1);
        for (int j=0; jabs(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;
}

你可能感兴趣的:(opencv)