Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute gradient vector field of an image

I want to read in an image - a picture of a circle, and compute the gradient vector field of that image (ie vectors pointing out uniformly and at normal to the circle). My logic is failing me a bit, but I have:

clear all;
im = im2double(imread('littlecircle.png'));
im = double(im);
[nr,nc]=size(im);
[dx,dy] = gradient(im);
[x y] = meshgrid(1:nc,1:nr);
u = x;
v = y;
quiver(x,y,u,v)

if I were to simply do the above, I get a vector field, but it is simply the gradient of an empty mesh (ie just a vector field of the gradient y=x). What I actually want is to use

[dx,dy] = gradient(im);

to detect the edges of the circle in the image, and then compute the gradient vector field due to the circle in the image. obviously, assigning u=x and v=y will only give me the vector field of a straight line - so bascially, I want to embed the gradient of the image into the vectors u and v. How do I do this?

my result

image that i am getting error with

like image 954
brucezepplin Avatar asked May 02 '12 15:05

brucezepplin


People also ask

How do you find the gradient of a vector image?

You can compute the gradient by subtracting left from right or right from left, you just have to be consistent across the image. 93 - 55 = 38 in the y-direction. Putting these two values together, we now have our gradient vector.

What is gradient magnitude of an image?

The gradient magnitude is used to measure how strong the change in image intensity is. The gradient magnitude is a real-valued number that quantifies the “strength” of the change in intensity. The gradient orientation is used to determine in which direction the change in intensity is pointing.

How do you prove a vector field is a gradient field?

If F is the gradient of a function, then curlF = 0. So far we have a condition that says when a vector field is not a gradient. The converse of Theorem 1 is the following: Given vector field F = Pi + Qj on D with C1 coefficients, if Py = Qx, then F is the gradient of some function.

Is the vector field a gradient vector field?

This is a vector field and is often called a gradient vector field. In these cases, the function f(x,y,z) f ( x , y , z ) is often called a scalar function to differentiate it from the vector field.


1 Answers

You have made a mistake in the code (other than that, it works fine). You should replace the following:

u = dx;
v = dy;

not

u = x;
v = y;

It works with this image like a charm!

EDIT: If you want to super-impose the vectors on the image, then do the following:

clear all;
im = imread('littlecircle.png');
[nr,nc]=size(im);
[dx,dy] = gradient(double(im));
[x y] = meshgrid(1:nc,1:nr);
u = dx;
v = dy;
imshow(im);
hold on
quiver(x,y,u,v)

Notice that i do not convert the im to double, since it would not appear correctly with imshow (needs uint8). Depending on your image dimensions, you might want to zoom in in order to see the grad vectors.

You can see a zoomed in area of the vectors superimposed on the image, below:

Gradient vectors of a circle in an image

Better quality image is at http://i.stack.imgur.com/fQbwI.jpg

like image 67
Jorge Avatar answered Sep 20 '22 14:09

Jorge