test_solvepnp_ransac.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #include "opencv2/core/utils/logger.hpp"
  44. namespace opencv_test { namespace {
  45. //Statistics Helpers
  46. struct ErrorInfo
  47. {
  48. ErrorInfo(double errT, double errR) : errorTrans(errT), errorRot(errR)
  49. {
  50. }
  51. bool operator<(const ErrorInfo& e) const
  52. {
  53. return sqrt(errorTrans*errorTrans + errorRot*errorRot) <
  54. sqrt(e.errorTrans*e.errorTrans + e.errorRot*e.errorRot);
  55. }
  56. double errorTrans;
  57. double errorRot;
  58. };
  59. //Try to find the translation and rotation thresholds to achieve a predefined percentage of success.
  60. //Since a success is defined by error_trans < trans_thresh && error_rot < rot_thresh
  61. //this just gives an idea of the values to use
  62. static void findThreshold(const std::vector<double>& v_trans, const std::vector<double>& v_rot, double percentage,
  63. double& transThresh, double& rotThresh)
  64. {
  65. if (v_trans.empty() || v_rot.empty() || v_trans.size() != v_rot.size())
  66. {
  67. transThresh = -1;
  68. rotThresh = -1;
  69. return;
  70. }
  71. std::vector<ErrorInfo> error_info;
  72. error_info.reserve(v_trans.size());
  73. for (size_t i = 0; i < v_trans.size(); i++)
  74. {
  75. error_info.push_back(ErrorInfo(v_trans[i], v_rot[i]));
  76. }
  77. std::sort(error_info.begin(), error_info.end());
  78. size_t idx = static_cast<size_t>(error_info.size() * percentage);
  79. transThresh = error_info[idx].errorTrans;
  80. rotThresh = error_info[idx].errorRot;
  81. }
  82. static double getMax(const std::vector<double>& v)
  83. {
  84. return *std::max_element(v.begin(), v.end());
  85. }
  86. static double getMean(const std::vector<double>& v)
  87. {
  88. if (v.empty())
  89. {
  90. return 0.0;
  91. }
  92. double sum = std::accumulate(v.begin(), v.end(), 0.0);
  93. return sum / v.size();
  94. }
  95. static double getMedian(const std::vector<double>& v)
  96. {
  97. if (v.empty())
  98. {
  99. return 0.0;
  100. }
  101. std::vector<double> v_copy = v;
  102. size_t size = v_copy.size();
  103. size_t n = size / 2;
  104. std::nth_element(v_copy.begin(), v_copy.begin() + n, v_copy.end());
  105. double val_n = v_copy[n];
  106. if (size % 2 == 1)
  107. {
  108. return val_n;
  109. } else
  110. {
  111. std::nth_element(v_copy.begin(), v_copy.begin() + n - 1, v_copy.end());
  112. return 0.5 * (val_n + v_copy[n - 1]);
  113. }
  114. }
  115. static void generatePose(const vector<Point3d>& points, Mat& rvec, Mat& tvec, RNG& rng, int nbTrials=10)
  116. {
  117. const double minVal = 1.0e-3;
  118. const double maxVal = 1.0;
  119. rvec.create(3, 1, CV_64FC1);
  120. tvec.create(3, 1, CV_64FC1);
  121. bool validPose = false;
  122. for (int trial = 0; trial < nbTrials && !validPose; trial++)
  123. {
  124. for (int i = 0; i < 3; i++)
  125. {
  126. rvec.at<double>(i,0) = rng.uniform(minVal, maxVal);
  127. tvec.at<double>(i,0) = (i == 2) ? rng.uniform(minVal*10, maxVal) : rng.uniform(-maxVal, maxVal);
  128. }
  129. Mat R;
  130. cv::Rodrigues(rvec, R);
  131. bool positiveDepth = true;
  132. for (size_t i = 0; i < points.size() && positiveDepth; i++)
  133. {
  134. Matx31d objPts(points[i].x, points[i].y, points[i].z);
  135. Mat camPts = R*objPts + tvec;
  136. if (camPts.at<double>(2,0) <= 0)
  137. {
  138. positiveDepth = false;
  139. }
  140. }
  141. validPose = positiveDepth;
  142. }
  143. }
  144. static void generatePose(const vector<Point3f>& points, Mat& rvec, Mat& tvec, RNG& rng, int nbTrials=10)
  145. {
  146. vector<Point3d> points_double(points.size());
  147. for (size_t i = 0; i < points.size(); i++)
  148. {
  149. points_double[i] = Point3d(points[i].x, points[i].y, points[i].z);
  150. }
  151. generatePose(points_double, rvec, tvec, rng, nbTrials);
  152. }
  153. static std::string printMethod(int method)
  154. {
  155. switch (method) {
  156. case 0:
  157. return "SOLVEPNP_ITERATIVE";
  158. case 1:
  159. return "SOLVEPNP_EPNP";
  160. case 2:
  161. return "SOLVEPNP_P3P";
  162. case 3:
  163. return "SOLVEPNP_DLS (remapped to SOLVEPNP_EPNP)";
  164. case 4:
  165. return "SOLVEPNP_UPNP (remapped to SOLVEPNP_EPNP)";
  166. case 5:
  167. return "SOLVEPNP_AP3P";
  168. case 6:
  169. return "SOLVEPNP_IPPE";
  170. case 7:
  171. return "SOLVEPNP_IPPE_SQUARE";
  172. case 8:
  173. return "SOLVEPNP_SQPNP";
  174. default:
  175. return "Unknown value";
  176. }
  177. }
  178. class CV_solvePnPRansac_Test : public cvtest::BaseTest
  179. {
  180. public:
  181. CV_solvePnPRansac_Test(bool planar_=false, bool planarTag_=false) : planar(planar_), planarTag(planarTag_)
  182. {
  183. eps[SOLVEPNP_ITERATIVE] = 1.0e-2;
  184. eps[SOLVEPNP_EPNP] = 1.0e-2;
  185. eps[SOLVEPNP_P3P] = 1.0e-2;
  186. eps[SOLVEPNP_AP3P] = 1.0e-2;
  187. eps[SOLVEPNP_DLS] = 1.0e-2; // DLS is remapped to EPnP, so we use the same threshold
  188. eps[SOLVEPNP_UPNP] = 1.0e-2; // UPnP is remapped to EPnP, so we use the same threshold
  189. eps[SOLVEPNP_IPPE] = 1.0e-2;
  190. eps[SOLVEPNP_IPPE_SQUARE] = 1.0e-2;
  191. eps[SOLVEPNP_SQPNP] = 1.0e-2;
  192. totalTestsCount = 1000;
  193. if (planar || planarTag)
  194. {
  195. if (planarTag)
  196. {
  197. pointsCount = 4;
  198. }
  199. else
  200. {
  201. pointsCount = 30;
  202. }
  203. }
  204. else
  205. {
  206. pointsCount = 500;
  207. }
  208. }
  209. ~CV_solvePnPRansac_Test() {}
  210. protected:
  211. void generate3DPointCloud(vector<Point3f>& points,
  212. Point3f pmin = Point3f(-1, -1, 5),
  213. Point3f pmax = Point3f(1, 1, 10))
  214. {
  215. RNG& rng = theRNG(); // fix the seed to use "fixed" input 3D points
  216. for (size_t i = 0; i < points.size(); i++)
  217. {
  218. float _x = rng.uniform(pmin.x, pmax.x);
  219. float _y = rng.uniform(pmin.y, pmax.y);
  220. float _z = rng.uniform(pmin.z, pmax.z);
  221. points[i] = Point3f(_x, _y, _z);
  222. }
  223. }
  224. void generatePlanarPointCloud(vector<Point3f>& points,
  225. Point2f pmin = Point2f(-1, -1),
  226. Point2f pmax = Point2f(1, 1))
  227. {
  228. RNG& rng = theRNG(); // fix the seed to use "fixed" input 3D points
  229. if (planarTag)
  230. {
  231. const float squareLength_2 = rng.uniform(0.01f, pmax.x) / 2;
  232. points.clear();
  233. points.push_back(Point3f(-squareLength_2, squareLength_2, 0));
  234. points.push_back(Point3f(squareLength_2, squareLength_2, 0));
  235. points.push_back(Point3f(squareLength_2, -squareLength_2, 0));
  236. points.push_back(Point3f(-squareLength_2, -squareLength_2, 0));
  237. }
  238. else
  239. {
  240. Mat rvec_double, tvec_double;
  241. generatePose(points, rvec_double, tvec_double, rng);
  242. Mat rvec, tvec, R;
  243. rvec_double.convertTo(rvec, CV_32F);
  244. tvec_double.convertTo(tvec, CV_32F);
  245. cv::Rodrigues(rvec, R);
  246. for (size_t i = 0; i < points.size(); i++)
  247. {
  248. float x = rng.uniform(pmin.x, pmax.x);
  249. float y = rng.uniform(pmin.y, pmax.y);
  250. float z = 0;
  251. Matx31f pt(x, y, z);
  252. Mat pt_trans = R * pt + tvec;
  253. points[i] = Point3f(pt_trans.at<float>(0,0), pt_trans.at<float>(1,0), pt_trans.at<float>(2,0));
  254. }
  255. }
  256. }
  257. void generateCameraMatrix(Mat& cameraMatrix, RNG& rng)
  258. {
  259. const double fcMinVal = 1e-3;
  260. const double fcMaxVal = 100;
  261. cameraMatrix.create(3, 3, CV_64FC1);
  262. cameraMatrix.setTo(Scalar(0));
  263. cameraMatrix.at<double>(0,0) = rng.uniform(fcMinVal, fcMaxVal);
  264. cameraMatrix.at<double>(1,1) = rng.uniform(fcMinVal, fcMaxVal);
  265. cameraMatrix.at<double>(0,2) = rng.uniform(fcMinVal, fcMaxVal);
  266. cameraMatrix.at<double>(1,2) = rng.uniform(fcMinVal, fcMaxVal);
  267. cameraMatrix.at<double>(2,2) = 1;
  268. }
  269. void generateDistCoeffs(Mat& distCoeffs, RNG& rng)
  270. {
  271. distCoeffs = Mat::zeros(4, 1, CV_64FC1);
  272. for (int i = 0; i < 3; i++)
  273. distCoeffs.at<double>(i,0) = rng.uniform(0.0, 1.0e-6);
  274. }
  275. virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, double& errorTrans, double& errorRot)
  276. {
  277. Mat rvec, tvec;
  278. vector<int> inliers;
  279. Mat trueRvec, trueTvec;
  280. Mat intrinsics, distCoeffs;
  281. generateCameraMatrix(intrinsics, rng);
  282. //UPnP is mapped to EPnP
  283. //Uncomment this when UPnP is fixed
  284. // if (method == SOLVEPNP_UPNP)
  285. // {
  286. // intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
  287. // }
  288. if (mode == 0)
  289. {
  290. distCoeffs = Mat::zeros(4, 1, CV_64FC1);
  291. }
  292. else
  293. {
  294. generateDistCoeffs(distCoeffs, rng);
  295. }
  296. generatePose(points, trueRvec, trueTvec, rng);
  297. vector<Point2f> projectedPoints;
  298. projectedPoints.resize(points.size());
  299. projectPoints(points, trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
  300. size_t numOutliers = 0;
  301. for (size_t i = 0; i < projectedPoints.size(); i++)
  302. {
  303. if (!planarTag && rng.uniform(0., 1.) > 0.95)
  304. {
  305. projectedPoints[i] = projectedPoints[rng.uniform(0,(int)points.size()-1)];
  306. numOutliers++;
  307. }
  308. }
  309. bool isEstimateSuccess = solvePnPRansac(points, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, pointsCount, 0.5f, 0.99, inliers, method);
  310. if (!isEstimateSuccess)
  311. {
  312. return false;
  313. }
  314. bool isTestSuccess = inliers.size() + numOutliers >= points.size();
  315. double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);
  316. isTestSuccess = isTestSuccess && rvecDiff < eps[method] && tvecDiff < eps[method];
  317. errorTrans = tvecDiff;
  318. errorRot = rvecDiff;
  319. return isTestSuccess;
  320. }
  321. virtual void run(int)
  322. {
  323. ts->set_failed_test_info(cvtest::TS::OK);
  324. vector<Point3f> points, points_dls;
  325. points.resize(static_cast<size_t>(pointsCount));
  326. if (planar || planarTag)
  327. {
  328. generatePlanarPointCloud(points);
  329. }
  330. else
  331. {
  332. generate3DPointCloud(points);
  333. }
  334. RNG& rng = ts->get_rng();
  335. for (int mode = 0; mode < 2; mode++)
  336. {
  337. for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)
  338. {
  339. // SOLVEPNP_IPPE need planar object
  340. if (!planar && method == SOLVEPNP_IPPE)
  341. {
  342. cout << "mode: " << printMode(mode) << ", method: " << printMethod(method) << " -> "
  343. << "Skip for non-planar object" << endl;
  344. continue;
  345. }
  346. // SOLVEPNP_IPPE_SQUARE need planar tag object
  347. if (!planarTag && method == SOLVEPNP_IPPE_SQUARE)
  348. {
  349. cout << "mode: " << printMode(mode) << ", method: " << printMethod(method) << " -> "
  350. << "Skip for non-planar tag object" << endl;
  351. continue;
  352. }
  353. //To get the same input for each methods
  354. RNG rngCopy = rng;
  355. std::vector<double> vec_errorTrans, vec_errorRot;
  356. vec_errorTrans.reserve(static_cast<size_t>(totalTestsCount));
  357. vec_errorRot.reserve(static_cast<size_t>(totalTestsCount));
  358. int successfulTestsCount = 0;
  359. for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
  360. {
  361. double errorTrans, errorRot;
  362. if (runTest(rngCopy, mode, method, points, errorTrans, errorRot))
  363. {
  364. successfulTestsCount++;
  365. }
  366. vec_errorTrans.push_back(errorTrans);
  367. vec_errorRot.push_back(errorRot);
  368. }
  369. double maxErrorTrans = getMax(vec_errorTrans);
  370. double maxErrorRot = getMax(vec_errorRot);
  371. double meanErrorTrans = getMean(vec_errorTrans);
  372. double meanErrorRot = getMean(vec_errorRot);
  373. double medianErrorTrans = getMedian(vec_errorTrans);
  374. double medianErrorRot = getMedian(vec_errorRot);
  375. if (successfulTestsCount < 0.7*totalTestsCount)
  376. {
  377. ts->printf(cvtest::TS::LOG, "Invalid accuracy for %s, failed %d tests from %d, %s, "
  378. "maxErrT: %f, maxErrR: %f, "
  379. "meanErrT: %f, meanErrR: %f, "
  380. "medErrT: %f, medErrR: %f\n",
  381. printMethod(method).c_str(), totalTestsCount - successfulTestsCount, totalTestsCount, printMode(mode).c_str(),
  382. maxErrorTrans, maxErrorRot, meanErrorTrans, meanErrorRot, medianErrorTrans, medianErrorRot);
  383. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  384. }
  385. cout << "mode: " << printMode(mode) << ", method: " << printMethod(method) << " -> "
  386. << ((double)successfulTestsCount / totalTestsCount) * 100 << "%"
  387. << " (maxErrT: " << maxErrorTrans << ", maxErrR: " << maxErrorRot
  388. << ", meanErrT: " << meanErrorTrans << ", meanErrR: " << meanErrorRot
  389. << ", medErrT: " << medianErrorTrans << ", medErrR: " << medianErrorRot << ")" << endl;
  390. double transThres, rotThresh;
  391. findThreshold(vec_errorTrans, vec_errorRot, 0.7, transThres, rotThresh);
  392. cout << "approximate translation threshold for 0.7: " << transThres
  393. << ", approximate rotation threshold for 0.7: " << rotThresh << endl;
  394. }
  395. cout << endl;
  396. }
  397. }
  398. std::string printMode(int mode)
  399. {
  400. switch (mode) {
  401. case 0:
  402. return "no distortion";
  403. case 1:
  404. default:
  405. return "distorsion";
  406. }
  407. }
  408. double eps[SOLVEPNP_MAX_COUNT];
  409. int totalTestsCount;
  410. int pointsCount;
  411. bool planar;
  412. bool planarTag;
  413. };
  414. class CV_solvePnP_Test : public CV_solvePnPRansac_Test
  415. {
  416. public:
  417. CV_solvePnP_Test(bool planar_=false, bool planarTag_=false) : CV_solvePnPRansac_Test(planar_, planarTag_)
  418. {
  419. eps[SOLVEPNP_ITERATIVE] = 1.0e-6;
  420. eps[SOLVEPNP_EPNP] = 1.0e-6;
  421. eps[SOLVEPNP_P3P] = 1.0e-4;
  422. eps[SOLVEPNP_AP3P] = 1.0e-4;
  423. eps[SOLVEPNP_DLS] = 1.0e-6; // DLS is remapped to EPnP, so we use the same threshold
  424. eps[SOLVEPNP_UPNP] = 1.0e-6; // UPnP is remapped to EPnP, so we use the same threshold
  425. eps[SOLVEPNP_IPPE] = 1.0e-6;
  426. eps[SOLVEPNP_IPPE_SQUARE] = 1.0e-6;
  427. eps[SOLVEPNP_SQPNP] = 1.0e-6;
  428. totalTestsCount = 1000;
  429. if (planar || planarTag)
  430. {
  431. if (planarTag)
  432. {
  433. pointsCount = 4;
  434. }
  435. else
  436. {
  437. pointsCount = 30;
  438. }
  439. }
  440. else
  441. {
  442. pointsCount = 500;
  443. }
  444. }
  445. ~CV_solvePnP_Test() {}
  446. protected:
  447. virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, double& errorTrans, double& errorRot)
  448. {
  449. //Tune thresholds...
  450. double epsilon_trans[SOLVEPNP_MAX_COUNT];
  451. memcpy(epsilon_trans, eps, SOLVEPNP_MAX_COUNT * sizeof(*epsilon_trans));
  452. double epsilon_rot[SOLVEPNP_MAX_COUNT];
  453. memcpy(epsilon_rot, eps, SOLVEPNP_MAX_COUNT * sizeof(*epsilon_rot));
  454. if (planar)
  455. {
  456. if (mode == 0)
  457. {
  458. epsilon_trans[SOLVEPNP_EPNP] = 5.0e-3;
  459. epsilon_trans[SOLVEPNP_DLS] = 5.0e-3; // DLS is remapped to EPnP, so we use the same threshold
  460. epsilon_trans[SOLVEPNP_UPNP] = 5.0e-3; // UPnP is remapped to EPnP, so we use the same threshold
  461. epsilon_rot[SOLVEPNP_EPNP] = 5.0e-3;
  462. epsilon_rot[SOLVEPNP_DLS] = 5.0e-3; // DLS is remapped to EPnP, so we use the same threshold
  463. epsilon_rot[SOLVEPNP_UPNP] = 5.0e-3; // UPnP is remapped to EPnP, so we use the same threshold
  464. }
  465. else
  466. {
  467. epsilon_trans[SOLVEPNP_ITERATIVE] = 1e-4;
  468. epsilon_trans[SOLVEPNP_EPNP] = 5e-3;
  469. epsilon_trans[SOLVEPNP_DLS] = 5e-3; // DLS is remapped to EPnP, so we use the same threshold
  470. epsilon_trans[SOLVEPNP_UPNP] = 5e-3; // UPnP is remapped to EPnP, so we use the same threshold
  471. epsilon_trans[SOLVEPNP_P3P] = 1e-4;
  472. epsilon_trans[SOLVEPNP_AP3P] = 1e-4;
  473. epsilon_trans[SOLVEPNP_IPPE] = 1e-4;
  474. epsilon_trans[SOLVEPNP_IPPE_SQUARE] = 1e-4;
  475. epsilon_rot[SOLVEPNP_ITERATIVE] = 1e-4;
  476. epsilon_rot[SOLVEPNP_EPNP] = 5e-3;
  477. epsilon_rot[SOLVEPNP_DLS] = 5e-3; // DLS is remapped to EPnP, so we use the same threshold
  478. epsilon_rot[SOLVEPNP_UPNP] = 5e-3; // UPnP is remapped to EPnP, so we use the same threshold
  479. epsilon_rot[SOLVEPNP_P3P] = 1e-4;
  480. epsilon_rot[SOLVEPNP_AP3P] = 1e-4;
  481. epsilon_rot[SOLVEPNP_IPPE] = 1e-4;
  482. epsilon_rot[SOLVEPNP_IPPE_SQUARE] = 1e-4;
  483. }
  484. }
  485. Mat trueRvec, trueTvec;
  486. Mat intrinsics, distCoeffs;
  487. generateCameraMatrix(intrinsics, rng);
  488. //UPnP is mapped to EPnP
  489. //Uncomment this when UPnP is fixed
  490. // if (method == SOLVEPNP_UPNP)
  491. // {
  492. // intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
  493. // }
  494. if (mode == 0)
  495. {
  496. distCoeffs = Mat::zeros(4, 1, CV_64FC1);
  497. }
  498. else
  499. {
  500. generateDistCoeffs(distCoeffs, rng);
  501. }
  502. generatePose(points, trueRvec, trueTvec, rng);
  503. std::vector<Point3f> opoints;
  504. switch(method)
  505. {
  506. case SOLVEPNP_P3P:
  507. case SOLVEPNP_AP3P:
  508. opoints = std::vector<Point3f>(points.begin(), points.begin()+4);
  509. break;
  510. //UPnP is mapped to EPnP
  511. //Uncomment this when UPnP is fixed
  512. // case SOLVEPNP_UPNP:
  513. // if (points.size() > 50)
  514. // {
  515. // opoints = std::vector<Point3f>(points.begin(), points.begin()+50);
  516. // }
  517. // else
  518. // {
  519. // opoints = points;
  520. // }
  521. // break;
  522. default:
  523. opoints = points;
  524. break;
  525. }
  526. vector<Point2f> projectedPoints;
  527. projectedPoints.resize(opoints.size());
  528. projectPoints(opoints, trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
  529. Mat rvec, tvec;
  530. bool isEstimateSuccess = solvePnP(opoints, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, method);
  531. if (!isEstimateSuccess)
  532. {
  533. return false;
  534. }
  535. double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);
  536. bool isTestSuccess = rvecDiff < epsilon_rot[method] && tvecDiff < epsilon_trans[method];
  537. errorTrans = tvecDiff;
  538. errorRot = rvecDiff;
  539. return isTestSuccess;
  540. }
  541. };
  542. class CV_solveP3P_Test : public CV_solvePnPRansac_Test
  543. {
  544. public:
  545. CV_solveP3P_Test()
  546. {
  547. eps[SOLVEPNP_P3P] = 1.0e-4;
  548. eps[SOLVEPNP_AP3P] = 1.0e-4;
  549. totalTestsCount = 1000;
  550. }
  551. ~CV_solveP3P_Test() {}
  552. protected:
  553. virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, double& errorTrans, double& errorRot)
  554. {
  555. std::vector<Mat> rvecs, tvecs;
  556. Mat trueRvec, trueTvec;
  557. Mat intrinsics, distCoeffs;
  558. generateCameraMatrix(intrinsics, rng);
  559. if (mode == 0)
  560. {
  561. distCoeffs = Mat::zeros(4, 1, CV_64FC1);
  562. }
  563. else
  564. {
  565. generateDistCoeffs(distCoeffs, rng);
  566. }
  567. generatePose(points, trueRvec, trueTvec, rng);
  568. std::vector<Point3f> opoints;
  569. opoints = std::vector<Point3f>(points.begin(), points.begin()+3);
  570. vector<Point2f> projectedPoints;
  571. projectedPoints.resize(opoints.size());
  572. projectPoints(opoints, trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
  573. int num_of_solutions = solveP3P(opoints, projectedPoints, intrinsics, distCoeffs, rvecs, tvecs, method);
  574. if (num_of_solutions != (int) rvecs.size() || num_of_solutions != (int) tvecs.size() || num_of_solutions == 0)
  575. {
  576. return false;
  577. }
  578. bool isTestSuccess = false;
  579. for (size_t i = 0; i < rvecs.size() && !isTestSuccess; i++) {
  580. double rvecDiff = cvtest::norm(rvecs[i], trueRvec, NORM_L2);
  581. double tvecDiff = cvtest::norm(tvecs[i], trueTvec, NORM_L2);
  582. isTestSuccess = rvecDiff < eps[method] && tvecDiff < eps[method];
  583. errorTrans = std::min(errorTrans, tvecDiff);
  584. errorRot = std::min(errorRot, rvecDiff);
  585. }
  586. return isTestSuccess;
  587. }
  588. virtual void run(int)
  589. {
  590. ts->set_failed_test_info(cvtest::TS::OK);
  591. vector<Point3f> points;
  592. points.resize(static_cast<size_t>(pointsCount));
  593. generate3DPointCloud(points);
  594. const int methodsCount = 2;
  595. int methods[] = {SOLVEPNP_P3P, SOLVEPNP_AP3P};
  596. RNG rng = ts->get_rng();
  597. for (int mode = 0; mode < 2; mode++)
  598. {
  599. //To get the same input for each methods
  600. RNG rngCopy = rng;
  601. for (int method = 0; method < methodsCount; method++)
  602. {
  603. std::vector<double> vec_errorTrans, vec_errorRot;
  604. vec_errorTrans.reserve(static_cast<size_t>(totalTestsCount));
  605. vec_errorRot.reserve(static_cast<size_t>(totalTestsCount));
  606. int successfulTestsCount = 0;
  607. for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
  608. {
  609. double errorTrans = 0, errorRot = 0;
  610. if (runTest(rngCopy, mode, methods[method], points, errorTrans, errorRot))
  611. {
  612. successfulTestsCount++;
  613. }
  614. vec_errorTrans.push_back(errorTrans);
  615. vec_errorRot.push_back(errorRot);
  616. }
  617. double maxErrorTrans = getMax(vec_errorTrans);
  618. double maxErrorRot = getMax(vec_errorRot);
  619. double meanErrorTrans = getMean(vec_errorTrans);
  620. double meanErrorRot = getMean(vec_errorRot);
  621. double medianErrorTrans = getMedian(vec_errorTrans);
  622. double medianErrorRot = getMedian(vec_errorRot);
  623. if (successfulTestsCount < 0.7*totalTestsCount)
  624. {
  625. ts->printf(cvtest::TS::LOG, "Invalid accuracy for %s, failed %d tests from %d, %s, "
  626. "maxErrT: %f, maxErrR: %f, "
  627. "meanErrT: %f, meanErrR: %f, "
  628. "medErrT: %f, medErrR: %f\n",
  629. printMethod(methods[method]).c_str(), totalTestsCount - successfulTestsCount, totalTestsCount, printMode(mode).c_str(),
  630. maxErrorTrans, maxErrorRot, meanErrorTrans, meanErrorRot, medianErrorTrans, medianErrorRot);
  631. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  632. }
  633. cout << "mode: " << printMode(mode) << ", method: " << printMethod(methods[method]) << " -> "
  634. << ((double)successfulTestsCount / totalTestsCount) * 100 << "%"
  635. << " (maxErrT: " << maxErrorTrans << ", maxErrR: " << maxErrorRot
  636. << ", meanErrT: " << meanErrorTrans << ", meanErrR: " << meanErrorRot
  637. << ", medErrT: " << medianErrorTrans << ", medErrR: " << medianErrorRot << ")" << endl;
  638. double transThres, rotThresh;
  639. findThreshold(vec_errorTrans, vec_errorRot, 0.7, transThres, rotThresh);
  640. cout << "approximate translation threshold for 0.7: " << transThres
  641. << ", approximate rotation threshold for 0.7: " << rotThresh << endl;
  642. }
  643. }
  644. }
  645. };
  646. TEST(Calib3d_SolveP3P, accuracy) { CV_solveP3P_Test test; test.safe_run();}
  647. TEST(Calib3d_SolvePnPRansac, accuracy) { CV_solvePnPRansac_Test test; test.safe_run(); }
  648. TEST(Calib3d_SolvePnPRansac, accuracy_planar) { CV_solvePnPRansac_Test test(true); test.safe_run(); }
  649. TEST(Calib3d_SolvePnPRansac, accuracy_planar_tag) { CV_solvePnPRansac_Test test(true, true); test.safe_run(); }
  650. TEST(Calib3d_SolvePnP, accuracy) { CV_solvePnP_Test test; test.safe_run(); }
  651. TEST(Calib3d_SolvePnP, accuracy_planar) { CV_solvePnP_Test test(true); test.safe_run(); }
  652. TEST(Calib3d_SolvePnP, accuracy_planar_tag) { CV_solvePnP_Test test(true, true); test.safe_run(); }
  653. TEST(Calib3d_SolvePnPRansac, concurrency)
  654. {
  655. int count = 7*13;
  656. Mat object(1, count, CV_32FC3);
  657. randu(object, -100, 100);
  658. Mat camera_mat(3, 3, CV_32FC1);
  659. randu(camera_mat, 0.5, 1);
  660. camera_mat.at<float>(0, 1) = 0.f;
  661. camera_mat.at<float>(1, 0) = 0.f;
  662. camera_mat.at<float>(2, 0) = 0.f;
  663. camera_mat.at<float>(2, 1) = 0.f;
  664. camera_mat.at<float>(2, 2) = 1.f;
  665. Mat dist_coef(1, 8, CV_32F, cv::Scalar::all(0));
  666. vector<cv::Point2f> image_vec;
  667. Mat rvec_gold(1, 3, CV_32FC1);
  668. randu(rvec_gold, 0, 1);
  669. Mat tvec_gold(1, 3, CV_32FC1);
  670. randu(tvec_gold, 0, 1);
  671. projectPoints(object, rvec_gold, tvec_gold, camera_mat, dist_coef, image_vec);
  672. Mat image(1, count, CV_32FC2, &image_vec[0]);
  673. Mat rvec1, rvec2;
  674. Mat tvec1, tvec2;
  675. int threads = getNumThreads();
  676. {
  677. // limit concurrency to get deterministic result
  678. theRNG().state = 20121010;
  679. setNumThreads(1);
  680. solvePnPRansac(object, image, camera_mat, dist_coef, rvec1, tvec1);
  681. }
  682. {
  683. setNumThreads(threads);
  684. Mat rvec;
  685. Mat tvec;
  686. // parallel executions
  687. for(int i = 0; i < 10; ++i)
  688. {
  689. cv::theRNG().state = 20121010;
  690. solvePnPRansac(object, image, camera_mat, dist_coef, rvec, tvec);
  691. }
  692. }
  693. {
  694. // single thread again
  695. theRNG().state = 20121010;
  696. setNumThreads(1);
  697. solvePnPRansac(object, image, camera_mat, dist_coef, rvec2, tvec2);
  698. }
  699. double rnorm = cvtest::norm(rvec1, rvec2, NORM_INF);
  700. double tnorm = cvtest::norm(tvec1, tvec2, NORM_INF);
  701. EXPECT_LT(rnorm, 1e-6);
  702. EXPECT_LT(tnorm, 1e-6);
  703. }
  704. TEST(Calib3d_SolvePnPRansac, input_type)
  705. {
  706. const int numPoints = 10;
  707. Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
  708. 5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);
  709. std::vector<cv::Point3f> points3d;
  710. std::vector<cv::Point2f> points2d;
  711. for (int i = 0; i < numPoints; i+=2)
  712. {
  713. points3d.push_back(cv::Point3i(5+i, 3, 2));
  714. points3d.push_back(cv::Point3i(5+i, 3+i, 2+i));
  715. points2d.push_back(cv::Point2i(0, i));
  716. points2d.push_back(cv::Point2i(-i, i));
  717. }
  718. Mat R1, t1, R2, t2, R3, t3, R4, t4;
  719. EXPECT_TRUE(solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R1, t1));
  720. Mat points3dMat(points3d);
  721. Mat points2dMat(points2d);
  722. EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R2, t2));
  723. points3dMat = points3dMat.reshape(3, 1);
  724. points2dMat = points2dMat.reshape(2, 1);
  725. EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R3, t3));
  726. points3dMat = points3dMat.reshape(1, numPoints);
  727. points2dMat = points2dMat.reshape(1, numPoints);
  728. EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R4, t4));
  729. EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-6);
  730. EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-6);
  731. EXPECT_LE(cvtest::norm(R1, R3, NORM_INF), 1e-6);
  732. EXPECT_LE(cvtest::norm(t1, t3, NORM_INF), 1e-6);
  733. EXPECT_LE(cvtest::norm(R1, R4, NORM_INF), 1e-6);
  734. EXPECT_LE(cvtest::norm(t1, t4, NORM_INF), 1e-6);
  735. }
  736. TEST(Calib3d_SolvePnPRansac, double_support)
  737. {
  738. Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
  739. 5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);
  740. std::vector<cv::Point3d> points3d;
  741. std::vector<cv::Point2d> points2d;
  742. std::vector<cv::Point3f> points3dF;
  743. std::vector<cv::Point2f> points2dF;
  744. for (int i = 0; i < 10 ; i+=2)
  745. {
  746. points3d.push_back(cv::Point3d(5+i, 3, 2));
  747. points3dF.push_back(cv::Point3f(static_cast<float>(5+i), 3, 2));
  748. points3d.push_back(cv::Point3d(5+i, 3+i, 2+i));
  749. points3dF.push_back(cv::Point3f(static_cast<float>(5+i), static_cast<float>(3+i), static_cast<float>(2+i)));
  750. points2d.push_back(cv::Point2d(0, i));
  751. points2dF.push_back(cv::Point2f(0, static_cast<float>(i)));
  752. points2d.push_back(cv::Point2d(-i, i));
  753. points2dF.push_back(cv::Point2f(static_cast<float>(-i), static_cast<float>(i)));
  754. }
  755. Mat R, t, RF, tF;
  756. vector<int> inliers;
  757. solvePnPRansac(points3dF, points2dF, intrinsics, cv::Mat(), RF, tF, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);
  758. solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R, t, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);
  759. EXPECT_LE(cvtest::norm(R, Mat_<double>(RF), NORM_INF), 1e-3);
  760. EXPECT_LE(cvtest::norm(t, Mat_<double>(tF), NORM_INF), 1e-3);
  761. }
  762. TEST(Calib3d_SolvePnPRansac, bad_input_points_19253)
  763. {
  764. // with this specific data
  765. // when computing the final pose using points in the consensus set with SOLVEPNP_ITERATIVE and solvePnP()
  766. // an exception is thrown from solvePnP because there are 5 non-coplanar 3D points and the DLT algorithm needs at least 6 non-coplanar 3D points
  767. // with PR #19253 we choose to return true, with the pose estimated from the MSS stage instead of throwing the exception
  768. float pts2d_[] = {
  769. -5.38358629e-01f, -5.09638414e-02f,
  770. -5.07192254e-01f, -2.20743284e-01f,
  771. -5.43107152e-01f, -4.90474701e-02f,
  772. -5.54325163e-01f, -1.86715424e-01f,
  773. -5.59334219e-01f, -4.01909500e-02f,
  774. -5.43504596e-01f, -4.61776406e-02f
  775. };
  776. Mat pts2d(6, 2, CV_32FC1, pts2d_);
  777. float pts3d_[] = {
  778. -3.01153604e-02f, -1.55665115e-01f, 4.50000018e-01f,
  779. 4.27827090e-01f, 4.28645730e-01f, 1.08600008e+00f,
  780. -3.14165242e-02f, -1.52656138e-01f, 4.50000018e-01f,
  781. -1.46217480e-01f, 5.57961613e-02f, 7.17000008e-01f,
  782. -4.89348806e-02f, -1.38795510e-01f, 4.47000027e-01f,
  783. -3.13065052e-02f, -1.52636901e-01f, 4.51000035e-01f
  784. };
  785. Mat pts3d(6, 3, CV_32FC1, pts3d_);
  786. Mat camera_mat = Mat::eye(3, 3, CV_64FC1);
  787. Mat rvec, tvec;
  788. vector<int> inliers;
  789. // solvePnPRansac will return true with 5 inliers, which means the result is from MSS stage.
  790. bool result = solvePnPRansac(pts3d, pts2d, camera_mat, noArray(), rvec, tvec, false, 100, 4.f / 460.f, 0.99, inliers);
  791. EXPECT_EQ(inliers.size(), size_t(5));
  792. EXPECT_TRUE(result);
  793. }
  794. TEST(Calib3d_SolvePnP, input_type)
  795. {
  796. Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
  797. 5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);
  798. vector<Point3d> points3d_;
  799. vector<Point3f> points3dF_;
  800. //Cube
  801. const float l = -0.1f;
  802. //Front face
  803. points3d_.push_back(Point3d(-l, -l, -l));
  804. points3dF_.push_back(Point3f(-l, -l, -l));
  805. points3d_.push_back(Point3d(l, -l, -l));
  806. points3dF_.push_back(Point3f(l, -l, -l));
  807. points3d_.push_back(Point3d(l, l, -l));
  808. points3dF_.push_back(Point3f(l, l, -l));
  809. points3d_.push_back(Point3d(-l, l, -l));
  810. points3dF_.push_back(Point3f(-l, l, -l));
  811. //Back face
  812. points3d_.push_back(Point3d(-l, -l, l));
  813. points3dF_.push_back(Point3f(-l, -l, l));
  814. points3d_.push_back(Point3d(l, -l, l));
  815. points3dF_.push_back(Point3f(l, -l, l));
  816. points3d_.push_back(Point3d(l, l, l));
  817. points3dF_.push_back(Point3f(l, l, l));
  818. points3d_.push_back(Point3d(-l, l, l));
  819. points3dF_.push_back(Point3f(-l, l, l));
  820. Mat trueRvec = (Mat_<double>(3,1) << 0.1, -0.25, 0.467);
  821. Mat trueTvec = (Mat_<double>(3,1) << -0.21, 0.12, 0.746);
  822. for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)
  823. {
  824. vector<Point3d> points3d;
  825. vector<Point2d> points2d;
  826. vector<Point3f> points3dF;
  827. vector<Point2f> points2dF;
  828. if (method == SOLVEPNP_IPPE || method == SOLVEPNP_IPPE_SQUARE)
  829. {
  830. const float tagSize_2 = 0.05f / 2;
  831. points3d.push_back(Point3d(-tagSize_2, tagSize_2, 0));
  832. points3d.push_back(Point3d( tagSize_2, tagSize_2, 0));
  833. points3d.push_back(Point3d( tagSize_2, -tagSize_2, 0));
  834. points3d.push_back(Point3d(-tagSize_2, -tagSize_2, 0));
  835. points3dF.push_back(Point3f(-tagSize_2, tagSize_2, 0));
  836. points3dF.push_back(Point3f( tagSize_2, tagSize_2, 0));
  837. points3dF.push_back(Point3f( tagSize_2, -tagSize_2, 0));
  838. points3dF.push_back(Point3f(-tagSize_2, -tagSize_2, 0));
  839. }
  840. else if (method == SOLVEPNP_P3P || method == SOLVEPNP_AP3P)
  841. {
  842. points3d = vector<Point3d>(points3d_.begin(), points3d_.begin()+4);
  843. points3dF = vector<Point3f>(points3dF_.begin(), points3dF_.begin()+4);
  844. }
  845. else
  846. {
  847. points3d = points3d_;
  848. points3dF = points3dF_;
  849. }
  850. projectPoints(points3d, trueRvec, trueTvec, intrinsics, noArray(), points2d);
  851. projectPoints(points3dF, trueRvec, trueTvec, intrinsics, noArray(), points2dF);
  852. //solvePnP
  853. {
  854. Mat R, t, RF, tF;
  855. solvePnP(points3dF, points2dF, Matx33f(intrinsics), Mat(), RF, tF, false, method);
  856. solvePnP(points3d, points2d, intrinsics, Mat(), R, t, false, method);
  857. //By default rvec and tvec must be returned in double precision
  858. EXPECT_EQ(RF.type(), tF.type());
  859. EXPECT_EQ(RF.type(), CV_64FC1);
  860. EXPECT_EQ(R.type(), t.type());
  861. EXPECT_EQ(R.type(), CV_64FC1);
  862. EXPECT_LE(cvtest::norm(R, RF, NORM_INF), 1e-3);
  863. EXPECT_LE(cvtest::norm(t, tF, NORM_INF), 1e-3);
  864. EXPECT_LE(cvtest::norm(trueRvec, R, NORM_INF), 1e-3);
  865. EXPECT_LE(cvtest::norm(trueTvec, t, NORM_INF), 1e-3);
  866. EXPECT_LE(cvtest::norm(trueRvec, RF, NORM_INF), 1e-3);
  867. EXPECT_LE(cvtest::norm(trueTvec, tF, NORM_INF), 1e-3);
  868. }
  869. {
  870. Mat R1, t1, R2, t2;
  871. solvePnP(points3dF, points2d, intrinsics, Mat(), R1, t1, false, method);
  872. solvePnP(points3d, points2dF, intrinsics, Mat(), R2, t2, false, method);
  873. //By default rvec and tvec must be returned in double precision
  874. EXPECT_EQ(R1.type(), t1.type());
  875. EXPECT_EQ(R1.type(), CV_64FC1);
  876. EXPECT_EQ(R2.type(), t2.type());
  877. EXPECT_EQ(R2.type(), CV_64FC1);
  878. EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-3);
  879. EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-3);
  880. EXPECT_LE(cvtest::norm(trueRvec, R1, NORM_INF), 1e-3);
  881. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  882. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  883. EXPECT_LE(cvtest::norm(trueTvec, t2, NORM_INF), 1e-3);
  884. }
  885. {
  886. Mat R1(3,1,CV_32FC1), t1(3,1,CV_64FC1);
  887. Mat R2(3,1,CV_64FC1), t2(3,1,CV_32FC1);
  888. solvePnP(points3dF, points2d, intrinsics, Mat(), R1, t1, false, method);
  889. solvePnP(points3d, points2dF, intrinsics, Mat(), R2, t2, false, method);
  890. //If not null, rvec and tvec must be returned in the same precision
  891. EXPECT_EQ(R1.type(), CV_32FC1);
  892. EXPECT_EQ(t1.type(), CV_64FC1);
  893. EXPECT_EQ(R2.type(), CV_64FC1);
  894. EXPECT_EQ(t2.type(), CV_32FC1);
  895. EXPECT_LE(cvtest::norm(Mat_<double>(R1), R2, NORM_INF), 1e-3);
  896. EXPECT_LE(cvtest::norm(t1, Mat_<double>(t2), NORM_INF), 1e-3);
  897. EXPECT_LE(cvtest::norm(trueRvec, Mat_<double>(R1), NORM_INF), 1e-3);
  898. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  899. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  900. EXPECT_LE(cvtest::norm(trueTvec, Mat_<double>(t2), NORM_INF), 1e-3);
  901. }
  902. {
  903. Matx31f R1, t2;
  904. Matx31d R2, t1;
  905. solvePnP(points3dF, points2d, intrinsics, Mat(), R1, t1, false, method);
  906. solvePnP(points3d, points2dF, intrinsics, Mat(), R2, t2, false, method);
  907. Matx31d R1d(R1(0), R1(1), R1(2));
  908. Matx31d t2d(t2(0), t2(1), t2(2));
  909. EXPECT_LE(cvtest::norm(R1d, R2, NORM_INF), 1e-3);
  910. EXPECT_LE(cvtest::norm(t1, t2d, NORM_INF), 1e-3);
  911. EXPECT_LE(cvtest::norm(trueRvec, R1d, NORM_INF), 1e-3);
  912. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  913. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  914. EXPECT_LE(cvtest::norm(trueTvec, t2d, NORM_INF), 1e-3);
  915. }
  916. //solvePnPGeneric
  917. {
  918. vector<Mat> Rs, ts, RFs, tFs;
  919. int res1 = solvePnPGeneric(points3dF, points2dF, Matx33f(intrinsics), Mat(), RFs, tFs, false, (SolvePnPMethod)method);
  920. int res2 = solvePnPGeneric(points3d, points2d, intrinsics, Mat(), Rs, ts, false, (SolvePnPMethod)method);
  921. EXPECT_GT(res1, 0);
  922. EXPECT_GT(res2, 0);
  923. Mat R = Rs.front(), t = ts.front(), RF = RFs.front(), tF = tFs.front();
  924. //By default rvecs and tvecs must be returned in double precision
  925. EXPECT_EQ(RF.type(), tF.type());
  926. EXPECT_EQ(RF.type(), CV_64FC1);
  927. EXPECT_EQ(R.type(), t.type());
  928. EXPECT_EQ(R.type(), CV_64FC1);
  929. EXPECT_LE(cvtest::norm(R, RF, NORM_INF), 1e-3);
  930. EXPECT_LE(cvtest::norm(t, tF, NORM_INF), 1e-3);
  931. EXPECT_LE(cvtest::norm(trueRvec, R, NORM_INF), 1e-3);
  932. EXPECT_LE(cvtest::norm(trueTvec, t, NORM_INF), 1e-3);
  933. EXPECT_LE(cvtest::norm(trueRvec, RF, NORM_INF), 1e-3);
  934. EXPECT_LE(cvtest::norm(trueTvec, tF, NORM_INF), 1e-3);
  935. }
  936. {
  937. vector<Mat> R1s, t1s, R2s, t2s;
  938. int res1 = solvePnPGeneric(points3dF, points2d, intrinsics, Mat(), R1s, t1s, false, (SolvePnPMethod)method);
  939. int res2 = solvePnPGeneric(points3d, points2dF, intrinsics, Mat(), R2s, t2s, false, (SolvePnPMethod)method);
  940. EXPECT_GT(res1, 0);
  941. EXPECT_GT(res2, 0);
  942. Mat R1 = R1s.front(), t1 = t1s.front(), R2 = R2s.front(), t2 = t2s.front();
  943. //By default rvecs and tvecs must be returned in double precision
  944. EXPECT_EQ(R1.type(), t1.type());
  945. EXPECT_EQ(R1.type(), CV_64FC1);
  946. EXPECT_EQ(R2.type(), t2.type());
  947. EXPECT_EQ(R2.type(), CV_64FC1);
  948. EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-3);
  949. EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-3);
  950. EXPECT_LE(cvtest::norm(trueRvec, R1, NORM_INF), 1e-3);
  951. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  952. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  953. EXPECT_LE(cvtest::norm(trueTvec, t2, NORM_INF), 1e-3);
  954. }
  955. {
  956. vector<Mat_<float> > R1s, t2s;
  957. vector<Mat_<double> > R2s, t1s;
  958. int res1 = solvePnPGeneric(points3dF, points2d, intrinsics, Mat(), R1s, t1s, false, (SolvePnPMethod)method);
  959. int res2 = solvePnPGeneric(points3d, points2dF, intrinsics, Mat(), R2s, t2s, false, (SolvePnPMethod)method);
  960. EXPECT_GT(res1, 0);
  961. EXPECT_GT(res2, 0);
  962. Mat R1 = R1s.front(), t1 = t1s.front();
  963. Mat R2 = R2s.front(), t2 = t2s.front();
  964. //If not null, rvecs and tvecs must be returned in the same precision
  965. EXPECT_EQ(R1.type(), CV_32FC1);
  966. EXPECT_EQ(t1.type(), CV_64FC1);
  967. EXPECT_EQ(R2.type(), CV_64FC1);
  968. EXPECT_EQ(t2.type(), CV_32FC1);
  969. EXPECT_LE(cvtest::norm(Mat_<double>(R1), R2, NORM_INF), 1e-3);
  970. EXPECT_LE(cvtest::norm(t1, Mat_<double>(t2), NORM_INF), 1e-3);
  971. EXPECT_LE(cvtest::norm(trueRvec, Mat_<double>(R1), NORM_INF), 1e-3);
  972. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  973. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  974. EXPECT_LE(cvtest::norm(trueTvec, Mat_<double>(t2), NORM_INF), 1e-3);
  975. }
  976. {
  977. vector<Matx31f> R1s, t2s;
  978. vector<Matx31d> R2s, t1s;
  979. int res1 = solvePnPGeneric(points3dF, points2d, intrinsics, Mat(), R1s, t1s, false, (SolvePnPMethod)method);
  980. int res2 = solvePnPGeneric(points3d, points2dF, intrinsics, Mat(), R2s, t2s, false, (SolvePnPMethod)method);
  981. EXPECT_GT(res1, 0);
  982. EXPECT_GT(res2, 0);
  983. Matx31f R1 = R1s.front(), t2 = t2s.front();
  984. Matx31d R2 = R2s.front(), t1 = t1s.front();
  985. Matx31d R1d(R1(0), R1(1), R1(2)), t2d(t2(0), t2(1), t2(2));
  986. EXPECT_LE(cvtest::norm(R1d, R2, NORM_INF), 1e-3);
  987. EXPECT_LE(cvtest::norm(t1, t2d, NORM_INF), 1e-3);
  988. EXPECT_LE(cvtest::norm(trueRvec, R1d, NORM_INF), 1e-3);
  989. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  990. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  991. EXPECT_LE(cvtest::norm(trueTvec, t2d, NORM_INF), 1e-3);
  992. }
  993. if (method == SOLVEPNP_P3P || method == SOLVEPNP_AP3P)
  994. {
  995. //solveP3P
  996. {
  997. vector<Mat> Rs, ts, RFs, tFs;
  998. int res1 = solveP3P(points3dF, points2dF, Matx33f(intrinsics), Mat(), RFs, tFs, (SolvePnPMethod)method);
  999. int res2 = solveP3P(points3d, points2d, intrinsics, Mat(), Rs, ts, (SolvePnPMethod)method);
  1000. EXPECT_GT(res1, 0);
  1001. EXPECT_GT(res2, 0);
  1002. Mat R = Rs.front(), t = ts.front(), RF = RFs.front(), tF = tFs.front();
  1003. //By default rvecs and tvecs must be returned in double precision
  1004. EXPECT_EQ(RF.type(), tF.type());
  1005. EXPECT_EQ(RF.type(), CV_64FC1);
  1006. EXPECT_EQ(R.type(), t.type());
  1007. EXPECT_EQ(R.type(), CV_64FC1);
  1008. EXPECT_LE(cvtest::norm(R, RF, NORM_INF), 1e-3);
  1009. EXPECT_LE(cvtest::norm(t, tF, NORM_INF), 1e-3);
  1010. EXPECT_LE(cvtest::norm(trueRvec, R, NORM_INF), 1e-3);
  1011. EXPECT_LE(cvtest::norm(trueTvec, t, NORM_INF), 1e-3);
  1012. EXPECT_LE(cvtest::norm(trueRvec, RF, NORM_INF), 1e-3);
  1013. EXPECT_LE(cvtest::norm(trueTvec, tF, NORM_INF), 1e-3);
  1014. }
  1015. {
  1016. vector<Mat> R1s, t1s, R2s, t2s;
  1017. int res1 = solveP3P(points3dF, points2d, intrinsics, Mat(), R1s, t1s, (SolvePnPMethod)method);
  1018. int res2 = solveP3P(points3d, points2dF, intrinsics, Mat(), R2s, t2s, (SolvePnPMethod)method);
  1019. EXPECT_GT(res1, 0);
  1020. EXPECT_GT(res2, 0);
  1021. Mat R1 = R1s.front(), t1 = t1s.front(), R2 = R2s.front(), t2 = t2s.front();
  1022. //By default rvecs and tvecs must be returned in double precision
  1023. EXPECT_EQ(R1.type(), t1.type());
  1024. EXPECT_EQ(R1.type(), CV_64FC1);
  1025. EXPECT_EQ(R2.type(), t2.type());
  1026. EXPECT_EQ(R2.type(), CV_64FC1);
  1027. EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-3);
  1028. EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-3);
  1029. EXPECT_LE(cvtest::norm(trueRvec, R1, NORM_INF), 1e-3);
  1030. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  1031. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  1032. EXPECT_LE(cvtest::norm(trueTvec, t2, NORM_INF), 1e-3);
  1033. }
  1034. {
  1035. vector<Mat_<float> > R1s, t2s;
  1036. vector<Mat_<double> > R2s, t1s;
  1037. int res1 = solveP3P(points3dF, points2d, intrinsics, Mat(), R1s, t1s, (SolvePnPMethod)method);
  1038. int res2 = solveP3P(points3d, points2dF, intrinsics, Mat(), R2s, t2s, (SolvePnPMethod)method);
  1039. EXPECT_GT(res1, 0);
  1040. EXPECT_GT(res2, 0);
  1041. Mat R1 = R1s.front(), t1 = t1s.front();
  1042. Mat R2 = R2s.front(), t2 = t2s.front();
  1043. //If not null, rvecs and tvecs must be returned in the same precision
  1044. EXPECT_EQ(R1.type(), CV_32FC1);
  1045. EXPECT_EQ(t1.type(), CV_64FC1);
  1046. EXPECT_EQ(R2.type(), CV_64FC1);
  1047. EXPECT_EQ(t2.type(), CV_32FC1);
  1048. EXPECT_LE(cvtest::norm(Mat_<double>(R1), R2, NORM_INF), 1e-3);
  1049. EXPECT_LE(cvtest::norm(t1, Mat_<double>(t2), NORM_INF), 1e-3);
  1050. EXPECT_LE(cvtest::norm(trueRvec, Mat_<double>(R1), NORM_INF), 1e-3);
  1051. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  1052. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  1053. EXPECT_LE(cvtest::norm(trueTvec, Mat_<double>(t2), NORM_INF), 1e-3);
  1054. }
  1055. {
  1056. vector<Matx31f> R1s, t2s;
  1057. vector<Matx31d> R2s, t1s;
  1058. int res1 = solveP3P(points3dF, points2d, intrinsics, Mat(), R1s, t1s, (SolvePnPMethod)method);
  1059. int res2 = solveP3P(points3d, points2dF, intrinsics, Mat(), R2s, t2s, (SolvePnPMethod)method);
  1060. EXPECT_GT(res1, 0);
  1061. EXPECT_GT(res2, 0);
  1062. Matx31f R1 = R1s.front(), t2 = t2s.front();
  1063. Matx31d R2 = R2s.front(), t1 = t1s.front();
  1064. Matx31d R1d(R1(0), R1(1), R1(2)), t2d(t2(0), t2(1), t2(2));
  1065. EXPECT_LE(cvtest::norm(R1d, R2, NORM_INF), 1e-3);
  1066. EXPECT_LE(cvtest::norm(t1, t2d, NORM_INF), 1e-3);
  1067. EXPECT_LE(cvtest::norm(trueRvec, R1d, NORM_INF), 1e-3);
  1068. EXPECT_LE(cvtest::norm(trueTvec, t1, NORM_INF), 1e-3);
  1069. EXPECT_LE(cvtest::norm(trueRvec, R2, NORM_INF), 1e-3);
  1070. EXPECT_LE(cvtest::norm(trueTvec, t2d, NORM_INF), 1e-3);
  1071. }
  1072. }
  1073. }
  1074. }
  1075. TEST(Calib3d_SolvePnP, translation)
  1076. {
  1077. Mat cameraIntrinsic = Mat::eye(3,3, CV_32FC1);
  1078. vector<float> crvec;
  1079. crvec.push_back(0.f);
  1080. crvec.push_back(0.f);
  1081. crvec.push_back(0.f);
  1082. vector<float> ctvec;
  1083. ctvec.push_back(100.f);
  1084. ctvec.push_back(100.f);
  1085. ctvec.push_back(0.f);
  1086. vector<Point3f> p3d;
  1087. p3d.push_back(Point3f(0,0,0));
  1088. p3d.push_back(Point3f(0,0,10));
  1089. p3d.push_back(Point3f(0,10,10));
  1090. p3d.push_back(Point3f(10,10,10));
  1091. p3d.push_back(Point3f(2,5,5));
  1092. p3d.push_back(Point3f(-4,8,6));
  1093. vector<Point2f> p2d;
  1094. projectPoints(p3d, crvec, ctvec, cameraIntrinsic, noArray(), p2d);
  1095. Mat rvec;
  1096. Mat tvec;
  1097. rvec =(Mat_<float>(3,1) << 0, 0, 0);
  1098. tvec = (Mat_<float>(3,1) << 100, 100, 0);
  1099. solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);
  1100. EXPECT_TRUE(checkRange(rvec));
  1101. EXPECT_TRUE(checkRange(tvec));
  1102. rvec =(Mat_<double>(3,1) << 0, 0, 0);
  1103. tvec = (Mat_<double>(3,1) << 100, 100, 0);
  1104. solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);
  1105. EXPECT_TRUE(checkRange(rvec));
  1106. EXPECT_TRUE(checkRange(tvec));
  1107. solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, false);
  1108. EXPECT_TRUE(checkRange(rvec));
  1109. EXPECT_TRUE(checkRange(tvec));
  1110. }
  1111. TEST(Calib3d_SolvePnP, iterativeInitialGuess3pts)
  1112. {
  1113. {
  1114. Matx33d intrinsics(605.4, 0.0, 317.35,
  1115. 0.0, 601.2, 242.63,
  1116. 0.0, 0.0, 1.0);
  1117. double L = 0.1;
  1118. vector<Point3d> p3d;
  1119. p3d.push_back(Point3d(-L, -L, 0.0));
  1120. p3d.push_back(Point3d(L, -L, 0.0));
  1121. p3d.push_back(Point3d(L, L, 0.0));
  1122. Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
  1123. Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);
  1124. vector<Point2d> p2d;
  1125. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1126. Mat rvec_est = (Mat_<double>(3,1) << 0.2, -0.1, 0.6);
  1127. Mat tvec_est = (Mat_<double>(3,1) << 0.05, -0.05, 1.0);
  1128. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
  1129. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1130. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1131. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1132. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1133. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1134. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1135. EXPECT_EQ(rvec_est.type(), CV_64FC1);
  1136. EXPECT_EQ(tvec_est.type(), CV_64FC1);
  1137. }
  1138. {
  1139. Matx33f intrinsics(605.4f, 0.0f, 317.35f,
  1140. 0.0f, 601.2f, 242.63f,
  1141. 0.0f, 0.0f, 1.0f);
  1142. float L = 0.1f;
  1143. vector<Point3f> p3d;
  1144. p3d.push_back(Point3f(-L, -L, 0.0f));
  1145. p3d.push_back(Point3f(L, -L, 0.0f));
  1146. p3d.push_back(Point3f(L, L, 0.0f));
  1147. Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);
  1148. Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);
  1149. vector<Point2f> p2d;
  1150. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1151. Mat rvec_est = (Mat_<float>(3,1) << -0.5f, 0.2f, 0.2f);
  1152. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.2f, 1.0f);
  1153. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
  1154. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1155. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1156. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1157. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1158. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1159. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1160. EXPECT_EQ(rvec_est.type(), CV_32FC1);
  1161. EXPECT_EQ(tvec_est.type(), CV_32FC1);
  1162. }
  1163. }
  1164. TEST(Calib3d_SolvePnP, iterativeInitialGuess)
  1165. {
  1166. {
  1167. Matx33d intrinsics(605.4, 0.0, 317.35,
  1168. 0.0, 601.2, 242.63,
  1169. 0.0, 0.0, 1.0);
  1170. double L = 0.1;
  1171. vector<Point3d> p3d;
  1172. p3d.push_back(Point3d(-L, -L, 0.0));
  1173. p3d.push_back(Point3d(L, -L, 0.0));
  1174. p3d.push_back(Point3d(L, L, 0.0));
  1175. p3d.push_back(Point3d(-L, L, L/2));
  1176. p3d.push_back(Point3d(0, 0, -L/2));
  1177. Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
  1178. Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);
  1179. vector<Point2d> p2d;
  1180. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1181. Mat rvec_est = (Mat_<double>(3,1) << 0.1, -0.1, 0.1);
  1182. Mat tvec_est = (Mat_<double>(3,1) << 0.0, -0.5, 1.0);
  1183. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
  1184. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1185. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1186. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1187. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1188. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1189. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1190. EXPECT_EQ(rvec_est.type(), CV_64FC1);
  1191. EXPECT_EQ(tvec_est.type(), CV_64FC1);
  1192. }
  1193. {
  1194. Matx33f intrinsics(605.4f, 0.0f, 317.35f,
  1195. 0.0f, 601.2f, 242.63f,
  1196. 0.0f, 0.0f, 1.0f);
  1197. float L = 0.1f;
  1198. vector<Point3f> p3d;
  1199. p3d.push_back(Point3f(-L, -L, 0.0f));
  1200. p3d.push_back(Point3f(L, -L, 0.0f));
  1201. p3d.push_back(Point3f(L, L, 0.0f));
  1202. p3d.push_back(Point3f(-L, L, L/2));
  1203. p3d.push_back(Point3f(0, 0, -L/2));
  1204. Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);
  1205. Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);
  1206. vector<Point2f> p2d;
  1207. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1208. Mat rvec_est = (Mat_<float>(3,1) << -0.1f, 0.1f, 0.1f);
  1209. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.0f, 1.0f);
  1210. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
  1211. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1212. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1213. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1214. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1215. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1216. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1217. EXPECT_EQ(rvec_est.type(), CV_32FC1);
  1218. EXPECT_EQ(tvec_est.type(), CV_32FC1);
  1219. }
  1220. }
  1221. TEST(Calib3d_SolvePnP, generic)
  1222. {
  1223. {
  1224. Matx33d intrinsics(605.4, 0.0, 317.35,
  1225. 0.0, 601.2, 242.63,
  1226. 0.0, 0.0, 1.0);
  1227. double L = 0.1;
  1228. vector<Point3d> p3d_;
  1229. p3d_.push_back(Point3d(-L, L, 0));
  1230. p3d_.push_back(Point3d(L, L, 0));
  1231. p3d_.push_back(Point3d(L, -L, 0));
  1232. p3d_.push_back(Point3d(-L, -L, 0));
  1233. p3d_.push_back(Point3d(-L, L, L/2));
  1234. p3d_.push_back(Point3d(0, 0, -L/2));
  1235. const int ntests = 10;
  1236. for (int numTest = 0; numTest < ntests; numTest++)
  1237. {
  1238. Mat rvec_ground_truth;
  1239. Mat tvec_ground_truth;
  1240. generatePose(p3d_, rvec_ground_truth, tvec_ground_truth, theRNG());
  1241. vector<Point2d> p2d_;
  1242. projectPoints(p3d_, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d_);
  1243. for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)
  1244. {
  1245. vector<Mat> rvecs_est;
  1246. vector<Mat> tvecs_est;
  1247. vector<Point3d> p3d;
  1248. vector<Point2d> p2d;
  1249. if (method == SOLVEPNP_P3P || method == SOLVEPNP_AP3P ||
  1250. method == SOLVEPNP_IPPE || method == SOLVEPNP_IPPE_SQUARE)
  1251. {
  1252. p3d = vector<Point3d>(p3d_.begin(), p3d_.begin()+4);
  1253. p2d = vector<Point2d>(p2d_.begin(), p2d_.begin()+4);
  1254. }
  1255. else
  1256. {
  1257. p3d = p3d_;
  1258. p2d = p2d_;
  1259. }
  1260. vector<double> reprojectionErrors;
  1261. solvePnPGeneric(p3d, p2d, intrinsics, noArray(), rvecs_est, tvecs_est, false, (SolvePnPMethod)method,
  1262. noArray(), noArray(), reprojectionErrors);
  1263. EXPECT_TRUE(!rvecs_est.empty());
  1264. EXPECT_TRUE(rvecs_est.size() == tvecs_est.size() && tvecs_est.size() == reprojectionErrors.size());
  1265. for (size_t i = 0; i < reprojectionErrors.size()-1; i++)
  1266. {
  1267. EXPECT_GE(reprojectionErrors[i+1], reprojectionErrors[i]);
  1268. }
  1269. bool isTestSuccess = false;
  1270. for (size_t i = 0; i < rvecs_est.size() && !isTestSuccess; i++) {
  1271. double rvecDiff = cvtest::norm(rvecs_est[i], rvec_ground_truth, NORM_L2);
  1272. double tvecDiff = cvtest::norm(tvecs_est[i], tvec_ground_truth, NORM_L2);
  1273. const double threshold = 1e-4;
  1274. isTestSuccess = rvecDiff < threshold && tvecDiff < threshold;
  1275. }
  1276. EXPECT_TRUE(isTestSuccess);
  1277. }
  1278. }
  1279. }
  1280. {
  1281. Matx33f intrinsics(605.4f, 0.0f, 317.35f,
  1282. 0.0f, 601.2f, 242.63f,
  1283. 0.0f, 0.0f, 1.0f);
  1284. float L = 0.1f;
  1285. vector<Point3f> p3f_;
  1286. p3f_.push_back(Point3f(-L, L, 0));
  1287. p3f_.push_back(Point3f(L, L, 0));
  1288. p3f_.push_back(Point3f(L, -L, 0));
  1289. p3f_.push_back(Point3f(-L, -L, 0));
  1290. p3f_.push_back(Point3f(-L, L, L/2));
  1291. p3f_.push_back(Point3f(0, 0, -L/2));
  1292. const int ntests = 10;
  1293. for (int numTest = 0; numTest < ntests; numTest++)
  1294. {
  1295. Mat rvec_ground_truth;
  1296. Mat tvec_ground_truth;
  1297. generatePose(p3f_, rvec_ground_truth, tvec_ground_truth, theRNG());
  1298. vector<Point2f> p2f_;
  1299. projectPoints(p3f_, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2f_);
  1300. for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)
  1301. {
  1302. vector<Mat> rvecs_est;
  1303. vector<Mat> tvecs_est;
  1304. vector<Point3f> p3f;
  1305. vector<Point2f> p2f;
  1306. if (method == SOLVEPNP_P3P || method == SOLVEPNP_AP3P ||
  1307. method == SOLVEPNP_IPPE || method == SOLVEPNP_IPPE_SQUARE)
  1308. {
  1309. p3f = vector<Point3f>(p3f_.begin(), p3f_.begin()+4);
  1310. p2f = vector<Point2f>(p2f_.begin(), p2f_.begin()+4);
  1311. }
  1312. else
  1313. {
  1314. p3f = vector<Point3f>(p3f_.begin(), p3f_.end());
  1315. p2f = vector<Point2f>(p2f_.begin(), p2f_.end());
  1316. }
  1317. vector<double> reprojectionErrors;
  1318. solvePnPGeneric(p3f, p2f, intrinsics, noArray(), rvecs_est, tvecs_est, false, (SolvePnPMethod)method,
  1319. noArray(), noArray(), reprojectionErrors);
  1320. EXPECT_TRUE(!rvecs_est.empty());
  1321. EXPECT_TRUE(rvecs_est.size() == tvecs_est.size() && tvecs_est.size() == reprojectionErrors.size());
  1322. for (size_t i = 0; i < reprojectionErrors.size()-1; i++)
  1323. {
  1324. EXPECT_GE(reprojectionErrors[i+1], reprojectionErrors[i]);
  1325. }
  1326. bool isTestSuccess = false;
  1327. for (size_t i = 0; i < rvecs_est.size() && !isTestSuccess; i++) {
  1328. double rvecDiff = cvtest::norm(rvecs_est[i], rvec_ground_truth, NORM_L2);
  1329. double tvecDiff = cvtest::norm(tvecs_est[i], tvec_ground_truth, NORM_L2);
  1330. const double threshold = 1e-4;
  1331. isTestSuccess = rvecDiff < threshold && tvecDiff < threshold;
  1332. }
  1333. EXPECT_TRUE(isTestSuccess);
  1334. }
  1335. }
  1336. }
  1337. }
  1338. TEST(Calib3d_SolvePnP, refine3pts)
  1339. {
  1340. {
  1341. Matx33d intrinsics(605.4, 0.0, 317.35,
  1342. 0.0, 601.2, 242.63,
  1343. 0.0, 0.0, 1.0);
  1344. double L = 0.1;
  1345. vector<Point3d> p3d;
  1346. p3d.push_back(Point3d(-L, -L, 0.0));
  1347. p3d.push_back(Point3d(L, -L, 0.0));
  1348. p3d.push_back(Point3d(L, L, 0.0));
  1349. Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
  1350. Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);
  1351. vector<Point2d> p2d;
  1352. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1353. {
  1354. Mat rvec_est = (Mat_<double>(3,1) << 0.2, -0.1, 0.6);
  1355. Mat tvec_est = (Mat_<double>(3,1) << 0.05, -0.05, 1.0);
  1356. solvePnPRefineLM(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1357. cout << "\nmethod: Levenberg-Marquardt" << endl;
  1358. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1359. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1360. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1361. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1362. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1363. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1364. }
  1365. {
  1366. Mat rvec_est = (Mat_<double>(3,1) << 0.2, -0.1, 0.6);
  1367. Mat tvec_est = (Mat_<double>(3,1) << 0.05, -0.05, 1.0);
  1368. solvePnPRefineVVS(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1369. cout << "\nmethod: Virtual Visual Servoing" << endl;
  1370. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1371. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1372. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1373. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1374. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1375. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1376. }
  1377. }
  1378. {
  1379. Matx33f intrinsics(605.4f, 0.0f, 317.35f,
  1380. 0.0f, 601.2f, 242.63f,
  1381. 0.0f, 0.0f, 1.0f);
  1382. float L = 0.1f;
  1383. vector<Point3f> p3d;
  1384. p3d.push_back(Point3f(-L, -L, 0.0f));
  1385. p3d.push_back(Point3f(L, -L, 0.0f));
  1386. p3d.push_back(Point3f(L, L, 0.0f));
  1387. Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);
  1388. Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);
  1389. vector<Point2f> p2d;
  1390. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1391. {
  1392. Mat rvec_est = (Mat_<float>(3,1) << -0.5f, 0.2f, 0.2f);
  1393. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.2f, 1.0f);
  1394. solvePnPRefineLM(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1395. cout << "\nmethod: Levenberg-Marquardt" << endl;
  1396. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1397. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1398. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1399. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1400. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1401. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1402. }
  1403. {
  1404. Mat rvec_est = (Mat_<float>(3,1) << -0.5f, 0.2f, 0.2f);
  1405. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.2f, 1.0f);
  1406. solvePnPRefineVVS(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1407. cout << "\nmethod: Virtual Visual Servoing" << endl;
  1408. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1409. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1410. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1411. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1412. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1413. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1414. }
  1415. }
  1416. }
  1417. TEST(Calib3d_SolvePnP, refine)
  1418. {
  1419. //double
  1420. {
  1421. Matx33d intrinsics(605.4, 0.0, 317.35,
  1422. 0.0, 601.2, 242.63,
  1423. 0.0, 0.0, 1.0);
  1424. double L = 0.1;
  1425. vector<Point3d> p3d;
  1426. p3d.push_back(Point3d(-L, -L, 0.0));
  1427. p3d.push_back(Point3d(L, -L, 0.0));
  1428. p3d.push_back(Point3d(L, L, 0.0));
  1429. p3d.push_back(Point3d(-L, L, L/2));
  1430. p3d.push_back(Point3d(0, 0, -L/2));
  1431. Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
  1432. Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);
  1433. vector<Point2d> p2d;
  1434. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1435. {
  1436. Mat rvec_est = (Mat_<double>(3,1) << 0.1, -0.1, 0.1);
  1437. Mat tvec_est = (Mat_<double>(3,1) << 0.0, -0.5, 1.0);
  1438. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
  1439. cout << "\nmethod: Levenberg-Marquardt (C API)" << endl;
  1440. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1441. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1442. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1443. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1444. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1445. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1446. }
  1447. {
  1448. Mat rvec_est = (Mat_<double>(3,1) << 0.1, -0.1, 0.1);
  1449. Mat tvec_est = (Mat_<double>(3,1) << 0.0, -0.5, 1.0);
  1450. solvePnPRefineLM(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1451. cout << "\nmethod: Levenberg-Marquardt (C++ API)" << endl;
  1452. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1453. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1454. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1455. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1456. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1457. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1458. }
  1459. {
  1460. Mat rvec_est = (Mat_<double>(3,1) << 0.1, -0.1, 0.1);
  1461. Mat tvec_est = (Mat_<double>(3,1) << 0.0, -0.5, 1.0);
  1462. solvePnPRefineVVS(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1463. cout << "\nmethod: Virtual Visual Servoing" << endl;
  1464. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1465. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1466. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1467. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1468. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1469. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1470. }
  1471. }
  1472. //float
  1473. {
  1474. Matx33f intrinsics(605.4f, 0.0f, 317.35f,
  1475. 0.0f, 601.2f, 242.63f,
  1476. 0.0f, 0.0f, 1.0f);
  1477. float L = 0.1f;
  1478. vector<Point3f> p3d;
  1479. p3d.push_back(Point3f(-L, -L, 0.0f));
  1480. p3d.push_back(Point3f(L, -L, 0.0f));
  1481. p3d.push_back(Point3f(L, L, 0.0f));
  1482. p3d.push_back(Point3f(-L, L, L/2));
  1483. p3d.push_back(Point3f(0, 0, -L/2));
  1484. Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);
  1485. Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);
  1486. vector<Point2f> p2d;
  1487. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1488. {
  1489. Mat rvec_est = (Mat_<float>(3,1) << -0.1f, 0.1f, 0.1f);
  1490. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.0f, 1.0f);
  1491. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);
  1492. cout << "\nmethod: Levenberg-Marquardt (C API)" << endl;
  1493. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1494. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1495. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1496. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1497. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1498. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1499. }
  1500. {
  1501. Mat rvec_est = (Mat_<float>(3,1) << -0.1f, 0.1f, 0.1f);
  1502. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.0f, 1.0f);
  1503. solvePnPRefineLM(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1504. cout << "\nmethod: Levenberg-Marquardt (C++ API)" << endl;
  1505. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1506. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1507. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1508. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1509. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1510. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1511. }
  1512. {
  1513. Mat rvec_est = (Mat_<float>(3,1) << -0.1f, 0.1f, 0.1f);
  1514. Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.0f, 1.0f);
  1515. solvePnPRefineVVS(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est);
  1516. cout << "\nmethod: Virtual Visual Servoing" << endl;
  1517. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1518. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1519. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1520. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1521. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
  1522. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
  1523. }
  1524. }
  1525. //refine after solvePnP
  1526. {
  1527. Matx33d intrinsics(605.4, 0.0, 317.35,
  1528. 0.0, 601.2, 242.63,
  1529. 0.0, 0.0, 1.0);
  1530. double L = 0.1;
  1531. vector<Point3d> p3d;
  1532. p3d.push_back(Point3d(-L, -L, 0.0));
  1533. p3d.push_back(Point3d(L, -L, 0.0));
  1534. p3d.push_back(Point3d(L, L, 0.0));
  1535. p3d.push_back(Point3d(-L, L, L/2));
  1536. p3d.push_back(Point3d(0, 0, -L/2));
  1537. Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
  1538. Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);
  1539. vector<Point2d> p2d;
  1540. projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);
  1541. //add small Gaussian noise
  1542. RNG& rng = theRNG();
  1543. for (size_t i = 0; i < p2d.size(); i++)
  1544. {
  1545. p2d[i].x += rng.gaussian(5e-2);
  1546. p2d[i].y += rng.gaussian(5e-2);
  1547. }
  1548. Mat rvec_est, tvec_est;
  1549. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, false, SOLVEPNP_EPNP);
  1550. {
  1551. Mat rvec_est_refine = rvec_est.clone(), tvec_est_refine = tvec_est.clone();
  1552. solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est_refine, tvec_est_refine, true, SOLVEPNP_ITERATIVE);
  1553. cout << "\nmethod: Levenberg-Marquardt (C API)" << endl;
  1554. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1555. cout << "rvec_est (EPnP): " << rvec_est.t() << std::endl;
  1556. cout << "rvec_est_refine: " << rvec_est_refine.t() << std::endl;
  1557. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1558. cout << "tvec_est (EPnP): " << tvec_est.t() << std::endl;
  1559. cout << "tvec_est_refine: " << tvec_est_refine.t() << std::endl;
  1560. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-2);
  1561. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-3);
  1562. EXPECT_LT(cvtest::norm(rvec_ground_truth, rvec_est_refine, NORM_INF), cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF));
  1563. EXPECT_LT(cvtest::norm(tvec_ground_truth, tvec_est_refine, NORM_INF), cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF));
  1564. }
  1565. {
  1566. Mat rvec_est_refine = rvec_est.clone(), tvec_est_refine = tvec_est.clone();
  1567. solvePnPRefineLM(p3d, p2d, intrinsics, noArray(), rvec_est_refine, tvec_est_refine);
  1568. cout << "\nmethod: Levenberg-Marquardt (C++ API)" << endl;
  1569. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1570. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1571. cout << "rvec_est_refine: " << rvec_est_refine.t() << std::endl;
  1572. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1573. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1574. cout << "tvec_est_refine: " << tvec_est_refine.t() << std::endl;
  1575. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-2);
  1576. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-3);
  1577. EXPECT_LT(cvtest::norm(rvec_ground_truth, rvec_est_refine, NORM_INF), cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF));
  1578. EXPECT_LT(cvtest::norm(tvec_ground_truth, tvec_est_refine, NORM_INF), cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF));
  1579. }
  1580. {
  1581. Mat rvec_est_refine = rvec_est.clone(), tvec_est_refine = tvec_est.clone();
  1582. solvePnPRefineVVS(p3d, p2d, intrinsics, noArray(), rvec_est_refine, tvec_est_refine);
  1583. cout << "\nmethod: Virtual Visual Servoing" << endl;
  1584. cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
  1585. cout << "rvec_est: " << rvec_est.t() << std::endl;
  1586. cout << "rvec_est_refine: " << rvec_est_refine.t() << std::endl;
  1587. cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
  1588. cout << "tvec_est: " << tvec_est.t() << std::endl;
  1589. cout << "tvec_est_refine: " << tvec_est_refine.t() << std::endl;
  1590. EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-2);
  1591. EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-3);
  1592. EXPECT_LT(cvtest::norm(rvec_ground_truth, rvec_est_refine, NORM_INF), cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF));
  1593. EXPECT_LT(cvtest::norm(tvec_ground_truth, tvec_est_refine, NORM_INF), cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF));
  1594. }
  1595. }
  1596. }
  1597. TEST(Calib3d_SolvePnPRansac, minPoints)
  1598. {
  1599. //https://github.com/opencv/opencv/issues/14423
  1600. Mat matK = Mat::eye(3,3,CV_64FC1);
  1601. Mat distCoeff = Mat::zeros(1,5,CV_64FC1);
  1602. Matx31d true_rvec(0.9072420896651262, 0.09226497171882152, 0.8880772883671504);
  1603. Matx31d true_tvec(7.376333362427632, 8.434449036856979, 13.79801619778456);
  1604. {
  1605. //nb points = 5 --> ransac_kernel_method = SOLVEPNP_EPNP
  1606. Mat keypoints13D = (Mat_<float>(5, 3) << 12.00604, -2.8654366, 18.472504,
  1607. 7.6863389, 4.9355154, 11.146358,
  1608. 14.260933, 2.8320458, 12.582781,
  1609. 3.4562225, 8.2668982, 11.300434,
  1610. 15.316854, 3.7486348, 12.491116);
  1611. vector<Point2f> imagesPoints;
  1612. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1613. Mat keypoints22D(keypoints13D.rows, 2, CV_32FC1);
  1614. vector<Point3f> objectPoints;
  1615. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1616. {
  1617. keypoints22D.at<float>(i,0) = imagesPoints[i].x;
  1618. keypoints22D.at<float>(i,1) = imagesPoints[i].y;
  1619. objectPoints.push_back(Point3f(keypoints13D.at<float>(i,0), keypoints13D.at<float>(i,1), keypoints13D.at<float>(i,2)));
  1620. }
  1621. Mat rvec = Mat::zeros(1,3,CV_64FC1);
  1622. Mat Tvec = Mat::zeros(1,3,CV_64FC1);
  1623. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1624. Mat rvec2, Tvec2;
  1625. solvePnP(objectPoints, imagesPoints, matK, distCoeff, rvec2, Tvec2, false, SOLVEPNP_EPNP);
  1626. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-4);
  1627. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-4);
  1628. EXPECT_LE(cvtest::norm(rvec, rvec2, NORM_INF), 1e-6);
  1629. EXPECT_LE(cvtest::norm(Tvec, Tvec2, NORM_INF), 1e-6);
  1630. }
  1631. {
  1632. //nb points = 4 --> ransac_kernel_method = SOLVEPNP_P3P
  1633. Mat keypoints13D = (Mat_<float>(4, 3) << 12.00604, -2.8654366, 18.472504,
  1634. 7.6863389, 4.9355154, 11.146358,
  1635. 14.260933, 2.8320458, 12.582781,
  1636. 3.4562225, 8.2668982, 11.300434);
  1637. vector<Point2f> imagesPoints;
  1638. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1639. Mat keypoints22D(keypoints13D.rows, 2, CV_32FC1);
  1640. vector<Point3f> objectPoints;
  1641. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1642. {
  1643. keypoints22D.at<float>(i,0) = imagesPoints[i].x;
  1644. keypoints22D.at<float>(i,1) = imagesPoints[i].y;
  1645. objectPoints.push_back(Point3f(keypoints13D.at<float>(i,0), keypoints13D.at<float>(i,1), keypoints13D.at<float>(i,2)));
  1646. }
  1647. Mat rvec = Mat::zeros(1,3,CV_64FC1);
  1648. Mat Tvec = Mat::zeros(1,3,CV_64FC1);
  1649. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1650. Mat rvec2, Tvec2;
  1651. solvePnP(objectPoints, imagesPoints, matK, distCoeff, rvec2, Tvec2, false, SOLVEPNP_P3P);
  1652. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-4);
  1653. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-4);
  1654. EXPECT_LE(cvtest::norm(rvec, rvec2, NORM_INF), 1e-6);
  1655. EXPECT_LE(cvtest::norm(Tvec, Tvec2, NORM_INF), 1e-6);
  1656. }
  1657. }
  1658. TEST(Calib3d_SolvePnPRansac, inputShape)
  1659. {
  1660. //https://github.com/opencv/opencv/issues/14423
  1661. Mat matK = Mat::eye(3,3,CV_64FC1);
  1662. Mat distCoeff = Mat::zeros(1,5,CV_64FC1);
  1663. Matx31d true_rvec(0.9072420896651262, 0.09226497171882152, 0.8880772883671504);
  1664. Matx31d true_tvec(7.376333362427632, 8.434449036856979, 13.79801619778456);
  1665. {
  1666. //Nx3 1-channel
  1667. Mat keypoints13D = (Mat_<float>(6, 3) << 12.00604, -2.8654366, 18.472504,
  1668. 7.6863389, 4.9355154, 11.146358,
  1669. 14.260933, 2.8320458, 12.582781,
  1670. 3.4562225, 8.2668982, 11.300434,
  1671. 10.00604, 2.8654366, 15.472504,
  1672. -4.6863389, 5.9355154, 13.146358);
  1673. vector<Point2f> imagesPoints;
  1674. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1675. Mat keypoints22D(keypoints13D.rows, 2, CV_32FC1);
  1676. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1677. {
  1678. keypoints22D.at<float>(i,0) = imagesPoints[i].x;
  1679. keypoints22D.at<float>(i,1) = imagesPoints[i].y;
  1680. }
  1681. Mat rvec, Tvec;
  1682. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1683. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-6);
  1684. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-6);
  1685. }
  1686. {
  1687. //1xN 3-channel
  1688. Mat keypoints13D(1, 6, CV_32FC3);
  1689. keypoints13D.at<Vec3f>(0,0) = Vec3f(12.00604f, -2.8654366f, 18.472504f);
  1690. keypoints13D.at<Vec3f>(0,1) = Vec3f(7.6863389f, 4.9355154f, 11.146358f);
  1691. keypoints13D.at<Vec3f>(0,2) = Vec3f(14.260933f, 2.8320458f, 12.582781f);
  1692. keypoints13D.at<Vec3f>(0,3) = Vec3f(3.4562225f, 8.2668982f, 11.300434f);
  1693. keypoints13D.at<Vec3f>(0,4) = Vec3f(10.00604f, 2.8654366f, 15.472504f);
  1694. keypoints13D.at<Vec3f>(0,5) = Vec3f(-4.6863389f, 5.9355154f, 13.146358f);
  1695. vector<Point2f> imagesPoints;
  1696. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1697. Mat keypoints22D(keypoints13D.rows, keypoints13D.cols, CV_32FC2);
  1698. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1699. {
  1700. keypoints22D.at<Vec2f>(0,i) = Vec2f(imagesPoints[i].x, imagesPoints[i].y);
  1701. }
  1702. Mat rvec, Tvec;
  1703. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1704. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-6);
  1705. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-6);
  1706. }
  1707. {
  1708. //Nx1 3-channel
  1709. Mat keypoints13D(6, 1, CV_32FC3);
  1710. keypoints13D.at<Vec3f>(0,0) = Vec3f(12.00604f, -2.8654366f, 18.472504f);
  1711. keypoints13D.at<Vec3f>(1,0) = Vec3f(7.6863389f, 4.9355154f, 11.146358f);
  1712. keypoints13D.at<Vec3f>(2,0) = Vec3f(14.260933f, 2.8320458f, 12.582781f);
  1713. keypoints13D.at<Vec3f>(3,0) = Vec3f(3.4562225f, 8.2668982f, 11.300434f);
  1714. keypoints13D.at<Vec3f>(4,0) = Vec3f(10.00604f, 2.8654366f, 15.472504f);
  1715. keypoints13D.at<Vec3f>(5,0) = Vec3f(-4.6863389f, 5.9355154f, 13.146358f);
  1716. vector<Point2f> imagesPoints;
  1717. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1718. Mat keypoints22D(keypoints13D.rows, keypoints13D.cols, CV_32FC2);
  1719. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1720. {
  1721. keypoints22D.at<Vec2f>(i,0) = Vec2f(imagesPoints[i].x, imagesPoints[i].y);
  1722. }
  1723. Mat rvec, Tvec;
  1724. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1725. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-6);
  1726. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-6);
  1727. }
  1728. {
  1729. //vector<Point3f>
  1730. vector<Point3f> keypoints13D;
  1731. keypoints13D.push_back(Point3f(12.00604f, -2.8654366f, 18.472504f));
  1732. keypoints13D.push_back(Point3f(7.6863389f, 4.9355154f, 11.146358f));
  1733. keypoints13D.push_back(Point3f(14.260933f, 2.8320458f, 12.582781f));
  1734. keypoints13D.push_back(Point3f(3.4562225f, 8.2668982f, 11.300434f));
  1735. keypoints13D.push_back(Point3f(10.00604f, 2.8654366f, 15.472504f));
  1736. keypoints13D.push_back(Point3f(-4.6863389f, 5.9355154f, 13.146358f));
  1737. vector<Point2f> keypoints22D;
  1738. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, keypoints22D);
  1739. Mat rvec, Tvec;
  1740. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1741. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-6);
  1742. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-6);
  1743. }
  1744. {
  1745. //vector<Point3d>
  1746. vector<Point3d> keypoints13D;
  1747. keypoints13D.push_back(Point3d(12.00604f, -2.8654366f, 18.472504f));
  1748. keypoints13D.push_back(Point3d(7.6863389f, 4.9355154f, 11.146358f));
  1749. keypoints13D.push_back(Point3d(14.260933f, 2.8320458f, 12.582781f));
  1750. keypoints13D.push_back(Point3d(3.4562225f, 8.2668982f, 11.300434f));
  1751. keypoints13D.push_back(Point3d(10.00604f, 2.8654366f, 15.472504f));
  1752. keypoints13D.push_back(Point3d(-4.6863389f, 5.9355154f, 13.146358f));
  1753. vector<Point2d> keypoints22D;
  1754. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, keypoints22D);
  1755. Mat rvec, Tvec;
  1756. solvePnPRansac(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec);
  1757. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-6);
  1758. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-6);
  1759. }
  1760. }
  1761. TEST(Calib3d_SolvePnP, inputShape)
  1762. {
  1763. //https://github.com/opencv/opencv/issues/14423
  1764. Mat matK = Mat::eye(3,3,CV_64FC1);
  1765. Mat distCoeff = Mat::zeros(1,5,CV_64FC1);
  1766. Matx31d true_rvec(0.407, 0.092, 0.88);
  1767. Matx31d true_tvec(0.576, -0.43, 1.3798);
  1768. vector<Point3d> objectPoints;
  1769. const double L = 0.5;
  1770. objectPoints.push_back(Point3d(-L, -L, L));
  1771. objectPoints.push_back(Point3d( L, -L, L));
  1772. objectPoints.push_back(Point3d( L, L, L));
  1773. objectPoints.push_back(Point3d(-L, L, L));
  1774. objectPoints.push_back(Point3d(-L, -L, -L));
  1775. objectPoints.push_back(Point3d( L, -L, -L));
  1776. const int methodsCount = 6;
  1777. int methods[] = {SOLVEPNP_ITERATIVE, SOLVEPNP_EPNP, SOLVEPNP_P3P, SOLVEPNP_AP3P, SOLVEPNP_IPPE, SOLVEPNP_IPPE_SQUARE};
  1778. for (int method = 0; method < methodsCount; method++)
  1779. {
  1780. if (methods[method] == SOLVEPNP_IPPE_SQUARE)
  1781. {
  1782. objectPoints[0] = Point3d(-L, L, 0);
  1783. objectPoints[1] = Point3d( L, L, 0);
  1784. objectPoints[2] = Point3d( L, -L, 0);
  1785. objectPoints[3] = Point3d(-L, -L, 0);
  1786. }
  1787. {
  1788. //Nx3 1-channel
  1789. Mat keypoints13D;
  1790. if (methods[method] == SOLVEPNP_P3P || methods[method] == SOLVEPNP_AP3P ||
  1791. methods[method] == SOLVEPNP_IPPE || methods[method] == SOLVEPNP_IPPE_SQUARE)
  1792. {
  1793. keypoints13D = Mat(4, 3, CV_32FC1);
  1794. }
  1795. else
  1796. {
  1797. keypoints13D = Mat(6, 3, CV_32FC1);
  1798. }
  1799. for (int i = 0; i < keypoints13D.rows; i++)
  1800. {
  1801. keypoints13D.at<float>(i,0) = static_cast<float>(objectPoints[i].x);
  1802. keypoints13D.at<float>(i,1) = static_cast<float>(objectPoints[i].y);
  1803. keypoints13D.at<float>(i,2) = static_cast<float>(objectPoints[i].z);
  1804. }
  1805. vector<Point2f> imagesPoints;
  1806. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1807. Mat keypoints22D(keypoints13D.rows, 2, CV_32FC1);
  1808. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1809. {
  1810. keypoints22D.at<float>(i,0) = imagesPoints[i].x;
  1811. keypoints22D.at<float>(i,1) = imagesPoints[i].y;
  1812. }
  1813. Mat rvec, Tvec;
  1814. solvePnP(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec, false, methods[method]);
  1815. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-3);
  1816. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-3);
  1817. }
  1818. {
  1819. //1xN 3-channel
  1820. Mat keypoints13D;
  1821. if (methods[method] == SOLVEPNP_P3P || methods[method] == SOLVEPNP_AP3P ||
  1822. methods[method] == SOLVEPNP_IPPE || methods[method] == SOLVEPNP_IPPE_SQUARE)
  1823. {
  1824. keypoints13D = Mat(1, 4, CV_32FC3);
  1825. }
  1826. else
  1827. {
  1828. keypoints13D = Mat(1, 6, CV_32FC3);
  1829. }
  1830. for (int i = 0; i < keypoints13D.cols; i++)
  1831. {
  1832. keypoints13D.at<Vec3f>(0,i) = Vec3f(static_cast<float>(objectPoints[i].x),
  1833. static_cast<float>(objectPoints[i].y),
  1834. static_cast<float>(objectPoints[i].z));
  1835. }
  1836. vector<Point2f> imagesPoints;
  1837. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1838. Mat keypoints22D(keypoints13D.rows, keypoints13D.cols, CV_32FC2);
  1839. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1840. {
  1841. keypoints22D.at<Vec2f>(0,i) = Vec2f(imagesPoints[i].x, imagesPoints[i].y);
  1842. }
  1843. Mat rvec, Tvec;
  1844. solvePnP(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec, false, methods[method]);
  1845. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-3);
  1846. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-3);
  1847. }
  1848. {
  1849. //Nx1 3-channel
  1850. Mat keypoints13D;
  1851. if (methods[method] == SOLVEPNP_P3P || methods[method] == SOLVEPNP_AP3P ||
  1852. methods[method] == SOLVEPNP_IPPE || methods[method] == SOLVEPNP_IPPE_SQUARE)
  1853. {
  1854. keypoints13D = Mat(4, 1, CV_32FC3);
  1855. }
  1856. else
  1857. {
  1858. keypoints13D = Mat(6, 1, CV_32FC3);
  1859. }
  1860. for (int i = 0; i < keypoints13D.rows; i++)
  1861. {
  1862. keypoints13D.at<Vec3f>(i,0) = Vec3f(static_cast<float>(objectPoints[i].x),
  1863. static_cast<float>(objectPoints[i].y),
  1864. static_cast<float>(objectPoints[i].z));
  1865. }
  1866. vector<Point2f> imagesPoints;
  1867. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, imagesPoints);
  1868. Mat keypoints22D(keypoints13D.rows, keypoints13D.cols, CV_32FC2);
  1869. for (int i = 0; i < static_cast<int>(imagesPoints.size()); i++)
  1870. {
  1871. keypoints22D.at<Vec2f>(i,0) = Vec2f(imagesPoints[i].x, imagesPoints[i].y);
  1872. }
  1873. Mat rvec, Tvec;
  1874. solvePnP(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec, false, methods[method]);
  1875. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-3);
  1876. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-3);
  1877. }
  1878. {
  1879. //vector<Point3f>
  1880. vector<Point3f> keypoints13D;
  1881. const int nbPts = (methods[method] == SOLVEPNP_P3P || methods[method] == SOLVEPNP_AP3P ||
  1882. methods[method] == SOLVEPNP_IPPE || methods[method] == SOLVEPNP_IPPE_SQUARE) ? 4 : 6;
  1883. for (int i = 0; i < nbPts; i++)
  1884. {
  1885. keypoints13D.push_back(Point3f(static_cast<float>(objectPoints[i].x),
  1886. static_cast<float>(objectPoints[i].y),
  1887. static_cast<float>(objectPoints[i].z)));
  1888. }
  1889. vector<Point2f> keypoints22D;
  1890. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, keypoints22D);
  1891. Mat rvec, Tvec;
  1892. solvePnP(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec, false, methods[method]);
  1893. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-3);
  1894. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-3);
  1895. }
  1896. {
  1897. //vector<Point3d>
  1898. vector<Point3d> keypoints13D;
  1899. const int nbPts = (methods[method] == SOLVEPNP_P3P || methods[method] == SOLVEPNP_AP3P ||
  1900. methods[method] == SOLVEPNP_IPPE || methods[method] == SOLVEPNP_IPPE_SQUARE) ? 4 : 6;
  1901. for (int i = 0; i < nbPts; i++)
  1902. {
  1903. keypoints13D.push_back(objectPoints[i]);
  1904. }
  1905. vector<Point2d> keypoints22D;
  1906. projectPoints(keypoints13D, true_rvec, true_tvec, matK, distCoeff, keypoints22D);
  1907. Mat rvec, Tvec;
  1908. solvePnP(keypoints13D, keypoints22D, matK, distCoeff, rvec, Tvec, false, methods[method]);
  1909. EXPECT_LE(cvtest::norm(true_rvec, rvec, NORM_INF), 1e-3);
  1910. EXPECT_LE(cvtest::norm(true_tvec, Tvec, NORM_INF), 1e-3);
  1911. }
  1912. }
  1913. }
  1914. bool hasNan(const cv::Mat& mat)
  1915. {
  1916. bool has = false;
  1917. if (mat.type() == CV_32F)
  1918. {
  1919. for(int i = 0; i < static_cast<int>(mat.total()); i++)
  1920. has |= cvIsNaN(mat.at<float>(i)) != 0;
  1921. }
  1922. else if (mat.type() == CV_64F)
  1923. {
  1924. for(int i = 0; i < static_cast<int>(mat.total()); i++)
  1925. has |= cvIsNaN(mat.at<double>(i)) != 0;
  1926. }
  1927. else
  1928. {
  1929. has = true;
  1930. CV_LOG_ERROR(NULL, "check hasNan called with unsupported type!");
  1931. }
  1932. return has;
  1933. }
  1934. TEST(AP3P, ctheta1p_nan_23607)
  1935. {
  1936. // the task is not well defined and may not converge (empty R, t) or should
  1937. // converge to some non-NaN solution
  1938. const std::array<cv::Point2d, 3> cameraPts = {
  1939. cv::Point2d{0.042784865945577621, 0.59844839572906494},
  1940. cv::Point2d{-0.028428621590137482, 0.60354739427566528},
  1941. cv::Point2d{0.0046037044376134872, 0.70674681663513184}
  1942. };
  1943. const std::array<cv::Point3d, 3> modelPts = {
  1944. cv::Point3d{-0.043258000165224075, 0.020459245890378952, -0.0069921980611979961},
  1945. cv::Point3d{-0.045648999512195587, 0.0029820732306689024, 0.0079000638797879219},
  1946. cv::Point3d{-0.043276999145746231, -0.013622495345771313, 0.0080113131552934647}
  1947. };
  1948. std::vector<Mat> R, t;
  1949. solveP3P(modelPts, cameraPts, Mat::eye(3, 3, CV_64F), Mat(), R, t, SOLVEPNP_AP3P);
  1950. EXPECT_EQ(R.size(), 2ul);
  1951. EXPECT_EQ(t.size(), 2ul);
  1952. // Try apply rvec and tvec to get model points from camera points.
  1953. Mat pts = Mat(modelPts).reshape(1, 3);
  1954. Mat expected = Mat(cameraPts).reshape(1, 3);
  1955. for (size_t i = 0; i < R.size(); ++i) {
  1956. EXPECT_TRUE(!hasNan(R[i]));
  1957. EXPECT_TRUE(!hasNan(t[i]));
  1958. Mat transform;
  1959. cv::Rodrigues(R[i], transform);
  1960. Mat res = pts * transform.t();
  1961. for (int j = 0; j < 3; ++j) {
  1962. res.row(j) += t[i].reshape(1, 1);
  1963. res.row(j) /= res.row(j).at<double>(2);
  1964. }
  1965. EXPECT_LE(cvtest::norm(res.colRange(0, 2), expected, NORM_INF), 3.34e-16);
  1966. }
  1967. }
  1968. }} // namespace