1
votes

I have two points in a 2D space:

(255.62746737327373, 257.61185343423432)
(247.86430198019812, 450.74937623762395)

Plotting them over a png with matplotlib i have this result:

enter image description here

Now i would like to calculate the real distance (in meters) between these two points. I know that the real dimension for that image is 125 meters x 86 meters. How can i do this in some way?

2
Use the Pythagorean Theorem with deltax and deltay. Square the deltas, add them, and take the square root.Tom Karzes
This distance =sqrt(pow((xMP - xMM), 2) + pow((yMP - yMM), 2))? This gives me not the real distance in metersslash89mf
Sure. You can just use a**b instead of pow(a, b) in Python. In this case, it would be a**2.Tom Karzes
That gives me the distance between two points that is not in metersslash89mf
Um, it gives it to you in the same units that your x and y values are in. Convert them to meters, then take the distance. The units will be preserved.Tom Karzes

2 Answers

2
votes

Let ImageDim be the length of the image in x and y coordinate. In this case it would be ImageDim = (700, 500), and let StadionDim be length of the stadium. StadionDim = (125, 86) So the function to calculate point in the stadium that is in the image would be:

def calc(ImageDim, StadionDim, Point):
    return (Point[0] * StadionDim[0]/ImageDim[0], Point[1] * StadionDim[1]/ImageDim[1])

So now you would get two points in the stadium. Calculate the distance:

Point_one = calc((700,500), (125,86), (257, 255))
Point_two = calc((700,500), (125,86), (450, 247))
Distance = sqrt((Point_one[0]-Point_two[0])**2 + (Point_one[1]-Point_two[1])**2)
1
votes

I believe your input coordinates are in world space. But when you plot the image without any scaling then you will have plot coordinates in image space from (0,0) in left bottom corner to (image_width, image_height) in right to corner. So to plot your points correctly to image there is need to transform them to image space and vice verse when any real world space calculations are needed to be done. I suppose you will not want to calculate lets say soccer ball speed in pixels per second but in meters in second.

So why not to draw an image in world coordinate to avoid the two spaces coordinates conversions pain? You may do it easily in matplotlib. Use the extent parameter.

extent : scalars (left, right, bottom, top), optional, default: None

The location, in data-coordinates, of the lower-left and upper-right corners. If None, the image is positioned such that the pixel centers fall on zero-based (row, column) indices.

For example this way:

imshow(imade_data, origin='upper',extent=[0, 0, field_width, field_height]);

Then you may plot your points on image in world coordinates. Also the distance calculation will become clear:

import math;
dx = x2-x1;
dy = y2-y1;
distance = math.sqrt(dx*dx+dy*dy);