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');
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With