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


当前回答

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

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

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).

其他回答

这就是我在Opencv中检测区域焦点质量的方法:

Mat grad;
int scale = 1;
int delta = 0;
int ddepth = CV_8U;
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
/// Gradient X
Sobel(matFromSensor, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
/// Gradient Y
Sobel(matFromSensor, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
convertScaleAbs(grad_y, abs_grad_y);
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
cv::Scalar mu, sigma;
cv::meanStdDev(grad, /* mean */ mu, /*stdev*/ sigma);
focusMeasure = mu.val[0] * mu.val[0];

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

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

编辑:

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

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

   1
1 -4  1
   1

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

我在matlab中使用FFT实现了它,并检查FFT的直方图,计算平均值和STD,还可以做拟合函数

fa =  abs(fftshift(fft(sharp_img)));
fb = abs(fftshift(fft(blured_img)));

f1=20*log10(0.001+fa);
f2=20*log10(0.001+fb);

figure,imagesc(f1);title('org')
figure,imagesc(f2);title('blur')

figure,hist(f1(:),100);title('org')
figure,hist(f2(:),100);title('blur')

mf1=mean(f1(:));
mf2=mean(f2(:));

mfd1=median(f1(:));
mfd2=median(f2(:));

sf1=std(f1(:));
sf2=std(f2(:));

在这篇文章中,我尝试了基于拉普拉斯滤波器的解决方案。这对我没有帮助。所以,我尝试了这篇文章中的解决方案,它对我的情况很好(但很慢):

import cv2

image = cv2.imread("test.jpeg")
height, width = image.shape[:2]
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

def px(x, y):
    return int(gray[y, x])

sum = 0
for x in range(width-1):
    for y in range(height):
        sum += abs(px(x, y) - px(x+1, y))

较少模糊的图像具有最大和值!

你也可以通过改变步长来调整速度和准确度。

这部分

for x in range(width - 1):

你可以用这个替换

for x in range(0, width - 1, 10):

谢谢妮基的拉普拉斯建议。 OpenCV文档给我指出了同样的方向: 使用python, cv2 (opencv 2.4.10)和numpy…

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
numpy.max(cv2.convertScaleAbs(cv2.Laplacian(gray, 3)))

结果在0-255之间。我发现任何超过200的东西都非常清晰,而到了100,就明显模糊了。Max从来没有真正低于20,即使它完全模糊。