kushal
kushal

Reputation: 323

solvePnPRansac returns zero value for rvecs and tvecs

I want to find out the rotation and translation vectors for my camera.

However, the solvePnPRansac method from the documentation is giving zero matrix for each; it returns values for corners as output.

 print corners
[[[ 372.48184204   80.71842194]]
 [[ 374.40280151  103.50676727]]
 [[ 377.49230957  128.53459167]]
      ... so on till ..
 [[ 204.40803528  168.18978882]]
 [[ 203.94168091  193.23649597]]
 [[ 204.39024353  220.48114014]]
 [[ 204.54725647  248.10583496]]]

I am using the code from the documentation and modified it according to my chessboard.

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((7*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)
axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)

for fname in glob.glob('../Right*.jpg'):
      img = cv2.imread(fname)
      gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      ret, corners = cv2.findChessboardCorners(gray, (7,7),None)
      print ret,corners #ret is true and corners returns an array
      if ret == True:
        # Find the rotation and translation vectors.
         rvecs, tvecs , inliers = cv2.solvePnPRansac(objp, corners, mtx, dist)

rvecs and tvecs are coming out to be [0,0,0] and [0,0,0], respectively.

Upvotes: 2

Views: 2301

Answers (2)

fduembgen
fduembgen

Reputation: 44

I have used solvePnPRansac with python and I had the same problem. What solvePnPRansac offers compared to solvePnP is that it calculates rvec and tvec based on a choice of 4 or more points from the set of point correspondances that it is given. This choice is made such that the thereby obtained projection will lead to a good enough fit for all given points. If you don't get a good enough fit with any choice of points, the function will return zeros.

You can increase the value for "reprojectError" (8 by default). This will accept a bigger error for the reprojection, so the result is less good but it might be better than no result at all (unless you are aiming for high precision). For example, try:

rvecs, tvecs , inliers = cv2.solvePnPRansac(objp, corners, mtx, dist,reprojectionError=10)

Finally, just for completeness, if you choose a value for reprojectError as high that all points can be taken into consideration while still leading to an acceptable fit, your result is going to be the same as when you use solvePnP.

Upvotes: 1

holesond
holesond

Reputation: 31

I had probably the same problem - getting zeros in rvec, tvec after calling solvePnPRansac. I have found a workaround - use solvePnP instead of solvePnPRansac. It works well for my 4 points. But my program is written in C++, not Python. Here http://docs.opencv.org/trunk/doc/py_tutorials/py_calib3d/py_pose/py_pose.html is some information that I used during the development but I guess you already know it. Hope this helps.

Upvotes: 0

Related Questions