can any one tell me how can I find the number of pixel between two point on a closed contour? I know that there are two path between two point on a closed contour. i need to find the shortest path. please help me. I think a lot but got no solution
1 view (last 30 days)
Show older comments
can any one tell me how can I find the number of pixel between two point on a closed contour? I know that there are two path between two point on a closed contour. i need to find the shortest path. please help me. I think a lot but got no solution
0 Comments
Answers (2)
Walter Roberson
on 7 Jul 2016
José-Luis's response is interesting and might be all you need. But in general, the path along curved contours defined by pixels is often ambiguous. Do you follow the inside of the curve? Do you follow the outside? Do you calculate pixel center to pixel center or do you interpolate?
A*
B
A to B is two over and 1 down so sqrt(5) Euclidean but it had to be approximated due to the coarseness of pixels. Do you calculate it as 1 + sqrt(2) -- that is, one over plus (1 over and 1 down = sqrt(2)) ?
For a curve you will often see multiple adjacent pixels like
****
**
**
***
** *****
****
There can be both horizontal and vertical "duplicates" at any given location, as needed to quantize the curve to pixels. Which pixels do you choose to follow and in which order? José-Luis's answer follows them in the order stored, which might not be the best solution depending on the problem at hand.
There is a different approach that can be used rather than José-Luis's: you can make use of http://www.mathworks.com/help/images/ref/bwdistgeodesic.html . Depending on what you are doing, you might also find graydist to be of interest.
3 Comments
Walter Roberson
on 7 Jul 2016
That contour is not closed.
In any case, bwdistgeodesic should work for that.
José-Luis
on 7 Jul 2016
%random contour, first point is also last point
c = rand(100,2);
c(:,1) = c(:,end);
%index for random starting and ending point
idx = sort(randsample(99,2));
startP = idx(1);
endP = idx(2);
%Computing the distance between successive points
distance2 = diff(c);
distance2 = [sum(distance2.*distance2,2); 0];
total_length = sum(distance2);
%Computing ditance of both paths
sub_distance1 = sum(distance2(startP:endP-1)) %Will be zero if startP == endP
sub_distance2 = total_length - sub_distance1
Voila.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!