What is the difference between conv2 and imfilter?
18 views (last 30 days)
Show older comments
Commented: Gn Gnk on 14 Oct 2020
i am trying to convolute an image (512x512) with a filter (3x3) .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 516x516 and in the second case (imfilter) i am getting a new image 514x514 .Why is this happening?
Image Analyst on 14 Oct 2020
Edited: Image Analyst on 14 Oct 2020
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.
More Answers (1)
Bjorn Gustavsson on 14 Oct 2020
The first question is very simple to answer, conv2 has as its default behaviour:
'full' - (default) returns the full 2-D convolution,
That is with sizes of your image of szIm1, and szIm2 and size of the filter szK1 and szK2 the result of conv2 shold be:
[szIm1 + 2*(szK1-1), szIm2 + 2*(szK2-1)] i.e. [512 + 2*(3-1), 512 + 2*(3-1)]. Typically I call conv2 with the 'same' shape-argument, which returns a result with the same size as the input-array.
Regarding imfilter your result is very confusing, the default shape-argument is 'same' which should return an image with the same size as the input-image, of 'full' which to the best of my understanding in your case should return an array with the size of 516x516, why it doesn't is difficult to speculate about. Have a look at the help and documentation of both functions.
Find more on Image Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!