MATLAB: What is the difference between conv2, filter2 and imfilter..

convolutionfilterImage Processing ToolboxMATLAB

I am confused with the way these three algorithms works. which function should i use for gaussian, prewitt etc. filters??

Best Answer

filter2 and conv2 do almost the same thing: the results are related like this:
conv2(image, mask) is the same as filter2(rot90(mask,2), image)
The relationship can also be stated by saying that filter2 implements correlation, whilst conv2 implements convolution.
In addition, filter2 implements an optimisation for the case of separable masks. This shouldn't affect significantly the numerical values of the results, just the speed.
The fex contribution convolve2 implements a stronger set of optimisations. There's a comparison here.
imfilter does the same as conv2/filter2/convolve2 but with lots of extra features, such as handling different data types, handling colour images, switching the algorithm between correlation and convolution, and offering more options than conv2 and filter2 for what happens at the image boundaries.
Bottom line: if you are happy to work with data of type double (MATLAB's default) use conv2. If it's slow try convolve2 to see if it will speed it up. If you need the extra features, use imfilter.