У меня есть набор совпадений точек 3d-2D, и я использую solvePnPRansac для оценки положения камеры, но функция дает неверный результат, если я установил useExtrinsicGuess = true. Я знаю, что реальная ротация близка к идентичности. На самом деле, если я установлю useExtrinsicGuess = false, результат будет выглядеть хорошо. Моя версия opencv 3.3.0
Вот код теста, данные соответствия и результат:
#include<fstream>
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/calib3d.hpp>
#include<vector>
using cv::Mat;
int main()
{
std::ifstream fin("data.txt", std::ios::in);
if(fin.bad()){
std::cout << "cannot load file" << std::endl;
return 1;
}
int match_num;
fin >> match_num;
std::cout << match_num << std::endl;
std::vector<cv::Point3f> pt3(match_num);
std::vector<cv::Point2f> pt2(match_num);
for(int i = 0; i < match_num; ++i){
fin >>pt3[i].x >> pt3[i].y >> pt3[i].z >> pt2[i].x >> pt2[i].y;
}
Mat K = (cv::Mat_<float>(3, 3) << 517.3, 0, 325.1,
0, 516.5, 249.7,
0, 0, 1.0);
Mat rvec = (cv::Mat_<float>(3, 1) << 0, 0, 0);
Mat tvec = (cv::Mat_<float>(3, 1) << 0, 0, 0);
Mat inliers;
cv::solvePnPRansac(pt3, pt2, K, Mat(), rvec, tvec, true, 100, 4.0f, 0.99, inliers, cv::SOLVEPNP_ITERATIVE);
Mat rvec1, tvec1;
cv::solvePnPRansac(pt3, pt2, K, Mat(), rvec1, tvec1, false, 100, 4.0f, 0.99, inliers, cv::SOLVEPNP_ITERATIVE);
std::cout << "roation by pnp with useExtrinsicGuess=true : "<< rvec << std::endl;
std::cout << "roation by pnp with useExtrinsicGuess=false: "<< rvec1 << std::endl;
return 0;
}
данные матча:
108
-0.825215 -0.129701 1.5822 60.48 155.52
-0.234795 0.112841 1.3228 252.288 247.104
-0.446903 0.242438 1.167 136 316
-0.658448 -0.176557 1.5752 117.504 138.24
-0.206966 0.205543 1.1258 246 302
-0.0938436 0.198847 1.1008 302.4 301.2
-0.517632 0.143996 1.2334 114 265.2
-0.224565 -0.0546336 1.4324 261.6 178.8
-0.231282 0.0910703 1.328 252.288 238.464
-0.110474 0.0389546 1.3702 304.128 216
-0.142114 0.0799027 1.2032 283 237
-0.414796 -0.109007 1.5892 205.632 162.432
-0.151002 -0.240434 1.732 303 123
-0.543786 0.11597 1.2606 108.864 252.288
-0.798659 0.0391596 1.4144 37 216
-0.217987 -0.26124 1.7592 282 117
-0.256666 0.0952179 1.2514 235 243
-0.118062 0.230154 1.0666 288.576 321.408
-0.177316 0.137414 1.1038 260 270
-0.0789204 0.153548 1.2334 314.4 270
-0.141931 -0.282573 1.7864 308 112
-0.302342 -0.0829659 1.547 240 170.4
-0.107675 0.191439 1.0696 293.76 300.672
-0.602337 0.130922 1.2424 79.488 257.472
-0.182519 0.147133 1.0966 256 276
-0.117732 0.231905 1.0666 288 322
-0.604591 0.120324 1.2606 83 253
-0.0868876 0.191195 1.0936 305 299
-0.135698 0.0755564 1.2082 286.8 235.2
-0.151223 0.0645052 1.2204 280.8 229.2
-0.407134 0.259809 1.144 148.8 326.4
-0.173633 -0.225973 1.724 295 127
-0.479299 0.285137 1.0966 106 344
0.0588883 0.129064 1.2746 378 258
-0.181705 0.138113 1.1108 257.76 269.28
-0.225673 -0.0535243 1.4324 261.6 178.8
-0.265257 -0.10232 1.5682 255.744 164.16
-0.412552 0.261162 1.148 148.8 326.4
-0.389858 0.337079 1.0444 139.68 378.72
-0.602739 0.118079 1.2606 82.944 254.016
-0.0989971 0.19087 1.0966 299.52 296.64
-0.0885788 0.192889 1.0936 304.8 298.8
-0.239053 -0.0833061 1.54 262.656 171.072
-0.449159 0.243793 1.167 134.4 316.8
-0.0942691 0.199274 1.1008 302.4 301.2
-0.137553 0.0758065 1.2122 286.8 235.2
-0.216629 -0.100023 1.533 271.296 164.16
-0.152138 0.0788953 1.2164 279.6 236.4
0.0618269 0.117109 1.2886 378.432 252.288
-0.556076 0.122278 1.2556 103.2 254.4
-0.601154 0.13007 1.2464 80.4 258
-0.115567 -0.142577 1.6114 309.6 151.2
-0.303356 -0.0861684 1.54 240 170.4
-0.219023 -0.120364 1.52 269.28 156.96
-0.599551 -0.200605 1.724 157.2 136.8
-0.241215 -0.279825 1.7954 276 114
-0.428835 0.356309 1.0162 114 393.6
-0.429307 0.355837 1.0162 113.76 393.12
-0.136259 0.0703679 1.0696 279.36 237.6
-0.127182 -0.1822 1.6656 308.4 139.2
0.0568228 0.127987 1.2836 377.28 256.32
-0.106484 0.192632 1.0696 293.76 300.672
-0.242815 -0.0457167 1.3972 252 181.44
-0.0798742 0.154503 1.2334 314.4 270
-0.448834 0.247915 1.167 134.784 317.952
0.0627781 0.119041 1.2836 379.2 252
0.611387 0.0441346 1.365 157.248 136.512
-0.219023 -0.119516 1.52 269.568 157.248
-0.820576 -0.647501 2.6026 177.12 63.36
-0.215776 -0.0948945 1.533 271.296 164.16
-0.524279 0.112283 1.2746 119.52 249.12
-0.138063 -0.260991 1.7864 307.584 119.232
-0.620284 0.114565 1.2606 76.032 252.288
-0.120616 -0.208198 1.6818 311.04 131.04
-0.418986 -0.212308 1.715 214.56 132.48
-0.148009 -0.270952 1.7864 303.84 115.2
-0.099741 0.187807 1.0936 299.52 296.64
-0.132009 0.222343 1.0836 281.664 316.224
-0.206096 0.202753 1.1258 245.376 302.4
-0.749817 -0.106716 1.5068 74.88 161.28
-0.531366 0.139602 1.2334 110.88 262.08
-0.601922 0.158026 1.2164 74.88 272.16
-0.0815909 0.153357 1.2334 313.92 269.28
-0.225711 0.108328 1.3128 253.44 246.24
-0.598751 -0.199003 1.724 157.248 136.512
0.366849 0.0762604 1.328 512.64 230.4
-0.765942 -0.111432 1.5138 69.12 159.84
-0.244112 -0.0483136 1.3972 252.288 181.44
-0.175223 0.22847 1.1288 262.656 314.496
-0.532052 0.138227 1.2334 108.864 262.656
0.0551554 0.126675 1.2836 376.704 255.744
-0.0826951 0.154231 1.2294 312.768 271.296
-0.142277 0.075018 1.2122 285.12 235.008
-0.123107 0.186005 1.0796 286.848 297.216
-0.155162 -0.270547 1.7772 302.4 115.776
-0.628247 0.112551 1.2696 74.304 248.832
-0.102503 0.184251 1.0906 298.944 295.488
-0.207349 0.205264 1.1258 245.376 302.4
-0.438637 0.240929 1.171 139.968 314.496
-0.144745 0.0812595 1.0796 274.752 241.92
-0.599711 -0.199003 1.724 157.248 136.512
-1.18272 -0.595891 2.491 88.128 70.848
-0.605985 0.15667 1.2164 72.576 271.296
-0.370314 0.1526 1.2164 177.984 269.568
0.367589 0.0777415 1.328 513.216 231.552
-0.519692 0.14648 1.2334 114.048 266.112
-0.671944 -0.177341 1.5822 117.504 138.24
-0.1212 -0.11806 1.5682 309.312 157.248
Результат :
roation by pnp with useExtrinsicGuess=true : [1.692596371663674;
-0.3229327792507064;
0.3394045257100589]
roation by pnp with useExtrinsicGuess=false: [0.07981319245729608;
0.04776900076086718;
0.0003654506546469515]
Я использовал cv :: projectPoints для проверки результата, тот, который с useExtrinsicGuess = true, полностью неверен.
Есть ли у вас какие-либо идеи? Пожалуйста, попробуйте. Благодарю.
Задача ещё не решена.
Других решений пока нет …