test_undistort.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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 "opencv2/imgproc/imgproc_c.h"
  44. namespace opencv_test { namespace {
  45. class CV_DefaultNewCameraMatrixTest : public cvtest::ArrayTest
  46. {
  47. public:
  48. CV_DefaultNewCameraMatrixTest();
  49. protected:
  50. int prepare_test_case (int test_case_idx);
  51. void prepare_to_validation( int test_case_idx );
  52. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  53. void run_func();
  54. private:
  55. cv::Size img_size;
  56. cv::Mat camera_mat;
  57. cv::Mat new_camera_mat;
  58. int matrix_type;
  59. bool center_principal_point;
  60. static const int MAX_X = 2048;
  61. static const int MAX_Y = 2048;
  62. //static const int MAX_VAL = 10000;
  63. };
  64. CV_DefaultNewCameraMatrixTest::CV_DefaultNewCameraMatrixTest()
  65. {
  66. test_array[INPUT].push_back(NULL);
  67. test_array[OUTPUT].push_back(NULL);
  68. test_array[REF_OUTPUT].push_back(NULL);
  69. matrix_type = 0;
  70. center_principal_point = false;
  71. }
  72. void CV_DefaultNewCameraMatrixTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  73. {
  74. cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx,sizes,types);
  75. RNG& rng = ts->get_rng();
  76. matrix_type = types[INPUT][0] = types[OUTPUT][0]= types[REF_OUTPUT][0] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  77. sizes[INPUT][0] = sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(3,3);
  78. }
  79. int CV_DefaultNewCameraMatrixTest::prepare_test_case(int test_case_idx)
  80. {
  81. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  82. if (code <= 0)
  83. return code;
  84. RNG& rng = ts->get_rng();
  85. img_size.width = cvtest::randInt(rng) % MAX_X + 1;
  86. img_size.height = cvtest::randInt(rng) % MAX_Y + 1;
  87. center_principal_point = ((cvtest::randInt(rng) % 2)!=0);
  88. // Generating camera_mat matrix
  89. double sz = MAX(img_size.width, img_size.height);
  90. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  91. double a[9] = {0,0,0,0,0,0,0,0,1};
  92. Mat _a(3,3,CV_64F,a);
  93. a[2] = (img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  94. a[5] = (img_size.height - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  95. a[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  96. a[4] = aspect_ratio*a[0];
  97. Mat& _a0 = test_mat[INPUT][0];
  98. cvtest::convert(_a, _a0, _a0.type());
  99. camera_mat = _a0;
  100. return code;
  101. }
  102. void CV_DefaultNewCameraMatrixTest::run_func()
  103. {
  104. new_camera_mat = cv::getDefaultNewCameraMatrix(camera_mat,img_size,center_principal_point);
  105. }
  106. void CV_DefaultNewCameraMatrixTest::prepare_to_validation( int /*test_case_idx*/ )
  107. {
  108. const Mat& src = test_mat[INPUT][0];
  109. Mat& dst = test_mat[REF_OUTPUT][0];
  110. Mat& test_output = test_mat[OUTPUT][0];
  111. Mat& output = new_camera_mat;
  112. cvtest::convert( output, test_output, test_output.type() );
  113. if (!center_principal_point)
  114. {
  115. cvtest::copy(src, dst);
  116. }
  117. else
  118. {
  119. double a[9] = {0,0,0,0,0,0,0,0,1};
  120. Mat _a(3,3,CV_64F,a);
  121. if (matrix_type == CV_64F)
  122. {
  123. a[0] = src.at<double>(0,0);
  124. a[4] = src.at<double>(1,1);
  125. }
  126. else
  127. {
  128. a[0] = src.at<float>(0,0);
  129. a[4] = src.at<float>(1,1);
  130. }
  131. a[2] = (img_size.width - 1)*0.5;
  132. a[5] = (img_size.height - 1)*0.5;
  133. cvtest::convert( _a, dst, dst.type() );
  134. }
  135. }
  136. //---------
  137. class CV_UndistortPointsTest : public cvtest::ArrayTest
  138. {
  139. public:
  140. CV_UndistortPointsTest();
  141. protected:
  142. int prepare_test_case (int test_case_idx);
  143. void prepare_to_validation( int test_case_idx );
  144. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  145. double get_success_error_level( int test_case_idx, int i, int j );
  146. void run_func();
  147. void distortPoints(const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatrix,
  148. const CvMat* _distCoeffs, const CvMat* matR, const CvMat* matP);
  149. private:
  150. bool useDstMat;
  151. static const int N_POINTS = 10;
  152. static const int MAX_X = 2048;
  153. static const int MAX_Y = 2048;
  154. bool zero_new_cam;
  155. bool zero_distortion;
  156. bool zero_R;
  157. cv::Size img_size;
  158. cv::Mat dst_points_mat;
  159. cv::Mat camera_mat;
  160. cv::Mat R;
  161. cv::Mat P;
  162. cv::Mat distortion_coeffs;
  163. cv::Mat src_points;
  164. std::vector<cv::Point2f> dst_points;
  165. };
  166. CV_UndistortPointsTest::CV_UndistortPointsTest()
  167. {
  168. test_array[INPUT].push_back(NULL); // points matrix
  169. test_array[INPUT].push_back(NULL); // camera matrix
  170. test_array[INPUT].push_back(NULL); // distortion coeffs
  171. test_array[INPUT].push_back(NULL); // R matrix
  172. test_array[INPUT].push_back(NULL); // P matrix
  173. test_array[OUTPUT].push_back(NULL); // distorted dst points
  174. test_array[TEMP].push_back(NULL); // dst points
  175. test_array[REF_OUTPUT].push_back(NULL);
  176. useDstMat = false;
  177. zero_new_cam = zero_distortion = zero_R = false;
  178. }
  179. void CV_UndistortPointsTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  180. {
  181. cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx,sizes,types);
  182. RNG& rng = ts->get_rng();
  183. //rng.next();
  184. types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = types[TEMP][0]= CV_32FC2;
  185. types[INPUT][1] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  186. types[INPUT][2] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  187. types[INPUT][3] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  188. types[INPUT][4] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  189. sizes[INPUT][0] = sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = sizes[TEMP][0]= cvtest::randInt(rng)%2 ? cvSize(1,N_POINTS) : cvSize(N_POINTS,1);
  190. sizes[INPUT][1] = sizes[INPUT][3] = cvSize(3,3);
  191. sizes[INPUT][4] = cvtest::randInt(rng)%2 ? cvSize(3,3) : cvSize(4,3);
  192. if (cvtest::randInt(rng)%2)
  193. {
  194. if (cvtest::randInt(rng)%2)
  195. {
  196. sizes[INPUT][2] = cvSize(1,4);
  197. }
  198. else
  199. {
  200. sizes[INPUT][2] = cvSize(1,5);
  201. }
  202. }
  203. else
  204. {
  205. if (cvtest::randInt(rng)%2)
  206. {
  207. sizes[INPUT][2] = cvSize(4,1);
  208. }
  209. else
  210. {
  211. sizes[INPUT][2] = cvSize(5,1);
  212. }
  213. }
  214. }
  215. int CV_UndistortPointsTest::prepare_test_case(int test_case_idx)
  216. {
  217. RNG& rng = ts->get_rng();
  218. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  219. if (code <= 0)
  220. return code;
  221. useDstMat = (cvtest::randInt(rng) % 2) == 0;
  222. img_size.width = cvtest::randInt(rng) % MAX_X + 1;
  223. img_size.height = cvtest::randInt(rng) % MAX_Y + 1;
  224. int dist_size = test_mat[INPUT][2].cols > test_mat[INPUT][2].rows ? test_mat[INPUT][2].cols : test_mat[INPUT][2].rows;
  225. double cam[9] = {0,0,0,0,0,0,0,0,1};
  226. vector<double> dist(dist_size);
  227. vector<double> proj(test_mat[INPUT][4].cols * test_mat[INPUT][4].rows);
  228. vector<Point2d> points(N_POINTS);
  229. Mat _camera(3,3,CV_64F,cam);
  230. Mat _distort(test_mat[INPUT][2].rows,test_mat[INPUT][2].cols,CV_64F,&dist[0]);
  231. Mat _proj(test_mat[INPUT][4].size(), CV_64F, &proj[0]);
  232. Mat _points(test_mat[INPUT][0].size(), CV_64FC2, &points[0]);
  233. _proj = Scalar::all(0);
  234. //Generating points
  235. for( int i = 0; i < N_POINTS; i++ )
  236. {
  237. points[i].x = cvtest::randReal(rng)*img_size.width;
  238. points[i].y = cvtest::randReal(rng)*img_size.height;
  239. }
  240. //Generating camera matrix
  241. double sz = MAX(img_size.width,img_size.height);
  242. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  243. cam[2] = (img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  244. cam[5] = (img_size.height - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  245. cam[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  246. cam[4] = aspect_ratio*cam[0];
  247. //Generating distortion coeffs
  248. dist[0] = cvtest::randReal(rng)*0.06 - 0.03;
  249. dist[1] = cvtest::randReal(rng)*0.06 - 0.03;
  250. if( dist[0]*dist[1] > 0 )
  251. dist[1] = -dist[1];
  252. if( cvtest::randInt(rng)%4 != 0 )
  253. {
  254. dist[2] = cvtest::randReal(rng)*0.004 - 0.002;
  255. dist[3] = cvtest::randReal(rng)*0.004 - 0.002;
  256. if (dist_size > 4)
  257. dist[4] = cvtest::randReal(rng)*0.004 - 0.002;
  258. }
  259. else
  260. {
  261. dist[2] = dist[3] = 0;
  262. if (dist_size > 4)
  263. dist[4] = 0;
  264. }
  265. //Generating P matrix (projection)
  266. if( test_mat[INPUT][4].cols != 4 )
  267. {
  268. proj[8] = 1;
  269. if (cvtest::randInt(rng)%2 == 0) // use identity new camera matrix
  270. {
  271. proj[0] = 1;
  272. proj[4] = 1;
  273. }
  274. else
  275. {
  276. proj[0] = cam[0] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[0]; //10%
  277. proj[4] = cam[4] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[4]; //10%
  278. proj[2] = cam[2] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.width; //15%
  279. proj[5] = cam[5] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.height; //15%
  280. }
  281. }
  282. else
  283. {
  284. proj[10] = 1;
  285. proj[0] = cam[0] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[0]; //10%
  286. proj[5] = cam[4] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[4]; //10%
  287. proj[2] = cam[2] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.width; //15%
  288. proj[6] = cam[5] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.height; //15%
  289. proj[3] = (img_size.height + img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  290. proj[7] = (img_size.height + img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  291. proj[11] = (img_size.height + img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  292. }
  293. //Generating R matrix
  294. Mat _rot(3,3,CV_64F);
  295. Mat rotation(1,3,CV_64F);
  296. rotation.at<double>(0) = CV_PI*(cvtest::randReal(rng) - (double)0.5); // phi
  297. rotation.at<double>(1) = CV_PI*(cvtest::randReal(rng) - (double)0.5); // ksi
  298. rotation.at<double>(2) = CV_PI*(cvtest::randReal(rng) - (double)0.5); //khi
  299. cvtest::Rodrigues(rotation, _rot);
  300. //copying data
  301. //src_points = &_points;
  302. _points.convertTo(test_mat[INPUT][0], test_mat[INPUT][0].type());
  303. _camera.convertTo(test_mat[INPUT][1], test_mat[INPUT][1].type());
  304. _distort.convertTo(test_mat[INPUT][2], test_mat[INPUT][2].type());
  305. _rot.convertTo(test_mat[INPUT][3], test_mat[INPUT][3].type());
  306. _proj.convertTo(test_mat[INPUT][4], test_mat[INPUT][4].type());
  307. zero_distortion = (cvtest::randInt(rng)%2) == 0 ? false : true;
  308. zero_new_cam = (cvtest::randInt(rng)%2) == 0 ? false : true;
  309. zero_R = (cvtest::randInt(rng)%2) == 0 ? false : true;
  310. _points.convertTo(src_points, CV_32F);
  311. camera_mat = test_mat[INPUT][1];
  312. distortion_coeffs = test_mat[INPUT][2];
  313. R = test_mat[INPUT][3];
  314. P = test_mat[INPUT][4];
  315. return code;
  316. }
  317. void CV_UndistortPointsTest::prepare_to_validation(int /*test_case_idx*/)
  318. {
  319. int dist_size = test_mat[INPUT][2].cols > test_mat[INPUT][2].rows ? test_mat[INPUT][2].cols : test_mat[INPUT][2].rows;
  320. double cam[9] = {0,0,0,0,0,0,0,0,1};
  321. double rot[9] = {1,0,0,0,1,0,0,0,1};
  322. double* dist = new double[dist_size ];
  323. double* proj = new double[test_mat[INPUT][4].cols * test_mat[INPUT][4].rows];
  324. double* points = new double[N_POINTS*2];
  325. double* r_points = new double[N_POINTS*2];
  326. //Run reference calculations
  327. CvMat ref_points= cvMat(test_mat[INPUT][0].rows,test_mat[INPUT][0].cols,CV_64FC2,r_points);
  328. CvMat _camera = cvMat(3,3,CV_64F,cam);
  329. CvMat _rot = cvMat(3,3,CV_64F,rot);
  330. CvMat _distort = cvMat(test_mat[INPUT][2].rows,test_mat[INPUT][2].cols,CV_64F,dist);
  331. CvMat _proj = cvMat(test_mat[INPUT][4].rows,test_mat[INPUT][4].cols,CV_64F,proj);
  332. CvMat _points= cvMat(test_mat[TEMP][0].rows,test_mat[TEMP][0].cols,CV_64FC2,points);
  333. Mat __camera = cvarrToMat(&_camera);
  334. Mat __distort = cvarrToMat(&_distort);
  335. Mat __rot = cvarrToMat(&_rot);
  336. Mat __proj = cvarrToMat(&_proj);
  337. Mat __points = cvarrToMat(&_points);
  338. Mat _ref_points = cvarrToMat(&ref_points);
  339. cvtest::convert(test_mat[INPUT][1], __camera, __camera.type());
  340. cvtest::convert(test_mat[INPUT][2], __distort, __distort.type());
  341. cvtest::convert(test_mat[INPUT][3], __rot, __rot.type());
  342. cvtest::convert(test_mat[INPUT][4], __proj, __proj.type());
  343. if (useDstMat)
  344. {
  345. CvMat temp = cvMat(dst_points_mat);
  346. for (int i=0;i<N_POINTS*2;i++)
  347. {
  348. points[i] = temp.data.fl[i];
  349. }
  350. }
  351. else
  352. {
  353. for (int i=0;i<N_POINTS;i++)
  354. {
  355. points[2*i] = dst_points[i].x;
  356. points[2*i+1] = dst_points[i].y;
  357. }
  358. }
  359. CvMat* input2 = zero_distortion ? 0 : &_distort;
  360. CvMat* input3 = zero_R ? 0 : &_rot;
  361. CvMat* input4 = zero_new_cam ? 0 : &_proj;
  362. distortPoints(&_points,&ref_points,&_camera,input2,input3,input4);
  363. Mat& dst = test_mat[REF_OUTPUT][0];
  364. cvtest::convert(_ref_points, dst, dst.type());
  365. cvtest::copy(test_mat[INPUT][0], test_mat[OUTPUT][0]);
  366. delete[] dist;
  367. delete[] proj;
  368. delete[] points;
  369. delete[] r_points;
  370. }
  371. void CV_UndistortPointsTest::run_func()
  372. {
  373. cv::Mat input2,input3,input4;
  374. input2 = zero_distortion ? cv::Mat() : cv::Mat(test_mat[INPUT][2]);
  375. input3 = zero_R ? cv::Mat() : cv::Mat(test_mat[INPUT][3]);
  376. input4 = zero_new_cam ? cv::Mat() : cv::Mat(test_mat[INPUT][4]);
  377. if (useDstMat)
  378. {
  379. //cv::undistortPoints(src_points,dst_points_mat,camera_mat,distortion_coeffs,R,P);
  380. cv::undistortPoints(src_points,dst_points_mat,camera_mat,input2,input3,input4);
  381. }
  382. else
  383. {
  384. //cv::undistortPoints(src_points,dst_points,camera_mat,distortion_coeffs,R,P);
  385. cv::undistortPoints(src_points,dst_points,camera_mat,input2,input3,input4);
  386. }
  387. }
  388. void CV_UndistortPointsTest::distortPoints(const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatrix,
  389. const CvMat* _distCoeffs,
  390. const CvMat* matR, const CvMat* matP)
  391. {
  392. double a[9];
  393. CvMat* __P;
  394. if ((!matP)||(matP->cols == 3))
  395. __P = cvCreateMat(3,3,CV_64F);
  396. else
  397. __P = cvCreateMat(3,4,CV_64F);
  398. if (matP)
  399. {
  400. cvtest::convert(cvarrToMat(matP), cvarrToMat(__P), -1);
  401. }
  402. else
  403. {
  404. cvZero(__P);
  405. __P->data.db[0] = 1;
  406. __P->data.db[4] = 1;
  407. __P->data.db[8] = 1;
  408. }
  409. CvMat* __R = cvCreateMat(3,3,CV_64F);
  410. if (matR)
  411. {
  412. cvCopy(matR,__R);
  413. }
  414. else
  415. {
  416. cvZero(__R);
  417. __R->data.db[0] = 1;
  418. __R->data.db[4] = 1;
  419. __R->data.db[8] = 1;
  420. }
  421. for (int i=0;i<N_POINTS;i++)
  422. {
  423. int movement = __P->cols > 3 ? 1 : 0;
  424. double x = (_src->data.db[2*i]-__P->data.db[2])/__P->data.db[0];
  425. double y = (_src->data.db[2*i+1]-__P->data.db[5+movement])/__P->data.db[4+movement];
  426. CvMat inverse = cvMat(3,3,CV_64F,a);
  427. cvInvert(__R,&inverse);
  428. double w1 = x*inverse.data.db[6]+y*inverse.data.db[7]+inverse.data.db[8];
  429. double _x = (x*inverse.data.db[0]+y*inverse.data.db[1]+inverse.data.db[2])/w1;
  430. double _y = (x*inverse.data.db[3]+y*inverse.data.db[4]+inverse.data.db[5])/w1;
  431. //Distortions
  432. double __x = _x;
  433. double __y = _y;
  434. if (_distCoeffs)
  435. {
  436. double r2 = _x*_x+_y*_y;
  437. __x = _x*(1+_distCoeffs->data.db[0]*r2+_distCoeffs->data.db[1]*r2*r2)+
  438. 2*_distCoeffs->data.db[2]*_x*_y+_distCoeffs->data.db[3]*(r2+2*_x*_x);
  439. __y = _y*(1+_distCoeffs->data.db[0]*r2+_distCoeffs->data.db[1]*r2*r2)+
  440. 2*_distCoeffs->data.db[3]*_x*_y+_distCoeffs->data.db[2]*(r2+2*_y*_y);
  441. if ((_distCoeffs->cols > 4) || (_distCoeffs->rows > 4))
  442. {
  443. __x+=_x*_distCoeffs->data.db[4]*r2*r2*r2;
  444. __y+=_y*_distCoeffs->data.db[4]*r2*r2*r2;
  445. }
  446. }
  447. _dst->data.db[2*i] = __x*_cameraMatrix->data.db[0]+_cameraMatrix->data.db[2];
  448. _dst->data.db[2*i+1] = __y*_cameraMatrix->data.db[4]+_cameraMatrix->data.db[5];
  449. }
  450. cvReleaseMat(&__R);
  451. cvReleaseMat(&__P);
  452. }
  453. double CV_UndistortPointsTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  454. {
  455. return 5e-2;
  456. }
  457. //------------------------------------------------------
  458. class CV_InitUndistortRectifyMapTest : public cvtest::ArrayTest
  459. {
  460. public:
  461. CV_InitUndistortRectifyMapTest();
  462. protected:
  463. int prepare_test_case (int test_case_idx);
  464. void prepare_to_validation( int test_case_idx );
  465. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  466. double get_success_error_level( int test_case_idx, int i, int j );
  467. void run_func();
  468. private:
  469. static const int MAX_X = 1024;
  470. static const int MAX_Y = 1024;
  471. bool zero_new_cam;
  472. bool zero_distortion;
  473. bool zero_R;
  474. cv::Size img_size;
  475. int map_type;
  476. };
  477. CV_InitUndistortRectifyMapTest::CV_InitUndistortRectifyMapTest()
  478. {
  479. test_array[INPUT].push_back(NULL); // camera matrix
  480. test_array[INPUT].push_back(NULL); // distortion coeffs
  481. test_array[INPUT].push_back(NULL); // R matrix
  482. test_array[INPUT].push_back(NULL); // new camera matrix
  483. test_array[OUTPUT].push_back(NULL); // distorted mapx
  484. test_array[OUTPUT].push_back(NULL); // distorted mapy
  485. test_array[REF_OUTPUT].push_back(NULL);
  486. test_array[REF_OUTPUT].push_back(NULL);
  487. zero_distortion = zero_new_cam = zero_R = false;
  488. map_type = 0;
  489. }
  490. void CV_InitUndistortRectifyMapTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  491. {
  492. cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx,sizes,types);
  493. RNG& rng = ts->get_rng();
  494. //rng.next();
  495. map_type = CV_32F;
  496. types[OUTPUT][0] = types[OUTPUT][1] = types[REF_OUTPUT][0] = types[REF_OUTPUT][1] = map_type;
  497. img_size.width = cvtest::randInt(rng) % MAX_X + 1;
  498. img_size.height = cvtest::randInt(rng) % MAX_Y + 1;
  499. types[INPUT][0] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  500. types[INPUT][1] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  501. types[INPUT][2] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  502. types[INPUT][3] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  503. sizes[OUTPUT][0] = sizes[OUTPUT][1] = sizes[REF_OUTPUT][0] = sizes[REF_OUTPUT][1] = img_size;
  504. sizes[INPUT][0] = sizes[INPUT][2] = sizes[INPUT][3] = cvSize(3,3);
  505. Size dsize;
  506. if (cvtest::randInt(rng)%2)
  507. {
  508. if (cvtest::randInt(rng)%2)
  509. {
  510. dsize = Size(1,4);
  511. }
  512. else
  513. {
  514. dsize = Size(1,5);
  515. }
  516. }
  517. else
  518. {
  519. if (cvtest::randInt(rng)%2)
  520. {
  521. dsize = Size(4,1);
  522. }
  523. else
  524. {
  525. dsize = Size(5,1);
  526. }
  527. }
  528. sizes[INPUT][1] = dsize;
  529. }
  530. int CV_InitUndistortRectifyMapTest::prepare_test_case(int test_case_idx)
  531. {
  532. RNG& rng = ts->get_rng();
  533. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  534. if (code <= 0)
  535. return code;
  536. int dist_size = test_mat[INPUT][1].cols > test_mat[INPUT][1].rows ? test_mat[INPUT][1].cols : test_mat[INPUT][1].rows;
  537. double cam[9] = {0,0,0,0,0,0,0,0,1};
  538. vector<double> dist(dist_size);
  539. vector<double> new_cam(test_mat[INPUT][3].cols * test_mat[INPUT][3].rows);
  540. Mat _camera(3,3,CV_64F,cam);
  541. Mat _distort(test_mat[INPUT][1].size(),CV_64F,&dist[0]);
  542. Mat _new_cam(test_mat[INPUT][3].size(),CV_64F,&new_cam[0]);
  543. //Generating camera matrix
  544. double sz = MAX(img_size.width,img_size.height);
  545. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  546. cam[2] = (img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  547. cam[5] = (img_size.height - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  548. cam[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  549. cam[4] = aspect_ratio*cam[0];
  550. //Generating distortion coeffs
  551. dist[0] = cvtest::randReal(rng)*0.06 - 0.03;
  552. dist[1] = cvtest::randReal(rng)*0.06 - 0.03;
  553. if( dist[0]*dist[1] > 0 )
  554. dist[1] = -dist[1];
  555. if( cvtest::randInt(rng)%4 != 0 )
  556. {
  557. dist[2] = cvtest::randReal(rng)*0.004 - 0.002;
  558. dist[3] = cvtest::randReal(rng)*0.004 - 0.002;
  559. if (dist_size > 4)
  560. dist[4] = cvtest::randReal(rng)*0.004 - 0.002;
  561. }
  562. else
  563. {
  564. dist[2] = dist[3] = 0;
  565. if (dist_size > 4)
  566. dist[4] = 0;
  567. }
  568. //Generating new camera matrix
  569. _new_cam = Scalar::all(0);
  570. new_cam[8] = 1;
  571. //new_cam[0] = cam[0];
  572. //new_cam[4] = cam[4];
  573. //new_cam[2] = cam[2];
  574. //new_cam[5] = cam[5];
  575. new_cam[0] = cam[0] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[0]; //10%
  576. new_cam[4] = cam[4] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[4]; //10%
  577. new_cam[2] = cam[2] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.width; //15%
  578. new_cam[5] = cam[5] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.height; //15%
  579. //Generating R matrix
  580. Mat _rot(3,3,CV_64F);
  581. Mat rotation(1,3,CV_64F);
  582. rotation.at<double>(0) = CV_PI/8*(cvtest::randReal(rng) - (double)0.5); // phi
  583. rotation.at<double>(1) = CV_PI/8*(cvtest::randReal(rng) - (double)0.5); // ksi
  584. rotation.at<double>(2) = CV_PI/3*(cvtest::randReal(rng) - (double)0.5); //khi
  585. cvtest::Rodrigues(rotation, _rot);
  586. //cvSetIdentity(_rot);
  587. //copying data
  588. cvtest::convert( _camera, test_mat[INPUT][0], test_mat[INPUT][0].type());
  589. cvtest::convert( _distort, test_mat[INPUT][1], test_mat[INPUT][1].type());
  590. cvtest::convert( _rot, test_mat[INPUT][2], test_mat[INPUT][2].type());
  591. cvtest::convert( _new_cam, test_mat[INPUT][3], test_mat[INPUT][3].type());
  592. zero_distortion = (cvtest::randInt(rng)%2) == 0 ? false : true;
  593. zero_new_cam = (cvtest::randInt(rng)%2) == 0 ? false : true;
  594. zero_R = (cvtest::randInt(rng)%2) == 0 ? false : true;
  595. return code;
  596. }
  597. void CV_InitUndistortRectifyMapTest::prepare_to_validation(int/* test_case_idx*/)
  598. {
  599. cvtest::initUndistortMap(test_mat[INPUT][0],
  600. zero_distortion ? cv::Mat() : test_mat[INPUT][1],
  601. zero_R ? cv::Mat() : test_mat[INPUT][2],
  602. zero_new_cam ? test_mat[INPUT][0] : test_mat[INPUT][3],
  603. img_size, test_mat[REF_OUTPUT][0], test_mat[REF_OUTPUT][1],
  604. test_mat[REF_OUTPUT][0].type());
  605. }
  606. void CV_InitUndistortRectifyMapTest::run_func()
  607. {
  608. cv::Mat camera_mat = test_mat[INPUT][0];
  609. cv::Mat dist = zero_distortion ? cv::Mat() : test_mat[INPUT][1];
  610. cv::Mat R = zero_R ? cv::Mat() : test_mat[INPUT][2];
  611. cv::Mat new_cam = zero_new_cam ? cv::Mat() : test_mat[INPUT][3];
  612. cv::Mat& mapx = test_mat[OUTPUT][0], &mapy = test_mat[OUTPUT][1];
  613. cv::initUndistortRectifyMap(camera_mat,dist,R,new_cam,img_size,map_type,mapx,mapy);
  614. }
  615. double CV_InitUndistortRectifyMapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  616. {
  617. return 8;
  618. }
  619. //------------------------------------------------------
  620. class CV_InitInverseRectificationMapTest : public cvtest::ArrayTest
  621. {
  622. public:
  623. CV_InitInverseRectificationMapTest();
  624. protected:
  625. int prepare_test_case (int test_case_idx);
  626. void prepare_to_validation( int test_case_idx );
  627. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  628. double get_success_error_level( int test_case_idx, int i, int j );
  629. void run_func();
  630. private:
  631. static const int MAX_X = 1024;
  632. static const int MAX_Y = 1024;
  633. bool zero_new_cam;
  634. bool zero_distortion;
  635. bool zero_R;
  636. cv::Size img_size;
  637. int map_type;
  638. };
  639. CV_InitInverseRectificationMapTest::CV_InitInverseRectificationMapTest()
  640. {
  641. test_array[INPUT].push_back(NULL); // camera matrix
  642. test_array[INPUT].push_back(NULL); // distortion coeffs
  643. test_array[INPUT].push_back(NULL); // R matrix
  644. test_array[INPUT].push_back(NULL); // new camera matrix
  645. test_array[OUTPUT].push_back(NULL); // inverse rectified mapx
  646. test_array[OUTPUT].push_back(NULL); // inverse rectified mapy
  647. test_array[REF_OUTPUT].push_back(NULL);
  648. test_array[REF_OUTPUT].push_back(NULL);
  649. zero_distortion = zero_new_cam = zero_R = false;
  650. map_type = 0;
  651. }
  652. void CV_InitInverseRectificationMapTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  653. {
  654. cvtest::ArrayTest::get_test_array_types_and_sizes(test_case_idx,sizes,types);
  655. RNG& rng = ts->get_rng();
  656. //rng.next();
  657. map_type = CV_32F;
  658. types[OUTPUT][0] = types[OUTPUT][1] = types[REF_OUTPUT][0] = types[REF_OUTPUT][1] = map_type;
  659. img_size.width = cvtest::randInt(rng) % MAX_X + 1;
  660. img_size.height = cvtest::randInt(rng) % MAX_Y + 1;
  661. types[INPUT][0] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  662. types[INPUT][1] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  663. types[INPUT][2] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  664. types[INPUT][3] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  665. sizes[OUTPUT][0] = sizes[OUTPUT][1] = sizes[REF_OUTPUT][0] = sizes[REF_OUTPUT][1] = img_size;
  666. sizes[INPUT][0] = sizes[INPUT][2] = sizes[INPUT][3] = cvSize(3,3);
  667. Size dsize;
  668. if (cvtest::randInt(rng)%2)
  669. {
  670. if (cvtest::randInt(rng)%2)
  671. {
  672. dsize = Size(1,4);
  673. }
  674. else
  675. {
  676. dsize = Size(1,5);
  677. }
  678. }
  679. else
  680. {
  681. if (cvtest::randInt(rng)%2)
  682. {
  683. dsize = Size(4,1);
  684. }
  685. else
  686. {
  687. dsize = Size(5,1);
  688. }
  689. }
  690. sizes[INPUT][1] = dsize;
  691. }
  692. int CV_InitInverseRectificationMapTest::prepare_test_case(int test_case_idx)
  693. {
  694. RNG& rng = ts->get_rng();
  695. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  696. if (code <= 0)
  697. return code;
  698. int dist_size = test_mat[INPUT][1].cols > test_mat[INPUT][1].rows ? test_mat[INPUT][1].cols : test_mat[INPUT][1].rows;
  699. double cam[9] = {0,0,0,0,0,0,0,0,1};
  700. vector<double> dist(dist_size);
  701. vector<double> new_cam(test_mat[INPUT][3].cols * test_mat[INPUT][3].rows);
  702. Mat _camera(3,3,CV_64F,cam);
  703. Mat _distort(test_mat[INPUT][1].size(),CV_64F,&dist[0]);
  704. Mat _new_cam(test_mat[INPUT][3].size(),CV_64F,&new_cam[0]);
  705. //Generating camera matrix
  706. double sz = MAX(img_size.width,img_size.height);
  707. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  708. cam[2] = (img_size.width - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  709. cam[5] = (img_size.height - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  710. cam[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  711. cam[4] = aspect_ratio*cam[0];
  712. //Generating distortion coeffs
  713. dist[0] = cvtest::randReal(rng)*0.06 - 0.03;
  714. dist[1] = cvtest::randReal(rng)*0.06 - 0.03;
  715. if( dist[0]*dist[1] > 0 )
  716. dist[1] = -dist[1];
  717. if( cvtest::randInt(rng)%4 != 0 )
  718. {
  719. dist[2] = cvtest::randReal(rng)*0.004 - 0.002;
  720. dist[3] = cvtest::randReal(rng)*0.004 - 0.002;
  721. if (dist_size > 4)
  722. dist[4] = cvtest::randReal(rng)*0.004 - 0.002;
  723. }
  724. else
  725. {
  726. dist[2] = dist[3] = 0;
  727. if (dist_size > 4)
  728. dist[4] = 0;
  729. }
  730. //Generating new camera matrix
  731. _new_cam = Scalar::all(0);
  732. new_cam[8] = 1;
  733. // If P == K
  734. //new_cam[0] = cam[0];
  735. //new_cam[4] = cam[4];
  736. //new_cam[2] = cam[2];
  737. //new_cam[5] = cam[5];
  738. // If P != K
  739. new_cam[0] = cam[0] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[0]; //10%
  740. new_cam[4] = cam[4] + (cvtest::randReal(rng) - (double)0.5)*0.2*cam[4]; //10%
  741. new_cam[2] = cam[2] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.width; //15%
  742. new_cam[5] = cam[5] + (cvtest::randReal(rng) - (double)0.5)*0.3*img_size.height; //15%
  743. //Generating R matrix
  744. Mat _rot(3,3,CV_64F);
  745. Mat rotation(1,3,CV_64F);
  746. rotation.at<double>(0) = CV_PI/8*(cvtest::randReal(rng) - (double)0.5); // phi
  747. rotation.at<double>(1) = CV_PI/8*(cvtest::randReal(rng) - (double)0.5); // ksi
  748. rotation.at<double>(2) = CV_PI/3*(cvtest::randReal(rng) - (double)0.5); //khi
  749. cvtest::Rodrigues(rotation, _rot);
  750. //cvSetIdentity(_rot);
  751. //copying data
  752. cvtest::convert( _camera, test_mat[INPUT][0], test_mat[INPUT][0].type());
  753. cvtest::convert( _distort, test_mat[INPUT][1], test_mat[INPUT][1].type());
  754. cvtest::convert( _rot, test_mat[INPUT][2], test_mat[INPUT][2].type());
  755. cvtest::convert( _new_cam, test_mat[INPUT][3], test_mat[INPUT][3].type());
  756. zero_distortion = (cvtest::randInt(rng)%2) == 0 ? false : true;
  757. zero_new_cam = (cvtest::randInt(rng)%2) == 0 ? false : true;
  758. zero_R = (cvtest::randInt(rng)%2) == 0 ? false : true;
  759. return code;
  760. }
  761. void CV_InitInverseRectificationMapTest::prepare_to_validation(int/* test_case_idx*/)
  762. {
  763. // Configure Parameters
  764. Mat _a0 = test_mat[INPUT][0];
  765. Mat _d0 = zero_distortion ? cv::Mat() : test_mat[INPUT][1];
  766. Mat _R0 = zero_R ? cv::Mat() : test_mat[INPUT][2];
  767. Mat _new_cam0 = zero_new_cam ? test_mat[INPUT][0] : test_mat[INPUT][3];
  768. Mat _mapx(img_size, CV_32F), _mapy(img_size, CV_32F);
  769. double a[9], d[5]={0., 0., 0., 0. , 0.}, R[9]={1., 0., 0., 0., 1., 0., 0., 0., 1.}, a1[9];
  770. Mat _a(3, 3, CV_64F, a), _a1(3, 3, CV_64F, a1);
  771. Mat _d(_d0.rows,_d0.cols, CV_MAKETYPE(CV_64F,_d0.channels()),d);
  772. Mat _R(3, 3, CV_64F, R);
  773. double fx, fy, cx, cy, ifx, ify, cxn, cyn;
  774. // Camera matrix
  775. CV_Assert(_a0.size() == Size(3, 3));
  776. _a0.convertTo(_a, CV_64F);
  777. if( !_new_cam0.empty() )
  778. {
  779. CV_Assert(_new_cam0.size() == Size(3, 3));
  780. _new_cam0.convertTo(_a1, CV_64F);
  781. }
  782. else
  783. {
  784. _a.copyTo(_a1);
  785. }
  786. // Distortion
  787. CV_Assert(_d0.empty() ||
  788. _d0.size() == Size(5, 1) ||
  789. _d0.size() == Size(1, 5) ||
  790. _d0.size() == Size(4, 1) ||
  791. _d0.size() == Size(1, 4));
  792. if( !_d0.empty() )
  793. _d0.convertTo(_d, CV_64F);
  794. // Rotation
  795. if( !_R0.empty() )
  796. {
  797. CV_Assert(_R0.size() == Size(3, 3));
  798. Mat tmp;
  799. _R0.convertTo(_R, CV_64F);
  800. }
  801. // Copy camera matrix
  802. fx = a[0]; fy = a[4]; cx = a[2]; cy = a[5];
  803. // Copy new camera matrix
  804. ifx = a1[0]; ify = a1[4]; cxn = a1[2]; cyn = a1[5];
  805. // Undistort
  806. for( int v = 0; v < img_size.height; v++ )
  807. {
  808. for( int u = 0; u < img_size.width; u++ )
  809. {
  810. // Convert from image to pin-hole coordinates
  811. double x = (u - cx)/fx;
  812. double y = (v - cy)/fy;
  813. // Undistort
  814. double x2 = x*x, y2 = y*y;
  815. double r2 = x2 + y2;
  816. double cdist = 1./(1. + (d[0] + (d[1] + d[4]*r2)*r2)*r2); // (1. + (d[5] + (d[6] + d[7]*r2)*r2)*r2) == 1 as d[5-7]=0;
  817. double x_ = (x - (d[2]*2.*x*y + d[3]*(r2 + 2.*x2)))*cdist;
  818. double y_ = (y - (d[3]*2.*x*y + d[2]*(r2 + 2.*y2)))*cdist;
  819. // Rectify
  820. double X = R[0]*x_ + R[1]*y_ + R[2];
  821. double Y = R[3]*x_ + R[4]*y_ + R[5];
  822. double Z = R[6]*x_ + R[7]*y_ + R[8];
  823. double x__ = X/Z;
  824. double y__ = Y/Z;
  825. // Convert from pin-hole to image coordinates
  826. _mapy.at<float>(v, u) = (float)(y__*ify + cyn);
  827. _mapx.at<float>(v, u) = (float)(x__*ifx + cxn);
  828. }
  829. }
  830. // Convert
  831. _mapx.convertTo(test_mat[REF_OUTPUT][0], test_mat[REF_OUTPUT][0].type());
  832. _mapy.convertTo(test_mat[REF_OUTPUT][1], test_mat[REF_OUTPUT][0].type());
  833. }
  834. void CV_InitInverseRectificationMapTest::run_func()
  835. {
  836. cv::Mat camera_mat = test_mat[INPUT][0];
  837. cv::Mat dist = zero_distortion ? cv::Mat() : test_mat[INPUT][1];
  838. cv::Mat R = zero_R ? cv::Mat() : test_mat[INPUT][2];
  839. cv::Mat new_cam = zero_new_cam ? cv::Mat() : test_mat[INPUT][3];
  840. cv::Mat& mapx = test_mat[OUTPUT][0], &mapy = test_mat[OUTPUT][1];
  841. cv::initInverseRectificationMap(camera_mat,dist,R,new_cam,img_size,map_type,mapx,mapy);
  842. }
  843. double CV_InitInverseRectificationMapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  844. {
  845. return 8;
  846. }
  847. //////////////////////////////////////////////////////////////////////////////////////////////////////
  848. TEST(Calib3d_DefaultNewCameraMatrix, accuracy) { CV_DefaultNewCameraMatrixTest test; test.safe_run(); }
  849. TEST(Calib3d_UndistortPoints, accuracy) { CV_UndistortPointsTest test; test.safe_run(); }
  850. TEST(Calib3d_InitUndistortRectifyMap, accuracy) { CV_InitUndistortRectifyMapTest test; test.safe_run(); }
  851. TEST(DISABLED_Calib3d_InitInverseRectificationMap, accuracy) { CV_InitInverseRectificationMapTest test; test.safe_run(); }
  852. ////////////////////////////// undistort /////////////////////////////////
  853. static void test_remap( const Mat& src, Mat& dst, const Mat& mapx, const Mat& mapy,
  854. Mat* mask=0, int interpolation=CV_INTER_LINEAR )
  855. {
  856. int x, y, k;
  857. int drows = dst.rows, dcols = dst.cols;
  858. int srows = src.rows, scols = src.cols;
  859. const uchar* sptr0 = src.ptr();
  860. int depth = src.depth(), cn = src.channels();
  861. int elem_size = (int)src.elemSize();
  862. int step = (int)(src.step / CV_ELEM_SIZE(depth));
  863. int delta;
  864. if( interpolation != CV_INTER_CUBIC )
  865. {
  866. delta = 0;
  867. scols -= 1; srows -= 1;
  868. }
  869. else
  870. {
  871. delta = 1;
  872. scols = MAX(scols - 3, 0);
  873. srows = MAX(srows - 3, 0);
  874. }
  875. int scols1 = MAX(scols - 2, 0);
  876. int srows1 = MAX(srows - 2, 0);
  877. if( mask )
  878. *mask = Scalar::all(0);
  879. for( y = 0; y < drows; y++ )
  880. {
  881. uchar* dptr = dst.ptr(y);
  882. const float* mx = mapx.ptr<float>(y);
  883. const float* my = mapy.ptr<float>(y);
  884. uchar* m = mask ? mask->ptr(y) : 0;
  885. for( x = 0; x < dcols; x++, dptr += elem_size )
  886. {
  887. float xs = mx[x];
  888. float ys = my[x];
  889. int ixs = cvFloor(xs);
  890. int iys = cvFloor(ys);
  891. if( (unsigned)(ixs - delta - 1) >= (unsigned)scols1 ||
  892. (unsigned)(iys - delta - 1) >= (unsigned)srows1 )
  893. {
  894. if( m )
  895. m[x] = 1;
  896. if( (unsigned)(ixs - delta) >= (unsigned)scols ||
  897. (unsigned)(iys - delta) >= (unsigned)srows )
  898. continue;
  899. }
  900. xs -= ixs;
  901. ys -= iys;
  902. switch( depth )
  903. {
  904. case CV_8U:
  905. {
  906. const uchar* sptr = sptr0 + iys*step + ixs*cn;
  907. for( k = 0; k < cn; k++ )
  908. {
  909. float v00 = sptr[k];
  910. float v01 = sptr[cn + k];
  911. float v10 = sptr[step + k];
  912. float v11 = sptr[step + cn + k];
  913. v00 = v00 + xs*(v01 - v00);
  914. v10 = v10 + xs*(v11 - v10);
  915. v00 = v00 + ys*(v10 - v00);
  916. dptr[k] = (uchar)cvRound(v00);
  917. }
  918. }
  919. break;
  920. case CV_16U:
  921. {
  922. const ushort* sptr = (const ushort*)sptr0 + iys*step + ixs*cn;
  923. for( k = 0; k < cn; k++ )
  924. {
  925. float v00 = sptr[k];
  926. float v01 = sptr[cn + k];
  927. float v10 = sptr[step + k];
  928. float v11 = sptr[step + cn + k];
  929. v00 = v00 + xs*(v01 - v00);
  930. v10 = v10 + xs*(v11 - v10);
  931. v00 = v00 + ys*(v10 - v00);
  932. ((ushort*)dptr)[k] = (ushort)cvRound(v00);
  933. }
  934. }
  935. break;
  936. case CV_32F:
  937. {
  938. const float* sptr = (const float*)sptr0 + iys*step + ixs*cn;
  939. for( k = 0; k < cn; k++ )
  940. {
  941. float v00 = sptr[k];
  942. float v01 = sptr[cn + k];
  943. float v10 = sptr[step + k];
  944. float v11 = sptr[step + cn + k];
  945. v00 = v00 + xs*(v01 - v00);
  946. v10 = v10 + xs*(v11 - v10);
  947. v00 = v00 + ys*(v10 - v00);
  948. ((float*)dptr)[k] = (float)v00;
  949. }
  950. }
  951. break;
  952. default:
  953. CV_Assert(0);
  954. }
  955. }
  956. }
  957. }
  958. class CV_ImgWarpBaseTest : public cvtest::ArrayTest
  959. {
  960. public:
  961. CV_ImgWarpBaseTest( bool warp_matrix );
  962. protected:
  963. int read_params( const cv::FileStorage& fs );
  964. int prepare_test_case( int test_case_idx );
  965. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  966. void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
  967. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  968. int interpolation;
  969. int max_interpolation;
  970. double spatial_scale_zoom, spatial_scale_decimate;
  971. };
  972. CV_ImgWarpBaseTest::CV_ImgWarpBaseTest( bool warp_matrix )
  973. {
  974. test_array[INPUT].push_back(NULL);
  975. if( warp_matrix )
  976. test_array[INPUT].push_back(NULL);
  977. test_array[INPUT_OUTPUT].push_back(NULL);
  978. test_array[REF_INPUT_OUTPUT].push_back(NULL);
  979. max_interpolation = 5;
  980. interpolation = 0;
  981. element_wise_relative_error = false;
  982. spatial_scale_zoom = 0.01;
  983. spatial_scale_decimate = 0.005;
  984. }
  985. int CV_ImgWarpBaseTest::read_params( const cv::FileStorage& fs )
  986. {
  987. int code = cvtest::ArrayTest::read_params( fs );
  988. return code;
  989. }
  990. void CV_ImgWarpBaseTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
  991. {
  992. cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
  993. if( CV_MAT_DEPTH(type) == CV_32F )
  994. {
  995. low = Scalar::all(-10.);
  996. high = Scalar::all(10);
  997. }
  998. }
  999. void CV_ImgWarpBaseTest::get_test_array_types_and_sizes( int test_case_idx,
  1000. vector<vector<Size> >& sizes, vector<vector<int> >& types )
  1001. {
  1002. RNG& rng = ts->get_rng();
  1003. int depth = cvtest::randInt(rng) % 3;
  1004. int cn = cvtest::randInt(rng) % 3 + 1;
  1005. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  1006. depth = depth == 0 ? CV_8U : depth == 1 ? CV_16U : CV_32F;
  1007. cn += cn == 2;
  1008. types[INPUT][0] = types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(depth, cn);
  1009. if( test_array[INPUT].size() > 1 )
  1010. types[INPUT][1] = cvtest::randInt(rng) & 1 ? CV_32FC1 : CV_64FC1;
  1011. interpolation = cvtest::randInt(rng) % max_interpolation;
  1012. }
  1013. void CV_ImgWarpBaseTest::fill_array( int test_case_idx, int i, int j, Mat& arr )
  1014. {
  1015. if( i != INPUT || j != 0 )
  1016. cvtest::ArrayTest::fill_array( test_case_idx, i, j, arr );
  1017. }
  1018. int CV_ImgWarpBaseTest::prepare_test_case( int test_case_idx )
  1019. {
  1020. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  1021. Mat& img = test_mat[INPUT][0];
  1022. int i, j, cols = img.cols;
  1023. int type = img.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
  1024. double scale = depth == CV_16U ? 1000. : 255.*0.5;
  1025. double space_scale = spatial_scale_decimate;
  1026. vector<float> buffer(img.cols*cn);
  1027. if( code <= 0 )
  1028. return code;
  1029. if( test_mat[INPUT_OUTPUT][0].cols >= img.cols &&
  1030. test_mat[INPUT_OUTPUT][0].rows >= img.rows )
  1031. space_scale = spatial_scale_zoom;
  1032. for( i = 0; i < img.rows; i++ )
  1033. {
  1034. uchar* ptr = img.ptr(i);
  1035. switch( cn )
  1036. {
  1037. case 1:
  1038. for( j = 0; j < cols; j++ )
  1039. buffer[j] = (float)((sin((i+1)*space_scale)*sin((j+1)*space_scale)+1.)*scale);
  1040. break;
  1041. case 2:
  1042. for( j = 0; j < cols; j++ )
  1043. {
  1044. buffer[j*2] = (float)((sin((i+1)*space_scale)+1.)*scale);
  1045. buffer[j*2+1] = (float)((sin((i+j)*space_scale)+1.)*scale);
  1046. }
  1047. break;
  1048. case 3:
  1049. for( j = 0; j < cols; j++ )
  1050. {
  1051. buffer[j*3] = (float)((sin((i+1)*space_scale)+1.)*scale);
  1052. buffer[j*3+1] = (float)((sin(j*space_scale)+1.)*scale);
  1053. buffer[j*3+2] = (float)((sin((i+j)*space_scale)+1.)*scale);
  1054. }
  1055. break;
  1056. case 4:
  1057. for( j = 0; j < cols; j++ )
  1058. {
  1059. buffer[j*4] = (float)((sin((i+1)*space_scale)+1.)*scale);
  1060. buffer[j*4+1] = (float)((sin(j*space_scale)+1.)*scale);
  1061. buffer[j*4+2] = (float)((sin((i+j)*space_scale)+1.)*scale);
  1062. buffer[j*4+3] = (float)((sin((i-j)*space_scale)+1.)*scale);
  1063. }
  1064. break;
  1065. default:
  1066. CV_Assert(0);
  1067. }
  1068. /*switch( depth )
  1069. {
  1070. case CV_8U:
  1071. for( j = 0; j < cols*cn; j++ )
  1072. ptr[j] = (uchar)cvRound(buffer[j]);
  1073. break;
  1074. case CV_16U:
  1075. for( j = 0; j < cols*cn; j++ )
  1076. ((ushort*)ptr)[j] = (ushort)cvRound(buffer[j]);
  1077. break;
  1078. case CV_32F:
  1079. for( j = 0; j < cols*cn; j++ )
  1080. ((float*)ptr)[j] = (float)buffer[j];
  1081. break;
  1082. default:
  1083. CV_Assert(0);
  1084. }*/
  1085. cv::Mat src(1, cols*cn, CV_32F, &buffer[0]);
  1086. cv::Mat dst(1, cols*cn, depth, ptr);
  1087. src.convertTo(dst, dst.type());
  1088. }
  1089. return code;
  1090. }
  1091. class CV_UndistortTest : public CV_ImgWarpBaseTest
  1092. {
  1093. public:
  1094. CV_UndistortTest();
  1095. protected:
  1096. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  1097. void run_func();
  1098. int prepare_test_case( int test_case_idx );
  1099. void prepare_to_validation( int /*test_case_idx*/ );
  1100. double get_success_error_level( int test_case_idx, int i, int j );
  1101. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  1102. private:
  1103. cv::Mat input0;
  1104. cv::Mat input1;
  1105. cv::Mat input2;
  1106. cv::Mat input_new_cam;
  1107. cv::Mat input_output;
  1108. bool zero_new_cam;
  1109. bool zero_distortion;
  1110. };
  1111. CV_UndistortTest::CV_UndistortTest() : CV_ImgWarpBaseTest( false )
  1112. {
  1113. //spatial_scale_zoom = spatial_scale_decimate;
  1114. test_array[INPUT].push_back(NULL);
  1115. test_array[INPUT].push_back(NULL);
  1116. test_array[INPUT].push_back(NULL);
  1117. spatial_scale_decimate = spatial_scale_zoom;
  1118. }
  1119. void CV_UndistortTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  1120. {
  1121. RNG& rng = ts->get_rng();
  1122. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  1123. int type = types[INPUT][0];
  1124. type = CV_MAKETYPE( CV_8U, CV_MAT_CN(type) );
  1125. types[INPUT][0] = types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = type;
  1126. types[INPUT][1] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  1127. types[INPUT][2] = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  1128. sizes[INPUT][1] = cvSize(3,3);
  1129. sizes[INPUT][2] = cvtest::randInt(rng)%2 ? cvSize(4,1) : cvSize(1,4);
  1130. types[INPUT][3] = types[INPUT][1];
  1131. sizes[INPUT][3] = sizes[INPUT][1];
  1132. interpolation = CV_INTER_LINEAR;
  1133. }
  1134. void CV_UndistortTest::fill_array( int test_case_idx, int i, int j, Mat& arr )
  1135. {
  1136. if( i != INPUT )
  1137. CV_ImgWarpBaseTest::fill_array( test_case_idx, i, j, arr );
  1138. }
  1139. void CV_UndistortTest::run_func()
  1140. {
  1141. if (zero_distortion)
  1142. {
  1143. cv::undistort(input0,input_output,input1,cv::Mat());
  1144. }
  1145. else
  1146. {
  1147. cv::undistort(input0,input_output,input1,input2);
  1148. }
  1149. }
  1150. double CV_UndistortTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  1151. {
  1152. int depth = test_mat[INPUT][0].depth();
  1153. return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
  1154. }
  1155. int CV_UndistortTest::prepare_test_case( int test_case_idx )
  1156. {
  1157. RNG& rng = ts->get_rng();
  1158. int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
  1159. const Mat& src = test_mat[INPUT][0];
  1160. double k[4], a[9] = {0,0,0,0,0,0,0,0,1};
  1161. double new_cam[9] = {0,0,0,0,0,0,0,0,1};
  1162. double sz = MAX(src.rows, src.cols);
  1163. Mat& _new_cam0 = test_mat[INPUT][3];
  1164. Mat _new_cam(test_mat[INPUT][3].rows,test_mat[INPUT][3].cols,CV_64F,new_cam);
  1165. Mat& _a0 = test_mat[INPUT][1];
  1166. Mat _a(3,3,CV_64F,a);
  1167. Mat& _k0 = test_mat[INPUT][2];
  1168. Mat _k(_k0.rows,_k0.cols, CV_MAKETYPE(CV_64F,_k0.channels()),k);
  1169. if( code <= 0 )
  1170. return code;
  1171. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  1172. a[2] = (src.cols - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  1173. a[5] = (src.rows - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  1174. a[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  1175. a[4] = aspect_ratio*a[0];
  1176. k[0] = cvtest::randReal(rng)*0.06 - 0.03;
  1177. k[1] = cvtest::randReal(rng)*0.06 - 0.03;
  1178. if( k[0]*k[1] > 0 )
  1179. k[1] = -k[1];
  1180. if( cvtest::randInt(rng)%4 != 0 )
  1181. {
  1182. k[2] = cvtest::randReal(rng)*0.004 - 0.002;
  1183. k[3] = cvtest::randReal(rng)*0.004 - 0.002;
  1184. }
  1185. else
  1186. k[2] = k[3] = 0;
  1187. new_cam[0] = a[0] + (cvtest::randReal(rng) - (double)0.5)*0.2*a[0]; //10%
  1188. new_cam[4] = a[4] + (cvtest::randReal(rng) - (double)0.5)*0.2*a[4]; //10%
  1189. new_cam[2] = a[2] + (cvtest::randReal(rng) - (double)0.5)*0.3*test_mat[INPUT][0].rows; //15%
  1190. new_cam[5] = a[5] + (cvtest::randReal(rng) - (double)0.5)*0.3*test_mat[INPUT][0].cols; //15%
  1191. _a.convertTo(_a0, _a0.depth());
  1192. zero_distortion = (cvtest::randInt(rng)%2) == 0 ? false : true;
  1193. _k.convertTo(_k0, _k0.depth());
  1194. zero_new_cam = (cvtest::randInt(rng)%2) == 0 ? false : true;
  1195. _new_cam.convertTo(_new_cam0, _new_cam0.depth());
  1196. //Testing C++ code
  1197. //useCPlus = ((cvtest::randInt(rng) % 2)!=0);
  1198. input0 = test_mat[INPUT][0];
  1199. input1 = test_mat[INPUT][1];
  1200. input2 = test_mat[INPUT][2];
  1201. input_new_cam = test_mat[INPUT][3];
  1202. return code;
  1203. }
  1204. void CV_UndistortTest::prepare_to_validation( int /*test_case_idx*/ )
  1205. {
  1206. Mat& output = test_mat[INPUT_OUTPUT][0];
  1207. input_output.convertTo(output, output.type());
  1208. Mat& src = test_mat[INPUT][0];
  1209. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  1210. Mat& dst0 = test_mat[INPUT_OUTPUT][0];
  1211. Mat mapx, mapy;
  1212. cvtest::initUndistortMap( test_mat[INPUT][1], test_mat[INPUT][2],
  1213. Mat(), Mat(), dst.size(), mapx, mapy, CV_32F );
  1214. Mat mask( dst.size(), CV_8U );
  1215. test_remap( src, dst, mapx, mapy, &mask, interpolation );
  1216. dst.setTo(Scalar::all(0), mask);
  1217. dst0.setTo(Scalar::all(0), mask);
  1218. }
  1219. class CV_UndistortMapTest : public cvtest::ArrayTest
  1220. {
  1221. public:
  1222. CV_UndistortMapTest();
  1223. protected:
  1224. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  1225. void run_func();
  1226. int prepare_test_case( int test_case_idx );
  1227. void prepare_to_validation( int /*test_case_idx*/ );
  1228. double get_success_error_level( int test_case_idx, int i, int j );
  1229. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  1230. private:
  1231. bool dualChannel;
  1232. };
  1233. CV_UndistortMapTest::CV_UndistortMapTest()
  1234. {
  1235. test_array[INPUT].push_back(NULL);
  1236. test_array[INPUT].push_back(NULL);
  1237. test_array[OUTPUT].push_back(NULL);
  1238. test_array[OUTPUT].push_back(NULL);
  1239. test_array[REF_OUTPUT].push_back(NULL);
  1240. test_array[REF_OUTPUT].push_back(NULL);
  1241. element_wise_relative_error = false;
  1242. }
  1243. void CV_UndistortMapTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  1244. {
  1245. RNG& rng = ts->get_rng();
  1246. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  1247. int depth = cvtest::randInt(rng)%2 ? CV_64F : CV_32F;
  1248. Size sz = sizes[OUTPUT][0];
  1249. types[INPUT][0] = types[INPUT][1] = depth;
  1250. dualChannel = cvtest::randInt(rng)%2 == 0;
  1251. types[OUTPUT][0] = types[OUTPUT][1] =
  1252. types[REF_OUTPUT][0] = types[REF_OUTPUT][1] = dualChannel ? CV_32FC2 : CV_32F;
  1253. sizes[INPUT][0] = cvSize(3,3);
  1254. sizes[INPUT][1] = cvtest::randInt(rng)%2 ? cvSize(4,1) : cvSize(1,4);
  1255. sz.width = MAX(sz.width,16);
  1256. sz.height = MAX(sz.height,16);
  1257. sizes[OUTPUT][0] = sizes[OUTPUT][1] =
  1258. sizes[REF_OUTPUT][0] = sizes[REF_OUTPUT][1] = sz;
  1259. }
  1260. void CV_UndistortMapTest::fill_array( int test_case_idx, int i, int j, Mat& arr )
  1261. {
  1262. if( i != INPUT )
  1263. cvtest::ArrayTest::fill_array( test_case_idx, i, j, arr );
  1264. }
  1265. void CV_UndistortMapTest::run_func()
  1266. {
  1267. cv::Mat a = test_mat[INPUT][0], k = test_mat[INPUT][1];
  1268. cv::Mat &mapx = test_mat[OUTPUT][0], &mapy = !dualChannel ? test_mat[OUTPUT][1] : mapx;
  1269. cv::Size mapsz = test_mat[OUTPUT][0].size();
  1270. cv::initUndistortRectifyMap(a, k, cv::Mat(), a,
  1271. mapsz, dualChannel ? CV_32FC2 : CV_32FC1,
  1272. mapx, !dualChannel ? cv::_InputOutputArray(mapy) : cv::noArray());
  1273. }
  1274. double CV_UndistortMapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  1275. {
  1276. return 1e-3;
  1277. }
  1278. int CV_UndistortMapTest::prepare_test_case( int test_case_idx )
  1279. {
  1280. RNG& rng = ts->get_rng();
  1281. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  1282. const Mat& mapx = test_mat[OUTPUT][0];
  1283. double k[4], a[9] = {0,0,0,0,0,0,0,0,1};
  1284. double sz = MAX(mapx.rows, mapx.cols);
  1285. Mat& _a0 = test_mat[INPUT][0], &_k0 = test_mat[INPUT][1];
  1286. Mat _a(3,3,CV_64F,a);
  1287. Mat _k(_k0.rows,_k0.cols, CV_MAKETYPE(CV_64F,_k0.channels()),k);
  1288. if( code <= 0 )
  1289. return code;
  1290. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  1291. a[2] = (mapx.cols - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  1292. a[5] = (mapx.rows - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  1293. a[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  1294. a[4] = aspect_ratio*a[0];
  1295. k[0] = cvtest::randReal(rng)*0.06 - 0.03;
  1296. k[1] = cvtest::randReal(rng)*0.06 - 0.03;
  1297. if( k[0]*k[1] > 0 )
  1298. k[1] = -k[1];
  1299. k[2] = cvtest::randReal(rng)*0.004 - 0.002;
  1300. k[3] = cvtest::randReal(rng)*0.004 - 0.002;
  1301. _a.convertTo(_a0, _a0.depth());
  1302. _k.convertTo(_k0, _k0.depth());
  1303. if (dualChannel)
  1304. {
  1305. test_mat[REF_OUTPUT][1] = Scalar::all(0);
  1306. test_mat[OUTPUT][1] = Scalar::all(0);
  1307. }
  1308. return code;
  1309. }
  1310. void CV_UndistortMapTest::prepare_to_validation( int )
  1311. {
  1312. Mat mapx, mapy;
  1313. cvtest::initUndistortMap( test_mat[INPUT][0], test_mat[INPUT][1], Mat(), Mat(),
  1314. test_mat[REF_OUTPUT][0].size(), mapx, mapy, CV_32F );
  1315. if( !dualChannel )
  1316. {
  1317. mapx.copyTo(test_mat[REF_OUTPUT][0]);
  1318. mapy.copyTo(test_mat[REF_OUTPUT][1]);
  1319. }
  1320. else
  1321. {
  1322. Mat p[2] = {mapx, mapy};
  1323. cv::merge(p, 2, test_mat[REF_OUTPUT][0]);
  1324. }
  1325. }
  1326. TEST(Calib3d_UndistortImgproc, accuracy) { CV_UndistortTest test; test.safe_run(); }
  1327. TEST(Calib3d_InitUndistortMap, accuracy) { CV_UndistortMapTest test; test.safe_run(); }
  1328. TEST(Calib3d_UndistortPoints, inputShape)
  1329. {
  1330. //https://github.com/opencv/opencv/issues/14423
  1331. Matx33d cameraMatrix = Matx33d::eye();
  1332. {
  1333. //2xN 1-channel
  1334. Mat imagePoints(2, 3, CV_32FC1);
  1335. imagePoints.at<float>(0,0) = 320; imagePoints.at<float>(1,0) = 240;
  1336. imagePoints.at<float>(0,1) = 0; imagePoints.at<float>(1,1) = 240;
  1337. imagePoints.at<float>(0,2) = 320; imagePoints.at<float>(1,2) = 0;
  1338. vector<Point2f> normalized;
  1339. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1340. EXPECT_EQ(static_cast<int>(normalized.size()), imagePoints.cols);
  1341. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1342. EXPECT_NEAR(normalized[i].x, imagePoints.at<float>(0,i), std::numeric_limits<float>::epsilon());
  1343. EXPECT_NEAR(normalized[i].y, imagePoints.at<float>(1,i), std::numeric_limits<float>::epsilon());
  1344. }
  1345. }
  1346. {
  1347. //Nx2 1-channel
  1348. Mat imagePoints(3, 2, CV_32FC1);
  1349. imagePoints.at<float>(0,0) = 320; imagePoints.at<float>(0,1) = 240;
  1350. imagePoints.at<float>(1,0) = 0; imagePoints.at<float>(1,1) = 240;
  1351. imagePoints.at<float>(2,0) = 320; imagePoints.at<float>(2,1) = 0;
  1352. vector<Point2f> normalized;
  1353. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1354. EXPECT_EQ(static_cast<int>(normalized.size()), imagePoints.rows);
  1355. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1356. EXPECT_NEAR(normalized[i].x, imagePoints.at<float>(i,0), std::numeric_limits<float>::epsilon());
  1357. EXPECT_NEAR(normalized[i].y, imagePoints.at<float>(i,1), std::numeric_limits<float>::epsilon());
  1358. }
  1359. }
  1360. {
  1361. //1xN 2-channel
  1362. Mat imagePoints(1, 3, CV_32FC2);
  1363. imagePoints.at<Vec2f>(0,0) = Vec2f(320, 240);
  1364. imagePoints.at<Vec2f>(0,1) = Vec2f(0, 240);
  1365. imagePoints.at<Vec2f>(0,2) = Vec2f(320, 0);
  1366. vector<Point2f> normalized;
  1367. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1368. EXPECT_EQ(static_cast<int>(normalized.size()), imagePoints.cols);
  1369. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1370. EXPECT_NEAR(normalized[i].x, imagePoints.at<Vec2f>(0,i)(0), std::numeric_limits<float>::epsilon());
  1371. EXPECT_NEAR(normalized[i].y, imagePoints.at<Vec2f>(0,i)(1), std::numeric_limits<float>::epsilon());
  1372. }
  1373. }
  1374. {
  1375. //Nx1 2-channel
  1376. Mat imagePoints(3, 1, CV_32FC2);
  1377. imagePoints.at<Vec2f>(0,0) = Vec2f(320, 240);
  1378. imagePoints.at<Vec2f>(1,0) = Vec2f(0, 240);
  1379. imagePoints.at<Vec2f>(2,0) = Vec2f(320, 0);
  1380. vector<Point2f> normalized;
  1381. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1382. EXPECT_EQ(static_cast<int>(normalized.size()), imagePoints.rows);
  1383. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1384. EXPECT_NEAR(normalized[i].x, imagePoints.at<Vec2f>(i,0)(0), std::numeric_limits<float>::epsilon());
  1385. EXPECT_NEAR(normalized[i].y, imagePoints.at<Vec2f>(i,0)(1), std::numeric_limits<float>::epsilon());
  1386. }
  1387. }
  1388. {
  1389. //vector<Point2f>
  1390. vector<Point2f> imagePoints;
  1391. imagePoints.push_back(Point2f(320, 240));
  1392. imagePoints.push_back(Point2f(0, 240));
  1393. imagePoints.push_back(Point2f(320, 0));
  1394. vector<Point2f> normalized;
  1395. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1396. EXPECT_EQ(normalized.size(), imagePoints.size());
  1397. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1398. EXPECT_NEAR(normalized[i].x, imagePoints[i].x, std::numeric_limits<float>::epsilon());
  1399. EXPECT_NEAR(normalized[i].y, imagePoints[i].y, std::numeric_limits<float>::epsilon());
  1400. }
  1401. }
  1402. {
  1403. //vector<Point2d>
  1404. vector<Point2d> imagePoints;
  1405. imagePoints.push_back(Point2d(320, 240));
  1406. imagePoints.push_back(Point2d(0, 240));
  1407. imagePoints.push_back(Point2d(320, 0));
  1408. vector<Point2d> normalized;
  1409. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1410. EXPECT_EQ(normalized.size(), imagePoints.size());
  1411. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1412. EXPECT_NEAR(normalized[i].x, imagePoints[i].x, std::numeric_limits<double>::epsilon());
  1413. EXPECT_NEAR(normalized[i].y, imagePoints[i].y, std::numeric_limits<double>::epsilon());
  1414. }
  1415. }
  1416. }
  1417. TEST(Calib3d_UndistortPoints, outputShape)
  1418. {
  1419. Matx33d cameraMatrix = Matx33d::eye();
  1420. {
  1421. vector<Point2f> imagePoints;
  1422. imagePoints.push_back(Point2f(320, 240));
  1423. imagePoints.push_back(Point2f(0, 240));
  1424. imagePoints.push_back(Point2f(320, 0));
  1425. //Mat --> will be Nx1 2-channel
  1426. Mat normalized;
  1427. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1428. EXPECT_EQ(static_cast<int>(imagePoints.size()), normalized.rows);
  1429. for (int i = 0; i < normalized.rows; i++) {
  1430. EXPECT_NEAR(normalized.at<Vec2f>(i,0)(0), imagePoints[i].x, std::numeric_limits<float>::epsilon());
  1431. EXPECT_NEAR(normalized.at<Vec2f>(i,0)(1), imagePoints[i].y, std::numeric_limits<float>::epsilon());
  1432. }
  1433. }
  1434. {
  1435. vector<Point2f> imagePoints;
  1436. imagePoints.push_back(Point2f(320, 240));
  1437. imagePoints.push_back(Point2f(0, 240));
  1438. imagePoints.push_back(Point2f(320, 0));
  1439. //Nx1 2-channel
  1440. Mat normalized(static_cast<int>(imagePoints.size()), 1, CV_32FC2);
  1441. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1442. EXPECT_EQ(static_cast<int>(imagePoints.size()), normalized.rows);
  1443. for (int i = 0; i < normalized.rows; i++) {
  1444. EXPECT_NEAR(normalized.at<Vec2f>(i,0)(0), imagePoints[i].x, std::numeric_limits<float>::epsilon());
  1445. EXPECT_NEAR(normalized.at<Vec2f>(i,0)(1), imagePoints[i].y, std::numeric_limits<float>::epsilon());
  1446. }
  1447. }
  1448. {
  1449. vector<Point2f> imagePoints;
  1450. imagePoints.push_back(Point2f(320, 240));
  1451. imagePoints.push_back(Point2f(0, 240));
  1452. imagePoints.push_back(Point2f(320, 0));
  1453. //1xN 2-channel
  1454. Mat normalized(1, static_cast<int>(imagePoints.size()), CV_32FC2);
  1455. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1456. EXPECT_EQ(static_cast<int>(imagePoints.size()), normalized.cols);
  1457. for (int i = 0; i < normalized.rows; i++) {
  1458. EXPECT_NEAR(normalized.at<Vec2f>(0,i)(0), imagePoints[i].x, std::numeric_limits<float>::epsilon());
  1459. EXPECT_NEAR(normalized.at<Vec2f>(0,i)(1), imagePoints[i].y, std::numeric_limits<float>::epsilon());
  1460. }
  1461. }
  1462. {
  1463. vector<Point2f> imagePoints;
  1464. imagePoints.push_back(Point2f(320, 240));
  1465. imagePoints.push_back(Point2f(0, 240));
  1466. imagePoints.push_back(Point2f(320, 0));
  1467. //vector<Point2f>
  1468. vector<Point2f> normalized;
  1469. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1470. EXPECT_EQ(imagePoints.size(), normalized.size());
  1471. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1472. EXPECT_NEAR(normalized[i].x, imagePoints[i].x, std::numeric_limits<float>::epsilon());
  1473. EXPECT_NEAR(normalized[i].y, imagePoints[i].y, std::numeric_limits<float>::epsilon());
  1474. }
  1475. }
  1476. {
  1477. vector<Point2d> imagePoints;
  1478. imagePoints.push_back(Point2d(320, 240));
  1479. imagePoints.push_back(Point2d(0, 240));
  1480. imagePoints.push_back(Point2d(320, 0));
  1481. //vector<Point2d>
  1482. vector<Point2d> normalized;
  1483. undistortPoints(imagePoints, normalized, cameraMatrix, noArray());
  1484. EXPECT_EQ(imagePoints.size(), normalized.size());
  1485. for (int i = 0; i < static_cast<int>(normalized.size()); i++) {
  1486. EXPECT_NEAR(normalized[i].x, imagePoints[i].x, std::numeric_limits<double>::epsilon());
  1487. EXPECT_NEAR(normalized[i].y, imagePoints[i].y, std::numeric_limits<double>::epsilon());
  1488. }
  1489. }
  1490. }
  1491. TEST(Imgproc_undistort, regression_15286)
  1492. {
  1493. double kmat_data[9] = { 3217, 0, 1592, 0, 3217, 1201, 0, 0, 1 };
  1494. Mat kmat(3, 3, CV_64F, kmat_data);
  1495. double dist_coeff_data[5] = { 0.04, -0.4, -0.01, 0.04, 0.7 };
  1496. Mat dist_coeffs(5, 1, CV_64F, dist_coeff_data);
  1497. Mat img = Mat::zeros(512, 512, CV_8UC1);
  1498. img.at<uchar>(128, 128) = 255;
  1499. img.at<uchar>(128, 384) = 255;
  1500. img.at<uchar>(384, 384) = 255;
  1501. img.at<uchar>(384, 128) = 255;
  1502. Mat ref = Mat::zeros(512, 512, CV_8UC1);
  1503. ref.at<uchar>(Point(24, 98)) = 78;
  1504. ref.at<uchar>(Point(24, 99)) = 114;
  1505. ref.at<uchar>(Point(25, 98)) = 36;
  1506. ref.at<uchar>(Point(25, 99)) = 60;
  1507. ref.at<uchar>(Point(27, 361)) = 6;
  1508. ref.at<uchar>(Point(28, 361)) = 188;
  1509. ref.at<uchar>(Point(28, 362)) = 49;
  1510. ref.at<uchar>(Point(29, 361)) = 44;
  1511. ref.at<uchar>(Point(29, 362)) = 16;
  1512. ref.at<uchar>(Point(317, 366)) = 134;
  1513. ref.at<uchar>(Point(317, 367)) = 78;
  1514. ref.at<uchar>(Point(318, 366)) = 40;
  1515. ref.at<uchar>(Point(318, 367)) = 29;
  1516. ref.at<uchar>(Point(310, 104)) = 106;
  1517. ref.at<uchar>(Point(310, 105)) = 30;
  1518. ref.at<uchar>(Point(311, 104)) = 112;
  1519. ref.at<uchar>(Point(311, 105)) = 38;
  1520. Mat img_undist;
  1521. undistort(img, img_undist, kmat, dist_coeffs);
  1522. ASSERT_EQ(0.0, cvtest::norm(img_undist, ref, cv::NORM_INF));
  1523. }
  1524. TEST(Calib3d_initUndistortRectifyMap, regression_14467)
  1525. {
  1526. Size size_w_h(512 + 3, 512);
  1527. Matx33f k(
  1528. 6200, 0, size_w_h.width / 2.0f,
  1529. 0, 6200, size_w_h.height / 2.0f,
  1530. 0, 0, 1
  1531. );
  1532. Mat mesh_uv(size_w_h, CV_32FC2);
  1533. for (int i = 0; i < size_w_h.height; i++)
  1534. {
  1535. for (int j = 0; j < size_w_h.width; j++)
  1536. {
  1537. mesh_uv.at<Vec2f>(i, j) = Vec2f((float)j, (float)i);
  1538. }
  1539. }
  1540. Matx<double, 1, 14> d(
  1541. 0, 0, 0, 0, 0,
  1542. 0, 0, 0, 0, 0, 0, 0,
  1543. 0.09, 0.0
  1544. );
  1545. Mat mapxy, dst;
  1546. initUndistortRectifyMap(k, d, noArray(), k, size_w_h, CV_32FC2, mapxy, noArray());
  1547. undistortPoints(mapxy.reshape(2, (int)mapxy.total()), dst, k, d, noArray(), k);
  1548. dst = dst.reshape(2, mapxy.rows);
  1549. EXPECT_LE(cvtest::norm(dst, mesh_uv, NORM_INF), 1e-3);
  1550. }
  1551. TEST(Calib3d_initInverseRectificationMap, regression_20165)
  1552. {
  1553. Size size_w_h(1280, 800);
  1554. Mat dst(size_w_h, CV_32FC2); // Reference for validation
  1555. Mat mapxy; // Output of initInverseRectificationMap()
  1556. // Camera Matrix
  1557. double k[9]={
  1558. 1.5393951443032472e+03, 0., 6.7491727003047140e+02,
  1559. 0., 1.5400748240626747e+03, 5.1226968329123963e+02,
  1560. 0., 0., 1.
  1561. };
  1562. Mat _K(3, 3, CV_64F, k);
  1563. // Distortion
  1564. // double d[5]={0,0,0,0,0}; // Zero Distortion
  1565. double d[5]={ // Non-zero distortion
  1566. -3.4134571357400023e-03, 2.9733267766101856e-03, // K1, K2
  1567. 3.6653586399031184e-03, -3.1960714017365702e-03, // P1, P2
  1568. 0. // K3
  1569. };
  1570. Mat _d(1, 5, CV_64F, d);
  1571. // Rotation
  1572. //double R[9]={1., 0., 0., 0., 1., 0., 0., 0., 1.}; // Identity transform (none)
  1573. double R[9]={ // Random transform
  1574. 9.6625486010428052e-01, 1.6055789378989216e-02, 2.5708706103628531e-01,
  1575. -8.0300261706161002e-03, 9.9944797497929860e-01, -3.2237617614807819e-02,
  1576. -2.5746274294459848e-01, 2.9085338870243265e-02, 9.6585039165403186e-01
  1577. };
  1578. Mat _R(3, 3, CV_64F, R);
  1579. // --- Validation --- //
  1580. initInverseRectificationMap(_K, _d, _R, _K, size_w_h, CV_32FC2, mapxy, noArray());
  1581. // Copy camera matrix
  1582. double fx, fy, cx, cy, ifx, ify, cxn, cyn;
  1583. fx = k[0]; fy = k[4]; cx = k[2]; cy = k[5];
  1584. // Copy new camera matrix
  1585. ifx = k[0]; ify = k[4]; cxn = k[2]; cyn = k[5];
  1586. // Distort Points
  1587. for( int v = 0; v < size_w_h.height; v++ )
  1588. {
  1589. for( int u = 0; u < size_w_h.width; u++ )
  1590. {
  1591. // Convert from image to pin-hole coordinates
  1592. double x = (u - cx)/fx;
  1593. double y = (v - cy)/fy;
  1594. // Undistort
  1595. double x2 = x*x, y2 = y*y;
  1596. double r2 = x2 + y2;
  1597. double cdist = 1./(1. + (d[0] + (d[1] + d[4]*r2)*r2)*r2); // (1. + (d[5] + (d[6] + d[7]*r2)*r2)*r2) == 1 as d[5-7]=0;
  1598. double x_ = (x - (d[2]*2.*x*y + d[3]*(r2 + 2.*x2)))*cdist;
  1599. double y_ = (y - (d[3]*2.*x*y + d[2]*(r2 + 2.*y2)))*cdist;
  1600. // Rectify
  1601. double X = R[0]*x_ + R[1]*y_ + R[2];
  1602. double Y = R[3]*x_ + R[4]*y_ + R[5];
  1603. double Z = R[6]*x_ + R[7]*y_ + R[8];
  1604. double x__ = X/Z;
  1605. double y__ = Y/Z;
  1606. // Convert from pin-hole to image coordinates
  1607. dst.at<Vec2f>(v, u) = Vec2f((float)(x__*ifx + cxn), (float)(y__*ify + cyn));
  1608. }
  1609. }
  1610. // Check Result
  1611. EXPECT_LE(cvtest::norm(dst, mapxy, NORM_INF), 2e-1);
  1612. }
  1613. }} // namespace