test_cameracalibration_artificial.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #include "test_chessboardgenerator.hpp"
  44. namespace opencv_test { namespace {
  45. //template<class T> ostream& operator<<(ostream& out, const Mat_<T>& mat)
  46. //{
  47. // for(Mat_<T>::const_iterator pos = mat.begin(), end = mat.end(); pos != end; ++pos)
  48. // out << *pos << " ";
  49. // return out;
  50. //}
  51. //ostream& operator<<(ostream& out, const Mat& mat) { return out << Mat_<double>(mat); }
  52. Mat calcRvec(const vector<Point3f>& points, const Size& cornerSize)
  53. {
  54. Point3f p00 = points[0];
  55. Point3f p10 = points[1];
  56. Point3f p01 = points[cornerSize.width];
  57. Vec3d ex(p10.x - p00.x, p10.y - p00.y, p10.z - p00.z);
  58. Vec3d ey(p01.x - p00.x, p01.y - p00.y, p01.z - p00.z);
  59. Vec3d ez = ex.cross(ey);
  60. Mat rot(3, 3, CV_64F);
  61. *rot.ptr<Vec3d>(0) = ex;
  62. *rot.ptr<Vec3d>(1) = ey;
  63. *rot.ptr<Vec3d>(2) = ez * (1.0/cv::norm(ez)); // TODO cvtest
  64. Mat res;
  65. cvtest::Rodrigues(rot.t(), res);
  66. return res.reshape(1, 1);
  67. }
  68. class CV_CalibrateCameraArtificialTest : public cvtest::BaseTest
  69. {
  70. public:
  71. CV_CalibrateCameraArtificialTest() :
  72. r(0)
  73. {
  74. }
  75. ~CV_CalibrateCameraArtificialTest() {}
  76. protected:
  77. int r;
  78. const static int JUST_FIND_CORNERS = 0;
  79. const static int USE_CORNERS_SUBPIX = 1;
  80. const static int USE_4QUAD_CORNERS = 2;
  81. const static int ARTIFICIAL_CORNERS = 4;
  82. bool checkErr(double a, double a0, double eps, double delta)
  83. {
  84. return fabs(a - a0) > eps * (fabs(a0) + delta);
  85. }
  86. void compareCameraMatrs(const Mat_<double>& camMat, const Mat& camMat_est)
  87. {
  88. if ( camMat_est.at<double>(0, 1) != 0 || camMat_est.at<double>(1, 0) != 0 ||
  89. camMat_est.at<double>(2, 0) != 0 || camMat_est.at<double>(2, 1) != 0 ||
  90. camMat_est.at<double>(2, 2) != 1)
  91. {
  92. ts->printf( cvtest::TS::LOG, "Bad shape of camera matrix returned \n");
  93. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  94. }
  95. double fx_e = camMat_est.at<double>(0, 0), fy_e = camMat_est.at<double>(1, 1);
  96. double cx_e = camMat_est.at<double>(0, 2), cy_e = camMat_est.at<double>(1, 2);
  97. double fx = camMat(0, 0), fy = camMat(1, 1), cx = camMat(0, 2), cy = camMat(1, 2);
  98. const double eps = 1e-2;
  99. const double dlt = 1e-5;
  100. bool fail = checkErr(fx_e, fx, eps, dlt) || checkErr(fy_e, fy, eps, dlt) ||
  101. checkErr(cx_e, cx, eps, dlt) || checkErr(cy_e, cy, eps, dlt);
  102. if (fail)
  103. {
  104. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  105. }
  106. ts->printf( cvtest::TS::LOG, "%d) Expected [Fx Fy Cx Cy] = [%.3f %.3f %.3f %.3f]\n", r, fx, fy, cx, cy);
  107. ts->printf( cvtest::TS::LOG, "%d) Estimated [Fx Fy Cx Cy] = [%.3f %.3f %.3f %.3f]\n", r, fx_e, fy_e, cx_e, cy_e);
  108. }
  109. void compareDistCoeffs(const Mat_<double>& distCoeffs, const Mat& distCoeffs_est)
  110. {
  111. const double *dt_e = distCoeffs_est.ptr<double>();
  112. double k1_e = dt_e[0], k2_e = dt_e[1], k3_e = dt_e[4];
  113. double p1_e = dt_e[2], p2_e = dt_e[3];
  114. double k1 = distCoeffs(0, 0), k2 = distCoeffs(0, 1), k3 = distCoeffs(0, 4);
  115. double p1 = distCoeffs(0, 2), p2 = distCoeffs(0, 3);
  116. const double eps = 5e-2;
  117. const double dlt = 1e-3;
  118. const double eps_k3 = 5;
  119. const double dlt_k3 = 1e-3;
  120. bool fail = checkErr(k1_e, k1, eps, dlt) || checkErr(k2_e, k2, eps, dlt) || checkErr(k3_e, k3, eps_k3, dlt_k3) ||
  121. checkErr(p1_e, p1, eps, dlt) || checkErr(p2_e, p2, eps, dlt);
  122. if (fail)
  123. {
  124. // commented according to vp123's recommendation. TODO - improve accuracy
  125. //ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); ss
  126. }
  127. ts->printf( cvtest::TS::LOG, "%d) DistCoeff exp=(%.2f, %.2f, %.4f, %.4f %.2f)\n", r, k1, k2, p1, p2, k3);
  128. ts->printf( cvtest::TS::LOG, "%d) DistCoeff est=(%.2f, %.2f, %.4f, %.4f %.2f)\n", r, k1_e, k2_e, p1_e, p2_e, k3_e);
  129. ts->printf( cvtest::TS::LOG, "%d) AbsError = [%.5f %.5f %.5f %.5f %.5f]\n", r, fabs(k1-k1_e), fabs(k2-k2_e), fabs(p1-p1_e), fabs(p2-p2_e), fabs(k3-k3_e));
  130. }
  131. void compareShiftVecs(const vector<Mat>& tvecs, const vector<Mat>& tvecs_est)
  132. {
  133. const double eps = 1e-2;
  134. const double dlt = 1e-4;
  135. int err_count = 0;
  136. const int errMsgNum = 4;
  137. for(size_t i = 0; i < tvecs.size(); ++i)
  138. {
  139. const Point3d& tvec = *tvecs[i].ptr<Point3d>();
  140. const Point3d& tvec_est = *tvecs_est[i].ptr<Point3d>();
  141. double n1 = cv::norm(tvec_est - tvec); // TODO cvtest
  142. double n2 = cv::norm(tvec); // TODO cvtest
  143. if (n1 > eps* (n2 + dlt))
  144. {
  145. if (err_count++ < errMsgNum)
  146. {
  147. if (err_count == errMsgNum)
  148. ts->printf( cvtest::TS::LOG, "%d) ...\n", r);
  149. else
  150. {
  151. ts->printf( cvtest::TS::LOG, "%d) Bad accuracy in returned tvecs. Index = %d\n", r, i);
  152. ts->printf( cvtest::TS::LOG, "%d) norm(tvec_est - tvec) = %f, norm(tvec_exp) = %f \n", r, n1, n2);
  153. }
  154. }
  155. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  156. }
  157. }
  158. }
  159. void compareRotationVecs(const vector<Mat>& rvecs, const vector<Mat>& rvecs_est)
  160. {
  161. const double eps = 2e-2;
  162. const double dlt = 1e-4;
  163. Mat rmat, rmat_est;
  164. int err_count = 0;
  165. const int errMsgNum = 4;
  166. for(size_t i = 0; i < rvecs.size(); ++i)
  167. {
  168. cvtest::Rodrigues(rvecs[i], rmat);
  169. cvtest::Rodrigues(rvecs_est[i], rmat_est);
  170. if (cvtest::norm(rmat_est, rmat, NORM_L2) > eps* (cvtest::norm(rmat, NORM_L2) + dlt))
  171. {
  172. if (err_count++ < errMsgNum)
  173. {
  174. if (err_count == errMsgNum)
  175. ts->printf( cvtest::TS::LOG, "%d) ...\n", r);
  176. else
  177. {
  178. ts->printf( cvtest::TS::LOG, "%d) Bad accuracy in returned rvecs (rotation matrs). Index = %d\n", r, i);
  179. ts->printf( cvtest::TS::LOG, "%d) norm(rot_mat_est - rot_mat_exp) = %f, norm(rot_mat_exp) = %f \n", r,
  180. cvtest::norm(rmat_est, rmat, NORM_L2), cvtest::norm(rmat, NORM_L2));
  181. }
  182. }
  183. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  184. }
  185. }
  186. }
  187. double reprojectErrorWithoutIntrinsics(const vector<Point3f>& cb3d, const vector<Mat>& _rvecs_exp, const vector<Mat>& _tvecs_exp,
  188. const vector<Mat>& rvecs_est, const vector<Mat>& tvecs_est)
  189. {
  190. const static Mat eye33 = Mat::eye(3, 3, CV_64F);
  191. const static Mat zero15 = Mat::zeros(1, 5, CV_64F);
  192. Mat _chessboard3D(cb3d);
  193. vector<Point2f> uv_exp, uv_est;
  194. double res = 0;
  195. for(size_t i = 0; i < rvecs_exp.size(); ++i)
  196. {
  197. projectPoints(_chessboard3D, _rvecs_exp[i], _tvecs_exp[i], eye33, zero15, uv_exp);
  198. projectPoints(_chessboard3D, rvecs_est[i], tvecs_est[i], eye33, zero15, uv_est);
  199. for(size_t j = 0; j < cb3d.size(); ++j)
  200. res += cv::norm(uv_exp[i] - uv_est[i]); // TODO cvtest
  201. }
  202. return res;
  203. }
  204. Size2f sqSile;
  205. vector<Point3f> chessboard3D;
  206. vector<Mat> boards, rvecs_exp, tvecs_exp, rvecs_spnp, tvecs_spnp;
  207. vector< vector<Point3f> > objectPoints;
  208. vector< vector<Point2f> > imagePoints_art;
  209. vector< vector<Point2f> > imagePoints_findCb;
  210. void prepareForTest(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, size_t brdsNum, const ChessBoardGenerator& cbg)
  211. {
  212. sqSile = Size2f(1.f, 1.f);
  213. Size cornersSize = cbg.cornersSize();
  214. chessboard3D.clear();
  215. for(int j = 0; j < cornersSize.height; ++j)
  216. for(int i = 0; i < cornersSize.width; ++i)
  217. chessboard3D.push_back(Point3f(sqSile.width * i, sqSile.height * j, 0));
  218. boards.resize(brdsNum);
  219. rvecs_exp.resize(brdsNum);
  220. tvecs_exp.resize(brdsNum);
  221. objectPoints.clear();
  222. objectPoints.resize(brdsNum, chessboard3D);
  223. imagePoints_art.clear();
  224. imagePoints_findCb.clear();
  225. vector<Point2f> corners_art, corners_fcb;
  226. for(size_t i = 0; i < brdsNum; ++i)
  227. {
  228. for(;;)
  229. {
  230. boards[i] = cbg(bg, camMat, distCoeffs, sqSile, corners_art);
  231. if(findChessboardCorners(boards[i], cornersSize, corners_fcb))
  232. break;
  233. }
  234. //cv::namedWindow("CB"); imshow("CB", boards[i]); cv::waitKey();
  235. imagePoints_art.push_back(corners_art);
  236. imagePoints_findCb.push_back(corners_fcb);
  237. tvecs_exp[i].create(1, 3, CV_64F);
  238. *tvecs_exp[i].ptr<Point3d>() = cbg.corners3d[0];
  239. rvecs_exp[i] = calcRvec(cbg.corners3d, cbg.cornersSize());
  240. }
  241. }
  242. void runTest(const Size& imgSize, const Mat_<double>& camMat, const Mat_<double>& distCoeffs, size_t brdsNum, const Size& cornersSize, int flag = 0)
  243. {
  244. const TermCriteria tc(TermCriteria::EPS|TermCriteria::MAX_ITER, 30, 0.1);
  245. vector< vector<Point2f> > imagePoints;
  246. switch(flag)
  247. {
  248. case JUST_FIND_CORNERS: imagePoints = imagePoints_findCb; break;
  249. case ARTIFICIAL_CORNERS: imagePoints = imagePoints_art; break;
  250. case USE_CORNERS_SUBPIX:
  251. for(size_t i = 0; i < brdsNum; ++i)
  252. {
  253. Mat gray;
  254. cvtColor(boards[i], gray, COLOR_BGR2GRAY);
  255. vector<Point2f> tmp = imagePoints_findCb[i];
  256. cornerSubPix(gray, tmp, Size(5, 5), Size(-1,-1), tc);
  257. imagePoints.push_back(tmp);
  258. }
  259. break;
  260. case USE_4QUAD_CORNERS:
  261. for(size_t i = 0; i < brdsNum; ++i)
  262. {
  263. Mat gray;
  264. cvtColor(boards[i], gray, COLOR_BGR2GRAY);
  265. vector<Point2f> tmp = imagePoints_findCb[i];
  266. find4QuadCornerSubpix(gray, tmp, Size(5, 5));
  267. imagePoints.push_back(tmp);
  268. }
  269. break;
  270. default:
  271. throw std::exception();
  272. }
  273. Mat camMat_est = Mat::eye(3, 3, CV_64F), distCoeffs_est = Mat::zeros(1, 5, CV_64F);
  274. vector<Mat> rvecs_est, tvecs_est;
  275. int flags = /*CALIB_FIX_K3|*/CALIB_FIX_K4|CALIB_FIX_K5|CALIB_FIX_K6; //CALIB_FIX_K3; //CALIB_FIX_ASPECT_RATIO | | CALIB_ZERO_TANGENT_DIST;
  276. TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 100, DBL_EPSILON);
  277. double rep_error = calibrateCamera(objectPoints, imagePoints, imgSize, camMat_est, distCoeffs_est, rvecs_est, tvecs_est, flags, criteria);
  278. rep_error /= brdsNum * cornersSize.area();
  279. const double thres = 1;
  280. if (rep_error > thres)
  281. {
  282. ts->printf( cvtest::TS::LOG, "%d) Too big reproject error = %f\n", r, rep_error);
  283. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  284. }
  285. compareCameraMatrs(camMat, camMat_est);
  286. compareDistCoeffs(distCoeffs, distCoeffs_est);
  287. compareShiftVecs(tvecs_exp, tvecs_est);
  288. compareRotationVecs(rvecs_exp, rvecs_est);
  289. double rep_errorWOI = reprojectErrorWithoutIntrinsics(chessboard3D, rvecs_exp, tvecs_exp, rvecs_est, tvecs_est);
  290. rep_errorWOI /= brdsNum * cornersSize.area();
  291. const double thres2 = 0.01;
  292. if (rep_errorWOI > thres2)
  293. {
  294. ts->printf( cvtest::TS::LOG, "%d) Too big reproject error without intrinsics = %f\n", r, rep_errorWOI);
  295. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  296. }
  297. ts->printf( cvtest::TS::LOG, "%d) Testing solvePnP...\n", r);
  298. rvecs_spnp.resize(brdsNum);
  299. tvecs_spnp.resize(brdsNum);
  300. for(size_t i = 0; i < brdsNum; ++i)
  301. solvePnP(objectPoints[i], imagePoints[i], camMat, distCoeffs, rvecs_spnp[i], tvecs_spnp[i]);
  302. compareShiftVecs(tvecs_exp, tvecs_spnp);
  303. compareRotationVecs(rvecs_exp, rvecs_spnp);
  304. }
  305. void run(int)
  306. {
  307. ts->set_failed_test_info(cvtest::TS::OK);
  308. RNG& rng = theRNG();
  309. int progress = 0;
  310. int repeat_num = 3;
  311. for(r = 0; r < repeat_num; ++r)
  312. {
  313. const int brds_num = 20;
  314. Mat bg(Size(640, 480), CV_8UC3);
  315. randu(bg, Scalar::all(32), Scalar::all(255));
  316. GaussianBlur(bg, bg, Size(5, 5), 2);
  317. double fx = 300 + (20 * (double)rng - 10);
  318. double fy = 300 + (20 * (double)rng - 10);
  319. double cx = bg.cols/2 + (40 * (double)rng - 20);
  320. double cy = bg.rows/2 + (40 * (double)rng - 20);
  321. Mat_<double> camMat(3, 3);
  322. camMat << fx, 0., cx, 0, fy, cy, 0., 0., 1.;
  323. double k1 = 0.5 + (double)rng/5;
  324. double k2 = (double)rng/5;
  325. double k3 = (double)rng/5;
  326. double p1 = 0.001 + (double)rng/10;
  327. double p2 = 0.001 + (double)rng/10;
  328. Mat_<double> distCoeffs(1, 5, 0.0);
  329. distCoeffs << k1, k2, p1, p2, k3;
  330. ChessBoardGenerator cbg(Size(9, 8));
  331. cbg.min_cos = 0.9;
  332. cbg.cov = 0.8;
  333. progress = update_progress(progress, r, repeat_num, 0);
  334. ts->printf( cvtest::TS::LOG, "\n");
  335. prepareForTest(bg, camMat, distCoeffs, brds_num, cbg);
  336. ts->printf( cvtest::TS::LOG, "artificial corners\n");
  337. runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), ARTIFICIAL_CORNERS);
  338. progress = update_progress(progress, r, repeat_num, 0);
  339. ts->printf( cvtest::TS::LOG, "findChessboard corners\n");
  340. runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), JUST_FIND_CORNERS);
  341. progress = update_progress(progress, r, repeat_num, 0);
  342. ts->printf( cvtest::TS::LOG, "cornersSubPix corners\n");
  343. runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), USE_CORNERS_SUBPIX);
  344. progress = update_progress(progress, r, repeat_num, 0);
  345. ts->printf( cvtest::TS::LOG, "4quad corners\n");
  346. runTest(bg.size(), camMat, distCoeffs, brds_num, cbg.cornersSize(), USE_4QUAD_CORNERS);
  347. progress = update_progress(progress, r, repeat_num, 0);
  348. }
  349. }
  350. };
  351. TEST(Calib3d_CalibrateCamera_CPP, DISABLED_accuracy_on_artificial_data) { CV_CalibrateCameraArtificialTest test; test.safe_run(); }
  352. }} // namespace