How to find the Direction (angle) of Points on an Edge in a Digital Image
9 views (last 30 days)
Show older comments
Randima Hettiarachchi
on 30 Sep 2013
Commented: David Mandel
on 31 Jan 2021
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...
0 Comments
Accepted Answer
Sean de Wolski
on 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 Comments
Dipak Majhi
on 8 Jun 2015
the code is amazing. thanks a lot for it. can you please change the orientations range in positive angles [0,360] ?
David Mandel
on 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
More Answers (1)
Image Analyst
on 30 Sep 2013
Edited: Image Analyst
on 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 Comments
Image Analyst
on 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?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!