Thread Subject: conv2 the same as filter2 ?

Subject: conv2 the same as filter2 ?

From: nor ki

Date: 21 Nov, 2007 09:27:21

Message: 1 of 6

Hello,

seeking for a fast convolution in spatial domain where i
just need the valid parts i tested conv2 and i found out
that the speed is the same with the options 'valid' and
'same'. This should not be the case if the kernel is nearly
as large as the image which is convoluted.
As the help on the filter2 command mentions that filter2
uses conv2 to calculate and then just extracts the central
part I wonder if there is any diffrence between the two commands
or if there are just two identical commands? or am I missing
something?

confused kinor

this is the testcode, i thought the second one must be much
faster as i only ask for 11x11 results but both take about
10 seconds

l = 80;
siz = 90;
anz = 100;
img = single(randn(siz)*4000);
kern = img(1:l,1:l);
kern = double(kern);
tic
for n = 1:anz
    res1 = conv2(img, kern, 'same');
end
toc

tic
for n = 1:anz
    res1 = conv2(img, kern, 'valid');
end
toc

Subject: conv2 the same as filter2 ?

From: Sachitha Obeysekara

Date: 16 Jul, 2008 15:40:23

Message: 2 of 6

> part I wonder if there is any diffrence between the two
commands

..essentially they are the same. filter2 checks to see if
the 2D filter given to it is seperable and then uses conv2
using the seperated fiters (faster); if not it uses conv2
straight away (slower)

try: edit filter2 in Matlab
and have a look at
http://blogs.mathworks.com/steve/2006/10/04/separable-convolution/

Regards,
Sach

Subject: conv2 the same as filter2 ?

From: Sachitha Obeysekara

Date: 16 Jul, 2008 15:41:03

Message: 3 of 6

> part I wonder if there is any diffrence between the two
commands

..essentially they are the same. filter2 checks to see if
the 2D filter given to it is seperable and then uses conv2
using the seperated fiters (faster); if not it uses conv2
straight away (slower)

try: edit filter2 in Matlab
and have a look at
http://blogs.mathworks.com/steve/2006/10/04/separable-convolution/

Regards,
Sach

Subject: conv2 the same as filter2 ?

From: Robert Pollak

Date: 28 Aug, 2010 16:35:04

Message: 4 of 6

So to get back to the original post:
> seeking for a fast convolution in spatial domain where i
> just need the valid parts i tested conv2 and i found out
> that the speed is the same with the options 'valid' and
> 'same'. This should not be the case if the kernel is nearly
> as large as the image which is convoluted.

I also would need 'valid' to be faster. (In my use case, it's only a 1x1 result.)
Is there any way? Spelling out the convolution in Matlab code would certainly be even slower, wouldn't it?

Regards,
Robert

Subject: conv2 the same as filter2 ?

From: Bruno Luong

Date: 28 Aug, 2010 18:48:03

Message: 5 of 6

"Robert Pollak" <robert.pollak@jku.at> wrote in message <i5bdro$do$1@fred.mathworks.com>...
> So to get back to the original post:
> > seeking for a fast convolution in spatial domain where i
> > just need the valid parts i tested conv2 and i found out
> > that the speed is the same with the options 'valid' and
> > 'same'. This should not be the case if the kernel is nearly
> > as large as the image which is convoluted.
>
> I also would need 'valid' to be faster. (In my use case, it's only a 1x1 result.)

Why not take the sum of the element-wise product of two arrays then (flip one of them if necessary)????

Bruno

Subject: conv2 the same as filter2 ?

From: Robert Pollak

Date: 30 Aug, 2010 07:50:05

Message: 6 of 6

> Why not take the sum of the element-wise product of two arrays then (flip one of them if necessary)????

Good question, Bruno - thank you! My C++ half of the brain seems to have taken over :)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
conv2 speed Robert Pollak 28 Aug, 2010 12:39:08
filter Sachitha Obeysekara 16 Jul, 2008 11:45:12
seperable Sachitha Obeysekara 16 Jul, 2008 11:45:12
conv2 Sachitha Obeysekara 16 Jul, 2008 11:45:11
filter2 Sachitha Obeysekara 16 Jul, 2008 11:45:11
conv2 filter2 nor ki 21 Nov, 2007 04:30:03
rssFeed for this Thread

Contact us at files@mathworks.com