5

Based on this question and this one I thought that "imfilter" and "conv2" should have the same results. But try this code you will see the differences. What is the problem?

I = imread('tire.tif');  
fil=[1 2 3;4 5 6;7 8 9];  
out1=conv2(double(I),fil,'same');  
out2=uint8(out1);  
out3=imfilter(I,fil,'same');
Community
  • 1
  • 1
Sepideh Abadpour
  • 2,550
  • 10
  • 52
  • 88

2 Answers2

10

If you use imfilter(I,fil,'same','conv') then they are the same.

The difference is that imfilter uses correlation to filter images by default, which has some small differences - basically, convolution starts from one side of the image, whereas correlation starts from the other, so there is some small differences in the filter output. If you flip the image first, you get the same output:

out4=fliplr(flipud(imfilter(fliplr(flipud(I)),fil,'same')));

This is exactly equal to out2.

Hugh Nolan
  • 2,508
  • 13
  • 15
  • Thanks @Hugh Nolan. I think that you should replace 'save' with 'same'. and one question rose up. You see in [this question](http://stackoverflow.com/questions/10672184/implementing-imfilter-in-matlab) [petrichor](http://stackoverflow.com/users/198428/petrichor) hasn't used 'conv' but his answer has been accepted by the questioner!!! – Sepideh Abadpour Jun 28 '13 at 16:32
  • If your're familiar with image processing, you'll know that spacial linear filters are convolution masks in fact. But matlab's default for a function that applies linear spatial filters is correlation. I studied it right now in the documentation!!!!!!!!!!!!! – Sepideh Abadpour Jun 28 '13 at 16:41
  • 1
    Thanks, fixed typo. In the link you posted, the filter used is symmetric, so the output will be the same. You can also flip the filter instead of the image, as is mentioned in that answer. – Hugh Nolan Jun 28 '13 at 16:44
  • You can also flip the filter instead of the image, as is mentioned in that answer!!!!!!!!!!!!!!!! Where is it mentioned? – Sepideh Abadpour Jun 28 '13 at 17:02
  • English is not my native language. What do you mean here by flip? I searched the dictionary. It means something like moving the filter over the image? What do you mean here by flipping? In filtering we usually move the filter around the image!!! what's the purpose of flipping here? guide me alittle,please. – Sepideh Abadpour Jun 28 '13 at 17:10
  • 1
    I mean mirror the filter kernel, i.e. instead of (as in my answer), using `fliplr` and `flipud` on the image `I`, you can use it on `fil` instead. You could write `out4=imfilter(I,fil(end:-1:1,end:-1:1),'same');` – Hugh Nolan Jun 28 '13 at 20:43
3

Your answer lies in the explanation of the fourth input argument to imfilter.

  • Correlation and convolution
    'corr'       imfilter performs multidimensional filtering using
                 correlation, which is the same way that FILTER2
                 performs filtering.  When no correlation or
                 convolution option is specified, imfilter uses
                 correlation.

    'conv'       imfilter performs multidimensional filtering using
                 convolution.

Try out3=imfilter(I,fil,'same','conv'); and you'll get identical results to conv2.

Zaphod
  • 1,927
  • 11
  • 13