Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different results using imfilter and conv2

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');
like image 688
Sepideh Abadpour Avatar asked Jun 28 '13 15:06

Sepideh Abadpour


2 Answers

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.

like image 59
Hugh Nolan Avatar answered Oct 04 '22 17:10

Hugh Nolan


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.

like image 37
Zaphod Avatar answered Oct 04 '22 17:10

Zaphod