MATLAB: What is the difference between conv2 and imfilter

conv2convolutionimage processingImage Processing Toolboximfilter

Hello ,
i am trying to convolute an image (512×512) with a filter (3×3) .For this process i am using both covv2() and imfilter() . These functions are supposed to do the same but after the convolution in the first case(conv2) i am getting a new image 516×516 and in the second case (imfilter) i am getting a new image 514×514 .Why is this happening?

Best Answer

The two main reasons. One is that imfilter has options to not flip the filter window like convolution does (the correlation option), or to flip it like convolution does. If your fliter is radially symmetric or uniform, there is no difference.
The second reason is how they handle edge effects. imfilter() has a lot more options for what to do when the filter window gets near ("falls off") the edge of the image. But imfilter() returns an image of the same size and class as the input whereas conv2() returns a floating point image, but has options to give the "full" size convolution, which gives values even when the center of the filter is off the image but some parts of the filter still overlap the image.