How to find the Direction (angle) of Points on an Edge in a Digital Image

9 vues (au cours des 30 derniers jours)
Hi,
I'm working on a problem, which needs the direction or angle at every point on edges of an image. I tried to get the edge direction with the following code. But, it seems that I'm not getting direction or angle of each point on the edges. A good amount of direction information is lost.
I=imread('Lenna.png');
G=rgb2gray(I);
[BW] = edge(G,'canny');
[~,gdir]=imgradient(G);
[m,n,r]=size(I);
for i=1:m
for j=1:n
if (BW(i,j)==0)
gdir(i,j)=0;
end
end
end
figure;
imshow(BW);
figure;
imshow(gdir);
I tried a different method by getting gradient in x and y directions. But this method gives a similar result to the first one. Still no luck with getting angles of all points on edges.
I=imread('Lenna.png');
G=rgb2gray(I);
[BW] = edge(G,'canny');
[Gx, Gy] = imgradientxy(G);
theta = atan2(Gy, Gx);
[m,n,r]=size(I);
for i=1:m
for j=1:n
if (BW(i,j)==0)
theta(i,j)=0;
end
end
end
figure;
imshow(BW);
figure;
imshow(theta);
Can somebody please suggest a better method to do this.
Thanks in advance...

Réponse acceptée

Sean de Wolski
Sean de Wolski le 1 Oct 2013
Randima, you're in luck. I've been working on a tool for this and I finally polished it on the plane last week.
BW = your_binary_edge_image;
Orientations = skeletonOrientation(BW,5); %5x5 box
Onormal = Orientations+90; %easier to view normals
Onr = sind(Onormal); %vv
Onc = cosd(Onormal); %uu
[r,c] = find(BW); %row/cols
idx = find(BW); %Linear indices into Onr/Onc
imshow(BW,[]);
%Overlay normals to verify
hold on
quiver(c,r,-Onc(idx),Onr(idx));
skeletonOrientation is attached. It'll find its way onto the FEX at some point but first I need to figure out what else I want it to give as outputs, indices, normals etc..
  5 commentaires
Dipak Majhi
Dipak Majhi le 8 Juin 2015
the code is amazing. thanks a lot for it. can you please change the orientations range in positive angles [0,360] ?
David Mandel
David Mandel le 31 Jan 2021
wow, this code is amazing! thanks a lot for sharing it on this platform
i'm currently working in my bachelors thesis, writing an edge detection algorihtm using local contrasts and been stuck on determining the actual thickness (and therefor orientation) of my detected edges, with this it became almost too easy :D

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 30 Sep 2013
Modifié(e) : Image Analyst le 30 Sep 2013
Not sure what you're looking for. The variables gdir and theta are your angles. Why do you say you don't have them?
  3 commentaires
Image Analyst
Image Analyst le 1 Oct 2013
I'm not sure you understand what edge is doing. First of all it's thresholding at some sort of arbitrary level so of course there could be breaks in the outlines compared to what you would draw with your eye, which can easily jump across breaks because of your higher level knowledge. Then, edge does not give continuous contours like the contour function would give. So now, let's just jump to the larger context. WHY do you want the edge directions? Let's pretend for a moment that you could get them. Then what? What would you do with that information?
Randima Hettiarachchi
Randima Hettiarachchi le 1 Oct 2013
I do understand your point. The problem I had was gdir being discontinuous at points where edge function is continuous. I was hoping gdir to be consistent with the edge function. I was using imtool function to compare points on the edges and gdir. Later I found that imtool has a coordinate system (col,row) different to the coordinate system used to represent images (row,col). After putting the correct coordinate system, I found that although gdir seems discontinuous, direction information is preserved. So I have directions available at each point on edges. I'm working on a pattern recognition problem and getting the edge directions is a preliminary phase of that. Thanks a lot for helping me. I greatly appreciate that.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by