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:
question about gradient function

Subject: question about gradient function

From: arron

Date: 16 Apr, 2012 19:47:08

Message: 1 of 10

Hi, just a simple question as I'm confusing myself with vectors on matlab. If I take:

[dx,dy] = gradient(I);

are dx and dy vectors? I am looking to compute directional derivatives.....

Thanks in advance!

Subject: question about gradient function

From: Matt J

Date: 16 Apr, 2012 20:08:07

Message: 2 of 10

"arron" wrote in message <jmhsvs$83f$1@newscl01ah.mathworks.com>...
> Hi, just a simple question as I'm confusing myself with vectors on matlab. If I take:
>
> [dx,dy] = gradient(I);
>
> are dx and dy vectors? I am looking to compute directional derivatives.....


[dx(i,j), dy(i,j)] will be the gradient at pixel (i,j)

Subject: question about gradient function

From: Roger Stafford

Date: 16 Apr, 2012 20:14:06

Message: 3 of 10

"arron" wrote in message <jmhsvs$83f$1@newscl01ah.mathworks.com>...
> Hi, just a simple question as I'm confusing myself with vectors on matlab. If I take:
>
> [dx,dy] = gradient(I);
>
> are dx and dy vectors? I am looking to compute directional derivatives.....
>
> Thanks in advance!
- - - - - - - -
  No, with the form you have used, 'I' must be a two-dimensional array and your 'dx' and 'dy' will be the same size. These are 2nd order (except at the boundaries) approximations to the first partial derivatives with respect to x and to y of the function which 'I' represents in discrete form over the field covered by the grid of 'I'.

Roger Stafford

Subject: question about gradient function

From: arron

Date: 16 Apr, 2012 21:18:07

Message: 4 of 10

Thanks both for your replies. So if I do:

I = imread('Image.png');
I = I(:,:,1);
I = double(I);
i = size(I,1);
j = size(I,2);
[dx(i,j), dy(i,j)] = gradient(I(i,j));

How do I set up i,j to be the image planes of each pixel? I am getting an error message saying:

Error in gradient at 49.

Thanks again.

Subject: question about gradient function

From: Matt J

Date: 16 Apr, 2012 21:49:06

Message: 5 of 10

"arron" wrote in message <jmi2af$t0k$1@newscl01ah.mathworks.com>...
> Thanks both for your replies. So if I do:
>
> I = imread('Image.png');
> I = I(:,:,1);
> I = double(I);
> i = size(I,1);
> j = size(I,2);
> [dx(i,j), dy(i,j)] = gradient(I(i,j));
===================

No, you're meant to do

[dx,dy]=gradient(I);

as before. Then, if you're interested in a particular pixel at coordinate (i,j) where i and j are scalar integers, then you could do

gradient_ij=[dx(i,j), dy(i,j)];


> How do I set up i,j to be the image planes of each pixel?

Define "image planes of each pixel".

Subject: question about gradient function

From: arron

Date: 16 Apr, 2012 22:26:07

Message: 6 of 10

"Matt J" wrote in message <jmi44i$655$1@newscl01ah.mathworks.com>...
> "arron" wrote in message <jmi2af$t0k$1@newscl01ah.mathworks.com>...
> > Thanks both for your replies. So if I do:
> >
> > I = imread('Image.png');
> > I = I(:,:,1);
> > I = double(I);
> > i = size(I,1);
> > j = size(I,2);
> > [dx(i,j), dy(i,j)] = gradient(I(i,j));
> ===================
>
> No, you're meant to do
>
> [dx,dy]=gradient(I);
>
> as before. Then, if you're interested in a particular pixel at coordinate (i,j) where i and j are scalar integers, then you could do
>
> gradient_ij=[dx(i,j), dy(i,j)];
>
>
> > How do I set up i,j to be the image planes of each pixel?
>
> Define "image planes of each pixel".

I guess i just want to take the image co-ordinates of the vector field. this link is pretty much what i want to achieve including the orientation of the boundary.

http://opencv-users.1802565.n2.nabble.com/gradients-amp-angles-td2191671.html

Subject: question about gradient function

From: arron

Date: 16 Apr, 2012 22:33:08

Message: 7 of 10

also when i try what you have said, i get:

subscript indices must be real positive integers or logicals. How do I define the image in terms of i and j?

I'm getting there slowly, your help has been great.

Subject: question about gradient function

From: Matt J

Date: 17 Apr, 2012 15:41:07

Message: 8 of 10

"arron" wrote in message <jmi6n4$f6j$1@newscl01ah.mathworks.com>...
> also when i try what you have said, i get:
>
> subscript indices must be real positive integers or logicals.
============

If you've set i and j to values that are not real positive integers, it makes sense that you would get an error when trying to subscript with them.

Subject: question about gradient function

From: ike

Date: 2 May, 2012 07:48:06

Message: 9 of 10

"Matt J" wrote in message <jmk2uj$jsb$1@newscl01ah.mathworks.com>...
> "arron" wrote in message <jmi6n4$f6j$1@newscl01ah.mathworks.com>...
> > also when i try what you have said, i get:
> >
> > subscript indices must be real positive integers or logicals.
> ============
>
> If you've set i and j to values that are not real positive integers, it makes sense that you would get an error when trying to subscript with them.

how can i get gradient direction from every pixel ?
so how can i select pixels randomly ?

Subject: question about gradient function

From: Nasser M. Abbasi

Date: 2 May, 2012 08:09:55

Message: 10 of 10

On 05/02/2012 02:48 AM, ike wrote:

> how can i get gradient direction from every pixel ?
> so how can i select pixels randomly ?

I'll answer the easy one. The second one.

to select a pixel by random, suppose you have an image that is 40 pixels
by 30 pixels. The to access a pixel by random, you can try

   myImage(ceil(40*rand), ceil(30*rand))

see if that works for you.

--Nasser

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