You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
i want to calculate the perpendicular distance between one point on line (A) and another on line B
2 views (last 30 days)
Show older comments
I want to be able to find the maximum and minimum distance in the given image that I worked on using a couple of filters and got it's boundaries using bwboundaries(). I have the x and y positions of all the points. I also have the distance between two points on the same line.
Last thing . . . is there a way to show the perpendicular line that is being measured between the two points?
Thank you in advance

Accepted Answer
Image Analyst
on 8 Jun 2018
Sure. If you have the stats toolbox, simply use pdist2():
distances = pdist2(xy1, xy2);
19 Comments
mohammed radwan
on 8 Jun 2018
Edited: mohammed radwan
on 8 Jun 2018
pdist gives the euclidean distance , i'm trying to figure the perpendicular euclidean distance, The line from a point on line A has to be perpendicular to the other point on line B, Thank you.
Image Analyst
on 8 Jun 2018
The minimum of all the distances in the array will be as perpendicular as you can get. Just think about it? Does that not make sense to you? How could it not? If it were not, then some other point would be the perpendicular line endpoints, thus contradicting that the pair of points was the min. So the min of the distances IS the closest to perpendicular you're going to get.
mohammed radwan
on 8 Jun 2018
yes i agree with you on this point but there's still a problem with the maximum perpendicular distance ?, cause in case of just maximum distance then i get it ,it will be the distance between the first point in one line and the last point in the other line...
Image Analyst
on 8 Jun 2018
That's trickier. You have to get a binary image of the portion between the two lines, then skeletonize it with bwmorph(). Then traverse that line fitting a section of it to a line, then taking minus one over the slope of that line, then go out from the skeleton until you hit a boundary point.
mohammed radwan
on 8 Jun 2018
you gave me a hint , since that the smallest distance has to be perpendicular , i could get all the smallest distances between all the points,which means i'll have all the perpendicular distances in the binary image all that would be left is to look for biggest smallest distance which will be my maximum perpendicular distance, correct ??
Image Analyst
on 9 Jun 2018
No. There will only be one smallest distance. The biggest distance using pdist2() will be from one end of the line to the opposite end of the other line. It will not be the longest perpendicular distance along the centerline. To do that you'd have to use the algorithm I outlined.
mohammed radwan
on 9 Jun 2018
yes i understand , but i mean i'll get the smallest distance of each point alone and save them in a new array , doing two for loops at which i'll take the first x,y from the 1st line and get it's smallest distance and save that and then take the 2nd x,y and get it's smallest distance and so on ,which will give me all the perpendicular distance of all the coordinates and save them in a new array then i'll be free to choose the maximum and minimum of that new array
mohammed radwan
on 9 Jun 2018
Edited: mohammed radwan
on 9 Jun 2018
if true
% code
end
i=1:LL
x1=arxxai(i);
y1=aryyai(i);
for j=1:LL
x2=axxai2(j);
y2=ayyai2(j);
distance =pdist2([x1 y1],[x2 y2],'euclidean','smallest',LL);
distance(j)=distance;
end
end
mohammed radwan
on 9 Jun 2018
my problem now if this code is correct is how to save all the new distances in a new array
Image Analyst
on 9 Jun 2018
Have a loop where you loop over xy1, and xy1 is just one point of the one line, and xy2 is all the points of the second line. Attach your data in a .mat file with the paper clip icone if you still can't figure it out.
mohammed radwan
on 9 Jun 2018
my code is all over the place now but the above code is where i have the problem , if you can create any two arrays then apply this code and fix the problem i have , where x1 and y1 are for the first line and x2,y2 are for the 2nd line
mohammed radwan
on 9 Jun 2018
ok after reading pdist2 function in matlab , i thought that writing smallest means that it returns the smallest distances of what it measured, turns out it just arranges the matrix or the array in ascending order if so that means that the 1st element every time will be the smallest , i want to save the 1st element(minimum distance) from the variable distance in the for loop each time it takes a new (x,y) from the 1st line in a new array
mohammed radwan
on 9 Jun 2018
im thinking now that the whole code is wrong and this method will never work after trying all night , please confirm this
mohammed radwan
on 9 Jun 2018
if true
% code
end
c=1;
for i=1:1:LL
x1=arxxai(i);
y1=aryyai(i);
for j=1:1:LL
x2=axxai2(j);
y2=ayyai2(j);
distance=pdist2([x1 y1],[x2 y2],'euclidean','Smallest',1);
p(j)=distance;
end
qq(c)=min(p);
c=c+1;
end
mohammed radwan
on 9 Jun 2018
ok one last thing i think this code will work fine when the two lines are parallel to each other but when the shape is irregular the minimum distance is correct but the maximum is wrong
Image Analyst
on 9 Jun 2018
Not exactly sure what you want. Perhaps a diagram would help. If you turn the "in between" zone into a binary image then you can skeletonize it to get the centerline. You can then take the Euclidean Distance with bwdist(). Then multiply then to get the distance from the centerline to the closest edge. However, this distance is not the same distance as the distance perpendicular to the centerline in the general case. Just think of a funnel shape. The skeleton can be though of as the center point of the smallest disk that can be fit into the funnel and if the sides are not parallel, then the ball/disk will touch the sides at points that are not perpendicular to the center line.
mohammed radwan
on 11 Jun 2018
ok finally im sure this code is correct it gets the maximum and minimum perpendicular distances
if true
% code
end
c=1;
cc=1;
for i=1:1:LL
woux2=axxai2(i);
wouy2=ayyai2(i);
for j=1:1:LL
woux1=arxxai(j);
wouy1=aryyai(j);
distance=pdist2([woux1 wouy1],[woux2 wouy2],'euclidean','Smallest',10);
p(j)=distance;
end
qq(c)=min(p);
qqq(cc)=max(p);
c=c+1;
cc=cc+1;
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)