How to use Matlab filter2

Y=filter2(h,x,’shape’)

h is the filter and x is the data to be filtered; h moves over x to perform template (mask) filtering.
shape can be full, same or valid (same is the default if omitted).

Let’s go straight to an example.
First, use fspecial(‘average’) to create an averaging filter. It is 3*3 by default, and every value is 1/(3*3)=0.1111

>> h=fspecial('average')

h =
    0.1111    0.1111    0.1111
    0.1111    0.1111    0.1111
    0.1111    0.1111    0.1111

Then create a simple matrix

>> x=[1,2,3;4,5,6;2,2,2]

x =
    1     2     3
    4     5     6
    2     2     2

Use filter2(h,x)

>> y=filter2(h,x)

y =
    1.3333    2.3333    1.7778
    1.7778    3.0000    2.2222
    1.4444    2.3333    1.6667

Let’s look at the process. First, align the bottom-right point of the filter template h with the top-left point of x. The other points of h then have no corresponding points in x, so x must be zero-padded (after padding, x becomes):

x =
    0     0     0     0     0     0     0
    0     0     0     0     0     0     0
    0     0     1     2     3     0     0
    0     0     4     5     6     0     0
    0     0     2     2     2     0     0
    0     0     0     0     0     0     0
    0     0     0     0     0     0     0

Now compute the sum of products of the corresponding elements of h and x: 0+0+0+0+0+0+0+0+1*0.1111=0.1111
This value is the value of the zero-padded matrix x at x(2,2), which is also the position of h’s center point h(2,2).
Then h moves one column to the right along x, the sum of products is recalculated, and the process continues.
The final result is:

y =
    0.1111    0.3333    0.6667    0.5556    0.3333
    0.5556    1.3333    2.3333    1.7778    1.0000
    0.7778    1.7778    3.0000    2.2222    1.2222
    0.6667    1.4444    2.3333    1.6667    0.8889
    0.2222    0.4444    0.6667    0.4444    0.2222

This result differs from the previous one because the previous result is for shape=same, while this one is for shape=full. You can easily see that the 9 values in the middle of this result are exactly the shape=same result. Since same returns a matrix of the same size as x, when the bottom-right point of h is aligned with the 5 in x (in other words, when the center of h is aligned with the top-left point 1 of x), the product is (1+2+4+5)*0.1111=1.3333. This is the value of matrix x at x(1,1), and it is also the first value of the same result; the other values follow in the same way.
From this we get (assuming h is p*q and x is m*n):

  • With shape=same, the result is m*n
  • With shape=full, the result is (p+m-1)*(q+n-1)
  • With shape=valid, the borders are not zero-padded, and the result is (m-p+1)*(n-q+1)

Here are the results for the three cases:

>> y=filter2(h,x,'full')

y =
    0.1111    0.3333    0.6667    0.5556    0.3333
    0.5556    1.3333    2.3333    1.7778    1.0000
    0.7778    1.7778    3.0000    2.2222    1.2222
    0.6667    1.4444    2.3333    1.6667    0.8889
    0.2222    0.4444    0.6667    0.4444    0.2222

>> y=filter2(h,x,'same')

y =
    1.3333    2.3333    1.7778
    1.7778    3.0000    2.2222
    1.4444    2.3333    1.6667

>> y=filter2(h,x,'valid')

y =
    3.0000

————————————————
Copyright notice: this is an original article by CSDN blogger 「JMasker」, licensed under CC 4.0 BY-SA. When reposting, please include a link to the original and this notice. Original link: https://blog.csdn.net/JMasker/article/details/81906348

Categories: Matlab

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *