Ahmad ALsattouf
Ahmad ALsattouf

Reputation: 5

how to get the real distance between two points in image?

HI I am using opencv to detect two objects in frame and calculate the distance between them in pixel but I need to calculate the distance between them in meter can you give the realtionship between the meter and pixel

Upvotes: 0

Views: 4147

Answers (2)

Eni
Eni

Reputation: 125

"thank you I try to use the size but I need the relationship between the real size in meter and the size in pixel with known distance between camera and the object"

What you need to do is have the object is question at a certain distance from the camera and measure the pixels. Then, move this object further or closer from/to the camera and measure the change pixels again.

From this you can ascertain the ratio of the change in pixels which is equivalent to the change in distance, thus the distance of the object from the camera. It's just a simple differential equation.

Assuming the size of the object in question is fixed or scaled to the object used to calculate the ratio, this approach should provide a rough estimate of the distance of the objects to the camera.

You then need to use this ratio to help with calculating the distance between the objects as it would be somewhat inverse proportional to the distance between the objects when their distance to the camera increases.

However this method is very messy and can be inefficient. A better approach would be using two different cameras and looking at the disparity between both as mentioned by Hannes

Upvotes: 0

Hannes Ovrén
Hannes Ovrén

Reputation: 21831

This is generally impossible: From a single image the scale of reconstructed 3d points is unknown.

Your options are either to

  1. Add more views, and calculate the depth using stereo vision algorithms.

  2. Use knowledge about the size of the objects to determine the distance.

Edit

Given the depth z, camera calibration matrix K and image point x we can get the corresponding 3D-point Xas:

X = z * inv(K) * x

with x is in homogeneous coordinates.

When you have the two 3D points on the object, calculating the distance is trivial.

Upvotes: 3

Related Questions