Ask Your Question

Measuring Distance from Camera to Target in Image

asked 2014-12-05 09:16:17 -0500

ricardo.nascimento gravatar image

updated 2015-01-24 08:38:51 -0500

Hi ! I have implemented an algorithm that detects several QR Codes using OpenCV for Android. I now would like to know the distance between my camera and the QR Code. I believe this can actually be done since I know the size of the code in the image, I also know the vertices points of three markers I identified. Anyone can recomend some reading on this subject?


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-12-06 12:21:02 -0500

cv::solvePNP will solve that problem for you:

You pass the coordinates of the corners of your QR-Code in the coordinate system of your marker (e.g. if your QR-code has a size of 3cm, the topleft corner could have the coordinates (-0.015,-0.015,0) and the corresponding pixels in your camera.

edit flag offensive delete link more

answered 2014-12-06 09:12:37 -0500

updated 2015-01-23 17:29:44 -0500

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-12-05 09:16:17 -0500

Seen: 3,183 times

Last updated: Jan 23 '15