# Is it possible to convert from 2D to 3D form?

I have captured car positions at different frames from the video. Suppose car's(e.g.left side car which is coming towards us in the images) centroid at video frame1 is P(x1,y1) and Q(x2,y2) at video frame4.

Is it possible to represent P and Q points into 3D? so that i can calculate correct pixel distance d(PQ) and finally compute actual distance

Note:u can assume that camera is stationary, placed at height 10 m from ground level .u can also assume any suitable data if u want also u can refer the next figure also.

• Marking 50% of your questions bold, really doesn't help.
– jojeck
Commented Jun 10, 2014 at 10:13
• @jojek sir i have added one figure for reference Commented Jun 10, 2014 at 10:17

[EDITED] Here's how it's done.

Steps: 1. Isolate the Road Divider Part.

Then, using Houghlines, find out the longest lines in Image. Find out the extrema points that cross image boundary. You got the Quadilateral points. I skipped this part by Manually Choosing them. In my case, the width of road at top of image is 10, and at bottom is 60.

Now, for the Destination, we have to Image so that Divider Quadilateral has uniform width of 60, so we save in another array new points, which have same bottom points, but top points are modified as they form a rectangle with height same as image (or bigger, you can adjust as it suits you after running it once and seeing the results), but width 60. Remember, we are transforming our image such that the divider fits into this Rectangular Area. Image will change automatically.

Now, using Mat TransformMat = getPerspectiveTransform(ipPts, opPts);

of opencv on initial and destination quadilateral Points, we get a Transform Matrix, Which we apply to our Test Image.

warpPerspective(ipImg, opImg, TransformMat, ipImg.size());

You'll see a lot of Area is Cropped to fit the Output in InputImage Size. However you'll notice that Image is Transformed to get parallel Divider, as we Expected. By performing some Translation and Stuff, and taking a bigger Size Output Image, You'll get this. We can never get a perfectly wrapped Output Image, since its size can be very large. But we'll reach some constraint that'll fulfill our task. Here's Last image :

• @A Matlabber sir in Matlab is there any function to convert from 2d to 3d projection.?? Commented Jun 10, 2014 at 9:44
• Your problem is not a 2d to 3d projection problem. Its a problem of Homographic transformation and Perspective Mapping, then further processing that 'll help you to compute distance(what you are trying to accomplish by making a '3d'). Perhaps this discussion may help you with it. Commented Jun 10, 2014 at 10:25
• @A Matlabber sir its getting difficult to understand Homographic transformation and Perspective Mapping.i searched on google,but not getting idea about it.. Commented Jun 11, 2014 at 10:28
• You can google for several examples of Homographic Transformation and Perspective Mapping. This is an introductory topic, which is taught in many of computer vision online courses. I'm quite busy right now, else I would have gave you some refrences about them. Try looking for courses on Image Processing/Computer Vision, they seldom have demonstration/programming exercise on such type of problems. Commented Jun 12, 2014 at 10:10
• @A Matlabber ok ,i will do as u say.thank u. Commented Jun 12, 2014 at 10:18

This looks like a sort of ray tracing problem. If you know the position and orientation of your camera you should be able to calculate the 3x4 projection matrix and its inverse.

This should allow you to convert from image points to 3d position (on the road). This discussion http://opencv-users.1802565.n2.nabble.com/2D-to-3D-projection-with-given-plane-td7272428.html might have more useful information.

As for applying it in matlab. There is an affine3d which you can use for storing the transform. There might also be useful things for projections/ray-tracing in the file exchange such as http://www.mathworks.com/matlabcentral/fileexchange/25974-raytracer but I haven't looked in detail to find what is relevant for what you want.

• sir its getting difficult to understand Homographic transformation and Perspective Mapping.i searched on google also links u given,but not getting idea about it.. Commented Jun 11, 2014 at 11:19
• @sagar Look up for "Multiple View Geometry in Computer Vision" from Hartley & Zisserman. This is THE book explaining homography in a natural way (using homogeneous coordinates, in which the transform is linear). Commented Jun 21, 2014 at 8:25

If you can calibrate your camera, and if you can detect some reference points on the road surface, then you can get 3D coordinates of the image points that are on the road. This assumes that the road is on a plane. In other words, if you can detect a car, then you can calculate the 3D coordinates of the bottom of the car. See this example in MATLAB using the Computer Vision System Toolbox.