Ask Your Question

Revision history [back]

Use cv::solvePnP in the calib3d module. If you use the implementation CV_P3P, you need to pass in exactly 4 matching object and image points.

My use looks like the following:

cv::solvePnP(objectPoints, imagePoints, camMatrix, distCoeffs, rvec, tvec, useExtrinsicGuess, CV_ITERATIVE);

From the rvec and tvec you can get the 3D position of the camera in QR space or the 3D position of the QR code in camera space, depending on how you set things up.

You'll probably want to use cv::Rodrigues(..).

My use is this:

cv::Mat R; cv::Rodrigues(rvec, R);

I then use the following to get the position:

// Determine camera position. OpenCV uses the RIGHT HAND RULE. // Determine the position in screen space (camera z will always be (+) using the RIGHT HAND RULE)
cv::Mat cameraCoords; cv::gemm(R.inv(), -tvec, 1, noArray(), 1, cameraCoords);

cameraCoords is a 3x1 cv::Mat of doubles, holding x, y, and z.

Use cv::solvePnP in the calib3d module. If you use the implementation CV_P3P, you need to pass in exactly 4 matching object and image points.

My use looks like the following:

cv::solvePnP(objectPoints, imagePoints, camMatrix, distCoeffs, rvec, tvec, useExtrinsicGuess, CV_ITERATIVE);

CV_ITERATIVE);

From the rvec and tvec you can get the 3D position of the camera in QR space or the 3D position of the QR code in camera space, depending on how you set things up.

You'll probably want to use cv::Rodrigues(..).

My use is this:

cv::Mat R;
cv::Rodrigues(rvec, R);

R);

I then use the following to get the position:

// Determine camera position. OpenCV uses the RIGHT HAND RULE.
// Determine the position in screen space (camera z will always be (+) using the RIGHT HAND RULE) 
cv::Mat cameraCoords; cv::gemm(R.inv(), -tvec, 1, noArray(), 1, cameraCoords);

cameraCoords);

cameraCoords is a 3x1 cv::Mat of doubles, holding x, y, and z.