Question

I want to measure actual distance between two points like on the road there is a vehicle in front of me,Whats the distance between me and the vehicle?

I proceeded like this :

1) Found the Perspective Transform to get the top view.

2) Then tried to find the distance.

Problem is that I am unable to get the proper top view.

If there is any alternate way please tell.

Was it helpful?

Solution

For measuring the distance between two 3D points you need to find the 3D coordinates of both points. Then it is easy to calculate the euclidean distance:

d(p1,p2)= sqrt{(p1_x - p2_x)^2+(p1_y - p2_y)^2+(p1_z - p2_z)^2}

The problem here is to find the 3D point of the object in the scene (because you know where you are, at the camera, the reference coordinates).

To find a 3D position, you need to find the homography or camera pose. For this purpose you need to detect at least 4 points in the scene that matches 4 points of a known model or image you know a priori. For example, if you have a 3D model of the car and you are able to detect points of the real car in the scene that correspond to your 3D model, you will be able to calculate the homography transformation and, therefore, the 3D positions of those points.

Anyway the problem is complex, you need to use many algorithms for detection, matching, calibration and you need to know which object you want to detect.

OTHER TIPS

The problem is harder than you think. Even under some assumptions:

  1. The entire road is plain (no changes in height).
  2. The pixel location of the vehicle is measured on the point its wheels touch the ground (not vehicle roof). If you measure the roofs the distance might be infinity.
  3. Your camera is stationary with constant zoom.

If one of this assumptions doesn't hold it would be extremely hard to measure distances. But if they do hold you need to do the following:

  1. Perspective transformation to top view will not help you (because perspectivity by definition distorts the distances). Even in top view the rays move in perspective way. What you need is 'orthographic' projection. It simulates top view when camera is in infinite height + infinitely large zoom. Like what you see in satellite images or maps.
  2. You ask why do you need such a strange transformation? The difference between orthographic and perspective transformations is that in perspective the distance between object is depends on tan(a) and in orthographic on sin(a). a - is the angle of camera field of view that is occupied by the distance line (line between 2 points). When 'a' is a very small angle than tan('a') = sin('a') and you can use protective transformation. But since your web cam has a wide angle ~50 - 70 degrees you will be able to calculate the distance accurately with protective transform only in the center of the image.
  3. More over, you must remove camera lens distortion before calculating distance.
  4. So the first step is calibrate your camera and us cvUnDistort() to correct its view.
  5. As far as I know there is not build-in orthographic transformation in openCV. So you can use the top view projective transformation on the corrected image but your distance will be accurate only when both vehicles are near the center of the image.
  6. You can implement orthographic transformation using the unDisrtort() but it will take you some time, since you probably unfamiliar with camera model and camera calibration math. In unDistort() you should basically insert the coefficients of Taylor expansion series that match sin('a'), i.e. a,-a^2/2,... etc...

Anyway as, I stated before, measuring distance is an extremely hard problem. I suggest the following easy but less accurate solution:

  1. Correct lens distortion
  2. Apply top view projective transformation
  3. If you see that the distance measurement produces errors when vehicles are far from image center try to add a manual correction. You can find an open source that does almost what you need here: http://code.google.com/p/signfinder/wiki/HowSignfinderWorks This software detects street name signs, and applies projective correction in order to be able to read text. Hope it helped
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top