8
votes

I'm trying to run the code from this tutorial - Pose Estimation,

and I get the following error, after calling solvePnPRansac function:

rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners2, mtx, dist)

ValueError: too many values to unpack

According the documentation:

Python: cv2.solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs[, rvec[, tvec[, useExtrinsicGuess[, iterationsCount[, reprojectionError[, minInliersCount[, inliers[, flags]]]]]]]]) → rvec, tvec, inliers

Did anyone handle with this issue ?

(Python 2.7 , OpenCV 3b)

3
does this code work if you run it under Python 2.7?chris
sorry, my bad, its python 2.7 & opencv 3.Guy P

3 Answers

13
votes

The exception says that there are more than 3 values returned. OpenCV3 has changed a lot of method signatures, unfortunately without really documenting it. I inspected the solvepnp.cpp and the signature reads:

bool cv::solvePnPRansac(InputArray _opoints, InputArray _ipoints,
                    InputArray _cameraMatrix, InputArray _distCoeffs,
                    OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess,
                    int iterationsCount, float reprojectionError, double confidence,
                    OutputArray _inliers, int flags)

which seems to indicate that nothing has changed. However, in python:

solvePnPRansac(...)
solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs[, rvec[, tvec[, useExtrinsicGuess[, iterationsCount[, reprojectionError[, confidence[, inliers[, flags]]]]]]]]) 
-> retval, rvec, tvec, inliers

So it might help to try out:

_, rvecs, tvecs, inliers  = cv2.solvePnPRansac(objp, corners2, mtx, dist)

or in case you just want to unpack the last 3 elements:

rvecs, tvecs, inliers  = cv2.solvePnPRansac(objp, corners2, mtx, dist)[:-3]
5
votes

_, rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners2, mtx, dist)

worked for me

1
votes

So I've come across the same issue, and when I print it out, the first value is a True/False value just like it is for the vanilla solvePnP

I think solvePnPRansac now combines the two outputs, making the result four items: retval, rvec, tvec, inliers

Obviously a good bit late for the original asker, but this still took me a good bit to figure out. I'm using Python 2.7.12 with Ubuntu 16.04. I expect the Python version to matter more, I don't know if Python 3.6+ reflects the same behavior.