Ask Your Question
2

Measuring Distance from Camera to Target in Image

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

ricardo.nascimento gravatar image

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

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?

Thanks

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

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

cv::solvePNP will solve that problem for you:

http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#solvepnp

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
2

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

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

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

Question Tools

Stats

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

Seen: 4,302 times

Last updated: Jan 23 '15