我尝试获取图像的模糊度。我引用 this tutorial计算开放式简历中拉普拉斯算子的方差。
import cv2
def variance_of_laplacian(image):
return cv2.Laplacian(image, cv2.CV_64F).var()
def check_blurry(image):
"""
:param: the image
:return: True or False for blurry
"""
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
return fm
当我尝试从两个看起来完全相同但大小不同的图像计算 fm 时。
filePath = 'small.jpeg'
image1 = cv2.imread(filePath)
print('small image shape', image1.shape)
print('small image fm', check_blurry(image1))
filePath = 'large.jpg'
image2 = cv2.imread(filePath)
print('large image shape', image2.shape)
print('large image fm', check_blurry(image2))
的输出是:
small image shape (1440, 1080, 3)
small image fm 4.7882723403428065
large image shape (4032, 3024, 3)
large image fm 8.44476634687877
很明显,小图按比例缩小了大图的 2.8 倍。 fm与图像大小有关吗?如果有,它们之间是什么关系?或者有什么解决方案可以评估不同尺寸图像的模糊程度?
最佳答案
“fm和图片大小有关系吗?”
部分是(至少就您的问题而言),因为如果缩放图像,则必须插入像素值。缩小不仅会丢失信息,还会通过像素插值(如果不是最近邻插值)创建新信息,从而影响生成图像的方差。但这仅适用于缩放后的图像,不适用于一开始就不同的图像。
关于python - 在opencv中用拉普拉斯算子的方差计算模糊度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808127/