test_cameracalibration_badarg.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. #include "test_chessboardgenerator.hpp"
  43. #include "opencv2/core/types_c.h"
  44. namespace opencv_test { namespace {
  45. class CV_CameraCalibrationBadArgTest : public cvtest::BadArgTest
  46. {
  47. public:
  48. CV_CameraCalibrationBadArgTest() {}
  49. ~CV_CameraCalibrationBadArgTest() {}
  50. protected:
  51. void run(int);
  52. void run_func(void) {}
  53. struct C_Caller
  54. {
  55. _InputArray imgPts_arg;
  56. _InputArray objPts_arg;
  57. _OutputArray rvecs_arg;
  58. _OutputArray tvecs_arg;
  59. _OutputArray newObjPts_arg;
  60. _InputOutputArray cameraMatrix_arg;
  61. _InputOutputArray distCoeffs_arg;
  62. std::vector<std::vector<Point2f> > imgPts;
  63. std::vector<std::vector<Point3f> > objPts;
  64. Size imageSize0, imageSize;
  65. int iFixedPoint0, iFixedPoint;
  66. Mat cameraMatrix;
  67. Mat distCoeffs;
  68. std::vector<Mat> rvecs;
  69. std::vector<Mat> tvecs;
  70. std::vector<Point3f> newObjPts;
  71. int flags0, flags;
  72. void initArgs()
  73. {
  74. imgPts_arg = imgPts;
  75. objPts_arg = objPts;
  76. rvecs_arg = rvecs;
  77. tvecs_arg = tvecs;
  78. newObjPts_arg = newObjPts;
  79. cameraMatrix_arg = cameraMatrix;
  80. distCoeffs_arg = distCoeffs;
  81. imageSize = imageSize0;
  82. flags = flags0;
  83. iFixedPoint = iFixedPoint0;
  84. }
  85. void operator()() const
  86. {
  87. calibrateCameraRO(objPts_arg, imgPts_arg, imageSize, iFixedPoint,
  88. cameraMatrix_arg, distCoeffs_arg, rvecs_arg, tvecs_arg,
  89. newObjPts_arg, flags);
  90. }
  91. };
  92. };
  93. void CV_CameraCalibrationBadArgTest::run( int /* start_from */ )
  94. {
  95. const int M = 2;
  96. Size imgSize(800, 600);
  97. Mat_<float> camMat(3, 3);
  98. Mat_<float> distCoeffs0(1, 5);
  99. camMat << 300.f, 0.f, imgSize.width/2.f, 0, 300.f, imgSize.height/2.f, 0.f, 0.f, 1.f;
  100. distCoeffs0 << 1.2f, 0.2f, 0.f, 0.f, 0.f;
  101. ChessBoardGenerator cbg(Size(8,6));
  102. Size corSize = cbg.cornersSize();
  103. vector<Point2f> corners;
  104. cbg(Mat(imgSize, CV_8U, Scalar(0)), camMat, distCoeffs0, corners);
  105. C_Caller caller;
  106. caller.imageSize0 = imgSize;
  107. caller.iFixedPoint0 = -1;
  108. caller.flags0 = 0;
  109. /////////////////////////////
  110. Mat cameraMatrix_cpp;
  111. Mat distCoeffs_cpp;
  112. Mat rvecs_cpp;
  113. Mat tvecs_cpp;
  114. Mat newObjPts_cpp;
  115. std::vector<Point3f> objPts_cpp;
  116. for(int y = 0; y < corSize.height; ++y)
  117. for(int x = 0; x < corSize.width; ++x)
  118. objPts_cpp.push_back(Point3f((float)x, (float)y, 0.f));
  119. caller.objPts.resize(M);
  120. caller.imgPts.resize(M);
  121. for(int i = 0; i < M; i++)
  122. {
  123. caller.objPts[i] = objPts_cpp;
  124. caller.imgPts[i] = corners;
  125. }
  126. caller.cameraMatrix.create(3, 3, CV_32F);
  127. caller.distCoeffs.create(5, 1, CV_32F);
  128. caller.rvecs.clear();
  129. caller.tvecs.clear();
  130. caller.newObjPts.clear();
  131. /* /*//*/ */
  132. int errors = 0;
  133. caller.initArgs();
  134. caller.objPts_arg = noArray();
  135. errors += run_test_case( CV_StsBadArg, "None passed in objPts", caller);
  136. caller.initArgs();
  137. caller.imgPts_arg = noArray();
  138. errors += run_test_case( CV_StsBadArg, "None passed in imgPts", caller );
  139. caller.initArgs();
  140. caller.cameraMatrix_arg = noArray();
  141. errors += run_test_case( CV_StsBadArg, "Zero passed in cameraMatrix", caller );
  142. caller.initArgs();
  143. caller.distCoeffs_arg = noArray();
  144. errors += run_test_case( CV_StsBadArg, "Zero passed in distCoeffs", caller );
  145. caller.initArgs();
  146. caller.imageSize.width = -1;
  147. errors += run_test_case( CV_StsOutOfRange, "Bad image width", caller );
  148. caller.initArgs();
  149. caller.imageSize.height = -1;
  150. errors += run_test_case( CV_StsOutOfRange, "Bad image height", caller );
  151. caller.initArgs();
  152. caller.imgPts[0].clear();
  153. errors += run_test_case( CV_StsBadSize, "Bad imgpts[0]", caller );
  154. caller.imgPts[0] = caller.imgPts[1];
  155. caller.initArgs();
  156. caller.objPts[1].clear();
  157. errors += run_test_case( CV_StsBadSize, "Bad objpts[1]", caller );
  158. caller.objPts[1] = caller.objPts[0];
  159. caller.initArgs();
  160. Mat badCM = Mat::zeros(4, 4, CV_64F);
  161. caller.cameraMatrix_arg = badCM;
  162. caller.flags = CALIB_USE_INTRINSIC_GUESS;
  163. errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", caller );
  164. caller.initArgs();
  165. Mat badDC = Mat::zeros(10, 10, CV_64F);
  166. caller.distCoeffs_arg = badDC;
  167. caller.flags = CALIB_USE_INTRINSIC_GUESS;
  168. errors += run_test_case( CV_StsBadArg, "Bad camearaMatrix header", caller );
  169. if (errors)
  170. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  171. else
  172. ts->set_failed_test_info(cvtest::TS::OK);
  173. }
  174. class CV_Rodrigues2BadArgTest : public cvtest::BadArgTest
  175. {
  176. public:
  177. CV_Rodrigues2BadArgTest() {}
  178. ~CV_Rodrigues2BadArgTest() {}
  179. protected:
  180. void run_func(void) {}
  181. struct C_Caller
  182. {
  183. _InputArray src_arg;
  184. _OutputArray dst_arg, j_arg;
  185. Mat src;
  186. Mat dst;
  187. Mat jacobian;
  188. void initArgs()
  189. {
  190. src_arg = src;
  191. dst_arg = dst;
  192. j_arg = jacobian;
  193. }
  194. void operator()()
  195. {
  196. cv::Rodrigues(src_arg, dst_arg, j_arg);
  197. }
  198. };
  199. void run(int /* start_from */ )
  200. {
  201. Mat zeros(1, sizeof(CvMat), CV_8U, Scalar(0));
  202. Mat src_cpp(3, 1, CV_32F);
  203. Mat dst_cpp(3, 3, CV_32F);
  204. C_Caller caller;
  205. /*/*//*/*/
  206. int errors = 0;
  207. caller.initArgs();
  208. caller.src_arg = noArray();
  209. errors += run_test_case( CV_StsBadArg, "Src is empty matrix", caller );
  210. caller.initArgs();
  211. caller.src = Mat::zeros(3, 1, CV_8U);
  212. errors += run_test_case( CV_StsUnsupportedFormat, "Bad src formart", caller );
  213. caller.initArgs();
  214. caller.src = Mat::zeros(1, 1, CV_32F);
  215. errors += run_test_case( CV_StsBadSize, "Bad src size", caller );
  216. if (errors)
  217. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  218. else
  219. ts->set_failed_test_info(cvtest::TS::OK);
  220. }
  221. };
  222. //////////////////////////////////////////////////////////////////////////////////
  223. //////////////////////////////////////////////////////////////////////////////////
  224. class CV_ProjectPoints2BadArgTest : public cvtest::BadArgTest
  225. {
  226. public:
  227. CV_ProjectPoints2BadArgTest() : camMat(3, 3), distCoeffs(1, 5)
  228. {
  229. Size imsSize(800, 600);
  230. camMat << 300.f, 0.f, imsSize.width/2.f, 0, 300.f, imsSize.height/2.f, 0.f, 0.f, 1.f;
  231. distCoeffs << 1.2f, 0.2f, 0.f, 0.f, 0.f;
  232. }
  233. ~CV_ProjectPoints2BadArgTest() {}
  234. protected:
  235. void run_func(void) {}
  236. Mat_<float> camMat;
  237. Mat_<float> distCoeffs;
  238. struct C_Caller
  239. {
  240. _InputArray objectPoints_arg, rvec_arg, tvec_arg, A_arg, DC_arg;
  241. _OutputArray imagePoints_arg;
  242. Mat objectPoints;
  243. Mat r_vec;
  244. Mat t_vec;
  245. Mat A;
  246. Mat distCoeffs;
  247. Mat imagePoints;
  248. Mat J;
  249. double aspectRatio0, aspectRatio;
  250. void initArgs()
  251. {
  252. objectPoints_arg = objectPoints;
  253. imagePoints_arg = imagePoints;
  254. rvec_arg = r_vec;
  255. tvec_arg = t_vec;
  256. A_arg = A;
  257. DC_arg = distCoeffs;
  258. aspectRatio = aspectRatio0;
  259. }
  260. void operator()()
  261. {
  262. projectPoints(objectPoints_arg, rvec_arg, tvec_arg, A_arg, DC_arg,
  263. imagePoints_arg, J, aspectRatio );
  264. }
  265. };
  266. void run(int /* start_from */ )
  267. {
  268. C_Caller caller;
  269. const int n = 10;
  270. Mat objectPoints_cpp(1, n, CV_32FC3);
  271. randu(objectPoints_cpp, Scalar::all(1), Scalar::all(10));
  272. caller.objectPoints = objectPoints_cpp;
  273. caller.t_vec = Mat::zeros(1, 3, CV_32F);
  274. cvtest::Rodrigues(Mat::eye(3, 3, CV_32F), caller.r_vec);
  275. caller.A = Mat::eye(3, 3, CV_32F);
  276. caller.distCoeffs = Mat::zeros(1, 5, CV_32F);
  277. caller.aspectRatio0 = 1.0;
  278. /********************/
  279. int errors = 0;
  280. caller.initArgs();
  281. caller.objectPoints_arg = noArray();
  282. errors += run_test_case( CV_StsBadArg, "Zero objectPoints", caller );
  283. caller.initArgs();
  284. caller.rvec_arg = noArray();
  285. errors += run_test_case( CV_StsBadArg, "Zero r_vec", caller );
  286. caller.initArgs();
  287. caller.tvec_arg = noArray();
  288. errors += run_test_case( CV_StsBadArg, "Zero t_vec", caller );
  289. caller.initArgs();
  290. caller.A_arg = noArray();
  291. errors += run_test_case( CV_StsBadArg, "Zero camMat", caller );
  292. caller.initArgs();
  293. caller.imagePoints_arg = noArray();
  294. errors += run_test_case( CV_StsBadArg, "Zero imagePoints", caller );
  295. Mat save_rvec = caller.r_vec;
  296. caller.initArgs();
  297. caller.r_vec.create(2, 2, CV_32F);
  298. errors += run_test_case( CV_StsBadArg, "Bad rvec format", caller );
  299. caller.initArgs();
  300. caller.r_vec.create(1, 3, CV_8U);
  301. errors += run_test_case( CV_StsBadArg, "Bad rvec format", caller );
  302. caller.r_vec = save_rvec;
  303. /****************************/
  304. Mat save_tvec = caller.t_vec;
  305. caller.initArgs();
  306. caller.t_vec.create(3, 3, CV_32F);
  307. errors += run_test_case( CV_StsBadArg, "Bad tvec format", caller );
  308. caller.initArgs();
  309. caller.t_vec.create(1, 3, CV_8U);
  310. errors += run_test_case( CV_StsBadArg, "Bad tvec format", caller );
  311. caller.t_vec = save_tvec;
  312. /****************************/
  313. Mat save_A = caller.A;
  314. caller.initArgs();
  315. caller.A.create(2, 2, CV_32F);
  316. errors += run_test_case( CV_StsBadArg, "Bad A format", caller );
  317. caller.A = save_A;
  318. /****************************/
  319. Mat save_DC = caller.distCoeffs;
  320. caller.initArgs();
  321. caller.distCoeffs.create(3, 3, CV_32F);
  322. errors += run_test_case( CV_StsBadArg, "Bad distCoeffs format", caller );
  323. caller.distCoeffs = save_DC;
  324. if (errors)
  325. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  326. else
  327. ts->set_failed_test_info(cvtest::TS::OK);
  328. }
  329. };
  330. TEST(Calib3d_CalibrateCamera_CPP, badarg) { CV_CameraCalibrationBadArgTest test; test.safe_run(); }
  331. TEST(Calib3d_Rodrigues_CPP, badarg) { CV_Rodrigues2BadArgTest test; test.safe_run(); }
  332. TEST(Calib3d_ProjectPoints_CPP, badarg) { CV_ProjectPoints2BadArgTest test; test.safe_run(); }
  333. }} // namespace