Reputation: 9752
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?
Upvotes: 17
Views: 23526
Reputation: 784
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:
Better quality image is at https://i.sstatic.net/fQbwI.jpg
Upvotes: 15