4

I use this both function to find edges on a scale. You have a input image, you apply a mask (for ex. prewitt) to the input image, and get the resultant pic.

mypic = imread('examplepic.jpg')
hy = fspecial('prewitt')
yimfilter = imfilter(mypic,hy) % Using imfilter
yconv2 = conv2(mypic,hy) % Using conv2

Which is the theorical difference between these two? I know I got different outputs, but which is the difference?

Thanks

user981227
  • 155
  • 2
  • 6
  • 12

2 Answers2

9

conv2 outputs the entire 2-D convolution, which means that yconv2 will be bigger than mypic. imfilter, on the other hand, by default trims the edges of the convolution so that yimfilter should be the same size as mypic. You can make imfilter leave the entire convolution like conv2 does, but that is not its default behavior.

There are other differences: imfilter's "replicate" option, imfilter can do convolution on arbitrary numbers of dimensions (not just 2), and so on, but I don't think you were asking about that.

Jim Clay
  • 963
  • 9
  • 24
0

Well, imfilter by default uses correlation, not convolution. If you call

yimfilter = imfilter(mypic,hy,'conv')

then yconv2 and yimfilter will be the same. As for the difference between correlation and convolution, well you can see it quite easily if you use a 1D convolution/correlation mask. The output will be the same, just shifted of a row/column (depending on the direction of the mask).

By the way, if you call

yimfilter = imfilter(mypic,hy)
yfilter2 = filter2(hy,mypic)

you'll find that yimfilter and yfilter2 are the same, because filter2 also uses correlation.

Roberto
  • 267
  • 4
  • 10