我在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]
内部阈值参数相同。这些参数似乎没有任何区别。结果实际上看起来不错,我只是想更好地理解它。
因此,我的结论是,不管参数如何,
solvePnPRansac()
都会执行相同的操作。我究竟做错了什么? 最佳答案
不幸的是,当前的(OpenCV 3.2)solvePnPRansac()
方法与文档不符:
SOLVEPNP_EPNP
方法,当点数为MSS(最小样本集)步骤的>= 5
(请参见here)时将使用useExtrinsicGuess
(请参阅here)如果根据文档的预期行为可以提高精度,我将尝试提交问题,甚至可以提出拉式请求(如果有时间的话),否则必须更改文档。
不确定是否了解您的代码:
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
,否则将在内部调用另一种方法来获得rvec
和tvec
的初始估计。关于c++ - OpenCV解决方案,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44131349/