Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
conv2(..) fault ???

Subject: conv2(..) fault ???

From: Le Tuan

Date: 12 Mar, 2012 06:34:15

Message: 1 of 7

I write:
I=[-5 5 0 5;
     5 5 0 0;
     0 5 0 5;
     5 5 5 5]

h=[-1 -1 -1;
       0 0 0;
       1 1 1]

c=conv2(I,h,'valid')

and output of c is:
-5 0
-5 -10
c(1,2), c(2,1), c(2,2) is right results. But why c(1,1)=-5, I think it must be 5 ???

Subject: conv2(..) fault ???

From: Roger Stafford

Date: 12 Mar, 2012 07:28:13

Message: 2 of 7

"Le Tuan" wrote in message <jjk5d7$ecs$1@newscl01ah.mathworks.com>...
> I write:
> I=[-5 5 0 5;
> 5 5 0 0;
> 0 5 0 5;
> 5 5 5 5]
>
> h=[-1 -1 -1;
> 0 0 0;
> 1 1 1]
>
> c=conv2(I,h,'valid')
>
> and output of c is:
> -5 0
> -5 -10
> c(1,2), c(2,1), c(2,2) is right results. But why c(1,1)=-5, I think it must be 5 ???
- - - - - - - - -
  I disagree, c(1,1) = -5 looks right to me:

 (-5)*1+5*1+0*1+5*0+5*0+0*0+0*(-1)+5*(-1)+0*(-1) = -5

Roger Stafford

Subject: conv2(..) fault ???

From: Le Tuan

Date: 12 Mar, 2012 08:09:11

Message: 3 of 7

"Roger Stafford" wrote in message <jjk8id$nbh$1@newscl01ah.mathworks.com>...
> "Le Tuan" wrote in message <jjk5d7$ecs$1@newscl01ah.mathworks.com>...
> > I write:
> > I=[-5 5 0 5;
> > 5 5 0 0;
> > 0 5 0 5;
> > 5 5 5 5]
> >
> > h=[-1 -1 -1;
> > 0 0 0;
> > 1 1 1]
> >
> > c=conv2(I,h,'valid')
> >
> > and output of c is:
> > -5 0
> > -5 -10
> > c(1,2), c(2,1), c(2,2) is right results. But why c(1,1)=-5, I think it must be 5 ???
> - - - - - - - - -
> I disagree, c(1,1) = -5 looks right to me:
>
> (-5)*1+5*1+0*1+5*0+5*0+0*0+0*(-1)+5*(-1)+0*(-1) = -5
>
> Roger Stafford
I don't understand.
According to:
http://docs.gimp.org/en/plug-in-convmatrix.html
I think c(1,1)=(-5)*(-1)+5*(-1)+0*(-1)+5*0+5*0+0*0+0*1+5*1+0*1=5

Subject: conv2(..) fault ???

From: Bruno Luong

Date: 12 Mar, 2012 08:59:12

Message: 4 of 7

"Le Tuan" wrote in message <jjkav6$18b$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jjk8id$nbh$1@newscl01ah.mathworks.com>...

> I don't understand.
> According to:
> http://docs.gimp.org/en/plug-in-convmatrix.html
> I think c(1,1)=(-5)*(-1)+5*(-1)+0*(-1)+5*0+5*0+0*0+0*1+5*1+0*1=5

This doc is not Matwork, conv() flips the second array before multiplication and summming.

Read mathworks doc instead.

Bruno

Subject: conv2(..) fault ???

From: Le Tuan

Date: 12 Mar, 2012 13:58:12

Message: 5 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jjkdt0$9f0$1@newscl01ah.mathworks.com>...
> "Le Tuan" wrote in message <jjkav6$18b$1@newscl01ah.mathworks.com>...
> > "Roger Stafford" wrote in message <jjk8id$nbh$1@newscl01ah.mathworks.com>...
>
> > I don't understand.
> > According to:
> > http://docs.gimp.org/en/plug-in-convmatrix.html
> > I think c(1,1)=(-5)*(-1)+5*(-1)+0*(-1)+5*0+5*0+0*0+0*1+5*1+0*1=5
>
> This doc is not Matwork, conv() flips the second array before multiplication and summming.
>
> Read mathworks doc instead.
>
> Bruno

Please, excuse me. What mean of your "flip" ? Can you give me an example ?
Sorry because my English is very bad !

Subject: conv2(..) fault ???

From: Matt J

Date: 12 Mar, 2012 18:56:13

Message: 6 of 7

"Le Tuan" wrote in message <jjkvdk$4vg$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jjkdt0$9f0$1@newscl01ah.mathworks.com>...
> > "Le Tuan" wrote in message <jjkav6$18b$1@newscl01ah.mathworks.com>...
> > > "Roger Stafford" wrote in message <jjk8id$nbh$1@newscl01ah.mathworks.com>...
> >
> > > I don't understand.
> > > According to:
> > > http://docs.gimp.org/en/plug-in-convmatrix.html
> > > I think c(1,1)=(-5)*(-1)+5*(-1)+0*(-1)+5*0+5*0+0*0+0*1+5*1+0*1=5
> >
> > This doc is not Matwork, conv() flips the second array before multiplication and summming.
> >
> > Read mathworks doc instead.
> >
> > Bruno
>
> Please, excuse me. What mean of your "flip" ? Can you give me an example ?
> Sorry because my English is very bad !

Flip means the order of the samples of h are reversed before the sliding operation is done. Remember the definition of convolution for 1D

out(j)=sum_i l(i)h(j-i)

As i changes, it causes the time-reversed kernel h(-k) to slide over your samples l(i). In 2D, it is similar. What you seem to want is for the non-time reversed kernel h(k) to do the sliding, as in

out(j)=sum_i l(i)h(i-j)

Note that if you use IMFILTER, instead of CONV2, you are given the option of flipping the kernel or not. The'conv' option will flip the kernel, but the 'corr' option will not.

Subject: conv2(..) fault ???

From: Roger Stafford

Date: 12 Mar, 2012 19:15:13

Message: 7 of 7

"Le Tuan" wrote in message <jjkvdk$4vg$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jjkdt0$9f0$1@newscl01ah.mathworks.com>...
> > "Le Tuan" wrote in message <jjkav6$18b$1@newscl01ah.mathworks.com>...
> > > "Roger Stafford" wrote in message <jjk8id$nbh$1@newscl01ah.mathworks.com>...
> >
> > > I don't understand.
> > > According to:
> > > http://docs.gimp.org/en/plug-in-convmatrix.html
> > > I think c(1,1)=(-5)*(-1)+5*(-1)+0*(-1)+5*0+5*0+0*0+0*1+5*1+0*1=5
> >
> > This doc is not Matwork, conv() flips the second array before multiplication and summming.
> >
> > Read mathworks doc instead.
> >
> > Bruno
>
> Please, excuse me. What mean of your "flip" ? Can you give me an example ?
> Sorry because my English is very bad !
- - - - - - - -
  What Bruno means by "flip" is what the matlab 'flipud' and 'fliplr' functions do. Look at this example:

 a = [1,2;3,4]; b = [11,12;13,14];
 c = conv2(a,b,'valid');

The result is c = 1*14+2*13+3*12+4*11 = 120.

  It's the same as doing sum(sum(a.*flipud(fliplr(b)))). Read the Mathworks documentation for 'conv2' carefully.

Roger Stafford

Tags for this Thread

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.

Contact us