Ask Your Question

Getting camera pos/rot after SolvePnP

asked 2015-03-23 14:21:05 -0600

zulman gravatar image

Hi. Classical situation:

cv::Mat Rvec;
  cv::Mat_<float> Tvec;
  cv::Mat raux,taux;

  cv::solvePnP(points3d, points2d, calibration.getIntrinsic(), calibration.getDistorsion(),raux,taux);
  taux.convertTo(Tvec ,CV_32F);

  cv::Mat_<float> rotMat(3,3); 
  cv::Rodrigues(Rvec, rotMat);

  // Copy to transformation matrix
  for (int col=0; col<3; col++)
    for (int row=0; row<3; row++)
     pose3d.r().mat[row][col] = rotMat(row,col); // Copy rotation component
    pose3d.t().data[col] = Tvec(col); // Copy translation component

  pose3d = pose3d.getInverted();

And after it it's easy to draw something in OpenGL with setting MV matrix as pose3d (Matrix4x4). But, i getting crazy to find solution for getting camera position and rotation in world coordinate system to setup camera without setting MV matrix. This is required for drawing camera position in Unity editor application (e.g. here is the image, here is the photo camera and here is the player = scene render camera ). Thank you, folks. It's the biggest stuck for me as a programmer ever :(

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-08-28 18:39:40 -0600


I faced a similar situation and here is how I found the camera position and rotations :

After solvePnP, we get the rotation and the translation for the object - we need to do the inverse to get the camera position and rotation.

CvInvoke.Rodrigues(rotationVector, rotationMatrix);        
// transpose the rotation to get the camera rotation 
rotationMatrix = rotationMatrix.Transpose();
Matrix4x4 R = new Matrix4x4();

copy the rotation matrix to R, and swap the 2nd and 3rd row to swap y and z. After that, rotate the matrix by 90 degrees. This will give you the rotation matrix for the camera. Convert the rotation matrix to a quaternion. and set that to the camera rotation.

As for position,

camera.transform.position = negativematrix * R * Tvec;

where negativematrix is a matrix with 1 on the diagonals.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-03-23 14:20:36 -0600

Seen: 3,725 times

Last updated: Mar 23 '15