test_fisheye.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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, undistortImage)
  90. {
  91. cv::Matx33d theK = this->K;
  92. cv::Mat theD = cv::Mat(this->D);
  93. std::string file = combine(datasets_repository_path, "/calib-3_stereo_from_JY/left/stereo_pair_014.jpg");
  94. cv::Matx33d newK = theK;
  95. cv::Mat distorted = cv::imread(file), undistorted;
  96. {
  97. newK(0, 0) = 100;
  98. newK(1, 1) = 100;
  99. cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
  100. cv::Mat correct = cv::imread(combine(datasets_repository_path, "new_f_100.png"));
  101. if (correct.empty())
  102. CV_Assert(cv::imwrite(combine(datasets_repository_path, "new_f_100.png"), undistorted));
  103. else
  104. EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
  105. }
  106. {
  107. double balance = 1.0;
  108. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
  109. cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
  110. cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_1.0.png"));
  111. if (correct.empty())
  112. CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_1.0.png"), undistorted));
  113. else
  114. EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
  115. }
  116. {
  117. double balance = 0.0;
  118. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
  119. cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
  120. cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_0.0.png"));
  121. if (correct.empty())
  122. CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_0.0.png"), undistorted));
  123. else
  124. EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
  125. }
  126. }
  127. TEST_F(fisheyeTest, undistortAndDistortImage)
  128. {
  129. cv::Matx33d K_src = this->K;
  130. cv::Mat D_src = cv::Mat(this->D);
  131. std::string file = combine(datasets_repository_path, "/calib-3_stereo_from_JY/left/stereo_pair_014.jpg");
  132. cv::Matx33d K_dst = K_src;
  133. cv::Mat image = cv::imread(file), image_projected;
  134. cv::Vec4d D_dst_vec (-1.0, 0.0, 0.0, 0.0);
  135. cv::Mat D_dst = cv::Mat(D_dst_vec);
  136. int imageWidth = (int)this->imageSize.width;
  137. int imageHeight = (int)this->imageSize.height;
  138. cv::Mat imagePoints(imageHeight, imageWidth, CV_32FC2), undPoints, distPoints;
  139. cv::Vec2f* pts = imagePoints.ptr<cv::Vec2f>();
  140. for(int y = 0, k = 0; y < imageHeight; ++y)
  141. {
  142. for(int x = 0; x < imageWidth; ++x)
  143. {
  144. cv::Vec2f point((float)x, (float)y);
  145. pts[k++] = point;
  146. }
  147. }
  148. cv::fisheye::undistortPoints(imagePoints, undPoints, K_dst, D_dst);
  149. cv::fisheye::distortPoints(undPoints, distPoints, K_src, D_src);
  150. cv::remap(image, image_projected, distPoints, cv::noArray(), cv::INTER_LINEAR);
  151. float dx, dy, r_sq;
  152. float R_MAX = 250;
  153. float imageCenterX = (float)imageWidth / 2;
  154. float imageCenterY = (float)imageHeight / 2;
  155. cv::Mat undPointsGt(imageHeight, imageWidth, CV_32FC2);
  156. cv::Mat imageGt(imageHeight, imageWidth, CV_8UC3);
  157. for(int y = 0, k = 0; y < imageHeight; ++y)
  158. {
  159. for(int x = 0; x < imageWidth; ++x)
  160. {
  161. dx = x - imageCenterX;
  162. dy = y - imageCenterY;
  163. r_sq = dy * dy + dx * dx;
  164. Vec2f & und_vec = undPoints.at<Vec2f>(y,x);
  165. Vec3b & pixel = image_projected.at<Vec3b>(y,x);
  166. Vec2f & undist_vec_gt = undPointsGt.at<Vec2f>(y,x);
  167. Vec3b & pixel_gt = imageGt.at<Vec3b>(y,x);
  168. if (r_sq > R_MAX * R_MAX)
  169. {
  170. undist_vec_gt[0] = -1e6;
  171. undist_vec_gt[1] = -1e6;
  172. pixel_gt[0] = 0;
  173. pixel_gt[1] = 0;
  174. pixel_gt[2] = 0;
  175. }
  176. else
  177. {
  178. undist_vec_gt[0] = und_vec[0];
  179. undist_vec_gt[1] = und_vec[1];
  180. pixel_gt[0] = pixel[0];
  181. pixel_gt[1] = pixel[1];
  182. pixel_gt[2] = pixel[2];
  183. }
  184. k++;
  185. }
  186. }
  187. EXPECT_MAT_NEAR(undPoints, undPointsGt, 1e-10);
  188. EXPECT_MAT_NEAR(image_projected, imageGt, 1e-10);
  189. Vec2f dist_point_1 = distPoints.at<Vec2f>(400, 640);
  190. Vec2f dist_point_1_gt(640.044f, 400.041f);
  191. Vec2f dist_point_2 = distPoints.at<Vec2f>(400, 440);
  192. Vec2f dist_point_2_gt(409.731f, 403.029f);
  193. Vec2f dist_point_3 = distPoints.at<Vec2f>(200, 640);
  194. Vec2f dist_point_3_gt(643.341f, 168.896f);
  195. Vec2f dist_point_4 = distPoints.at<Vec2f>(300, 480);
  196. Vec2f dist_point_4_gt(463.402f, 290.317f);
  197. Vec2f dist_point_5 = distPoints.at<Vec2f>(550, 750);
  198. Vec2f dist_point_5_gt(797.51f, 611.637f);
  199. EXPECT_MAT_NEAR(dist_point_1, dist_point_1_gt, 1e-2);
  200. EXPECT_MAT_NEAR(dist_point_2, dist_point_2_gt, 1e-2);
  201. EXPECT_MAT_NEAR(dist_point_3, dist_point_3_gt, 1e-2);
  202. EXPECT_MAT_NEAR(dist_point_4, dist_point_4_gt, 1e-2);
  203. EXPECT_MAT_NEAR(dist_point_5, dist_point_5_gt, 1e-2);
  204. CV_Assert(cv::imwrite(combine(datasets_repository_path, "new_distortion.png"), image_projected));
  205. }
  206. TEST_F(fisheyeTest, jacobians)
  207. {
  208. int n = 10;
  209. cv::Mat X(1, n, CV_64FC3);
  210. cv::Mat om(3, 1, CV_64F), theT(3, 1, CV_64F);
  211. cv::Mat f(2, 1, CV_64F), c(2, 1, CV_64F);
  212. cv::Mat k(4, 1, CV_64F);
  213. double alpha;
  214. cv::RNG r;
  215. r.fill(X, cv::RNG::NORMAL, 2, 1);
  216. X = cv::abs(X) * 10;
  217. r.fill(om, cv::RNG::NORMAL, 0, 1);
  218. om = cv::abs(om);
  219. r.fill(theT, cv::RNG::NORMAL, 0, 1);
  220. theT = cv::abs(theT); theT.at<double>(2) = 4; theT *= 10;
  221. r.fill(f, cv::RNG::NORMAL, 0, 1);
  222. f = cv::abs(f) * 1000;
  223. r.fill(c, cv::RNG::NORMAL, 0, 1);
  224. c = cv::abs(c) * 1000;
  225. r.fill(k, cv::RNG::NORMAL, 0, 1);
  226. k*= 0.5;
  227. alpha = 0.01*r.gaussian(1);
  228. cv::Mat x1, x2, xpred;
  229. cv::Matx33d theK(f.at<double>(0), alpha * f.at<double>(0), c.at<double>(0),
  230. 0, f.at<double>(1), c.at<double>(1),
  231. 0, 0, 1);
  232. cv::Mat jacobians;
  233. cv::fisheye::projectPoints(X, x1, om, theT, theK, k, alpha, jacobians);
  234. //test on T:
  235. cv::Mat dT(3, 1, CV_64FC1);
  236. r.fill(dT, cv::RNG::NORMAL, 0, 1);
  237. dT *= 1e-9*cv::norm(theT);
  238. cv::Mat T2 = theT + dT;
  239. cv::fisheye::projectPoints(X, x2, om, T2, theK, k, alpha, cv::noArray());
  240. xpred = x1 + cv::Mat(jacobians.colRange(11,14) * dT).reshape(2, 1);
  241. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  242. //test on om:
  243. cv::Mat dom(3, 1, CV_64FC1);
  244. r.fill(dom, cv::RNG::NORMAL, 0, 1);
  245. dom *= 1e-9*cv::norm(om);
  246. cv::Mat om2 = om + dom;
  247. cv::fisheye::projectPoints(X, x2, om2, theT, theK, k, alpha, cv::noArray());
  248. xpred = x1 + cv::Mat(jacobians.colRange(8,11) * dom).reshape(2, 1);
  249. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  250. //test on f:
  251. cv::Mat df(2, 1, CV_64FC1);
  252. r.fill(df, cv::RNG::NORMAL, 0, 1);
  253. df *= 1e-9*cv::norm(f);
  254. 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);
  255. cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());
  256. xpred = x1 + cv::Mat(jacobians.colRange(0,2) * df).reshape(2, 1);
  257. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  258. //test on c:
  259. cv::Mat dc(2, 1, CV_64FC1);
  260. r.fill(dc, cv::RNG::NORMAL, 0, 1);
  261. dc *= 1e-9*cv::norm(c);
  262. K2 = theK + cv::Matx33d(0, 0, dc.at<double>(0), 0, 0, dc.at<double>(1), 0, 0, 0);
  263. cv::fisheye::projectPoints(X, x2, om, theT, K2, k, alpha, cv::noArray());
  264. xpred = x1 + cv::Mat(jacobians.colRange(2,4) * dc).reshape(2, 1);
  265. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  266. //test on k:
  267. cv::Mat dk(4, 1, CV_64FC1);
  268. r.fill(dk, cv::RNG::NORMAL, 0, 1);
  269. dk *= 1e-9*cv::norm(k);
  270. cv::Mat k2 = k + dk;
  271. cv::fisheye::projectPoints(X, x2, om, theT, theK, k2, alpha, cv::noArray());
  272. xpred = x1 + cv::Mat(jacobians.colRange(4,8) * dk).reshape(2, 1);
  273. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  274. //test on alpha:
  275. cv::Mat dalpha(1, 1, CV_64FC1);
  276. r.fill(dalpha, cv::RNG::NORMAL, 0, 1);
  277. dalpha *= 1e-9*cv::norm(f);
  278. double alpha2 = alpha + dalpha.at<double>(0);
  279. K2 = theK + cv::Matx33d(0, f.at<double>(0) * dalpha.at<double>(0), 0, 0, 0, 0, 0, 0, 0);
  280. cv::fisheye::projectPoints(X, x2, om, theT, theK, k, alpha2, cv::noArray());
  281. xpred = x1 + cv::Mat(jacobians.col(14) * dalpha).reshape(2, 1);
  282. CV_Assert (cv::norm(x2 - xpred) < 1e-10);
  283. }
  284. TEST_F(fisheyeTest, Calibration)
  285. {
  286. const int n_images = 34;
  287. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  288. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  289. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  290. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  291. CV_Assert(fs_left.isOpened());
  292. for(int i = 0; i < n_images; ++i)
  293. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  294. fs_left.release();
  295. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  296. CV_Assert(fs_object.isOpened());
  297. for(int i = 0; i < n_images; ++i)
  298. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  299. fs_object.release();
  300. int flag = 0;
  301. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  302. flag |= cv::fisheye::CALIB_CHECK_COND;
  303. flag |= cv::fisheye::CALIB_FIX_SKEW;
  304. cv::Matx33d theK;
  305. cv::Vec4d theD;
  306. cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
  307. cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
  308. EXPECT_MAT_NEAR(theK, this->K, 1e-10);
  309. EXPECT_MAT_NEAR(theD, this->D, 1e-10);
  310. }
  311. TEST_F(fisheyeTest, CalibrationWithFixedFocalLength)
  312. {
  313. const int n_images = 34;
  314. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  315. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  316. const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
  317. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  318. CV_Assert(fs_left.isOpened());
  319. for(int i = 0; i < n_images; ++i)
  320. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  321. fs_left.release();
  322. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  323. CV_Assert(fs_object.isOpened());
  324. for(int i = 0; i < n_images; ++i)
  325. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  326. fs_object.release();
  327. int flag = 0;
  328. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  329. flag |= cv::fisheye::CALIB_CHECK_COND;
  330. flag |= cv::fisheye::CALIB_FIX_SKEW;
  331. flag |= cv::fisheye::CALIB_FIX_FOCAL_LENGTH;
  332. flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;
  333. cv::Matx33d theK = this->K;
  334. const cv::Matx33d newK(
  335. 558.478088, 0.000000, 620.458461,
  336. 0.000000, 560.506767, 381.939362,
  337. 0.000000, 0.000000, 1.000000);
  338. cv::Vec4d theD;
  339. const cv::Vec4d newD(-0.001461, -0.003298, 0.006057, -0.003742);
  340. cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
  341. cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
  342. // ensure that CALIB_FIX_FOCAL_LENGTH works and focal lenght has not changed
  343. EXPECT_EQ(theK(0,0), K(0,0));
  344. EXPECT_EQ(theK(1,1), K(1,1));
  345. EXPECT_MAT_NEAR(theK, newK, 1e-6);
  346. EXPECT_MAT_NEAR(theD, newD, 1e-6);
  347. }
  348. TEST_F(fisheyeTest, Homography)
  349. {
  350. const int n_images = 1;
  351. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  352. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  353. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  354. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  355. CV_Assert(fs_left.isOpened());
  356. for(int i = 0; i < n_images; ++i)
  357. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  358. fs_left.release();
  359. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  360. CV_Assert(fs_object.isOpened());
  361. for(int i = 0; i < n_images; ++i)
  362. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  363. fs_object.release();
  364. cv::internal::IntrinsicParams param;
  365. param.Init(cv::Vec2d(cv::max(imageSize.width, imageSize.height) / CV_PI, cv::max(imageSize.width, imageSize.height) / CV_PI),
  366. cv::Vec2d(imageSize.width / 2.0 - 0.5, imageSize.height / 2.0 - 0.5));
  367. cv::Mat _imagePoints (imagePoints[0]);
  368. cv::Mat _objectPoints(objectPoints[0]);
  369. cv::Mat imagePointsNormalized = NormalizePixels(_imagePoints, param).reshape(1).t();
  370. _objectPoints = _objectPoints.reshape(1).t();
  371. cv::Mat objectPointsMean, covObjectPoints;
  372. int Np = imagePointsNormalized.cols;
  373. cv::calcCovarMatrix(_objectPoints, covObjectPoints, objectPointsMean, cv::COVAR_NORMAL | cv::COVAR_COLS);
  374. cv::SVD svd(covObjectPoints);
  375. cv::Mat theR(svd.vt);
  376. if (cv::norm(theR(cv::Rect(2, 0, 1, 2))) < 1e-6)
  377. theR = cv::Mat::eye(3,3, CV_64FC1);
  378. if (cv::determinant(theR) < 0)
  379. theR = -theR;
  380. cv::Mat theT = -theR * objectPointsMean;
  381. cv::Mat X_new = theR * _objectPoints + theT * cv::Mat::ones(1, Np, CV_64FC1);
  382. cv::Mat H = cv::internal::ComputeHomography(imagePointsNormalized, X_new.rowRange(0, 2));
  383. cv::Mat M = cv::Mat::ones(3, X_new.cols, CV_64FC1);
  384. X_new.rowRange(0, 2).copyTo(M.rowRange(0, 2));
  385. cv::Mat mrep = H * M;
  386. cv::divide(mrep, cv::Mat::ones(3,1, CV_64FC1) * mrep.row(2).clone(), mrep);
  387. cv::Mat merr = (mrep.rowRange(0, 2) - imagePointsNormalized).t();
  388. cv::Vec2d std_err;
  389. cv::meanStdDev(merr.reshape(2), cv::noArray(), std_err);
  390. std_err *= sqrt((double)merr.reshape(2).total() / (merr.reshape(2).total() - 1));
  391. cv::Vec2d correct_std_err(0.00516740156010384, 0.00644205331553901);
  392. EXPECT_MAT_NEAR(std_err, correct_std_err, 1e-12);
  393. }
  394. TEST_F(fisheyeTest, EstimateUncertainties)
  395. {
  396. const int n_images = 34;
  397. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  398. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  399. const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");
  400. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  401. CV_Assert(fs_left.isOpened());
  402. for(int i = 0; i < n_images; ++i)
  403. fs_left[cv::format("image_%d", i )] >> imagePoints[i];
  404. fs_left.release();
  405. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  406. CV_Assert(fs_object.isOpened());
  407. for(int i = 0; i < n_images; ++i)
  408. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  409. fs_object.release();
  410. int flag = 0;
  411. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  412. flag |= cv::fisheye::CALIB_CHECK_COND;
  413. flag |= cv::fisheye::CALIB_FIX_SKEW;
  414. cv::Matx33d theK;
  415. cv::Vec4d theD;
  416. std::vector<cv::Vec3d> rvec;
  417. std::vector<cv::Vec3d> tvec;
  418. cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, theK, theD,
  419. rvec, tvec, flag, cv::TermCriteria(3, 20, 1e-6));
  420. cv::internal::IntrinsicParams param, errors;
  421. cv::Vec2d err_std;
  422. double thresh_cond = 1e6;
  423. int check_cond = 1;
  424. param.Init(cv::Vec2d(theK(0,0), theK(1,1)), cv::Vec2d(theK(0,2), theK(1, 2)), theD);
  425. param.isEstimate = std::vector<uchar>(9, 1);
  426. param.isEstimate[4] = 0;
  427. errors.isEstimate = param.isEstimate;
  428. double rms;
  429. cv::internal::EstimateUncertainties(objectPoints, imagePoints, param, rvec, tvec,
  430. errors, err_std, thresh_cond, check_cond, rms);
  431. EXPECT_MAT_NEAR(errors.f, cv::Vec2d(1.29837104202046, 1.31565641071524), 1e-10);
  432. EXPECT_MAT_NEAR(errors.c, cv::Vec2d(0.890439368129246, 0.816096854937896), 1e-10);
  433. EXPECT_MAT_NEAR(errors.k, cv::Vec4d(0.00516248605191506, 0.0168181467500934, 0.0213118690274604, 0.00916010877545648), 1e-10);
  434. EXPECT_MAT_NEAR(err_std, cv::Vec2d(0.187475975266883, 0.185678953263995), 1e-10);
  435. CV_Assert(fabs(rms - 0.263782587133546) < 1e-10);
  436. CV_Assert(errors.alpha == 0);
  437. }
  438. TEST_F(fisheyeTest, stereoRectify)
  439. {
  440. // For consistency purposes
  441. CV_StaticAssert(
  442. static_cast<int>(cv::CALIB_ZERO_DISPARITY) == static_cast<int>(cv::fisheye::CALIB_ZERO_DISPARITY),
  443. "For the purpose of continuity the following should be true: cv::CALIB_ZERO_DISPARITY == cv::fisheye::CALIB_ZERO_DISPARITY"
  444. );
  445. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  446. cv::Size calibration_size = this->imageSize, requested_size = calibration_size;
  447. cv::Matx33d K1 = this->K, K2 = K1;
  448. cv::Mat D1 = cv::Mat(this->D), D2 = D1;
  449. cv::Vec3d theT = this->T;
  450. cv::Matx33d theR = this->R;
  451. double balance = 0.0, fov_scale = 1.1;
  452. cv::Mat R1, R2, P1, P2, Q;
  453. cv::fisheye::stereoRectify(K1, D1, K2, D2, calibration_size, theR, theT, R1, R2, P1, P2, Q,
  454. cv::fisheye::CALIB_ZERO_DISPARITY, requested_size, balance, fov_scale);
  455. // Collected with these CMake flags: -DWITH_IPP=OFF -DCV_ENABLE_INTRINSICS=OFF -DCV_DISABLE_OPTIMIZATION=ON -DCMAKE_BUILD_TYPE=Debug
  456. cv::Matx33d R1_ref(
  457. 0.9992853269091279, 0.03779164101000276, -0.0007920188690205426,
  458. -0.03778569762983931, 0.9992646472015868, 0.006511981857667881,
  459. 0.001037534936357442, -0.006477400933964018, 0.9999784831677112
  460. );
  461. cv::Matx33d R2_ref(
  462. 0.9994868963898833, -0.03197579751378937, -0.001868774538573449,
  463. 0.03196298186616116, 0.9994677442608699, -0.0065265589947392,
  464. 0.002076471801477729, 0.006463478587068991, 0.9999769555891836
  465. );
  466. cv::Matx34d P1_ref(
  467. 420.8551870450913, 0, 586.501617798451, 0,
  468. 0, 420.8551870450913, 374.7667511986098, 0,
  469. 0, 0, 1, 0
  470. );
  471. cv::Matx34d P2_ref(
  472. 420.8551870450913, 0, 586.501617798451, -41.77758076597302,
  473. 0, 420.8551870450913, 374.7667511986098, 0,
  474. 0, 0, 1, 0
  475. );
  476. cv::Matx44d Q_ref(
  477. 1, 0, 0, -586.501617798451,
  478. 0, 1, 0, -374.7667511986098,
  479. 0, 0, 0, 420.8551870450913,
  480. 0, 0, 10.07370889670733, -0
  481. );
  482. const double eps = 1e-10;
  483. EXPECT_MAT_NEAR(R1_ref, R1, eps);
  484. EXPECT_MAT_NEAR(R2_ref, R2, eps);
  485. EXPECT_MAT_NEAR(P1_ref, P1, eps);
  486. EXPECT_MAT_NEAR(P2_ref, P2, eps);
  487. EXPECT_MAT_NEAR(Q_ref, Q, eps);
  488. if (::testing::Test::HasFailure())
  489. {
  490. std::cout << "Actual values are:" << std::endl
  491. << "R1 =" << std::endl << R1 << std::endl
  492. << "R2 =" << std::endl << R2 << std::endl
  493. << "P1 =" << std::endl << P1 << std::endl
  494. << "P2 =" << std::endl << P2 << std::endl
  495. << "Q =" << std::endl << Q << std::endl;
  496. }
  497. if (cvtest::debugLevel == 0)
  498. return;
  499. // DEBUG code is below
  500. cv::Mat lmapx, lmapy, rmapx, rmapy;
  501. //rewrite for fisheye
  502. cv::fisheye::initUndistortRectifyMap(K1, D1, R1, P1, requested_size, CV_32F, lmapx, lmapy);
  503. cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, requested_size, CV_32F, rmapx, rmapy);
  504. cv::Mat l, r, lundist, rundist;
  505. for (int i = 0; i < 34; ++i)
  506. {
  507. SCOPED_TRACE(cv::format("image %d", i));
  508. l = imread(combine(folder, cv::format("left/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);
  509. r = imread(combine(folder, cv::format("right/stereo_pair_%03d.jpg", i)), cv::IMREAD_COLOR);
  510. ASSERT_FALSE(l.empty());
  511. ASSERT_FALSE(r.empty());
  512. int ndisp = 128;
  513. cv::rectangle(l, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));
  514. cv::rectangle(r, cv::Rect(255, 0, 829, l.rows-1), cv::Scalar(0, 0, 255));
  515. cv::rectangle(r, cv::Rect(255-ndisp, 0, 829+ndisp ,l.rows-1), cv::Scalar(0, 0, 255));
  516. cv::remap(l, lundist, lmapx, lmapy, cv::INTER_LINEAR);
  517. cv::remap(r, rundist, rmapx, rmapy, cv::INTER_LINEAR);
  518. for (int ii = 0; ii < lundist.rows; ii += 20)
  519. {
  520. cv::line(lundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));
  521. cv::line(rundist, cv::Point(0, ii), cv::Point(lundist.cols, ii), cv::Scalar(0, 255, 0));
  522. }
  523. cv::Mat rectification;
  524. merge4(l, r, lundist, rundist, rectification);
  525. cv::imwrite(cv::format("fisheye_rectification_AB_%03d.png", i), rectification);
  526. }
  527. }
  528. TEST_F(fisheyeTest, stereoCalibrate)
  529. {
  530. const int n_images = 34;
  531. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  532. std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
  533. std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
  534. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  535. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  536. CV_Assert(fs_left.isOpened());
  537. for(int i = 0; i < n_images; ++i)
  538. fs_left[cv::format("image_%d", i )] >> leftPoints[i];
  539. fs_left.release();
  540. cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
  541. CV_Assert(fs_right.isOpened());
  542. for(int i = 0; i < n_images; ++i)
  543. fs_right[cv::format("image_%d", i )] >> rightPoints[i];
  544. fs_right.release();
  545. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  546. CV_Assert(fs_object.isOpened());
  547. for(int i = 0; i < n_images; ++i)
  548. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  549. fs_object.release();
  550. cv::Matx33d K1, K2, theR;
  551. cv::Vec3d theT;
  552. cv::Vec4d D1, D2;
  553. int flag = 0;
  554. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  555. flag |= cv::fisheye::CALIB_CHECK_COND;
  556. flag |= cv::fisheye::CALIB_FIX_SKEW;
  557. cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
  558. K1, D1, K2, D2, imageSize, theR, theT, flag,
  559. cv::TermCriteria(3, 12, 0));
  560. cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
  561. -0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
  562. -0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
  563. cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
  564. cv::Matx33d K1_correct (561.195925927249, 0, 621.282400272412,
  565. 0, 562.849402029712, 380.555455380889,
  566. 0, 0, 1);
  567. cv::Matx33d K2_correct (560.395452535348, 0, 678.971652040359,
  568. 0, 561.90171021422, 380.401340535339,
  569. 0, 0, 1);
  570. cv::Vec4d D1_correct (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
  571. cv::Vec4d D2_correct (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
  572. EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
  573. EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
  574. EXPECT_MAT_NEAR(K1, K1_correct, 1e-10);
  575. EXPECT_MAT_NEAR(K2, K2_correct, 1e-10);
  576. EXPECT_MAT_NEAR(D1, D1_correct, 1e-10);
  577. EXPECT_MAT_NEAR(D2, D2_correct, 1e-10);
  578. }
  579. TEST_F(fisheyeTest, stereoCalibrateFixIntrinsic)
  580. {
  581. const int n_images = 34;
  582. const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
  583. std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
  584. std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
  585. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  586. cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
  587. CV_Assert(fs_left.isOpened());
  588. for(int i = 0; i < n_images; ++i)
  589. fs_left[cv::format("image_%d", i )] >> leftPoints[i];
  590. fs_left.release();
  591. cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
  592. CV_Assert(fs_right.isOpened());
  593. for(int i = 0; i < n_images; ++i)
  594. fs_right[cv::format("image_%d", i )] >> rightPoints[i];
  595. fs_right.release();
  596. cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
  597. CV_Assert(fs_object.isOpened());
  598. for(int i = 0; i < n_images; ++i)
  599. fs_object[cv::format("image_%d", i )] >> objectPoints[i];
  600. fs_object.release();
  601. cv::Matx33d theR;
  602. cv::Vec3d theT;
  603. int flag = 0;
  604. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  605. flag |= cv::fisheye::CALIB_CHECK_COND;
  606. flag |= cv::fisheye::CALIB_FIX_SKEW;
  607. flag |= cv::fisheye::CALIB_FIX_INTRINSIC;
  608. cv::Matx33d K1 (561.195925927249, 0, 621.282400272412,
  609. 0, 562.849402029712, 380.555455380889,
  610. 0, 0, 1);
  611. cv::Matx33d K2 (560.395452535348, 0, 678.971652040359,
  612. 0, 561.90171021422, 380.401340535339,
  613. 0, 0, 1);
  614. cv::Vec4d D1 (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
  615. cv::Vec4d D2 (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
  616. cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
  617. K1, D1, K2, D2, imageSize, theR, theT, flag,
  618. cv::TermCriteria(3, 12, 0));
  619. cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
  620. -0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
  621. -0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
  622. cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
  623. EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
  624. EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
  625. }
  626. TEST_F(fisheyeTest, CalibrationWithDifferentPointsNumber)
  627. {
  628. const int n_images = 2;
  629. std::vector<std::vector<cv::Point2d> > imagePoints(n_images);
  630. std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
  631. std::vector<cv::Point2d> imgPoints1(10);
  632. std::vector<cv::Point2d> imgPoints2(15);
  633. std::vector<cv::Point3d> objectPoints1(imgPoints1.size());
  634. std::vector<cv::Point3d> objectPoints2(imgPoints2.size());
  635. for (size_t i = 0; i < imgPoints1.size(); i++)
  636. {
  637. imgPoints1[i] = cv::Point2d((double)i, (double)i);
  638. objectPoints1[i] = cv::Point3d((double)i, (double)i, 10.0);
  639. }
  640. for (size_t i = 0; i < imgPoints2.size(); i++)
  641. {
  642. imgPoints2[i] = cv::Point2d(i + 0.5, i + 0.5);
  643. objectPoints2[i] = cv::Point3d(i + 0.5, i + 0.5, 10.0);
  644. }
  645. imagePoints[0] = imgPoints1;
  646. imagePoints[1] = imgPoints2;
  647. objectPoints[0] = objectPoints1;
  648. objectPoints[1] = objectPoints2;
  649. cv::Matx33d theK = cv::Matx33d::eye();
  650. cv::Vec4d theD;
  651. int flag = 0;
  652. flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
  653. flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS;
  654. flag |= cv::fisheye::CALIB_FIX_SKEW;
  655. cv::fisheye::calibrate(objectPoints, imagePoints, cv::Size(100, 100), theK, theD,
  656. cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
  657. }
  658. TEST_F(fisheyeTest, estimateNewCameraMatrixForUndistortRectify)
  659. {
  660. cv::Size size(1920, 1080);
  661. cv::Mat K_fullhd(3, 3, cv::DataType<double>::type);
  662. K_fullhd.at<double>(0, 0) = 600.44477382;
  663. K_fullhd.at<double>(0, 1) = 0.0;
  664. K_fullhd.at<double>(0, 2) = 992.06425788;
  665. K_fullhd.at<double>(1, 0) = 0.0;
  666. K_fullhd.at<double>(1, 1) = 578.99298055;
  667. K_fullhd.at<double>(1, 2) = 549.26826242;
  668. K_fullhd.at<double>(2, 0) = 0.0;
  669. K_fullhd.at<double>(2, 1) = 0.0;
  670. K_fullhd.at<double>(2, 2) = 1.0;
  671. cv::Mat K_new_truth(3, 3, cv::DataType<double>::type);
  672. K_new_truth.at<double>(0, 0) = 387.4809086880343;
  673. K_new_truth.at<double>(0, 1) = 0.0;
  674. K_new_truth.at<double>(0, 2) = 1036.669802754649;
  675. K_new_truth.at<double>(1, 0) = 0.0;
  676. K_new_truth.at<double>(1, 1) = 373.6375700303157;
  677. K_new_truth.at<double>(1, 2) = 538.8373261247601;
  678. K_new_truth.at<double>(2, 0) = 0.0;
  679. K_new_truth.at<double>(2, 1) = 0.0;
  680. K_new_truth.at<double>(2, 2) = 1.0;
  681. cv::Mat D_fullhd(4, 1, cv::DataType<double>::type);
  682. D_fullhd.at<double>(0, 0) = -0.05090103223466704;
  683. D_fullhd.at<double>(1, 0) = 0.030944413642173308;
  684. D_fullhd.at<double>(2, 0) = -0.021509225493198905;
  685. D_fullhd.at<double>(3, 0) = 0.0043378096628297145;
  686. cv::Mat E = cv::Mat::eye(3, 3, cv::DataType<double>::type);
  687. cv::Mat K_new(3, 3, cv::DataType<double>::type);
  688. cv::fisheye::estimateNewCameraMatrixForUndistortRectify(K_fullhd, D_fullhd, size, E, K_new, 0.0, size);
  689. EXPECT_MAT_NEAR(K_new, K_new_truth, 1e-6);
  690. }
  691. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  692. /// fisheyeTest::
  693. const cv::Size fisheyeTest::imageSize(1280, 800);
  694. const cv::Matx33d fisheyeTest::K(558.478087865323, 0, 620.458515360843,
  695. 0, 560.506767351568, 381.939424848348,
  696. 0, 0, 1);
  697. const cv::Vec4d fisheyeTest::D(-0.0014613319981768, -0.00329861110580401, 0.00605760088590183, -0.00374209380722371);
  698. const cv::Matx33d fisheyeTest::R ( 9.9756700084424932e-01, 6.9698277640183867e-02, 1.4929569991321144e-03,
  699. -6.9711825162322980e-02, 9.9748249845531767e-01, 1.2997180766418455e-02,
  700. -5.8331736398316541e-04,-1.3069635393884985e-02, 9.9991441852366736e-01);
  701. const cv::Vec3d fisheyeTest::T(-9.9217369356044638e-02, 3.1741831972356663e-03, 1.8551007952921010e-04);
  702. std::string fisheyeTest::combine(const std::string& _item1, const std::string& _item2)
  703. {
  704. std::string item1 = _item1, item2 = _item2;
  705. std::replace(item1.begin(), item1.end(), '\\', '/');
  706. std::replace(item2.begin(), item2.end(), '\\', '/');
  707. if (item1.empty())
  708. return item2;
  709. if (item2.empty())
  710. return item1;
  711. char last = item1[item1.size()-1];
  712. return item1 + (last != '/' ? "/" : "") + item2;
  713. }
  714. void fisheyeTest::merge4(const cv::Mat& tl, const cv::Mat& tr, const cv::Mat& bl, const cv::Mat& br, cv::Mat& merged)
  715. {
  716. int type = tl.type();
  717. cv::Size sz = tl.size();
  718. ASSERT_EQ(type, tr.type()); ASSERT_EQ(type, bl.type()); ASSERT_EQ(type, br.type());
  719. ASSERT_EQ(sz.width, tr.cols); ASSERT_EQ(sz.width, bl.cols); ASSERT_EQ(sz.width, br.cols);
  720. ASSERT_EQ(sz.height, tr.rows); ASSERT_EQ(sz.height, bl.rows); ASSERT_EQ(sz.height, br.rows);
  721. merged.create(cv::Size(sz.width * 2, sz.height * 2), type);
  722. tl.copyTo(merged(cv::Rect(0, 0, sz.width, sz.height)));
  723. tr.copyTo(merged(cv::Rect(sz.width, 0, sz.width, sz.height)));
  724. bl.copyTo(merged(cv::Rect(0, sz.height, sz.width, sz.height)));
  725. br.copyTo(merged(cv::Rect(sz.width, sz.height, sz.width, sz.height)));
  726. }
  727. }} // namespace