Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Too many values to unpack' with solvePnPRansac() - Pose Estimation

Tags:

python

opencv

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)

like image 599
Guy P Avatar asked Feb 10 '15 19:02

Guy P


3 Answers

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]
like image 96
runDOSrun Avatar answered Nov 13 '22 09:11

runDOSrun


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

worked for me

like image 22
Sikander SD Avatar answered Nov 13 '22 10:11

Sikander SD


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.

like image 26
Ian Avatar answered Nov 13 '22 10:11

Ian