solvePnP fails with perfect coordinates (and cvPOSIT passes)
Hi,
I have found a set of parameters for which solvePnP does not give correct results although :
- the image points are perfect coordinates obtained with projectPoints applied to the objectPoints (no error at all in the image points)
- the objects points consist of 4 non coplanar points
- the parameters are quite innocent looking
- cvPOSIT is perfectly able to reconstruct the pose, when solvePnp seems not to be able to do so
This problem occurs with opencv 2.4.1 and 3.1.0, under osx and windows
When I reproject the point using the rotation and translation provided by solvePnP, I have a total error of about 8 pixels (I have tried all flags : ITERATIVE, P3P and EPNP)
If instead I use the rotation and translation provided by cvPOSIT the reprojection error can be reduced to 0.00014 pixels !
Here is a short program that demonstrates the error : I simply instantiate a camera matrix, some object points and their corresponding image points obtained through projectPoints. Then I compare the reprojection and I get 8 pixels of error!
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream>
void TestSolvePnp()
{
std::vector<cv::Point3f> objectPoints;
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
cv::Mat_<double> cameraIntrinsicParams(cv::Size(3, 3));
cameraIntrinsicParams = 0.;
cameraIntrinsicParams(0, 0) = 3844.4400000000001f;
cameraIntrinsicParams(1, 1) = 3841.0599999999999f;
cameraIntrinsicParams(0, 2) = 640.f;
cameraIntrinsicParams(1, 2) = 380.f;
cameraIntrinsicParams(2, 2) = 1.f;
cv::Mat_<double> distCoeffs(cv::Size(1, 4));
distCoeffs = 0.f;
cv::Mat_<double> rotation(cv::Size(3, 1));
rotation(0,0) = 0.07015543380659847f;
rotation(0,1) = 0.06922079477774973f;
rotation(0,2) = -0.00254676088325f;
cv::Mat_<double> translation(cv::Size(3, 1));
translation(0,0) = -35.3236f;
translation(0,1) = -48.1699f;
translation(0,2) = 769.068f;
std::vector<cv::Point2f> imagePoints;
cv::projectPoints(objectPoints, rotation, translation, cameraIntrinsicParams, distCoeffs, imagePoints);
cv::Mat_<double> rotation2(cv::Size(3, 1));
cv::Mat_<double> translation2(cv::Size(3, 1));
cv::solvePnP(objectPoints, imagePoints,
cameraIntrinsicParams, distCoeffs,
rotation2, translation2,
//false,//useExtrinsicGuess
//Choose solvepnp flag below
//cv::SOLVEPNP_UPNP
//cv::SOLVEPNP_DLS
//cv::SOLVEPNP_EPNP
//cv::SOLVEPNP_P3P
cv::SOLVEPNP_ITERATIVE
);
std::vector<cv::Point2f> imagePoints_Reproj(3);
cv::projectPoints(objectPoints, rotation2, translation2, cameraIntrinsicParams, distCoeffs, imagePoints_Reproj);
float sum = 0.;
for (size_t i = 0; i < imagePoints.size(); i++)
sum += DistPoint(imagePoints_Reproj[i], imagePoints[i]);
std::cout << "sum=" << sum << std::endl;
}
int main(int argc, char **argv)
{
TestSolvePnp();
}
However, if I replace the call to solvePnp by a call to cvPOSIT, the reprojection error goes down to 0.00014 pixels !
Below is the implementation of a function named MySolvePnpPosit() that reproduces the behavior of solvePnP, but uses cvPOSIT internally => when I use it, the error goes down almost zero.
namespace
{
void Posit_IdealCameraCoords(const std::vector<cv::Point3f> & objectPoints, const std::vector<cv::Point2f> & imagePoints,
cv::Mat &outRotationEstimated, cv::Mat & outTranslationEstimated)
{
CvPoint2D32f * imagePoints_c = (CvPoint2D32f ...
How did you create the rotation vector? It works if I set it to zero, so maybe there is something wrong. If you don't want to use distortion factors, just pass an empty matrix. And btw: you don;t have to implement DistPoint yourself, you can use
cv::Norm(v1-v2);
(in which units are you working?)Hello FooBar, The pixel coordinates and object Points + rotation were obtained from a real image on which my project works. The rotation was obtained through cvPOSIT and confirmed with Matlab. Nothing special about this (rodrigues) rotation vector. Concerning the unit, the sizes are in millimeters. I tried to change it to meters (i.e divide the object size and translation by 1000), with no success. However if I divide the object size and translation by 10000, the error goes down to zero. Strange...
Which units did you use while calibrating the camera?
millimeters also, with a chessboard and opencv calibration functions. Are you surprised like me to see that cvPOSIT works here and solvePnp seems to fail ?