我在Windows上使用OpenCV 3.1。
一段代码:
RNG rng; // random number generator
cv::Mat rVec = (cv::Mat_<double>(3, 1) << 0.08257, -0.6168, 1.4675);
cv::Mat tVec = (cv::Mat_<double>(3, 1) << -0.3806, -0.1605, 0.6087);
for (int i = 0; i < 10000; i++)
{
rVec.ptr<double>(0)[0] += rng.rand_linear(0.0, 0.5); // mean 0, marin +-0.5
rVec.ptr<double>(1)[0] += rng.rand_linear(0.0, 0.5);
rVec.ptr<double>(2)[0] += rng.rand_linear(0.0, 0.5);
tVec.ptr<double>(0)[0] += rng.rand_linear(0.0, 0.5);
tVec.ptr<double>(1)[0] += rng.rand_linear(0.0, 0.5);
tVec.ptr<double>(2)[0] += rng.rand_linear(0.0, 0.5);
std::cout << rVec.t() << " --> ";
bool success = cv::solvePnPRansac(patternPoints3d, imgPoints, camIntrinsics.camMat, camIntrinsics.distCoeffs, rVec, tVec, true, 100, 8.f, 0.99, cv::noArray(), cv::SOLVEPNP_ITERATIVE);
std::cout << rVec.t() << std::endl;
}
输出类似于:
[-0.2853612945502569, -0.9418475404979531, 1.68440248184304] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[0.1479919034434538, -0.2763278773652259, 1.150822641518221] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[0.0706268803594689, -0.9919233518319074, 1.32315553697224] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[0.3478958481835257, -0.3697621750777457, 1.716206426456824] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[-0.3340069694997688, -0.3675019960516933, 1.51973527339685] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[0.5445069792592954, -0.9075993847234044, 1.259690332649529] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
因此,无论起始假设如何,我都得到完全相同的最终结果。
继续前进,我减少了100次迭代次数
bool success = cv::solvePnPRansac(patternPoints3d, imgPoints, camIntrinsics.camMat, camIntrinsics.distCoeffs, rVec, tVec, true, 100, 8.f, 0.99, cv::noArray(), cv::SOLVEPNP_ITERATIVE);
仅 1次迭代
bool success = cv::solvePnPRansac(patternPoints3d, imgPoints, camIntrinsics.camMat, camIntrinsics.distCoeffs, rVec, tVec, true, 1, 8.f, 0.99, cv::noArray(), cv::SOLVEPNP_ITERATIVE);
结果相同:
[0.4316089014435242, -0.3745184350425247, 1.000847428296015] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[-0.06206055466029242, -0.6728777329569552, 1.324249691752971] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[-0.2321970797231366, -0.2713987283075098, 1.36880229898195] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[-0.3178144781006445, -0.5075788347182665, 1.912844335384921] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
此外,从 0.99
更改置信度参数bool success = cv::solvePnPRansac(patternPoints3d, imgPoints, camIntrinsics.camMat, camIntrinsics.distCoeffs, rVec, tVec, true, 1, 8.f, 0.99, cv::noArray(), cv::SOLVEPNP_ITERATIVE);
降至 0.01
bool success = cv::solvePnPRansac(patternPoints3d, imgPoints, camIntrinsics.camMat, camIntrinsics.distCoeffs, rVec, tVec, true, 1, 8.f, 0.01, cv::noArray(), cv::SOLVEPNP_ITERATIVE);
结果相同:
[-0.1541070262057652, -0.9795359918514136, 0.9881938066838982] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[-0.09741225946638182, -0.2123314354700837, 1.35100669316414] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[0.4136190534016173, -0.5970452204944435, 1.596524650886908] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
[-0.1438873709732612, -0.6913048753647003, 1.76558963228415] --> [0.0825705957511495, -0.6167564127260689, 1.467542801941067]
与inlier阈值参数相同。看起来好像这些论点没有任何区别。结果实际上看起来很不错,我只想更好地理解它。
所以,我的结论是,无论参数如何,solvePnPRansac()
都会做同样的事情。我做错了什么?
答案 0 :(得分:3)
不幸的是,当前(OpenCV 3.2)solvePnPRansac()
方法不符合文档:
如果根据文档的预期行为提高了精度,我将尝试提交问题并提交拉取请求(当我将有一些时间),否则必须更改文档。
不确定理解你的代码:
rvec
和tvec
projectPoints()
)?您的电话是:
bool success = cv::solvePnPRansac(patternPoints3d, imgPoints, camIntrinsics.camMat, camIntrinsics.distCoeffs, rVec, tVec, true, 100, 8.f, 0.99, cv::noArray(), cv::SOLVEPNP_ITERATIVE);
。
当您查看doc时,您使用标志SOLVEPNP_ITERATIVE
这是一种估算使用迭代优化方案的相机姿态的方法,因此需要对解决方案进行初步估计。
提供 useExtrinsicGuess = true
时,它会在参数中直接使用rvec
和tvec
,否则会在内部调用另一种方法来获得{{ {1}}和rvec
。