test_undistort.cpp 68 KB

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