1

I have a set of 3d points in world coordinates and respective correspondences with 2d points in an image. I want to find a matrix that gives me the transformation between these set of points. How can I do this in OpenCV?

manatttta
  • 3,054
  • 4
  • 34
  • 72

2 Answers2

2

cv::solvePnP() is what you are looking for, it finds an object pose from 3D-2D point correspondences and results a rotation vector (rvec), that, together with translation vector (tvec), brings points from the model coordinate system to the camera coordinate system.

Kornel
  • 5,264
  • 2
  • 21
  • 28
  • Thank you! But how would I invert that transformation? i.e. how would I project an image point to the 3d world? (Assuming some fixed z coordinate) – manatttta Apr 09 '15 at 13:51
  • but how would I then project that image onto the 3d space or vice versa? – manatttta Apr 10 '15 at 08:38
  • Something like this: http://stackoverflow.com/questions/28011873/find-world-space-coordinate-for-pixel-in-opencv – Kornel Apr 10 '15 at 11:34
0

you can use solvePnP for this:

    // camMatrix based on img size
    int max_d = std::max(img.rows,img.cols);
    Mat camMatrix = (Mat_<double>(3,3) <<
        max_d,   0, img.cols/2.0,
        0,   max_d, img.rows/2.0,
        0,   0,     1.0);

    // 2d -> 3d correspondence
    vector<Point2d> pts2d = ...
    vector<Point3d> pts3d = ...
    Mat rvec,tvec;
    solvePnP(pts3d, pts2d, camMatrix, Mat(1,4,CV_64F,0.0), rvec, tvec, false, SOLVEPNP_EPNP);
    // get 3d rot mat
    Mat rotM(3, 3, CV_64F);
    Rodrigues(rvec, rotM);

    // push tvec to transposed Mat
    Mat rotMT = rotM.t();
    rotMT.push_back(tvec.reshape(1, 1));

    // transpose back, and multiply
    return camMatrix * rotMT.t();
berak
  • 39,159
  • 9
  • 91
  • 89