在使用 MATLAB 2D 滤波器函数 filter2(B,X)
和卷积函数 conv(X,B,'')
时,我发现 filter2
> 函数本质上是 2D 卷积,但将滤波器系数矩阵旋转 180 度。就 filter2
和 conv2
的输出而言,我发现以下关系成立:
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?
发布评论
评论(1)
我将首先使用维基百科中的以下图像对卷积进行非常简短的讨论:
如图所示,卷积两个一维函数涉及反射其中之一(即卷积核),将两个函数滑过彼此之间,并计算其乘积的积分。
当对二维矩阵进行卷积时,卷积核会在两个维度上反映出来,然后针对与其他矩阵的每个唯一重叠组合计算乘积之和。内核维度的这种反映是卷积的固有步骤。
然而,在执行滤波时,我们喜欢将滤波矩阵视为一个“模板”,直接按原样放置在要滤波的矩阵上(即没有反射)。换句话说,我们想要执行与卷积等效的操作,但不反映过滤矩阵的维度。为了消除卷积期间执行的反射,我们可以在执行卷积之前添加滤波器矩阵维度的附加反射。
现在,对于任何给定的二维矩阵
A
,您可以使用函数 A 向自己证明,翻转两个维度相当于将矩阵旋转 180 度。 mathworks.com/help/techdoc/ref/flipdim.html" rel="noreferrer">FLIPDIM 和 ROT90:这就是为什么
filter2(f,A)
等价于conv2(A,rot90(f,2),'same ')
。为了进一步说明滤波器矩阵与卷积核的不同看法,我们可以看看当我们应用 FILTER2 和 CONV2 相同矩阵f
和A
的集合,定义如下:现在,当执行
B = filter2(f,A);
输出元素的计算B(2,2)
可以通过将滤波器的中心元素与A(2,2)
对齐并乘以重叠元素来可视化:由于滤波器矩阵外部的元素被忽略,我们可以看到乘积之和将为
17*1 + 4*1 + 5*1 = 26
。请注意,这里我们只是像“模板”一样将f
放置在A
之上,这就是过滤矩阵在矩阵上运行的方式。时,输出元素
B(2,2)
的计算如下所示:当我们执行
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:
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:This is why
filter2(f,A)
is equivalent toconv2(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 matricesf
andA
, defined as follows:Now, when performing
B = filter2(f,A);
the computation of output elementB(2,2)
can be visualized by lining up the center element of the filter withA(2,2)
and multiplying overlapping elements: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 layingf
on top ofA
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 elementB(2,2)
instead looks like this:and the sum of the products will instead be
5*1 + 1*1 + 13*1 = 19
. Notice that whenf
is taken to be a convolution kernel, we have to flip its dimensions before laying it on top ofA
.