what is the difference between conv2, filter2 and imfilter..?
Ältere Kommentare anzeigen
I am confused with the way these three algorithms works. which function should i use for gaussian, prewitt etc. filters??
Akzeptierte Antwort
Weitere Antworten (2)
Alex Taylor
am 7 Okt. 2011
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 Kommentar
Chris Turnes
am 11 Aug. 2017
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
am 5 Okt. 2011
0 Stimmen
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.
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!