图像计算方差背后的理论是什么?

What's the theory behind computing variance of an image?

我正在尝试使用 LaplacianFilter 计算图像的模糊度。

根据这篇文章:https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/我必须计算输出图像的方差。问题是我在概念上不明白如何计算图像的方差。

每个像素的每个颜色通道都有 4 个值,因此我可以计算每个通道的方差,但是通过计算方差-协方差矩阵我得到 4 个值,甚至 16 个,但是根据 OpenCV 示例,它们只有 1 个号码。

计算出该数字后,他们只是利用阈值来做出二元决策,判断图像是否模糊。

PS。我绝不是这个话题的专家,因此我的陈述毫无意义。如果是这样,请编辑问题。

关于句子描述:

模糊图像的边缘是smoothed,所以variance很小。


1.如何计算方差。

post的核心功能是:

def variance_of_laplacian(image):
    # compute the Laplacian of the image and then return the focus
    # measure, which is simply the variance of the Laplacian
    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。用于图片

也就是说,var 是在展平的拉普拉斯图像或展平的一维数组上计算的。

计算数组xvariance为:

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

对于laplacian图像,就是edged图像。使用 GaussianBlur 和不同的 r 制作图像,然后对它们执行 laplacian filter,并计算变量:

模糊图像的边缘是smoothed,所以variance很小。

首先,如果你看到你给的教程,他们将图像转换为灰度,因此它只有 1 个通道和 1 个方差。您可以为每个通道执行此操作并尝试用它计算更复杂的公式,或者只使用所有数字的方差......但是我认为作者还将它转换为灰度,因为它是融合信息的好方法在作者提供的一篇论文中实际上说

A well focused image is expected to have a high variation in grey levels.

教程作者其实很通俗易懂的解释了。首先,想想拉普拉斯滤波器的作用。它将显示定义良好的边缘,这是一个使用他拥有的图片网格的示例。 (点击查看详情)

正如你所见,模糊的图像几乎没有任何边缘,而聚焦的图像有很多反应。现在,如果你计算方差会发生什么。让我们想象一下白色为 255 黑色为 0 的情况。如果一切都是黑色......那么方差很低(模糊的情况),但如果它们有一半那么方差很高。

然而,正如作者已经说过的,这个阈值取决于领域,如果你拍一张天空的照片,即使它是焦点,它也可能有低方差,因为它非常相似并且没有很很好地定义边缘...

希望这能解答您的疑惑:)