模糊图像的边缘被平滑处理,因此方差小。
1. 如何计算方差。
这篇文章的核心函数是:
def variance_of_laplacian(image):
return cv2.Laplacian(image, cv2.CV_64F).var()
由于Opencv-Python使用numpy.ndarray
来表示图像,因此我们查看numpy.var
:
Help on function var in module numpy.core.fromnumeric:
var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<class 'numpy._globals$
Compute the variance along the specified axis.
Returns the variance of the array elements, a measure of the spread of a distribution.
The variance is computed for the flattened array by default, otherwise over the specified axis.
2. 用于图片
也就是说,计算的变量是在平坦化的拉普拉斯图像或者一维数组上进行的。
计算数组x
的方差,公式为:
var = mean(abs(x - x.mean())**2)
例如:
>>> x = np.array([[1, 2], [3, 4]])
>>> x.var()
1.25
>>> np.mean(np.abs(x - x.mean())**2)
1.25
对于拉普拉斯图像,它是一个边缘图像。使用不同的r
值制作具有GaussianBlur
效果的图像,然后在它们上面执行laplacian filter
,并计算变量:
模糊图像的边缘会被平滑处理
,因此方差
很小。