Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the 3D position from a 2D with opencv

I'm trying to get the 3D coordinates and orientation of an object from its projection onto an image with a single camera with openCV. I have been reading and the steps to follow are: calibrating the image to get the rotational and translational matrices in addition to the matrix k. I have found many examples with a chessboard --> https://littlecodes.wordpress.com/2013/06/24/calibracion-de-camaras-y-procesamiento-de-imagenes-ii/. But my question is: once these parameters have been obtained, how do I get the position of any other object? I have not found any complete examples, only mathematical explanations that I do not quite understand. Something like this, but using the matrices and being able to get the orientation with some accuracy.

I already have the segmented image, and I can locate the points of the corners.

Something like that is what I like to get: Video

Greetings and thanks.

like image 882
Juan Alvarez Fernandez Avatar asked Sep 17 '17 08:09

Juan Alvarez Fernandez


People also ask

How do you convert 2D coordinates to 3D?

To formulate a surface if co-ordinates are given in parametric form x(t),y(t) forming any base contour, then add z=cu, where c is chosen for the depth you want. (x(t),y(t),cu); In general conversion of 2D projection to 3D as you ask is indeterminate.

How do I find my 3D coordinates on Opencv?

The 2-D point is defined on the image plane, so first of all you should the normalized distorted point coordinates (which doesn't depend on the camera matrix, see cv::undistortPoints(...) ). Then you can transform this normalized point to world by the inverse of projection matrix: [R^T|-R^T*t] .


2 Answers

You can use cv2.solvePnPRansac to get the rotational and translational vectors and then use cv2.projectPoints to project the 3d points to the plane. There's a complete tutorial on this that you can find here.(Internet archive)

like image 61
I.Newton Avatar answered Oct 24 '22 00:10

I.Newton


You need to know the object model in its coordinates to retrieve the object's position by solving the Perspective-n-Point problem (i.e. using the cv::solvePnP or cv::solvePnPRansac function included in OpenCV).

Try to look into visual fiducial markers and pose estimation of those particular kind of reference objects. Markers are objects used to do (also) this. It's a good simplification starting from the original PnP problem and you could understand better what is going on. After understanding what happens under the hood, you can do that with any object of which you have a model.

like image 2
valleymanbs Avatar answered Oct 24 '22 00:10

valleymanbs