Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top