filter2函数中滤波器矩阵旋转的物理意义

发布于 2024-10-09 02:40:19 字数 487 浏览 3 评论 0 原文

在使用 MATLAB 2D 滤波器函数 filter2(B,X) 和卷积函数 conv(X,B,'') 时,我发现 filter2 > 函数本质上是 2D 卷积,但将滤波器系数矩阵旋转 180 度。就 filter2conv2 的输出而言,我发现以下关系成立:

 output matrix of filter2 = each element negated of output of conv2

编辑:我错了;上述关系在一般情况下并不成立,但我在一些情况下看到了这一点。一般来说,两个输出矩阵是不相关的,因为两个输出矩阵都获得了两个完全不同的内核,用于卷积。

我了解2D卷积是如何执行的。我想了解的是这在图像处理术语中的含义。我如何想象这里发生的事情?将滤波器系数矩阵旋转 180 度意味着什么?

While using MATLAB 2D filter funcion filter2(B,X) and convolution function conv(X,B,''), I see that the filter2 function is essentially 2D convolution but with a rotation by 180 degrees of the filter coefficients matrix. In terms of the outputs of filter2 and conv2, I see that the below relation holds true:

 output matrix of filter2 = each element negated of output of conv2

EDIT: I was incorrect; the above relation does not hold true in general, but I saw it for a few cases. In general, the two output matrices are unrelated, due to the fact that 2 entirely different kernels are obtained in both which are used for convolution.

I understand how 2D convolution is performed. What I want to understand is the implication of this in image processing terms. How do I visualize what is happening here? What does it mean to rotate a filter coefficient matrix by 180 degrees?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不顾 2024-10-16 02:40:19

我将首先使用维基百科中的以下图像对卷积进行非常简短的讨论:

alt text

如图所示,卷积两个一维函数涉及反射其中之一(即卷积核),将两个函数滑过彼此之间,并计算其乘积的积分。

当对二维矩阵进行卷积时,卷积核会在两个维度上反映出来,然后针对与其他矩阵的每个唯一重叠组合计算乘积之和。内核维度的这种反映是卷积的固有步骤。

然而,在执行滤波时,我们喜欢将滤波矩阵视为一个“模板”,直接按原样放置在要滤波的矩阵上(即没有反射)。换句话说,我们想要执行与卷积等效的操作,但不反映过滤矩阵的维度。为了消除卷积期间执行的反射,我们可以在执行卷积之前添加滤波器矩阵维度的附加反射。

现在,对于任何给定的二维矩阵 A,您可以使用函数 A 向自己证明,翻转两个维度相当于将矩阵旋转 180 度。 mathworks.com/help/techdoc/ref/flipdim.html" rel="noreferrer">FLIPDIMROT90

A = rand(5);  %# A 5-by-5 matrix of random values
isequal(flipdim(flipdim(A,1),2),rot90(A,2))  %# Will return 1 (i.e. true)

这就是为什么 filter2(f,A) 等价于 conv2(A,rot90(f,2),'same ')。为了进一步说明滤波器矩阵与卷积核的不同看法,我们可以看看当我们应用 FILTER2CONV2 相同矩阵fA的集合,定义如下:

>> f = [1 0 0; 0 1 0; 1 0 0]  %# A 3-by-3 filter/kernel
f =
     1     0     0
     0     1     0
     1     0     0
>> A = magic(5)  %# A 5-by-5 matrix
A =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

现在,当执行B = filter2(f,A);输出元素的计算B(2,2) 可以通过将滤波器的中心元素与 A(2,2) 对齐并乘以重叠元素来可视化:

    17*1  24*0   1*0   8    15
    23*0   5*1   7*0  14    16
     4*1   6*0  13*0  20    22
    10    12    19    21     3
    11    18    25     2     9

由于滤波器矩阵外部的元素被忽略,我们可以看到乘积之和将为17*1 + 4*1 + 5*1 = 26。请注意,这里我们只是像“模板”一样将 f 放置在 A 之上,这就是过滤矩阵在矩阵上运行的方式。

时,输出元素 B(2,2) 的计算如下所示:

    17*0  24*0   1*1   8    15
    23*0   5*1   7*0  14    16
     4*0   6*0  13*1  20    22
    10    12    19    21     3
    11    18    25     2     9

当我们执行 B = conv2(A,f,'same'); 产品的数量将为 5*1 + 1*1 + 13*1 = 19。请注意,当将 f 视为卷积核时,我们必须先翻转其尺寸,然后再将其放置在 A 之上。

I'll start with a very brief discussion of convolution, using the following image from Wikipedia:

alt text

As illustrated, convolving two 1-D functions involves reflecting one of them (i.e. the convolution kernel), sliding the two functions over one another, and computing the integral of their product.

When convolving 2-D matrices, the convolution kernel is reflected in both dimensions, and then the sum of the products is computed for every unique overlapping combination with the other matrix. This reflection of the kernel's dimensions is an inherent step of the convolution.

However, when performing filtering we like to think of the filtering matrix as though it were a "stencil" that is directly laid as is (i.e. with no reflections) over the matrix to be filtered. In other words, we want to perform an equivalent operation as a convolution, but without reflecting the dimensions of the filtering matrix. In order to cancel the reflection performed during the convolution, we can therefore add an additional reflection of the dimensions of the filter matrix before the convolution is performed.

Now, for any given 2-D matrix A, you can prove to yourself that flipping both dimensions is equivalent to rotating the matrix 180 degrees by using the functions FLIPDIM and ROT90 in MATLAB:

A = rand(5);  %# A 5-by-5 matrix of random values
isequal(flipdim(flipdim(A,1),2),rot90(A,2))  %# Will return 1 (i.e. true)

This is why filter2(f,A) is equivalent to conv2(A,rot90(f,2),'same'). To illustrate further how there are different perceptions of filter matrices versus convolution kernels, we can look at what happens when we apply FILTER2 and CONV2 to the same set of matrices f and A, defined as follows:

>> f = [1 0 0; 0 1 0; 1 0 0]  %# A 3-by-3 filter/kernel
f =
     1     0     0
     0     1     0
     1     0     0
>> A = magic(5)  %# A 5-by-5 matrix
A =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

Now, when performing B = filter2(f,A); the computation of output element B(2,2) can be visualized by lining up the center element of the filter with A(2,2) and multiplying overlapping elements:

    17*1  24*0   1*0   8    15
    23*0   5*1   7*0  14    16
     4*1   6*0  13*0  20    22
    10    12    19    21     3
    11    18    25     2     9

Since elements outside the filter matrix are ignored, we can see that the sum of the products will be 17*1 + 4*1 + 5*1 = 26. Notice that here we are simply laying f on top of A like a "stencil", which is how filter matrices are perceived to operate on a matrix.

When we perform B = conv2(A,f,'same');, the computation of output element B(2,2) instead looks like this:

    17*0  24*0   1*1   8    15
    23*0   5*1   7*0  14    16
     4*0   6*0  13*1  20    22
    10    12    19    21     3
    11    18    25     2     9

and the sum of the products will instead be 5*1 + 1*1 + 13*1 = 19. Notice that when f is taken to be a convolution kernel, we have to flip its dimensions before laying it on top of A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文