Differences between 'imfilter' and 'conv2' [MATLAB]

人盡茶涼 提交于 2019-12-07 10:56:58

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7909466/differences-between-imfilter-and-conv2-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!