The physical significance of the rotation of the filter matrix in the filter2 function

Using the 2D filter function MATLAB filter2(B,X) and the conv(X,B,'') convolution function conv(X,B,'') I see that filter2 is essentially a two-dimensional convolution, but with a 180-degree rotation of the filter coefficient matrix. As for the outputs of filter2 and conv2 , I see that the relation below is true:

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

EDIT: I was incorrect; the above ratio does not hold in general, but I have seen this for several cases. In the general case, the two output matrices are not connected to each other due to the fact that in both cases two completely different kernels are used, which are used for convolution.

I understand how 2D convolution is done. What I want to understand is a consequence of this in terms of image processing. How can I imagine what is going on here? What does a 180 degree filter matrix rotate mean?

+4
source share
1 answer

I will start with a very short discussion of convolution using the following image from Wikipedia:

alt text

As shown, the convolution of two one-dimensional functions involves the reflection of one of them (i.e., the convolution kernel) that shifts the two functions one above the other and calculates the integral of their product.

When folding two-dimensional matrices, the convolution kernel is reflected in both dimensions, and then the sum of the products is calculated for each unique overlapping combination with another matrix. This reflection of the size of the core is an essential step in convolution.

However, when performing filtering, we like to think about the filtering matrix, as if it were a “stencil” that is directly stacked as is (i.e. without reflections) over the filtered matrix. In other words, we want to perform the equivalent operation as a convolution, but without reflecting the dimensions of the filter matrix. To cancel the reflection performed during the convolution, we can 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 turning both dimensions is equivalent to rotating the matrix 180 degrees using the FLIPDIM and ROT90 functions 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 (ie true) 

This is why filter2(f,A) equivalent to conv2(A,rot90(f,2),'same') . To further illustrate how there are different representations of filter matrices compared to convolution kernels, we can see what happens when we apply FILTER2 and CONV2 to the same set of matrices f and A , which is 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 executing B = filter2(f,A); the calculation of the output element B(2,2) can be visualized by aligning the central filter element with A(2,2) and multiplying the 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 . Note that here we simply put f on top of A as a “stencil”, namely, how the filter matrices are perceived to work with the matrix.

When we execute B = conv2(A,f,'same'); , the calculation of the output element B(2,2) as follows:

  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 works will be 5*1 + 1*1 + 13*1 = 19 . Note that when f taken as the convolution kernel, we must flip its dimensions before putting it on top of A

+6
source

Source: https://habr.com/ru/post/1333053/


All Articles