what is the difference between conv2, filter2 and imfilter..?
43 次查看(过去 30 天)
显示 更早的评论
I am confused with the way these three algorithms works. which function should i use for gaussian, prewitt etc. filters??
0 个评论
采纳的回答
David Young
2011-10-5
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.
3 个评论
David Young
2011-10-6
I'll have to check the current version. In 2010b, convolve2 (which uses the SVD to expoit any rank-deficient kernel, not just separable ones) was faster than conv2 on many kernels. In addition, filter2 explicitly codes a test for separability before calling conv2, which suggests that conv2 doesn't do this. I seem to remember that I've found previously found imfilter slow compared to the other routines, but I've never looked at that systematically.
You will find some systematic comparisons between some of the other functions, under 2010b, here: http://www.mathworks.co.uk/matlabcentral/answers/5011-what-is-the-fastest-way-to-do-2-d-convolution-in-matlab
更多回答(2 个)
Alex Taylor
2011-10-7
Hi Jigar,
Good question, and everyone else has provided a lot of great information. I'd like to add a bit, hopefully it'll be useful.
1) imfilter performs correlation by default. However, it will also return the convolution result if you supply 'conv' as an optional argument:
out = imfilter(a,h,'conv');
2) By default, conv2 gives the full filtered result. This is different than the default behavior of imfilter, which yields the output as the same size as the input. Both conv2 and imfilter provide optional arguments which can be used to get either result, which is to say:
imfilter(a,h,'conv')
is the same as
conv2(a,h,'same')
within double precision floating point error.
3) I'll give a few rules of thumb about performance based on the current state of the code. Both imfilter and conv2 are hardware optimized and multi-threaded. For floating point inputs, the performance of both will be roughly equivalent. For integer input images, imfilter will be faster because it has integer optimized code paths. For linearly separable filter kernels, imfilter does separation of the kernel into row and column vectors, as other people have pointed out. I'm not sure whether conv2 performs this optimization.
Hope this helps,
Alex.
1 个评论
Chris Turnes
2017-8-11
To add one bit of further clarity, conv2 will not analyze an input to determine if a kernel is separable and then decompose it into the separable parts. However, if you know in advance that the kernel is separable, you can use the three-input syntax conv2(u,v,A), which indeed performs a separable convolution.
Sean de Wolski
2011-10-5
TMW recommends imfilter if you have the IPT and are working with images. Personally, I only use conv2 since I hate the conversion to uint8 and often want (at least temporary) values above 255.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!