-1

I want to calculate the euclidean distance between a point with all the other points in an image and possibly sort the top 5-6 with the smallest distance and count them as the nearby points. I know i must do it recursively for all the points in terms of the calculations but have anyone tried it before? I have the coordinates of all the points but not sure how to perform the calculation recursively and rank/sort them. Please I would appreciate any form of help. Thank you.

DeadPool
  • 19
  • 3
  • what kind of question is this? we have 7.9 billion people on this planet and you're asking if anyone has ever calculated the Euclidean distance of a point set? what is your actual question? where is your attempt to solve this? share some code and ask something that makes more sense than "has anyone tried it befor?" – Piglet Oct 14 '21 at 08:19
  • It is good form to show some prior effort at finding an answer, either by reading the documentation, or applying some original thought, and sharing the details of your thought process as part of your question. Often times doing so will help clarify your thoughts and let you find an answer yourself. I have posted a very simple outline below for one approach, but you'd need to consider how you'd apply it in your situation. – Thomas Kimber Oct 14 '21 at 08:32
  • How many points do you have? There are very simple algorithms to do what you want, but they can be slow if there are too many points. Then there are more complicated algorithms, which will work with many points, but are not worth the trouble if you have only a few points. – Stef Oct 14 '21 at 10:09
  • I have a varying number of points say within the range of (75-150) depending on the various images. Basically I am using opencv contour functions to extract the points and midpoint coordinates of the points of interest. From the midpoint coordinates, I am planning to get the closest 5-6 points for each of the point present in the image. – DeadPool Oct 15 '21 at 03:14
  • So my thought process was to get/calculate the euclidean distances and then rank the distances in ascending order and pick the top 5-6 points and relate it back to the coordinate points present in the image. But am not sure if it will be possible to do so and it might be abit more tedious so would like to have some guidance in this matter. – DeadPool Oct 15 '21 at 03:17
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 15 '21 at 11:11

1 Answers1

0

In simple terms, it's just an application of Pythagoras' theorem, h = √(a2 + b2)

from math import sqrt
def func_euclid_dist(x,y,x1,y1):
    dx_sq=(x-x1)**2
    dy_sq=(y-y1)**2
    ed = sqrt(dx_sq+dy_sq)
    return ed

distances=[]
for i, (x,y) in enumerate(first_list_of_point_xy_coords):
    distances.append([])
    for j, (x1,y1) in enumerate(second_list_of_point_xy_coords):
        distances[i].append(func_euclid_dist(x,y,x1,y1))
        

This will yield a 2-d array containing the distances from each point in the first list to each other point in the second list.

If the first list contains only one point, that's not a problem.

Alternately, lots of tools exist, one option is the numpy.linalg.norm function as per this answer: How can the Euclidean distance be calculated with NumPy?

Thomas Kimber
  • 10,601
  • 3
  • 25
  • 42
  • 1
    You don't need to code your own euclidean distance function. In python>=3.8 there is [math.dist](https://docs.python.org/3/library/math.html#math.dist) – Stef Oct 14 '21 at 10:10
  • Euclidean distance applies for any size vector, not just 2D. – duffymo Oct 14 '21 at 13:38
  • Thanks a lot, Thomas Kimber. But I fear this is just going to return the euclidean distances between the points. I need to be able to rank/sort the euclidean distances and then relate the points back to the coordinate space in the image. But I am not sure how to do so or what methodology can be used to handle the coordinate points. – DeadPool Oct 15 '21 at 03:22
  • Take a look at numpy's argsort which should do all the sorting/ranking stuff. – Thomas Kimber Oct 15 '21 at 08:30