test_fisheye.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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-2011, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #include <opencv2/ts/cuda_test.hpp> // EXPECT_MAT_NEAR
  44. #include "../src/fisheye.hpp"
  45. #include "opencv2/videoio.hpp"
  46. namespace opencv_test { namespace {
  47. class fisheyeTest : public ::testing::Test {
  48. protected:
  49. const static cv::Size imageSize;
  50. const static cv::Matx33d K;
  51. const static cv::Vec4d D;
  52. const static cv::Matx33d R;
  53. const static cv::Vec3d T;
  54. std::string datasets_repository_path;
  55. virtual void SetUp() {
  56. datasets_repository_path = combine(cvtest::TS::ptr()->get_data_path(), "cv/cameracalibration/fisheye");
  57. }
  58. protected:
  59. std::string combine(const std::string& _item1, const std::string& _item2);
  60. static void merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged);
  61. };
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. /// TESTS::
  64. TEST_F(fisheyeTest, projectPoints)
  65. {
  66. double cols = this->imageSize.width,
  67. rows = this->imageSize.height;
  68. const int N = 20;
  69. cv::Mat distorted0(1, N*N, CV_64FC2), undist1, undist2, distorted1, distorted2;
  70. undist2.create(distorted0.size(), CV_MAKETYPE(distorted0.depth(), 3));
  71. cv::Vec2d* pts = distorted0.ptr<cv::Vec2d>();
  72. cv::Vec2d c(this->K(0, 2), this->K(1, 2));
  73. for(int y = 0, k = 0; y < N; ++y)
  74. for(int x = 0; x < N; ++x)
  75. {
  76. cv::Vec2d point(x*cols/(N-1.f), y*rows/(N-1.f));
  77. pts[k++] = (point - c) * 0.85 + c;
  78. }
  79. cv::fisheye::undistortPoints(distorted0, undist1, this->K, this->D);
  80. cv::Vec2d* u1 = undist1.ptr<cv::Vec2d>();
  81. cv::Vec3d* u2 = undist2.ptr<cv::Vec3d>();
  82. for(int i = 0; i < (int)distorted0.total(); ++i)
  83. u2[i] = cv::Vec3d(u1[i][0], u1[i][1], 1.0);
  84. cv::fisheye::distortPoints(undist1, distorted1, this->K, this->D);
  85. cv::fisheye::projectPoints(undist2, distorted2, cv::Vec3d::all(0), cv::Vec3d::all(0), this->K, this->D);
  86. EXPECT_MAT_NEAR(distorted0, distorted1, 1e-10);
  87. EXPECT_MAT_NEAR(distorted0, distorted2, 1e-10);
  88. }
  89. TEST_F(fisheyeTest, distortUndistortPoints)
  90. {
  91. int width = imageSize.width;
  92. int height = imageSize.height;
  93. /* Create test points */
  94. cv::Mat principalPoints = (cv::Mat_<double>(5, 2) << K(0, 2), K(1, 2), // (cx, cy)
  95. /* Image corners */
  96. 0, 0,
  97. 0, height,
  98. width, 0,
  99. width, height
  100. );
  101. /* Random points inside image */
  102. cv::Mat xy[2] = {};
  103. xy[0].create(100, 1, CV_64F);
  104. theRNG().fill(xy[0], cv::RNG::UNIFORM, 0, width); // x
  105. xy[1].create(100, 1, CV_64F);
  106. theRNG().fill(xy[1], cv::RNG::UNIFORM, 0, height); // y
  107. cv::Mat randomPoints;
  108. merge(xy, 2, randomPoints);
  109. cv::Mat points0;
  110. cv::vconcat(principalPoints.reshape(2), randomPoints, points0);
  111. /* Test with random D set */
  112. for (size_t i = 0; i < 10; ++i) {
  113. cv::Mat distortion(1, 4, CV_64F);
  114. theRNG().fill(distortion, cv::RNG::UNIFORM, -0.00001, 0.00001);
  115. /* Distort -> Undistort */
  116. cv::Mat distortedPoints;
  117. cv::fisheye::distortPoints(points0, distortedPoints, K, distortion);
  118. cv::Mat undistortedPoints;
  119. cv::fisheye::undistortPoints(distortedPoints, undistortedPoints, K, distortion);
  120. EXPECT_MAT_NEAR(points0, undistortedPoints, 1e-8);
  121. /* Undistort -> Distort */
  122. cv::fisheye::undistortPoints(points0, undistortedPoints, K, distortion);
  123. cv::fisheye::distortPoints(undistortedPoints, distortedPoints, K, distortion);
  124. EXPECT_MAT_NEAR(points0, distortedPoints, 1e-8);
  125. }
  126. }
  127. TEST_F(fisheyeTest, distortUndistortPointsNewCameraFixed)
  128. {
  129. int width = imageSize.width;
  130. int height = imageSize.height;
  131. /* Random points inside image */
  132. cv::Mat xy[2] = {};
  133. xy[0].create(100, 1, CV_64F);
  134. theRNG().fill(xy[0], cv::RNG::UNIFORM, 0, width); // x
  135. xy[1].create(100, 1, CV_64F);
  136. theRNG().fill(xy[1], cv::RNG::UNIFORM, 0, height); // y
  137. cv::Mat randomPoints;
  138. merge(xy, 2, randomPoints);
  139. cv::Mat points0 = randomPoints;
  140. cv::Mat Reye = cv::Mat::eye(3, 3, CV_64FC1);
  141. cv::Mat Knew;
  142. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(K, D, imageSize, Reye, Knew);
  143. /* Distort -> Undistort */
  144. cv::Mat distortedPoints;
  145. cv::fisheye::distortPoints(points0, distortedPoints, Knew, K, D);
  146. cv::Mat undistortedPoints;
  147. cv::fisheye::undistortPoints(distortedPoints, undistortedPoints, K, D, Reye, Knew);
  148. EXPECT_MAT_NEAR(points0, undistortedPoints, 1e-8);
  149. /* Undistort -> Distort */
  150. cv::fisheye::undistortPoints(points0, undistortedPoints, K, D, Reye, Knew);
  151. cv::fisheye::distortPoints(undistortedPoints, distortedPoints, Knew, K, D);
  152. EXPECT_MAT_NEAR(points0, distortedPoints, 1e-8);
  153. }
  154. TEST_F(fisheyeTest, distortUndistortPointsNewCameraRandom)
  155. {
  156. int width = imageSize.width;
  157. int height = imageSize.height;
  158. /* Create test points */
  159. std::vector<cv::Point2d> points0Vector;
  160. cv::Mat principalPoints = (cv::Mat_<double>(5, 2) << K(0, 2), K(1, 2), // (cx, cy)
  161. /* Image corners */
  162. 0, 0,
  163. 0, height,
  164. width, 0,
  165. width, height
  166. );
  167. /* Random points inside image */
  168. cv::Mat xy[2] = {};
  169. xy[0].create(100, 1, CV_64F);
  170. theRNG().fill(xy[0], cv::RNG::UNIFORM, 0, width); // x
  171. xy[1].create(100, 1, CV_64F);
  172. theRNG().fill(xy[1], cv::RNG::UNIFORM, 0, height); // y
  173. cv::Mat randomPoints;
  174. merge(xy, 2, randomPoints);
  175. cv::Mat points0;
  176. cv::Mat Reye = cv::Mat::eye(3, 3, CV_64FC1);
  177. cv::vconcat(principalPoints.reshape(2), randomPoints, points0);
  178. /* Test with random D set */
  179. for (size_t i = 0; i < 10; ++i) {
  180. cv::Mat distortion(1, 4, CV_64F);
  181. theRNG().fill(distortion, cv::RNG::UNIFORM, -0.001, 0.001);
  182. cv::Mat Knew;
  183. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(K, distortion, imageSize, Reye, Knew);
  184. /* Distort -> Undistort */
  185. cv::Mat distortedPoints;
  186. cv::fisheye::distortPoints(points0, distortedPoints, Knew, K, distortion);
  187. cv::Mat undistortedPoints;
  188. cv::fisheye::undistortPoints(distortedPoints, undistortedPoints, K, distortion, Reye, Knew);
  189. EXPECT_MAT_NEAR(points0, undistortedPoints, 1e-8);
  190. /* Undistort -> Distort */
  191. cv::fisheye::undistortPoints(points0, undistortedPoints, K, distortion, Reye, Knew);
  192. cv::fisheye::distortPoints(undistortedPoints, distortedPoints, Knew, K, distortion);
  193. EXPECT_MAT_NEAR(points0, distortedPoints, 1e-8);
  194. }
  195. }
  196. TEST_F(fisheyeTest, solvePnP)
  197. {
  198. const int n = 16;
  199. cv::Mat obj_points(1, n, CV_64FC3);
  200. theRNG().fill(obj_points, cv::RNG::NORMAL, 2, 1);
  201. obj_points = cv::abs(obj_points) * 10;
  202. cv::Mat rvec;
  203. cv::Rodrigues(this->R, rvec);
  204. cv::Mat img_points;
  205. cv::fisheye::projectPoints(obj_points, img_points, rvec, this->T, this->K, this->D);
  206. cv::Mat rvec_pred;
  207. cv::Mat tvec_pred;
  208. bool converged = cv::fisheye::solvePnP(obj_points, img_points, this->K, this->D, rvec_pred, tvec_pred);
  209. EXPECT_MAT_NEAR(rvec, rvec_pred, 1e-6);
  210. EXPECT_MAT_NEAR(this->T, tvec_pred, 1e-6);
  211. ASSERT_TRUE(converged);
  212. }
  213. TEST_F(fisheyeTest, solvePnPRansac)
  214. {
  215. const int inliers_n = 16;
  216. const int outliers_n = 4;
  217. const bool use_extrinsic_guess = false;
  218. const int iterations_count = 100;
  219. const float reprojection_error = 1.0;
  220. const double confidence = 0.99;
  221. cv::Mat rvec;
  222. cv::Rodrigues(this->R, rvec);
  223. // inliers
  224. cv::Mat inlier_obj_points(1, inliers_n, CV_64FC3);
  225. theRNG().fill(inlier_obj_points, cv::RNG::NORMAL, 2, 1);
  226. inlier_obj_points = cv::abs(inlier_obj_points) * 10;
  227. cv::Mat inlier_img_points;
  228. cv::fisheye::projectPoints(inlier_obj_points, inlier_img_points, rvec, this->T, this->K, this->D);
  229. // outliers
  230. cv::Mat outlier_obj_points(1, outliers_n, CV_64FC3);
  231. theRNG().fill(outlier_obj_points, cv::RNG::NORMAL, 2, 1);
  232. outlier_obj_points = cv::abs(outlier_obj_points) * 10;
  233. cv::Mat outlier_img_points;
  234. cv::fisheye::projectPoints(outlier_obj_points, outlier_img_points, rvec, (this->T * 10), this->K, this->D);
  235. cv::Mat obj_points;
  236. cv::hconcat(outlier_obj_points, inlier_obj_points, obj_points);
  237. cv::Mat img_points;
  238. cv::hconcat(outlier_img_points, inlier_img_points, img_points);
  239. cv::Mat rvec_pred;
  240. cv::Mat tvec_pred;
  241. cv::Mat inliers_pred;
  242. bool converged = cv::fisheye::solvePnPRansac(obj_points, img_points, this->K, this->D,
  243. rvec_pred, tvec_pred, use_extrinsic_guess,
  244. iterations_count, reprojection_error, confidence, inliers_pred);
  245. EXPECT_MAT_NEAR(rvec, rvec_pred, 1e-5);
  246. EXPECT_MAT_NEAR(this->T, tvec_pred, 1e-5);
  247. EXPECT_EQ(inliers_pred.size[0], inliers_n);
  248. ASSERT_TRUE(converged);
  249. }
  250. TEST_F(fisheyeTest, undistortImage)
  251. {
  252. // we use it to reduce patch size for images in testdata
  253. auto throwAwayHalf = [](Mat img)
  254. {
  255. int whalf = img.cols / 2, hhalf = img.rows / 2;
  256. Rect tl(0, 0, whalf, hhalf), br(whalf, hhalf, whalf, hhalf);
  257. img(tl) = 0;
  258. img(br) = 0;
  259. };
  260. cv::Matx33d theK = this->K;
  261. cv::Mat theD = cv::Mat(this->D);
  262. std::string file = combine(datasets_repository_path, "stereo_pair_014.png");
  263. cv::Matx33d newK = theK;
  264. cv::Mat distorted = cv::imread(file), undistorted;
  265. {
  266. newK(0, 0) = 100;
  267. newK(1, 1) = 100;
  268. cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
  269. std::string imageFilename = combine(datasets_repository_path, "new_f_100.png");
  270. cv::Mat correct = cv::imread(imageFilename);
  271. ASSERT_FALSE(correct.empty()) << "Correct image " << imageFilename.c_str() << " can not be read" << std::endl;
  272. throwAwayHalf(correct);
  273. throwAwayHalf(undistorted);
  274. EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
  275. }
  276. {
  277. double balance = 1.0;
  278. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
  279. cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
  280. std::string imageFilename = combine(datasets_repository_path, "balance_1.0.png");
  281. cv::Mat correct = cv::imread(imageFilename);
  282. ASSERT_FALSE(correct.empty()) << "Correct image " << imageFilename.c_str() << " can not be read" << std::endl;
  283. throwAwayHalf(correct);
  284. throwAwayHalf(undistorted);
  285. EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
  286. }
  287. {
  288. double balance = 0.0;
  289. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
  290. cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
  291. std::string imageFilename = combine(datasets_repository_path, "balance_0.0.png");
  292. cv::Mat correct = cv::imread(imageFilename);
  293. ASSERT_FALSE(correct.empty()) << "Correct image " << imageFilename.c_str() << " can not be read" << std::endl;
  294. throwAwayHalf(correct);
  295. throwAwayHalf(undistorted);
  296. EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
  297. }
  298. }
  299. TEST_F(fisheyeTest, undistortAndDistortImage)
  300. {
  301. cv::Matx33d K_src = this->K;
  302. cv::Mat D_src = cv::Mat(this->D);
  303. std::string file = combine(datasets_repository_path, "/calib-3_stereo_from_JY/left/stereo_pair_014.jpg");
  304. cv::Matx33d K_dst = K_src;
  305. cv::Mat image = cv::imread(file), image_projected;
  306. cv::Vec4d D_dst_vec (-1.0, 0.0, 0.0, 0.0);
  307. cv::Mat D_dst = cv::Mat(D_dst_vec);
  308. int imageWidth = (int)this->imageSize.width;
  309. int imageHeight = (int)this->imageSize.height;
  310. cv::Mat imagePoints(imageHeight, imageWidth, CV_32FC2), undPoints, distPoints;
  311. cv::Vec2f* pts = imagePoints.ptr<cv::Vec2f>();
  312. for(int y = 0, k = 0; y < imageHeight; ++y)
  313. {
  314. for(int x = 0; x < imageWidth; ++x)
  315. {
  316. cv::Vec2f point((float)x, (float)y);
  317. pts[k++] = point;
  318. }
  319. }
  320. cv::fisheye::undistortPoints(imagePoints, undPoints, K_dst, D_dst);
  321. cv::fisheye::distortPoints(undPoints, distPoints, K_src, D_src);
  322. cv::remap(image, image_projected, distPoints, cv::noArray(), cv::INTER_LINEAR);
  323. float dx, dy, r_sq;
  324. float R_MAX = 250;
  325. float imageCenterX = (float)imageWidth / 2;
  326. float imageCenterY = (float)imageHeight / 2;
  327. cv::Mat undPointsGt(imageHeight, imageWidth, CV_32FC2);
  328. cv::Mat imageGt(imageHeight, imageWidth, CV_8UC3);
  329. for(int y = 0; y < imageHeight; ++y)
  330. {
  331. for(int x = 0; x < imageWidth; ++x)
  332. {
  333. dx = x - imageCenterX;
  334. dy = y - imageCenterY;
  335. r_sq = dy * dy + dx * dx;
  336. Vec2f & und_vec = undPoints.at<Vec2f>(y,x);
  337. Vec3b & pixel = image_projected.at<Vec3b>(y,x);
  338. Vec2f & undist_vec_gt = undPointsGt.at<Vec2f>(y,x);
  339. Vec3b & pixel_gt = imageGt.at<Vec3b>(y,x);
  340. if (r_sq > R_MAX * R_MAX)
  341. {
  342. undist_vec_gt[0] = -1e6;
  343. undist_vec_gt[1] = -1e6;
  344. pixel_gt[0] = 0;
  345. pixel_gt[1] = 0;
  346. pixel_gt[2] = 0;
  347. }
  348. else
  349. {
  350. undist_vec_gt[0] = und_vec[0];
  351. undist_vec_gt[1] = und_vec[1];
  352. pixel_gt[0] = pixel[0];
  353. pixel_gt[1] = pixel[1];
  354. pixel_gt[2] = pixel[2];
  355. }
  356. }
  357. }
  358. EXPECT_MAT_NEAR(undPoints, undPointsGt, 1e-10);
  359. EXPECT_MAT_NEAR(image_projected, imageGt, 1e-10);
  360. Vec2f dist_point_1 = distPoints.at<Vec2f>(400, 640);
  361. Vec2f dist_point_1_gt(640.044f, 400.041f);
  362. Vec2f dist_point_2 = distPoints.at<Vec2f>(400, 440);
  363. Vec2f dist_point_2_gt(409.731f, 403.029f);
  364. Vec2f dist_point_3 = distPoints.at<Vec2f>(200, 640);
  365. Vec2f dist_point_3_gt(643.341f, 168.896f);
  366. Vec2f dist_point_4 = distPoints.at<Vec2f>(300, 480);
  367. Vec2f dist_point_4_gt(463.402f, 290.317f);
  368. Vec2f dist_point_5 = distPoints.at<Vec2f>(550, 750);
  369. Vec2f dist_point_5_gt(797.51f, 611.637f);
  370. EXPECT_MAT_NEAR(dist_point_1, dist_point_1_gt, 1e-2);
  371. EXPECT_MAT_NEAR(dist_point_2, dist_point_2_gt, 1e-2);
  372. EXPECT_MAT_NEAR(dist_point_3, dist_point_3_gt, 1e-2);
  373. EXPECT_MAT_NEAR(dist_point_4, dist_point_4_gt, 1e-2);
  374. EXPECT_MAT_NEAR(dist_point_5, dist_point_5_gt, 1e-2);
  375. // Add the "--test_debug" to arguments for file output
  376. if (cvtest::debugLevel > 0)
  377. cv::imwrite(combine(datasets_repository_path, "new_distortion.png"), image_projected);
  378. }
  379. TEST_F(fisheyeTest, jacobians)
  380. {
  381. int n = 10;
  382. cv::Mat X(1, n, CV_64FC3);
  383. cv::Mat om(3, 1, CV_64F), theT(3, 1, CV_64F);
  384. cv::Mat f(2, 1, CV_64F), c(2, 1, CV_64F);
  385. cv::Mat k(4, 1, CV_64F);
  386. double alpha;
  387. cv::RNG r;
  388. r.fill(X, cv::RNG::NORMAL, 2, 1);
  389. X = cv::abs(X) * 10;
  390. r.fill(om, cv::RNG::NORMAL, 0, 1);
  391. om = cv::abs(om);
  392. r.fill(theT, cv::RNG::NORMAL, 0, 1);
  393. theT = cv::abs(theT); theT.at<double>(2) = 4; theT *= 10;
  394. r.fill(f, cv::RNG::NORMAL, 0, 1);
  395. f = cv::abs(f) * 1000;
  396. r.fill(c, cv::RNG::NORMAL, 0, 1);
  397. c = cv::abs(c) * 1000;
  398. r.fill(k, cv::RNG::NORMAL, 0, 1);
  399. k*= 0.5;
  400. alpha = 0.01*r.gaussian(1);
  401. cv::Mat x1, x2, xpred;
  402. cv::Matx33d theK(f.at<double>(0), alpha * f.at<double>(0), c.at<double>(0),
  403. 0, f.at<double>(1), c.at<double>(1),
  404. 0, 0, 1);
  405. cv::Mat jacobians;
  406. cv::fisheye::projectPoints(X, x1, om, theT, theK, k, alpha, jacobians);
  407. //test on T:
  408. cv::Mat dT(3, 1, CV_64FC1);
  409. r.fill(dT, cv::RNG::NORMAL, 0, 1);
  410. dT *= 1e-9*cv::norm(theT);
  411. cv::Mat T2 = theT + dT;
  412. cv::fisheye::projectPoints(X, x2, om, T2, theK, k, alpha, cv::noArray());
  413. xpred = x1 + cv::Mat(jacobians.colRange(11,14) * dT).reshape(2, 1);
  414. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  415. //test on om:
  416. cv::Mat dom(3, 1, CV_64FC1);
  417. r.fill(dom, cv::RNG::NORMAL, 0, 1);
  418. dom *= 1e-9*cv::norm(om);
  419. cv::Mat om2 = om + dom;
  420. cv::fisheye::projectPoints(X, x2, om2, theT, theK, k, alpha, cv::noArray());
  421. xpred = x1 + cv::Mat(jacobians.colRange(8,11) * dom).reshape(2, 1);
  422. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  423. //test on f:
  424. cv::Mat df(2, 1, CV_64FC1);
  425. r.fill(df, cv::RNG::NORMAL, 0, 1);
  426. df *= 1e-9*cv::norm(f);
  427. cv::Matx33d K2 = theK + cv::Matx33d(df.at<double>(0), df.at<double>(0) * alpha, 0, 0, df.at<double>(1), 0, 0, 0, 0);
  428. cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());
  429. xpred = x1 + cv::Mat(jacobians.colRange(0,2) * df).reshape(2, 1);
  430. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  431. //test on c:
  432. cv::Mat dc(2, 1, CV_64FC1);
  433. r.fill(dc, cv::RNG::NORMAL, 0, 1);
  434. dc *= 1e-9*cv::norm(c);
  435. K2 = theK + cv::Matx33d(0, 0, dc.at<double>(0), 0, 0, dc.at<double>(1), 0, 0, 0);
  436. cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());
  437. xpred = x1 + cv::Mat(jacobians.colRange(2,4) * dc).reshape(2, 1);
  438. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  439. //test on k:
  440. cv::Mat dk(4, 1, CV_64FC1);
  441. r.fill(dk, cv::RNG::NORMAL, 0, 1);
  442. dk *= 1e-9*cv::norm(k);
  443. cv::Mat k2 = k + dk;
  444. cv::fisheye::projectPoints(X, x2, om, theT, theK, k2, alpha, cv::noArray());
  445. xpred = x1 + cv::Mat(jacobians.colRange(4,8) * dk).reshape(2, 1);
  446. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  447. //test on alpha:
  448. cv::Mat dalpha(1, 1, CV_64FC1);
  449. r.fill(dalpha, cv::RNG::NORMAL, 0, 1);
  450. dalpha *= 1e-9*cv::norm(f);
  451. double alpha2 = alpha + dalpha.at<double>(0);
  452. K2 = theK + cv::Matx33d(0, f.at<double>(0) * dalpha.at<double>(0), 0, 0, 0, 0, 0, 0, 0);
  453. cv::fisheye::projectPoints(X, x2, om, theT, theK, k, alpha2, cv::noArray());
  454. xpred = x1 + cv::Mat(jacobians.col(14) * dalpha).reshape(2, 1);
  455. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  456. }
  457. TEST_F(fisheyeTest, Calibration)
  458. {
  459. const int n_images = 34;
  460. const cv::Matx33d goldK(558.4780870585967, 0, 620.4585053962692,
  461. 0, 560.5067667343917, 381.9394122875291,
  462. 0, 0, 1);
  463. const cv::Vec4d goldD(-0.00146136, -0.00329847, 0.00605742, -0.00374201);
  464. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  465. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  466. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  467. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  468. CV_Assert(fs_left.isOpened());
  469. for(int i = 0; i < n_images; ++i)
  470. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  471. fs_left.release();
  472. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  473. CV_Assert(fs_object.isOpened());
  474. for(int i = 0; i < n_images; ++i)
  475. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  476. fs_object.release();
  477. int flag = 0;
  478. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  479. flag |= cv::fisheye::CALIB_CHECK_COND;
  480. flag |= cv::fisheye::CALIB_FIX_SKEW;
  481. cv::Matx33d theK;
  482. cv::Vec4d theD;
  483. cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
  484. cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
  485. EXPECT_MAT_NEAR(theK, goldK, 1e-8);
  486. EXPECT_MAT_NEAR(theD, goldD, 1e-8);
  487. }
  488. TEST_F(fisheyeTest, CalibrationWithFixedFocalLength)
  489. {
  490. const int n_images = 34;
  491. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  492. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  493. const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
  494. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  495. CV_Assert(fs_left.isOpened());
  496. for(int i = 0; i < n_images; ++i)
  497. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  498. fs_left.release();
  499. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  500. CV_Assert(fs_object.isOpened());
  501. for(int i = 0; i < n_images; ++i)
  502. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  503. fs_object.release();
  504. int flag = 0;
  505. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  506. flag |= cv::fisheye::CALIB_CHECK_COND;
  507. flag |= cv::fisheye::CALIB_FIX_SKEW;
  508. flag |= cv::fisheye::CALIB_FIX_FOCAL_LENGTH;
  509. flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;
  510. cv::Matx33d theK = this->K;
  511. const cv::Matx33d newK(
  512. 558.478088, 0.000000, 620.458461,
  513. 0.000000, 560.506767, 381.939362,
  514. 0.000000, 0.000000, 1.000000);
  515. cv::Vec4d theD;
  516. const cv::Vec4d newD(-0.001461, -0.003298, 0.006057, -0.003742);
  517. cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
  518. cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
  519. // ensure that CALIB_FIX_FOCAL_LENGTH works and focal length has not changed
  520. EXPECT_EQ(theK(0,0), K(0,0));
  521. EXPECT_EQ(theK(1,1), K(1,1));
  522. EXPECT_MAT_NEAR(theK, newK, 1e-6);
  523. EXPECT_MAT_NEAR(theD, newD, 1e-6);
  524. }
  525. TEST_F(fisheyeTest, Homography)
  526. {
  527. const int n_images = 1;
  528. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  529. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  530. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  531. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  532. CV_Assert(fs_left.isOpened());
  533. for(int i = 0; i < n_images; ++i)
  534. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  535. fs_left.release();
  536. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  537. CV_Assert(fs_object.isOpened());
  538. for(int i = 0; i < n_images; ++i)
  539. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  540. fs_object.release();
  541. cv::internal::IntrinsicParams param;
  542. param.Init(cv::Vec2d(cv::max(imageSize.width, imageSize.height) / CV_PI, cv::max(imageSize.width, imageSize.height) / CV_PI),
  543. cv::Vec2d(imageSize.width / 2.0 - 0.5, imageSize.height / 2.0 - 0.5));
  544. cv::Mat _imagePoints (imagePoints[0]);
  545. cv::Mat _objectPoints(objectPoints[0]);
  546. cv::Mat imagePointsNormalized = NormalizePixels(_imagePoints, param).reshape(1).t();
  547. _objectPoints = _objectPoints.reshape(1).t();
  548. cv::Mat objectPointsMean, covObjectPoints;
  549. int Np = imagePointsNormalized.cols;
  550. cv::calcCovarMatrix(_objectPoints, covObjectPoints, objectPointsMean, cv::COVAR_NORMAL | cv::COVAR_COLS);
  551. cv::SVD svd(covObjectPoints);
  552. cv::Mat theR(svd.vt);
  553. if (cv::norm(theR(cv::Rect(2, 0, 1, 2))) < 1e-6)
  554. theR = cv::Mat::eye(3,3, CV_64FC1);
  555. if (cv::determinant(theR) < 0)
  556. theR = -theR;
  557. cv::Mat theT = -theR * objectPointsMean;
  558. cv::Mat X_new = theR * _objectPoints + theT * cv::Mat::ones(1, Np, CV_64FC1);
  559. cv::Mat H = cv::internal::ComputeHomography(imagePointsNormalized, X_new.rowRange(0, 2));
  560. cv::Mat M = cv::Mat::ones(3, X_new.cols, CV_64FC1);
  561. X_new.rowRange(0, 2).copyTo(M.rowRange(0, 2));
  562. cv::Mat mrep = H * M;
  563. cv::divide(mrep, cv::Mat::ones(3,1, CV_64FC1) * mrep.row(2).clone(), mrep);
  564. cv::Mat merr = (mrep.rowRange(0, 2) - imagePointsNormalized).t();
  565. cv::Vec2d std_err;
  566. cv::meanStdDev(merr.reshape(2), cv::noArray(), std_err);
  567. std_err *= sqrt((double)merr.reshape(2).total() / (merr.reshape(2).total() - 1));
  568. cv::Vec2d correct_std_err(0.00516740156010384, 0.00644205331553901);
  569. EXPECT_MAT_NEAR(std_err, correct_std_err, 1e-12);
  570. }
  571. TEST_F(fisheyeTest, EstimateUncertainties)
  572. {
  573. const int n_images = 34;
  574. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  575. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  576. const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
  577. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  578. CV_Assert(fs_left.isOpened());
  579. for(int i = 0; i < n_images; ++i)
  580. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  581. fs_left.release();
  582. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  583. CV_Assert(fs_object.isOpened());
  584. for(int i = 0; i < n_images; ++i)
  585. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  586. fs_object.release();
  587. int flag = 0;
  588. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  589. flag |= cv::fisheye::CALIB_CHECK_COND;
  590. flag |= cv::fisheye::CALIB_FIX_SKEW;
  591. cv::Matx33d theK;
  592. cv::Vec4d theD;
  593. std::vector<cv::Vec3d> rvec;
  594. std::vector<cv::Vec3d> tvec;
  595. cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
  596. rvec, tvec, flag, cv::TermCriteria(3, 20, 1e-6));
  597. cv::internal::IntrinsicParams param, errors;
  598. cv::Vec2d err_std;
  599. double thresh_cond = 1e6;
  600. int check_cond = 1;
  601. param.Init(cv::Vec2d(theK(0,0), theK(1,1)), cv::Vec2d(theK(0,2), theK(1, 2)), theD);
  602. param.isEstimate = std::vector<uchar>(9, 1);
  603. param.isEstimate[4] = 0;
  604. errors.isEstimate = param.isEstimate;
  605. double rms;
  606. cv::internal::EstimateUncertainties(objectPoints, imagePoints, param, rvec, tvec,
  607. errors, err_std, thresh_cond, check_cond, rms);
  608. EXPECT_MAT_NEAR(errors.f, cv::Vec2d(1.34250246865020720, 1.36037536429654530), 1e-6);
  609. EXPECT_MAT_NEAR(errors.c, cv::Vec2d(0.92070526160049848, 0.84383585812851514), 1e-6);
  610. EXPECT_MAT_NEAR(errors.k, cv::Vec4d(0.0053379581373996041, 0.017389792901700545, 0.022036256089491224, 0.0094714594258908952), 1e-7);
  611. EXPECT_MAT_NEAR(err_std, cv::Vec2d(0.187475975266883, 0.185678953263995), 1e-7);
  612. CV_Assert(fabs(rms - 0.263782587133546) < 1e-10);
  613. CV_Assert(errors.alpha == 0);
  614. }
  615. TEST_F(fisheyeTest, stereoRectify)
  616. {
  617. // For consistency purposes
  618. CV_StaticAssert(
  619. static_cast<int>(cv::CALIB_ZERO_DISPARITY) == static_cast<int>(cv::fisheye::CALIB_ZERO_DISPARITY),
  620. "For the purpose of continuity the following should be true: cv::CALIB_ZERO_DISPARITY == cv::fisheye::CALIB_ZERO_DISPARITY"
  621. );
  622. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  623. cv::Size calibration_size = this->imageSize, requested_size = calibration_size;
  624. cv::Matx33d K1 = this->K, K2 = K1;
  625. cv::Mat D1 = cv::Mat(this->D), D2 = D1;
  626. cv::Vec3d theT = this->T;
  627. cv::Matx33d theR = this->R;
  628. double balance = 0.0, fov_scale = 1.1;
  629. cv::Mat R1, R2, P1, P2, Q;
  630. cv::fisheye::stereoRectify(K1, D1, K2, D2, calibration_size, theR, theT, R1, R2, P1, P2, Q,
  631. cv::fisheye::CALIB_ZERO_DISPARITY, requested_size, balance, fov_scale);
  632. // Collected with these CMake flags: -DWITH_IPP=OFF -DCV_ENABLE_INTRINSICS=OFF -DCV_DISABLE_OPTIMIZATION=ON -DCMAKE_BUILD_TYPE=Debug
  633. cv::Matx33d R1_ref(
  634. 0.9992853269091279, 0.03779164101000276, -0.0007920188690205426,
  635. -0.03778569762983931, 0.9992646472015868, 0.006511981857667881,
  636. 0.001037534936357442, -0.006477400933964018, 0.9999784831677112
  637. );
  638. cv::Matx33d R2_ref(
  639. 0.9994868963898833, -0.03197579751378937, -0.001868774538573449,
  640. 0.03196298186616116, 0.9994677442608699, -0.0065265589947392,
  641. 0.002076471801477729, 0.006463478587068991, 0.9999769555891836
  642. );
  643. cv::Matx34d P1_ref(
  644. 420.9684016542647, 0, 586.3059567784627, 0,
  645. 0, 420.9684016542647, 374.8571836462291, 0,
  646. 0, 0, 1, 0
  647. );
  648. cv::Matx34d P2_ref(
  649. 420.9684016542647, 0, 586.3059567784627, -41.78881938824554,
  650. 0, 420.9684016542647, 374.8571836462291, 0,
  651. 0, 0, 1, 0
  652. );
  653. cv::Matx44d Q_ref(
  654. 1, 0, 0, -586.3059567784627,
  655. 0, 1, 0, -374.8571836462291,
  656. 0, 0, 0, 420.9684016542647,
  657. 0, 0, 10.07370889670733, -0
  658. );
  659. const double eps = 1e-10;
  660. EXPECT_MAT_NEAR(R1_ref, R1, eps);
  661. EXPECT_MAT_NEAR(R2_ref, R2, eps);
  662. EXPECT_MAT_NEAR(P1_ref, P1, eps);
  663. EXPECT_MAT_NEAR(P2_ref, P2, eps);
  664. EXPECT_MAT_NEAR(Q_ref, Q, eps);
  665. if (::testing::Test::HasFailure())
  666. {
  667. std::cout << "Actual values are:" << std::endl
  668. << "R1 =" << std::endl << R1 << std::endl
  669. << "R2 =" << std::endl << R2 << std::endl
  670. << "P1 =" << std::endl << P1 << std::endl
  671. << "P2 =" << std::endl << P2 << std::endl
  672. << "Q =" << std::endl << Q << std::endl;
  673. }
  674. if (cvtest::debugLevel == 0)
  675. return;
  676. // DEBUG code is below
  677. cv::Mat lmapx, lmapy, rmapx, rmapy;
  678. //rewrite for fisheye
  679. cv::fisheye::initUndistortRectifyMap(K1, D1, R1, P1, requested_size, CV_32F, lmapx, lmapy);
  680. cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, requested_size, CV_32F, rmapx, rmapy);
  681. cv::Mat l, r, lundist, rundist;
  682. for (int i = 0; i < 34; ++i)
  683. {
  684. SCOPED_TRACE(cv::format("image %d", i));
  685. l = imread(combine(folder, cv::format("left/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);
  686. r = imread(combine(folder, cv::format("right/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);
  687. ASSERT_FALSE(l.empty());
  688. ASSERT_FALSE(r.empty());
  689. int ndisp = 128;
  690. cv::rectangle(l, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));
  691. cv::rectangle(r, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));
  692. cv::rectangle(r, cv::Rect(255-ndisp, 0, 829+ndisp ,l.rows-1), cv::Scalar(0, 0, 255));
  693. cv::remap(l, lundist, lmapx, lmapy, cv::INTER_LINEAR);
  694. cv::remap(r, rundist, rmapx, rmapy, cv::INTER_LINEAR);
  695. for (int ii = 0; ii < lundist.rows; ii += 20)
  696. {
  697. cv::line(lundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));
  698. cv::line(rundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));
  699. }
  700. cv::Mat rectification;
  701. merge4(l, r, lundist, rundist, rectification);
  702. // Add the "--test_debug" to arguments for file output
  703. if (cvtest::debugLevel > 0)
  704. cv::imwrite(cv::format("fisheye_rectification_AB_%03d.png", i), rectification);
  705. }
  706. }
  707. TEST_F(fisheyeTest, stereoCalibrate)
  708. {
  709. const int n_images = 34;
  710. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  711. std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
  712. std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
  713. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  714. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  715. CV_Assert(fs_left.isOpened());
  716. for(int i = 0; i < n_images; ++i)
  717. fs_left[cv::format("image_%d", i )] >> leftPoints[i];
  718. fs_left.release();
  719. cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
  720. CV_Assert(fs_right.isOpened());
  721. for(int i = 0; i < n_images; ++i)
  722. fs_right[cv::format("image_%d", i )] >> rightPoints[i];
  723. fs_right.release();
  724. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  725. CV_Assert(fs_object.isOpened());
  726. for(int i = 0; i < n_images; ++i)
  727. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  728. fs_object.release();
  729. cv::Matx33d K1, K2, theR;
  730. cv::Vec3d theT;
  731. cv::Vec4d D1, D2;
  732. int flag = 0;
  733. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  734. flag |= cv::fisheye::CALIB_CHECK_COND;
  735. flag |= cv::fisheye::CALIB_FIX_SKEW;
  736. cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
  737. K1, D1, K2, D2, imageSize, theR, theT, flag,
  738. cv::TermCriteria(3, 12, 0));
  739. cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
  740. -0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
  741. -0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
  742. cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
  743. cv::Matx33d K1_correct (561.195925927249, 0, 621.282400272412,
  744. 0, 562.849402029712, 380.555455380889,
  745. 0, 0, 1);
  746. cv::Matx33d K2_correct (560.395452535348, 0, 678.971652040359,
  747. 0, 561.90171021422, 380.401340535339,
  748. 0, 0, 1);
  749. cv::Vec4d D1_correct (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
  750. cv::Vec4d D2_correct (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
  751. EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
  752. EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
  753. EXPECT_MAT_NEAR(K1, K1_correct, 1e-10);
  754. EXPECT_MAT_NEAR(K2, K2_correct, 1e-10);
  755. EXPECT_MAT_NEAR(D1, D1_correct, 1e-10);
  756. EXPECT_MAT_NEAR(D2, D2_correct, 1e-10);
  757. }
  758. TEST_F(fisheyeTest, stereoCalibrateFixIntrinsic)
  759. {
  760. const int n_images = 34;
  761. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  762. std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
  763. std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
  764. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  765. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  766. CV_Assert(fs_left.isOpened());
  767. for(int i = 0; i < n_images; ++i)
  768. fs_left[cv::format("image_%d", i )] >> leftPoints[i];
  769. fs_left.release();
  770. cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
  771. CV_Assert(fs_right.isOpened());
  772. for(int i = 0; i < n_images; ++i)
  773. fs_right[cv::format("image_%d", i )] >> rightPoints[i];
  774. fs_right.release();
  775. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  776. CV_Assert(fs_object.isOpened());
  777. for(int i = 0; i < n_images; ++i)
  778. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  779. fs_object.release();
  780. cv::Matx33d theR;
  781. cv::Vec3d theT;
  782. int flag = 0;
  783. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  784. flag |= cv::fisheye::CALIB_CHECK_COND;
  785. flag |= cv::fisheye::CALIB_FIX_SKEW;
  786. flag |= cv::fisheye::CALIB_FIX_INTRINSIC;
  787. cv::Matx33d K1 (561.195925927249, 0, 621.282400272412,
  788. 0, 562.849402029712, 380.555455380889,
  789. 0, 0, 1);
  790. cv::Matx33d K2 (560.395452535348, 0, 678.971652040359,
  791. 0, 561.90171021422, 380.401340535339,
  792. 0, 0, 1);
  793. cv::Vec4d D1 (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
  794. cv::Vec4d D2 (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
  795. cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
  796. K1, D1, K2, D2, imageSize, theR, theT, flag,
  797. cv::TermCriteria(3, 12, 0));
  798. cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
  799. -0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
  800. -0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
  801. cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
  802. EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
  803. EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
  804. }
  805. TEST_F(fisheyeTest, CalibrationWithDifferentPointsNumber)
  806. {
  807. const int n_images = 2;
  808. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  809. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  810. std::vector<cv::Point2d> imgPoints1(10);
  811. std::vector<cv::Point2d> imgPoints2(15);
  812. std::vector<cv::Point3d> objectPoints1(imgPoints1.size());
  813. std::vector<cv::Point3d> objectPoints2(imgPoints2.size());
  814. for (size_t i = 0; i < imgPoints1.size(); i++)
  815. {
  816. imgPoints1[i] = cv::Point2d((double)i, (double)i);
  817. objectPoints1[i] = cv::Point3d((double)i, (double)i, 10.0);
  818. }
  819. for (size_t i = 0; i < imgPoints2.size(); i++)
  820. {
  821. imgPoints2[i] = cv::Point2d(i + 0.5, i + 0.5);
  822. objectPoints2[i] = cv::Point3d(i + 0.5, i + 0.5, 10.0);
  823. }
  824. imagePoints[0] = imgPoints1;
  825. imagePoints[1] = imgPoints2;
  826. objectPoints[0] = objectPoints1;
  827. objectPoints[1] = objectPoints2;
  828. cv::Matx33d theK = cv::Matx33d::eye();
  829. cv::Vec4d theD;
  830. int flag = 0;
  831. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  832. flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;
  833. flag |= cv::fisheye::CALIB_FIX_SKEW;
  834. cv::fisheye::calibrate(objectPoints, imagePoints, cv::Size(100, 100), theK, theD,
  835. cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
  836. }
  837. TEST_F(fisheyeTest, stereoCalibrateWithPerViewTransformations)
  838. {
  839. const int n_images = 34;
  840. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  841. std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
  842. std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
  843. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  844. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  845. CV_Assert(fs_left.isOpened());
  846. for(int i = 0; i < n_images; ++i)
  847. fs_left[cv::format("image_%d", i )] >> leftPoints[i];
  848. fs_left.release();
  849. cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
  850. CV_Assert(fs_right.isOpened());
  851. for(int i = 0; i < n_images; ++i)
  852. fs_right[cv::format("image_%d", i )] >> rightPoints[i];
  853. fs_right.release();
  854. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  855. CV_Assert(fs_object.isOpened());
  856. for(int i = 0; i < n_images; ++i)
  857. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  858. fs_object.release();
  859. cv::Matx33d K1, K2, theR;
  860. cv::Vec3d theT;
  861. cv::Vec4d D1, D2;
  862. std::vector<cv::Mat> rvecs, tvecs;
  863. int flag = 0;
  864. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  865. flag |= cv::fisheye::CALIB_CHECK_COND;
  866. flag |= cv::fisheye::CALIB_FIX_SKEW;
  867. double rmsErrorStereoCalib = cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
  868. K1, D1, K2, D2, imageSize, theR, theT, rvecs, tvecs, flag,
  869. cv::TermCriteria(3, 12, 0));
  870. std::vector<cv::Point2d> reprojectedImgPts[2] = {std::vector<cv::Point2d>(n_images), std::vector<cv::Point2d>(n_images)};
  871. size_t totalPoints = 0;
  872. double totalMSError[2] = { 0, 0 };
  873. for( size_t i = 0; i < n_images; i++ )
  874. {
  875. cv::Matx33d viewRotMat1, viewRotMat2;
  876. cv::Vec3d viewT1, viewT2;
  877. cv::Mat rVec;
  878. cv::Rodrigues( rvecs[i], rVec );
  879. rVec.convertTo(viewRotMat1, CV_64F);
  880. tvecs[i].convertTo(viewT1, CV_64F);
  881. viewRotMat2 = theR * viewRotMat1;
  882. cv::Vec3d T2t = theR * viewT1;
  883. viewT2 = T2t + theT;
  884. cv::Vec3d viewRotVec1, viewRotVec2;
  885. cv::Rodrigues(viewRotMat1, viewRotVec1);
  886. cv::Rodrigues(viewRotMat2, viewRotVec2);
  887. double alpha1 = K1(0, 1) / K1(0, 0);
  888. double alpha2 = K2(0, 1) / K2(0, 0);
  889. cv::fisheye::projectPoints(objectPoints[i], reprojectedImgPts[0], viewRotVec1, viewT1, K1, D1, alpha1);
  890. cv::fisheye::projectPoints(objectPoints[i], reprojectedImgPts[1], viewRotVec2, viewT2, K2, D2, alpha2);
  891. double viewMSError[2] = {
  892. cv::norm(leftPoints[i], reprojectedImgPts[0], cv::NORM_L2SQR),
  893. cv::norm(rightPoints[i], reprojectedImgPts[1], cv::NORM_L2SQR)
  894. };
  895. size_t n = objectPoints[i].size();
  896. totalMSError[0] += viewMSError[0];
  897. totalMSError[1] += viewMSError[1];
  898. totalPoints += n;
  899. }
  900. double rmsErrorFromReprojectedImgPts = std::sqrt((totalMSError[0] + totalMSError[1]) / (2 * totalPoints));
  901. cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
  902. -0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
  903. -0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
  904. cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
  905. cv::Matx33d K1_correct (561.195925927249, 0, 621.282400272412,
  906. 0, 562.849402029712, 380.555455380889,
  907. 0, 0, 1);
  908. cv::Matx33d K2_correct (560.395452535348, 0, 678.971652040359,
  909. 0, 561.90171021422, 380.401340535339,
  910. 0, 0, 1);
  911. cv::Vec4d D1_correct (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
  912. cv::Vec4d D2_correct (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
  913. EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
  914. EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
  915. EXPECT_MAT_NEAR(K1, K1_correct, 1e-10);
  916. EXPECT_MAT_NEAR(K2, K2_correct, 1e-10);
  917. EXPECT_MAT_NEAR(D1, D1_correct, 1e-10);
  918. EXPECT_MAT_NEAR(D2, D2_correct, 1e-10);
  919. EXPECT_NEAR(rmsErrorStereoCalib, rmsErrorFromReprojectedImgPts, 1e-4);
  920. }
  921. TEST_F(fisheyeTest, estimateNewCameraMatrixForUndistortRectify)
  922. {
  923. cv::Size size(1920, 1080);
  924. cv::Mat K_fullhd(3, 3, cv::DataType<double>::type);
  925. K_fullhd.at<double>(0, 0) = 600.44477382;
  926. K_fullhd.at<double>(0, 1) = 0.0;
  927. K_fullhd.at<double>(0, 2) = 992.06425788;
  928. K_fullhd.at<double>(1, 0) = 0.0;
  929. K_fullhd.at<double>(1, 1) = 578.99298055;
  930. K_fullhd.at<double>(1, 2) = 549.26826242;
  931. K_fullhd.at<double>(2, 0) = 0.0;
  932. K_fullhd.at<double>(2, 1) = 0.0;
  933. K_fullhd.at<double>(2, 2) = 1.0;
  934. cv::Mat K_new_truth(3, 3, cv::DataType<double>::type);
  935. K_new_truth.at<double>(0, 0) = 387.5118215642316;
  936. K_new_truth.at<double>(0, 1) = 0.0;
  937. K_new_truth.at<double>(0, 2) = 1033.936556777084;
  938. K_new_truth.at<double>(1, 0) = 0.0;
  939. K_new_truth.at<double>(1, 1) = 373.6673784974842;
  940. K_new_truth.at<double>(1, 2) = 538.794152656429;
  941. K_new_truth.at<double>(2, 0) = 0.0;
  942. K_new_truth.at<double>(2, 1) = 0.0;
  943. K_new_truth.at<double>(2, 2) = 1.0;
  944. cv::Mat D_fullhd(4, 1, cv::DataType<double>::type);
  945. D_fullhd.at<double>(0, 0) = -0.05090103223466704;
  946. D_fullhd.at<double>(1, 0) = 0.030944413642173308;
  947. D_fullhd.at<double>(2, 0) = -0.021509225493198905;
  948. D_fullhd.at<double>(3, 0) = 0.0043378096628297145;
  949. cv::Mat E = cv::Mat::eye(3, 3, cv::DataType<double>::type);
  950. cv::Mat K_new(3, 3, cv::DataType<double>::type);
  951. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(K_fullhd, D_fullhd, size, E, K_new, 0.0, size);
  952. EXPECT_MAT_NEAR(K_new, K_new_truth, 1e-6);
  953. }
  954. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  955. /// fisheyeTest::
  956. const cv::Size fisheyeTest::imageSize(1280, 800);
  957. const cv::Matx33d fisheyeTest::K(558.478087865323, 0, 620.458515360843,
  958. 0, 560.506767351568, 381.939424848348,
  959. 0, 0, 1);
  960. const cv::Vec4d fisheyeTest::D(-0.0014613319981768, -0.00329861110580401, 0.00605760088590183, -0.00374209380722371);
  961. const cv::Matx33d fisheyeTest::R ( 9.9756700084424932e-01, 6.9698277640183867e-02, 1.4929569991321144e-03,
  962. -6.9711825162322980e-02, 9.9748249845531767e-01, 1.2997180766418455e-02,
  963. -5.8331736398316541e-04,-1.3069635393884985e-02, 9.9991441852366736e-01);
  964. const cv::Vec3d fisheyeTest::T(-9.9217369356044638e-02, 3.1741831972356663e-03, 1.8551007952921010e-04);
  965. std::string fisheyeTest::combine(const std::string& _item1, const std::string& _item2)
  966. {
  967. std::string item1 = _item1, item2 = _item2;
  968. std::replace(item1.begin(), item1.end(), '\\', '/');
  969. std::replace(item2.begin(), item2.end(), '\\', '/');
  970. if (item1.empty())
  971. return item2;
  972. if (item2.empty())
  973. return item1;
  974. char last = item1[item1.size()-1];
  975. return item1 + (last != '/' ? "/" : "") + item2;
  976. }
  977. void fisheyeTest::merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged)
  978. {
  979. int type = tl.type();
  980. cv::Size sz = tl.size();
  981. ASSERT_EQ(type, tr.type()); ASSERT_EQ(type, bl.type()); ASSERT_EQ(type, br.type());
  982. ASSERT_EQ(sz.width, tr.cols); ASSERT_EQ(sz.width, bl.cols); ASSERT_EQ(sz.width, br.cols);
  983. ASSERT_EQ(sz.height, tr.rows); ASSERT_EQ(sz.height, bl.rows); ASSERT_EQ(sz.height, br.rows);
  984. merged.create(cv::Size(sz.width * 2, sz.height * 2), type);
  985. tl.copyTo(merged(cv::Rect(0, 0, sz.width, sz.height)));
  986. tr.copyTo(merged(cv::Rect(sz.width, 0, sz.width, sz.height)));
  987. bl.copyTo(merged(cv::Rect(0, sz.height, sz.width, sz.height)));
  988. br.copyTo(merged(cv::Rect(sz.width, sz.height, sz.width, sz.height)));
  989. }
  990. }} // namespace