我想知道是否有一种方法可以通过分析图像数据来确定图像是否模糊。


当前回答

基于耐克的答案。使用opencv实现基于拉普拉斯的方法很简单:

short GetSharpness(char* data, unsigned int width, unsigned int height)
{
    // assumes that your image is already in planner yuv or 8 bit greyscale
    IplImage* in = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
    IplImage* out = cvCreateImage(cvSize(width,height),IPL_DEPTH_16S,1);
    memcpy(in->imageData,data,width*height);

    // aperture size of 1 corresponds to the correct matrix
    cvLaplace(in, out, 1);

    short maxLap = -32767;
    short* imgData = (short*)out->imageData;
    for(int i =0;i<(out->imageSize/2);i++)
    {
        if(imgData[i] > maxLap) maxLap = imgData[i];
    }

    cvReleaseImage(&in);
    cvReleaseImage(&out);
    return maxLap;
}

将返回一个短消息,指示检测到的最大锐度,根据我对真实世界样本的测试,这是一个很好的指标,说明相机是否对焦。不足为奇的是,正常值是依赖于场景的,但远不如FFT方法,它有很高的假阳性率,在我的应用程序中是有用的。

其他回答

我想到了一个完全不同的解决方案。 我需要分析视频静止帧,以便在每(X)帧中找到最清晰的帧。这样,我将检测运动模糊和/或失焦图像。

我最终使用了Canny边缘检测,我得到了非常非常好的结果,几乎每一种视频(与尼基的方法,我有数字化的VHS视频和沉重的交错视频的问题)。

我通过在原始图像上设置感兴趣区域(ROI)来优化性能。

使用EmguCV:

//Convert image using Canny
using (Image<Gray, byte> imgCanny = imgOrig.Canny(225, 175))
{
    //Count the number of pixel representing an edge
    int nCountCanny = imgCanny.CountNonzero()[0];

    //Compute a sharpness grade:
    //< 1.5 = blurred, in movement
    //de 1.5 à 6 = acceptable
    //> 6 =stable, sharp
    double dSharpness = (nCountCanny * 1000.0 / (imgCanny.Cols * imgCanny.Rows));
}

估计图像清晰度的另一种非常简单的方法是使用拉普拉斯(或LoG)滤波器,并简单地选择最大值。如果你期待噪音,使用像99.9%分位数这样的稳健测量可能会更好(即选择第n高的对比度而不是最高的对比度)。如果你希望图像亮度变化,你还应该包括一个预处理步骤来标准化图像亮度/对比度(例如直方图均衡化)。

我已经在Mathematica中实现了Simon的建议,并在一些测试图像上进行了尝试:

第一个测试使用不同内核大小的高斯滤波器模糊测试图像,然后计算模糊图像的FFT,并取90%最高频率的平均值:

testFft[img_] := Table[
  (
   blurred = GaussianFilter[img, r];
   fft = Fourier[ImageData[blurred]];
   {w, h} = Dimensions[fft];
   windowSize = Round[w/2.1];
   Mean[Flatten[(Abs[
       fft[[w/2 - windowSize ;; w/2 + windowSize, 
         h/2 - windowSize ;; h/2 + windowSize]]])]]
   ), {r, 0, 10, 0.5}]

得到对数图:

5条线代表5张测试图像,X轴代表高斯滤波半径。图像是递减的,因此FFT是一个很好的度量清晰度的方法。

这是“最高LoG”模糊估计器的代码:它简单地应用一个LoG过滤器,并返回过滤结果中最亮的像素:

testLaplacian[img_] := Table[
  (
   blurred = GaussianFilter[img, r];
   Max[Flatten[ImageData[LaplacianGaussianFilter[blurred, 1]]]];
   ), {r, 0, 10, 0.5}]

得到对数图:

这里未模糊图像的传播稍微好一点(2.5 vs 3.3),主要是因为这种方法只使用图像中最强的对比度,而FFT本质上是整个图像的平均值。函数的减少速度也更快,所以设置一个“模糊”阈值可能更容易。

是的,它是。计算快速傅里叶变换并分析结果。傅里叶变换告诉你图像中出现了哪些频率。如果有少量的高频,那么图像是模糊的。

定义术语“低”和“高”取决于你。

编辑:

正如评论中所述,如果你想用一个浮点数来表示给定图像的模糊度,你必须计算出一个合适的度量。

尼基的回答提供了这样一个衡量标准。将图像与拉普拉斯核进行卷积:

   1
1 -4  1
   1

并且在输出上使用一个健壮的最大度量来获得一个可以用于阈值的数字。在计算拉普拉斯函数之前尽量避免平滑过多的图像,因为你只会发现平滑后的图像确实是模糊的:-)。

基于耐克的答案。使用opencv实现基于拉普拉斯的方法很简单:

short GetSharpness(char* data, unsigned int width, unsigned int height)
{
    // assumes that your image is already in planner yuv or 8 bit greyscale
    IplImage* in = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
    IplImage* out = cvCreateImage(cvSize(width,height),IPL_DEPTH_16S,1);
    memcpy(in->imageData,data,width*height);

    // aperture size of 1 corresponds to the correct matrix
    cvLaplace(in, out, 1);

    short maxLap = -32767;
    short* imgData = (short*)out->imageData;
    for(int i =0;i<(out->imageSize/2);i++)
    {
        if(imgData[i] > maxLap) maxLap = imgData[i];
    }

    cvReleaseImage(&in);
    cvReleaseImage(&out);
    return maxLap;
}

将返回一个短消息,指示检测到的最大锐度,根据我对真实世界样本的测试,这是一个很好的指标,说明相机是否对焦。不足为奇的是,正常值是依赖于场景的,但远不如FFT方法,它有很高的假阳性率,在我的应用程序中是有用的。

上面的回答阐明了许多事情,但我认为做一个概念上的区分是有用的。

如果你对一个模糊的图像拍摄一张完美对焦的照片呢?

The blurring detection problem is only well posed when you have a reference. If you need to design, e.g., an auto-focus system, you compare a sequence of images taken with different degrees of blurring, or smoothing, and you try to find the point of minimum blurring within this set. I other words you need to cross reference the various images using one of the techniques illustrated above (basically--with various possible levels of refinement in the approach--looking for the one image with the highest high-frequency content).