test_solvepnp_ransac.cpp 89 KB

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