Reputation:
I'm using OpenCV+Python+Numpy and have multiple 2D points in an image.
(P1, P2, P3,..., Pn);
(N1, N2);
I am trying to get the distances from each of P1, P2, P3,..., Pn perpendicular to a line drawn between N1 and N2. Let
P=(Xn, Yn) where n varies between 1 to n.
In vector notation, this would be pretty easy, and I understand the math behind calculating distance. But when it comes to image, I can't get anything that works accurately. I read the image and define the points
abs((x2-x1)*(y1-y0) - (x1-x0)*(y2-y1)) / np.sqrt(np.square(x2-x1) + np.square(y2-y1))
edited: I have detected the points with a deep learning algorithm and I want to measure the distance between the points and the line. I want to measured distance to be negative for red points and be positive for blue points and zero for the points which are located on the line. Here is the photo
I have two questions: I am getting the only measurement of one point. How can I measure the distance from all points? and how I define negative distance?
Edited 2: Thank you. I figured out how to measure the distance, but I am still struggling with defining the location of the points with the negative and positive sign.
Any tips appreciated, thanks!
Upvotes: 1
Views: 734
Reputation: 181
You can calculate the distance using "Numpy" and "linalg.norm", "numpy.cross".
distance = linalg.norm(np.cross(N2-N1,N1-Pn))/linalg.norm(N2-N1)
and You can know the location of point (You mentioned as 'positive' and 'negative') using the sign of return value from cross funtion.
sign = np.cross(N2-N1,N1-Pn)
and if Pn is exactly on the line 'N1-N2', sign value will be zero.
Upvotes: 0