Home > database >  Find physical distance between two points from image using Python
Find physical distance between two points from image using Python

Time:10-16

I have two coordinates (x, y) of an image

Point N: (x = 2314, y = 751), Width = 2880, Hight = 2304
Point S: (x = 1533, y = 670), Width = 2880, Hight = 2304

When I plot them in matplotlib they look like this. (Though there are some other points which also I plotted here.) -

(Though there are some other points which also I plotted here.) -

Now, I want to calculate the distance between S and N I know The distance between x-ray plate and x-ray the machine is 180 cm.

I already tried using the math function.

distance = math.sqrt( ((N[0]-S[0])**2) ((N[1]-S[1])**2) )
print(distance)

Which gives me results like this 755.2747844327918

But I need to know the distance (in meters). How can I do this in some way?

CodePudding user response:

You have computed the distance in pixels. You need to know the size of each pixel (eg, in mm), so you can convert it to mm by multiplying by a scale factor.

That scale factor is the product of 2 things:

  1. the image sensor has physical dimensions that allow the size of each pixel to be calculated. For example, if its active size is 288mm by 230.4mm and it produces an image of size 2880 x 2304 pixels, then the size of each pixel is 0.1mm.
  2. most Xray machines have a point source which illuminates the test subject, with the image plane behind. Because it's a point source, the image projected on the image sensor is larger than the subject, with a scale factor determined by the relative distances between the source, subject and imaging plane. That will make the real distance smaller than the size projected on the imaging plane. You can use geometry to compute that scale factor.

By combining those two factors you can convert your pixel distance into a subject physical distance. Be aware if images are made with different distances between the source, subject and image plane then the 2nd scale factor will be different.

I'd recommend testing your calculation by putting a known-dimensioned object in the same plane as the test subject and measuring the resulting image. You should also do it in both vertical and horizontal directions to make sure the pixels in the image sensor are square (which they should be).

Edit: is that a ruler in the top-right of the image? Assuming it is, and it is in the plane of the subject, then use that to get the scale factor in one go!

CodePudding user response:

The result you're getting is in pixels. The solution you're looking for is mathematical, not pythonic. Converting pixels to metres is complicated without using modules, because it varies based on your device's resolution.

You can still easily use a module, but if you know what the distance between the plate and machine you can trivially get the distance between two points.

Firstly you need two concrete points from the image which represent the machine and plate, both being 180 cm apart.

Then take these points as, in this example, A and B (they can be any variable but the have to be assigned with concrete widths and heights in a tuple as N and S were). Find the distance between these points in pixels using pythagorus' theorem (as you were doing with N and S).

distance_AB_pixels = math.sqrt( ((A[0]-B[0])**2) ((A[1]-B[1])**2) )

Dividing the distance in metres (1.8) by the distance in pixels will give you the scale factor.

distance_AB_metres = 1.8
sf = distance_AB_metres/distance_AB_pixels

Finally, multiply this scale factor by the distance in pixels you got for N and S to get the distance between them in metres.

distance_metres = sf * distance
  • Related