test_imgwarp.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. class CV_ImgWarpBaseTest : public cvtest::ArrayTest
  44. {
  45. public:
  46. CV_ImgWarpBaseTest( bool warp_matrix );
  47. protected:
  48. int read_params( const cv::FileStorage& fs );
  49. int prepare_test_case( int test_case_idx );
  50. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  51. void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
  52. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  53. int interpolation;
  54. int max_interpolation;
  55. double spatial_scale_zoom, spatial_scale_decimate;
  56. };
  57. CV_ImgWarpBaseTest::CV_ImgWarpBaseTest( bool warp_matrix )
  58. {
  59. test_array[INPUT].push_back(NULL);
  60. if( warp_matrix )
  61. test_array[INPUT].push_back(NULL);
  62. test_array[INPUT_OUTPUT].push_back(NULL);
  63. test_array[REF_INPUT_OUTPUT].push_back(NULL);
  64. max_interpolation = 5;
  65. interpolation = 0;
  66. element_wise_relative_error = false;
  67. spatial_scale_zoom = 0.01;
  68. spatial_scale_decimate = 0.005;
  69. }
  70. int CV_ImgWarpBaseTest::read_params( const cv::FileStorage& fs )
  71. {
  72. int code = cvtest::ArrayTest::read_params( fs );
  73. return code;
  74. }
  75. void CV_ImgWarpBaseTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
  76. {
  77. cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
  78. if( CV_MAT_DEPTH(type) == CV_32F )
  79. {
  80. low = Scalar::all(-10.);
  81. high = Scalar::all(10);
  82. }
  83. }
  84. void CV_ImgWarpBaseTest::get_test_array_types_and_sizes( int test_case_idx,
  85. vector<vector<Size> >& sizes, vector<vector<int> >& types )
  86. {
  87. RNG& rng = ts->get_rng();
  88. int depth = cvtest::randInt(rng) % 3;
  89. int cn = cvtest::randInt(rng) % 3 + 1;
  90. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  91. depth = depth == 0 ? CV_8U : depth == 1 ? CV_16U : CV_32F;
  92. cn += cn == 2;
  93. types[INPUT][0] = types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(depth, cn);
  94. if( test_array[INPUT].size() > 1 )
  95. types[INPUT][1] = cvtest::randInt(rng) & 1 ? CV_32FC1 : CV_64FC1;
  96. interpolation = cvtest::randInt(rng) % max_interpolation;
  97. }
  98. void CV_ImgWarpBaseTest::fill_array( int test_case_idx, int i, int j, Mat& arr )
  99. {
  100. if( i != INPUT || j != 0 )
  101. cvtest::ArrayTest::fill_array( test_case_idx, i, j, arr );
  102. }
  103. int CV_ImgWarpBaseTest::prepare_test_case( int test_case_idx )
  104. {
  105. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  106. Mat& img = test_mat[INPUT][0];
  107. int i, j, cols = img.cols;
  108. int type = img.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
  109. double scale = depth == CV_16U ? 1000. : 255.*0.5;
  110. double space_scale = spatial_scale_decimate;
  111. vector<float> buffer(img.cols*cn);
  112. if( code <= 0 )
  113. return code;
  114. if( test_mat[INPUT_OUTPUT][0].cols >= img.cols &&
  115. test_mat[INPUT_OUTPUT][0].rows >= img.rows )
  116. space_scale = spatial_scale_zoom;
  117. for( i = 0; i < img.rows; i++ )
  118. {
  119. uchar* ptr = img.ptr(i);
  120. switch( cn )
  121. {
  122. case 1:
  123. for( j = 0; j < cols; j++ )
  124. buffer[j] = (float)((sin((i+1)*space_scale)*sin((j+1)*space_scale)+1.)*scale);
  125. break;
  126. case 2:
  127. for( j = 0; j < cols; j++ )
  128. {
  129. buffer[j*2] = (float)((sin((i+1)*space_scale)+1.)*scale);
  130. buffer[j*2+1] = (float)((sin((i+j)*space_scale)+1.)*scale);
  131. }
  132. break;
  133. case 3:
  134. for( j = 0; j < cols; j++ )
  135. {
  136. buffer[j*3] = (float)((sin((i+1)*space_scale)+1.)*scale);
  137. buffer[j*3+1] = (float)((sin(j*space_scale)+1.)*scale);
  138. buffer[j*3+2] = (float)((sin((i+j)*space_scale)+1.)*scale);
  139. }
  140. break;
  141. case 4:
  142. for( j = 0; j < cols; j++ )
  143. {
  144. buffer[j*4] = (float)((sin((i+1)*space_scale)+1.)*scale);
  145. buffer[j*4+1] = (float)((sin(j*space_scale)+1.)*scale);
  146. buffer[j*4+2] = (float)((sin((i+j)*space_scale)+1.)*scale);
  147. buffer[j*4+3] = (float)((sin((i-j)*space_scale)+1.)*scale);
  148. }
  149. break;
  150. default:
  151. CV_Assert(0);
  152. }
  153. /*switch( depth )
  154. {
  155. case CV_8U:
  156. for( j = 0; j < cols*cn; j++ )
  157. ptr[j] = (uchar)cvRound(buffer[j]);
  158. break;
  159. case CV_16U:
  160. for( j = 0; j < cols*cn; j++ )
  161. ((ushort*)ptr)[j] = (ushort)cvRound(buffer[j]);
  162. break;
  163. case CV_32F:
  164. for( j = 0; j < cols*cn; j++ )
  165. ((float*)ptr)[j] = (float)buffer[j];
  166. break;
  167. default:
  168. CV_Assert(0);
  169. }*/
  170. cv::Mat src(1, cols*cn, CV_32F, &buffer[0]);
  171. cv::Mat dst(1, cols*cn, depth, ptr);
  172. src.convertTo(dst, dst.type());
  173. }
  174. return code;
  175. }
  176. /////////////////////////
  177. class CV_ResizeTest : public CV_ImgWarpBaseTest
  178. {
  179. public:
  180. CV_ResizeTest();
  181. protected:
  182. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  183. void run_func();
  184. void prepare_to_validation( int /*test_case_idx*/ );
  185. double get_success_error_level( int test_case_idx, int i, int j );
  186. };
  187. CV_ResizeTest::CV_ResizeTest() : CV_ImgWarpBaseTest( false )
  188. {
  189. }
  190. void CV_ResizeTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  191. {
  192. RNG& rng = ts->get_rng();
  193. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  194. Size sz;
  195. sz.width = (cvtest::randInt(rng) % sizes[INPUT][0].width) + 1;
  196. sz.height = (cvtest::randInt(rng) % sizes[INPUT][0].height) + 1;
  197. if( cvtest::randInt(rng) & 1 )
  198. {
  199. int xfactor = cvtest::randInt(rng) % 10 + 1;
  200. int yfactor = cvtest::randInt(rng) % 10 + 1;
  201. if( cvtest::randInt(rng) & 1 )
  202. yfactor = xfactor;
  203. sz.width = sizes[INPUT][0].width / xfactor;
  204. sz.width = MAX(sz.width,1);
  205. sz.height = sizes[INPUT][0].height / yfactor;
  206. sz.height = MAX(sz.height,1);
  207. sizes[INPUT][0].width = sz.width * xfactor;
  208. sizes[INPUT][0].height = sz.height * yfactor;
  209. }
  210. if( cvtest::randInt(rng) & 1 )
  211. sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = sz;
  212. else
  213. {
  214. sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = sizes[INPUT][0];
  215. sizes[INPUT][0] = sz;
  216. }
  217. if( interpolation == 4 &&
  218. (MIN(sizes[INPUT][0].width,sizes[INPUT_OUTPUT][0].width) < 4 ||
  219. MIN(sizes[INPUT][0].height,sizes[INPUT_OUTPUT][0].height) < 4))
  220. interpolation = 2;
  221. }
  222. void CV_ResizeTest::run_func()
  223. {
  224. cvResize( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], interpolation );
  225. }
  226. double CV_ResizeTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  227. {
  228. int depth = test_mat[INPUT][0].depth();
  229. return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 1e-1;
  230. }
  231. void CV_ResizeTest::prepare_to_validation( int /*test_case_idx*/ )
  232. {
  233. CvMat _src = cvMat(test_mat[INPUT][0]), _dst = cvMat(test_mat[REF_INPUT_OUTPUT][0]);
  234. CvMat *src = &_src, *dst = &_dst;
  235. int i, j, k;
  236. CvMat* x_idx = cvCreateMat( 1, dst->cols, CV_32SC1 );
  237. CvMat* y_idx = cvCreateMat( 1, dst->rows, CV_32SC1 );
  238. int* x_tab = x_idx->data.i;
  239. int elem_size = CV_ELEM_SIZE(src->type);
  240. int drows = dst->rows, dcols = dst->cols;
  241. if( interpolation == CV_INTER_NN )
  242. {
  243. for( j = 0; j < dcols; j++ )
  244. {
  245. int t = (j*src->cols*2 + MIN(src->cols,dcols) - 1)/(dcols*2);
  246. t -= t >= src->cols;
  247. x_idx->data.i[j] = t*elem_size;
  248. }
  249. for( j = 0; j < drows; j++ )
  250. {
  251. int t = (j*src->rows*2 + MIN(src->rows,drows) - 1)/(drows*2);
  252. t -= t >= src->rows;
  253. y_idx->data.i[j] = t;
  254. }
  255. }
  256. else
  257. {
  258. double scale_x = (double)src->cols/dcols;
  259. double scale_y = (double)src->rows/drows;
  260. for( j = 0; j < dcols; j++ )
  261. {
  262. double f = ((j+0.5)*scale_x - 0.5);
  263. i = cvRound(f);
  264. x_idx->data.i[j] = (i < 0 ? 0 : i >= src->cols ? src->cols - 1 : i)*elem_size;
  265. }
  266. for( j = 0; j < drows; j++ )
  267. {
  268. double f = ((j+0.5)*scale_y - 0.5);
  269. i = cvRound(f);
  270. y_idx->data.i[j] = i < 0 ? 0 : i >= src->rows ? src->rows - 1 : i;
  271. }
  272. }
  273. for( i = 0; i < drows; i++ )
  274. {
  275. uchar* dptr = dst->data.ptr + dst->step*i;
  276. const uchar* sptr0 = src->data.ptr + src->step*y_idx->data.i[i];
  277. for( j = 0; j < dcols; j++, dptr += elem_size )
  278. {
  279. const uchar* sptr = sptr0 + x_tab[j];
  280. for( k = 0; k < elem_size; k++ )
  281. dptr[k] = sptr[k];
  282. }
  283. }
  284. cvReleaseMat( &x_idx );
  285. cvReleaseMat( &y_idx );
  286. }
  287. class CV_ResizeExactTest : public CV_ResizeTest
  288. {
  289. public:
  290. CV_ResizeExactTest();
  291. protected:
  292. void get_test_array_types_and_sizes(int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types);
  293. };
  294. CV_ResizeExactTest::CV_ResizeExactTest() : CV_ResizeTest()
  295. {
  296. max_interpolation = 2;
  297. }
  298. void CV_ResizeExactTest::get_test_array_types_and_sizes(int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types)
  299. {
  300. CV_ResizeTest::get_test_array_types_and_sizes(test_case_idx, sizes, types);
  301. switch (interpolation)
  302. {
  303. case 0:
  304. interpolation = INTER_LINEAR_EXACT;
  305. break;
  306. case 1:
  307. interpolation = INTER_NEAREST_EXACT;
  308. break;
  309. default:
  310. CV_Assert(interpolation < max_interpolation);
  311. }
  312. if (CV_MAT_DEPTH(types[INPUT][0]) == CV_32F ||
  313. CV_MAT_DEPTH(types[INPUT][0]) == CV_64F)
  314. types[INPUT][0] = types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(CV_8U, CV_MAT_CN(types[INPUT][0]));
  315. }
  316. /////////////////////////
  317. static void test_remap( const Mat& src, Mat& dst, const Mat& mapx, const Mat& mapy,
  318. Mat* mask=0, int interpolation=CV_INTER_LINEAR )
  319. {
  320. int x, y, k;
  321. int drows = dst.rows, dcols = dst.cols;
  322. int srows = src.rows, scols = src.cols;
  323. const uchar* sptr0 = src.ptr();
  324. int depth = src.depth(), cn = src.channels();
  325. int elem_size = (int)src.elemSize();
  326. int step = (int)(src.step / CV_ELEM_SIZE(depth));
  327. int delta;
  328. if( interpolation != CV_INTER_CUBIC )
  329. {
  330. delta = 0;
  331. scols -= 1; srows -= 1;
  332. }
  333. else
  334. {
  335. delta = 1;
  336. scols = MAX(scols - 3, 0);
  337. srows = MAX(srows - 3, 0);
  338. }
  339. int scols1 = MAX(scols - 2, 0);
  340. int srows1 = MAX(srows - 2, 0);
  341. if( mask )
  342. *mask = Scalar::all(0);
  343. for( y = 0; y < drows; y++ )
  344. {
  345. uchar* dptr = dst.ptr(y);
  346. const float* mx = mapx.ptr<float>(y);
  347. const float* my = mapy.ptr<float>(y);
  348. uchar* m = mask ? mask->ptr(y) : 0;
  349. for( x = 0; x < dcols; x++, dptr += elem_size )
  350. {
  351. float xs = mx[x];
  352. float ys = my[x];
  353. int ixs = cvFloor(xs);
  354. int iys = cvFloor(ys);
  355. if( (unsigned)(ixs - delta - 1) >= (unsigned)scols1 ||
  356. (unsigned)(iys - delta - 1) >= (unsigned)srows1 )
  357. {
  358. if( m )
  359. m[x] = 1;
  360. if( (unsigned)(ixs - delta) >= (unsigned)scols ||
  361. (unsigned)(iys - delta) >= (unsigned)srows )
  362. continue;
  363. }
  364. xs -= ixs;
  365. ys -= iys;
  366. switch( depth )
  367. {
  368. case CV_8U:
  369. {
  370. const uchar* sptr = sptr0 + iys*step + ixs*cn;
  371. for( k = 0; k < cn; k++ )
  372. {
  373. float v00 = sptr[k];
  374. float v01 = sptr[cn + k];
  375. float v10 = sptr[step + k];
  376. float v11 = sptr[step + cn + k];
  377. v00 = v00 + xs*(v01 - v00);
  378. v10 = v10 + xs*(v11 - v10);
  379. v00 = v00 + ys*(v10 - v00);
  380. dptr[k] = (uchar)cvRound(v00);
  381. }
  382. }
  383. break;
  384. case CV_16U:
  385. {
  386. const ushort* sptr = (const ushort*)sptr0 + iys*step + ixs*cn;
  387. for( k = 0; k < cn; k++ )
  388. {
  389. float v00 = sptr[k];
  390. float v01 = sptr[cn + k];
  391. float v10 = sptr[step + k];
  392. float v11 = sptr[step + cn + k];
  393. v00 = v00 + xs*(v01 - v00);
  394. v10 = v10 + xs*(v11 - v10);
  395. v00 = v00 + ys*(v10 - v00);
  396. ((ushort*)dptr)[k] = (ushort)cvRound(v00);
  397. }
  398. }
  399. break;
  400. case CV_32F:
  401. {
  402. const float* sptr = (const float*)sptr0 + iys*step + ixs*cn;
  403. for( k = 0; k < cn; k++ )
  404. {
  405. float v00 = sptr[k];
  406. float v01 = sptr[cn + k];
  407. float v10 = sptr[step + k];
  408. float v11 = sptr[step + cn + k];
  409. v00 = v00 + xs*(v01 - v00);
  410. v10 = v10 + xs*(v11 - v10);
  411. v00 = v00 + ys*(v10 - v00);
  412. ((float*)dptr)[k] = (float)v00;
  413. }
  414. }
  415. break;
  416. default:
  417. CV_Assert(0);
  418. }
  419. }
  420. }
  421. }
  422. /////////////////////////
  423. class CV_WarpAffineTest : public CV_ImgWarpBaseTest
  424. {
  425. public:
  426. CV_WarpAffineTest();
  427. protected:
  428. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  429. void run_func();
  430. int prepare_test_case( int test_case_idx );
  431. void prepare_to_validation( int /*test_case_idx*/ );
  432. double get_success_error_level( int test_case_idx, int i, int j );
  433. };
  434. CV_WarpAffineTest::CV_WarpAffineTest() : CV_ImgWarpBaseTest( true )
  435. {
  436. //spatial_scale_zoom = spatial_scale_decimate;
  437. spatial_scale_decimate = spatial_scale_zoom;
  438. }
  439. void CV_WarpAffineTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  440. {
  441. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  442. Size sz = sizes[INPUT][0];
  443. // run for the second time to get output of a different size
  444. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  445. sizes[INPUT][0] = sz;
  446. sizes[INPUT][1] = Size( 3, 2 );
  447. }
  448. void CV_WarpAffineTest::run_func()
  449. {
  450. CvMat mtx = cvMat(test_mat[INPUT][1]);
  451. cvWarpAffine( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], &mtx, interpolation );
  452. }
  453. double CV_WarpAffineTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  454. {
  455. int depth = test_mat[INPUT][0].depth();
  456. return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
  457. }
  458. int CV_WarpAffineTest::prepare_test_case( int test_case_idx )
  459. {
  460. RNG& rng = ts->get_rng();
  461. int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
  462. const Mat& src = test_mat[INPUT][0];
  463. const Mat& dst = test_mat[INPUT_OUTPUT][0];
  464. Mat& mat = test_mat[INPUT][1];
  465. Point2f center;
  466. double scale, angle;
  467. if( code <= 0 )
  468. return code;
  469. double buffer[6];
  470. Mat tmp( 2, 3, mat.type(), buffer );
  471. center.x = (float)((cvtest::randReal(rng)*1.2 - 0.1)*src.cols);
  472. center.y = (float)((cvtest::randReal(rng)*1.2 - 0.1)*src.rows);
  473. angle = cvtest::randReal(rng)*360;
  474. scale = ((double)dst.rows/src.rows + (double)dst.cols/src.cols)*0.5;
  475. getRotationMatrix2D(center, angle, scale).convertTo(mat, mat.depth());
  476. rng.fill( tmp, CV_RAND_NORMAL, Scalar::all(1.), Scalar::all(0.01) );
  477. cv::max(tmp, 0.9, tmp);
  478. cv::min(tmp, 1.1, tmp);
  479. cv::multiply(tmp, mat, mat, 1.);
  480. return code;
  481. }
  482. void CV_WarpAffineTest::prepare_to_validation( int /*test_case_idx*/ )
  483. {
  484. const Mat& src = test_mat[INPUT][0];
  485. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  486. Mat& dst0 = test_mat[INPUT_OUTPUT][0];
  487. Mat mapx(dst.size(), CV_32F), mapy(dst.size(), CV_32F);
  488. double m[6];
  489. Mat srcAb, dstAb( 2, 3, CV_64FC1, m );
  490. //cvInvert( &tM, &M, CV_LU );
  491. // [R|t] -> [R^-1 | -(R^-1)*t]
  492. test_mat[INPUT][1].convertTo( srcAb, CV_64F );
  493. Mat A = srcAb.colRange(0, 2);
  494. Mat b = srcAb.col(2);
  495. Mat invA = dstAb.colRange(0, 2);
  496. Mat invAb = dstAb.col(2);
  497. cv::invert(A, invA, CV_SVD);
  498. cv::gemm(invA, b, -1, Mat(), 0, invAb);
  499. for( int y = 0; y < dst.rows; y++ )
  500. for( int x = 0; x < dst.cols; x++ )
  501. {
  502. mapx.at<float>(y, x) = (float)(x*m[0] + y*m[1] + m[2]);
  503. mapy.at<float>(y, x) = (float)(x*m[3] + y*m[4] + m[5]);
  504. }
  505. Mat mask( dst.size(), CV_8U );
  506. test_remap( src, dst, mapx, mapy, &mask );
  507. dst.setTo(Scalar::all(0), mask);
  508. dst0.setTo(Scalar::all(0), mask);
  509. }
  510. /////////////////////////
  511. class CV_WarpPerspectiveTest : public CV_ImgWarpBaseTest
  512. {
  513. public:
  514. CV_WarpPerspectiveTest();
  515. protected:
  516. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  517. void run_func();
  518. int prepare_test_case( int test_case_idx );
  519. void prepare_to_validation( int /*test_case_idx*/ );
  520. double get_success_error_level( int test_case_idx, int i, int j );
  521. };
  522. CV_WarpPerspectiveTest::CV_WarpPerspectiveTest() : CV_ImgWarpBaseTest( true )
  523. {
  524. //spatial_scale_zoom = spatial_scale_decimate;
  525. spatial_scale_decimate = spatial_scale_zoom;
  526. }
  527. void CV_WarpPerspectiveTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  528. {
  529. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  530. Size sz = sizes[INPUT][0];
  531. // run for the second time to get output of a different size
  532. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  533. sizes[INPUT][0] = sz;
  534. sizes[INPUT][1] = Size( 3, 3 );
  535. }
  536. void CV_WarpPerspectiveTest::run_func()
  537. {
  538. CvMat mtx = cvMat(test_mat[INPUT][1]);
  539. cvWarpPerspective( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], &mtx, interpolation );
  540. }
  541. double CV_WarpPerspectiveTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  542. {
  543. int depth = test_mat[INPUT][0].depth();
  544. return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
  545. }
  546. int CV_WarpPerspectiveTest::prepare_test_case( int test_case_idx )
  547. {
  548. RNG& rng = ts->get_rng();
  549. int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
  550. const CvMat src = cvMat(test_mat[INPUT][0]);
  551. const CvMat dst = cvMat(test_mat[INPUT_OUTPUT][0]);
  552. Mat& mat = test_mat[INPUT][1];
  553. Point2f s[4], d[4];
  554. int i;
  555. if( code <= 0 )
  556. return code;
  557. s[0] = Point2f(0,0);
  558. d[0] = Point2f(0,0);
  559. s[1] = Point2f(src.cols-1.f,0);
  560. d[1] = Point2f(dst.cols-1.f,0);
  561. s[2] = Point2f(src.cols-1.f,src.rows-1.f);
  562. d[2] = Point2f(dst.cols-1.f,dst.rows-1.f);
  563. s[3] = Point2f(0,src.rows-1.f);
  564. d[3] = Point2f(0,dst.rows-1.f);
  565. float bufer[16];
  566. Mat tmp( 1, 16, CV_32FC1, bufer );
  567. rng.fill( tmp, CV_RAND_NORMAL, Scalar::all(0.), Scalar::all(0.1) );
  568. for( i = 0; i < 4; i++ )
  569. {
  570. s[i].x += bufer[i*4]*src.cols/2;
  571. s[i].y += bufer[i*4+1]*src.rows/2;
  572. d[i].x += bufer[i*4+2]*dst.cols/2;
  573. d[i].y += bufer[i*4+3]*dst.rows/2;
  574. }
  575. cv::getPerspectiveTransform( s, d ).convertTo( mat, mat.depth() );
  576. return code;
  577. }
  578. void CV_WarpPerspectiveTest::prepare_to_validation( int /*test_case_idx*/ )
  579. {
  580. Mat& src = test_mat[INPUT][0];
  581. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  582. Mat& dst0 = test_mat[INPUT_OUTPUT][0];
  583. Mat mapx(dst.size(), CV_32F), mapy(dst.size(), CV_32F);
  584. double m[9];
  585. Mat srcM, dstM(3, 3, CV_64F, m);
  586. //cvInvert( &tM, &M, CV_LU );
  587. // [R|t] -> [R^-1 | -(R^-1)*t]
  588. test_mat[INPUT][1].convertTo( srcM, CV_64F );
  589. cv::invert(srcM, dstM, CV_SVD);
  590. for( int y = 0; y < dst.rows; y++ )
  591. {
  592. for( int x = 0; x < dst.cols; x++ )
  593. {
  594. double xs = x*m[0] + y*m[1] + m[2];
  595. double ys = x*m[3] + y*m[4] + m[5];
  596. double ds = x*m[6] + y*m[7] + m[8];
  597. ds = ds ? 1./ds : 0;
  598. xs *= ds;
  599. ys *= ds;
  600. mapx.at<float>(y, x) = (float)xs;
  601. mapy.at<float>(y, x) = (float)ys;
  602. }
  603. }
  604. Mat mask( dst.size(), CV_8U );
  605. test_remap( src, dst, mapx, mapy, &mask );
  606. dst.setTo(Scalar::all(0), mask);
  607. dst0.setTo(Scalar::all(0), mask);
  608. }
  609. /////////////////////////
  610. class CV_RemapTest : public CV_ImgWarpBaseTest
  611. {
  612. public:
  613. CV_RemapTest();
  614. protected:
  615. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  616. void run_func();
  617. int prepare_test_case( int test_case_idx );
  618. void prepare_to_validation( int /*test_case_idx*/ );
  619. double get_success_error_level( int test_case_idx, int i, int j );
  620. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  621. };
  622. CV_RemapTest::CV_RemapTest() : CV_ImgWarpBaseTest( false )
  623. {
  624. //spatial_scale_zoom = spatial_scale_decimate;
  625. test_array[INPUT].push_back(NULL);
  626. test_array[INPUT].push_back(NULL);
  627. spatial_scale_decimate = spatial_scale_zoom;
  628. }
  629. void CV_RemapTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  630. {
  631. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  632. types[INPUT][1] = types[INPUT][2] = CV_32FC1;
  633. interpolation = CV_INTER_LINEAR;
  634. }
  635. void CV_RemapTest::fill_array( int test_case_idx, int i, int j, Mat& arr )
  636. {
  637. if( i != INPUT )
  638. CV_ImgWarpBaseTest::fill_array( test_case_idx, i, j, arr );
  639. }
  640. void CV_RemapTest::run_func()
  641. {
  642. cvRemap( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
  643. test_array[INPUT][1], test_array[INPUT][2], interpolation );
  644. }
  645. double CV_RemapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  646. {
  647. int depth = test_mat[INPUT][0].depth();
  648. return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
  649. }
  650. int CV_RemapTest::prepare_test_case( int test_case_idx )
  651. {
  652. RNG& rng = ts->get_rng();
  653. int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
  654. const Mat& src = test_mat[INPUT][0];
  655. double a[9] = {0,0,0,0,0,0,0,0,1}, k[4];
  656. Mat _a( 3, 3, CV_64F, a );
  657. Mat _k( 4, 1, CV_64F, k );
  658. double sz = MAX(src.rows, src.cols);
  659. if( code <= 0 )
  660. return code;
  661. double aspect_ratio = cvtest::randReal(rng)*0.6 + 0.7;
  662. a[2] = (src.cols - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  663. a[5] = (src.rows - 1)*0.5 + cvtest::randReal(rng)*10 - 5;
  664. a[0] = sz/(0.9 - cvtest::randReal(rng)*0.6);
  665. a[4] = aspect_ratio*a[0];
  666. k[0] = cvtest::randReal(rng)*0.06 - 0.03;
  667. k[1] = cvtest::randReal(rng)*0.06 - 0.03;
  668. if( k[0]*k[1] > 0 )
  669. k[1] = -k[1];
  670. k[2] = cvtest::randReal(rng)*0.004 - 0.002;
  671. k[3] = cvtest::randReal(rng)*0.004 - 0.002;
  672. cvtest::initUndistortMap( _a, _k, Mat(), Mat(), test_mat[INPUT][1].size(), test_mat[INPUT][1], test_mat[INPUT][2], CV_32F );
  673. return code;
  674. }
  675. void CV_RemapTest::prepare_to_validation( int /*test_case_idx*/ )
  676. {
  677. Mat& dst = test_mat[REF_INPUT_OUTPUT][0];
  678. Mat& dst0 = test_mat[INPUT_OUTPUT][0];
  679. Mat mask( dst.size(), CV_8U );
  680. test_remap(test_mat[INPUT][0], dst, test_mat[INPUT][1],
  681. test_mat[INPUT][2], &mask, interpolation );
  682. dst.setTo(Scalar::all(0), mask);
  683. dst0.setTo(Scalar::all(0), mask);
  684. }
  685. ////////////////////////////// GetRectSubPix /////////////////////////////////
  686. static void
  687. test_getQuadrangeSubPix( const Mat& src, Mat& dst, double* a )
  688. {
  689. int sstep = (int)(src.step / sizeof(float));
  690. int scols = src.cols, srows = src.rows;
  691. CV_Assert( src.depth() == CV_32F && src.type() == dst.type() );
  692. int cn = dst.channels();
  693. for( int y = 0; y < dst.rows; y++ )
  694. for( int x = 0; x < dst.cols; x++ )
  695. {
  696. float* d = dst.ptr<float>(y) + x*cn;
  697. float sx = (float)(a[0]*x + a[1]*y + a[2]);
  698. float sy = (float)(a[3]*x + a[4]*y + a[5]);
  699. int ix = cvFloor(sx), iy = cvFloor(sy);
  700. int dx = cn, dy = sstep;
  701. const float* s;
  702. sx -= ix; sy -= iy;
  703. if( (unsigned)ix >= (unsigned)(scols-1) )
  704. ix = ix < 0 ? 0 : scols - 1, sx = 0, dx = 0;
  705. if( (unsigned)iy >= (unsigned)(srows-1) )
  706. iy = iy < 0 ? 0 : srows - 1, sy = 0, dy = 0;
  707. s = src.ptr<float>(iy) + ix*cn;
  708. for( int k = 0; k < cn; k++, s++ )
  709. {
  710. float t0 = s[0] + sx*(s[dx] - s[0]);
  711. float t1 = s[dy] + sx*(s[dy + dx] - s[dy]);
  712. d[k] = t0 + sy*(t1 - t0);
  713. }
  714. }
  715. }
  716. class CV_GetRectSubPixTest : public CV_ImgWarpBaseTest
  717. {
  718. public:
  719. CV_GetRectSubPixTest();
  720. protected:
  721. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  722. void run_func();
  723. int prepare_test_case( int test_case_idx );
  724. void prepare_to_validation( int /*test_case_idx*/ );
  725. double get_success_error_level( int test_case_idx, int i, int j );
  726. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  727. CvPoint2D32f center;
  728. bool test_cpp;
  729. };
  730. CV_GetRectSubPixTest::CV_GetRectSubPixTest() : CV_ImgWarpBaseTest( false )
  731. {
  732. //spatial_scale_zoom = spatial_scale_decimate;
  733. spatial_scale_decimate = spatial_scale_zoom;
  734. test_cpp = false;
  735. }
  736. void CV_GetRectSubPixTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  737. {
  738. RNG& rng = ts->get_rng();
  739. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  740. int src_depth = cvtest::randInt(rng) % 2, dst_depth;
  741. int cn = cvtest::randInt(rng) % 2 ? 3 : 1;
  742. Size src_size, dst_size;
  743. dst_depth = src_depth = src_depth == 0 ? CV_8U : CV_32F;
  744. if( src_depth < CV_32F && cvtest::randInt(rng) % 2 )
  745. dst_depth = CV_32F;
  746. types[INPUT][0] = CV_MAKETYPE(src_depth,cn);
  747. types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(dst_depth,cn);
  748. src_size = sizes[INPUT][0];
  749. dst_size.width = cvRound(sqrt(cvtest::randReal(rng)*src_size.width) + 1);
  750. dst_size.height = cvRound(sqrt(cvtest::randReal(rng)*src_size.height) + 1);
  751. dst_size.width = MIN(dst_size.width,src_size.width);
  752. dst_size.height = MIN(dst_size.width,src_size.height);
  753. sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = dst_size;
  754. center.x = (float)(cvtest::randReal(rng)*src_size.width);
  755. center.y = (float)(cvtest::randReal(rng)*src_size.height);
  756. interpolation = CV_INTER_LINEAR;
  757. test_cpp = (cvtest::randInt(rng) & 256) == 0;
  758. }
  759. void CV_GetRectSubPixTest::fill_array( int test_case_idx, int i, int j, Mat& arr )
  760. {
  761. if( i != INPUT )
  762. CV_ImgWarpBaseTest::fill_array( test_case_idx, i, j, arr );
  763. }
  764. void CV_GetRectSubPixTest::run_func()
  765. {
  766. if(!test_cpp)
  767. cvGetRectSubPix( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], center );
  768. else
  769. {
  770. cv::Mat _out = cv::cvarrToMat(test_array[INPUT_OUTPUT][0]);
  771. cv::getRectSubPix( cv::cvarrToMat(test_array[INPUT][0]), _out.size(), center, _out, _out.type());
  772. }
  773. }
  774. double CV_GetRectSubPixTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  775. {
  776. int in_depth = test_mat[INPUT][0].depth();
  777. int out_depth = test_mat[INPUT_OUTPUT][0].depth();
  778. return in_depth >= CV_32F ? 1e-3 : out_depth >= CV_32F ? 1e-2 : 1;
  779. }
  780. int CV_GetRectSubPixTest::prepare_test_case( int test_case_idx )
  781. {
  782. return CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
  783. }
  784. void CV_GetRectSubPixTest::prepare_to_validation( int /*test_case_idx*/ )
  785. {
  786. Mat& src0 = test_mat[INPUT][0];
  787. Mat& dst0 = test_mat[REF_INPUT_OUTPUT][0];
  788. Mat src = src0, dst = dst0;
  789. int ftype = CV_MAKETYPE(CV_32F,src0.channels());
  790. double a[] = { 1, 0, center.x - dst.cols*0.5 + 0.5,
  791. 0, 1, center.y - dst.rows*0.5 + 0.5 };
  792. if( src.depth() != CV_32F )
  793. src0.convertTo(src, CV_32F);
  794. if( dst.depth() != CV_32F )
  795. dst.create(dst0.size(), ftype);
  796. test_getQuadrangeSubPix( src, dst, a );
  797. if( dst.data != dst0.data )
  798. dst.convertTo(dst0, dst0.depth());
  799. }
  800. class CV_GetQuadSubPixTest : public CV_ImgWarpBaseTest
  801. {
  802. public:
  803. CV_GetQuadSubPixTest();
  804. protected:
  805. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  806. void run_func();
  807. int prepare_test_case( int test_case_idx );
  808. void prepare_to_validation( int /*test_case_idx*/ );
  809. double get_success_error_level( int test_case_idx, int i, int j );
  810. };
  811. CV_GetQuadSubPixTest::CV_GetQuadSubPixTest() : CV_ImgWarpBaseTest( true )
  812. {
  813. //spatial_scale_zoom = spatial_scale_decimate;
  814. spatial_scale_decimate = spatial_scale_zoom;
  815. }
  816. void CV_GetQuadSubPixTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
  817. {
  818. int min_size = 4;
  819. CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  820. Size sz = sizes[INPUT][0], dsz;
  821. RNG& rng = ts->get_rng();
  822. int msz, src_depth = cvtest::randInt(rng) % 2, dst_depth;
  823. int cn = cvtest::randInt(rng) % 2 ? 3 : 1;
  824. dst_depth = src_depth = src_depth == 0 ? CV_8U : CV_32F;
  825. if( src_depth < CV_32F && cvtest::randInt(rng) % 2 )
  826. dst_depth = CV_32F;
  827. types[INPUT][0] = CV_MAKETYPE(src_depth,cn);
  828. types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(dst_depth,cn);
  829. sz.width = MAX(sz.width,min_size);
  830. sz.height = MAX(sz.height,min_size);
  831. sizes[INPUT][0] = sz;
  832. msz = MIN( sz.width, sz.height );
  833. dsz.width = cvRound(sqrt(cvtest::randReal(rng)*msz) + 1);
  834. dsz.height = cvRound(sqrt(cvtest::randReal(rng)*msz) + 1);
  835. dsz.width = MIN(dsz.width,msz);
  836. dsz.height = MIN(dsz.width,msz);
  837. dsz.width = MAX(dsz.width,min_size);
  838. dsz.height = MAX(dsz.height,min_size);
  839. sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = dsz;
  840. sizes[INPUT][1] = cvSize( 3, 2 );
  841. }
  842. void CV_GetQuadSubPixTest::run_func()
  843. {
  844. CvMat mtx = cvMat(test_mat[INPUT][1]);
  845. cvGetQuadrangleSubPix( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], &mtx );
  846. }
  847. double CV_GetQuadSubPixTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  848. {
  849. int in_depth = test_mat[INPUT][0].depth();
  850. //int out_depth = test_mat[INPUT_OUTPUT][0].depth();
  851. return in_depth >= CV_32F ? 1e-2 : 4;
  852. }
  853. int CV_GetQuadSubPixTest::prepare_test_case( int test_case_idx )
  854. {
  855. RNG& rng = ts->get_rng();
  856. int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
  857. const Mat& src = test_mat[INPUT][0];
  858. Mat& mat = test_mat[INPUT][1];
  859. Point2f center;
  860. double scale, angle;
  861. if( code <= 0 )
  862. return code;
  863. double a[6];
  864. Mat A( 2, 3, CV_64FC1, a );
  865. center.x = (float)((cvtest::randReal(rng)*1.2 - 0.1)*src.cols);
  866. center.y = (float)((cvtest::randReal(rng)*1.2 - 0.1)*src.rows);
  867. angle = cvtest::randReal(rng)*360;
  868. scale = cvtest::randReal(rng)*0.2 + 0.9;
  869. // y = Ax + b -> x = A^-1(y - b) = A^-1*y - A^-1*b
  870. scale = 1./scale;
  871. angle = angle*(CV_PI/180.);
  872. a[0] = a[4] = cos(angle)*scale;
  873. a[1] = sin(angle)*scale;
  874. a[3] = -a[1];
  875. a[2] = center.x - a[0]*center.x - a[1]*center.y;
  876. a[5] = center.y - a[3]*center.x - a[4]*center.y;
  877. A.convertTo( mat, mat.depth() );
  878. return code;
  879. }
  880. void CV_GetQuadSubPixTest::prepare_to_validation( int /*test_case_idx*/ )
  881. {
  882. Mat& src0 = test_mat[INPUT][0];
  883. Mat& dst0 = test_mat[REF_INPUT_OUTPUT][0];
  884. Mat src = src0, dst = dst0;
  885. int ftype = CV_MAKETYPE(CV_32F,src0.channels());
  886. double a[6], dx = (dst0.cols - 1)*0.5, dy = (dst0.rows - 1)*0.5;
  887. Mat A( 2, 3, CV_64F, a );
  888. if( src.depth() != CV_32F )
  889. src0.convertTo(src, CV_32F);
  890. if( dst.depth() != CV_32F )
  891. dst.create(dst0.size(), ftype);
  892. test_mat[INPUT][1].convertTo( A, CV_64F );
  893. a[2] -= a[0]*dx + a[1]*dy;
  894. a[5] -= a[3]*dx + a[4]*dy;
  895. test_getQuadrangeSubPix( src, dst, a );
  896. if( dst.data != dst0.data )
  897. dst.convertTo(dst0, dst0.depth());
  898. }
  899. ////////////////////////////// resizeArea /////////////////////////////////
  900. template <typename T>
  901. static void check_resize_area(const Mat& expected, const Mat& actual, double tolerance = 1.0)
  902. {
  903. ASSERT_EQ(actual.type(), expected.type());
  904. ASSERT_EQ(actual.size(), expected.size());
  905. Mat diff;
  906. absdiff(actual, expected, diff);
  907. Mat one_channel_diff = diff; //.reshape(1);
  908. Size dsize = actual.size();
  909. bool next = true;
  910. for (int dy = 0; dy < dsize.height && next; ++dy)
  911. {
  912. const T* eD = expected.ptr<T>(dy);
  913. const T* aD = actual.ptr<T>(dy);
  914. for (int dx = 0; dx < dsize.width && next; ++dx)
  915. if (fabs(static_cast<double>(aD[dx] - eD[dx])) > tolerance)
  916. {
  917. cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, "Inf norm: %f\n", static_cast<float>(cvtest::norm(actual, expected, NORM_INF)));
  918. cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, "Error in : (%d, %d)\n", dx, dy);
  919. const int radius = 3;
  920. int rmin = MAX(dy - radius, 0), rmax = MIN(dy + radius, dsize.height);
  921. int cmin = MAX(dx - radius, 0), cmax = MIN(dx + radius, dsize.width);
  922. std::cout << "Abs diff:" << std::endl << diff << std::endl;
  923. std::cout << "actual result:\n" << actual(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
  924. std::cout << "expected result:\n" << expected(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
  925. next = false;
  926. }
  927. }
  928. ASSERT_EQ(0, cvtest::norm(one_channel_diff, cv::NORM_INF));
  929. }
  930. ///////////////////////////////////////////////////////////////////////////
  931. TEST(Imgproc_cvWarpAffine, regression)
  932. {
  933. IplImage* src = cvCreateImage(cvSize(100, 100), IPL_DEPTH_8U, 1);
  934. IplImage* dst = cvCreateImage(cvSize(100, 100), IPL_DEPTH_8U, 1);
  935. cvZero(src);
  936. float m[6];
  937. CvMat M = cvMat( 2, 3, CV_32F, m );
  938. int w = src->width;
  939. int h = src->height;
  940. cv2DRotationMatrix(cvPoint2D32f(w*0.5f, h*0.5f), 45.0, 1.0, &M);
  941. cvWarpAffine(src, dst, &M);
  942. cvReleaseImage(&src);
  943. cvReleaseImage(&dst);
  944. }
  945. TEST(Imgproc_fitLine_vector_3d, regression)
  946. {
  947. std::vector<Point3f> points_vector;
  948. Point3f p21(4,4,4);
  949. Point3f p22(8,8,8);
  950. points_vector.push_back(p21);
  951. points_vector.push_back(p22);
  952. std::vector<float> line;
  953. cv::fitLine(points_vector, line, CV_DIST_L2, 0 ,0 ,0);
  954. ASSERT_EQ(line.size(), (size_t)6);
  955. }
  956. TEST(Imgproc_fitLine_vector_2d, regression)
  957. {
  958. std::vector<Point2f> points_vector;
  959. Point2f p21(4,4);
  960. Point2f p22(8,8);
  961. Point2f p23(16,16);
  962. points_vector.push_back(p21);
  963. points_vector.push_back(p22);
  964. points_vector.push_back(p23);
  965. std::vector<float> line;
  966. cv::fitLine(points_vector, line, CV_DIST_L2, 0 ,0 ,0);
  967. ASSERT_EQ(line.size(), (size_t)4);
  968. }
  969. TEST(Imgproc_fitLine_Mat_2dC2, regression)
  970. {
  971. cv::Mat mat1 = Mat::zeros(3, 1, CV_32SC2);
  972. std::vector<float> line1;
  973. cv::fitLine(mat1, line1, CV_DIST_L2, 0 ,0 ,0);
  974. ASSERT_EQ(line1.size(), (size_t)4);
  975. }
  976. TEST(Imgproc_fitLine_Mat_2dC1, regression)
  977. {
  978. cv::Matx<int, 3, 2> mat2;
  979. std::vector<float> line2;
  980. cv::fitLine(mat2, line2, CV_DIST_L2, 0 ,0 ,0);
  981. ASSERT_EQ(line2.size(), (size_t)4);
  982. }
  983. TEST(Imgproc_fitLine_Mat_3dC3, regression)
  984. {
  985. cv::Mat mat1 = Mat::zeros(2, 1, CV_32SC3);
  986. std::vector<float> line1;
  987. cv::fitLine(mat1, line1, CV_DIST_L2, 0 ,0 ,0);
  988. ASSERT_EQ(line1.size(), (size_t)6);
  989. }
  990. TEST(Imgproc_fitLine_Mat_3dC1, regression)
  991. {
  992. cv::Mat mat2 = Mat::zeros(2, 3, CV_32SC1);
  993. std::vector<float> line2;
  994. cv::fitLine(mat2, line2, CV_DIST_L2, 0 ,0 ,0);
  995. ASSERT_EQ(line2.size(), (size_t)6);
  996. }
  997. TEST(Imgproc_resize_area, regression)
  998. {
  999. static ushort input_data[16 * 16] = {
  1000. 90, 94, 80, 3, 231, 2, 186, 245, 188, 165, 10, 19, 201, 169, 8, 228,
  1001. 86, 5, 203, 120, 136, 185, 24, 94, 81, 150, 163, 137, 88, 105, 132, 132,
  1002. 236, 48, 250, 218, 19, 52, 54, 221, 159, 112, 45, 11, 152, 153, 112, 134,
  1003. 78, 133, 136, 83, 65, 76, 82, 250, 9, 235, 148, 26, 236, 179, 200, 50,
  1004. 99, 51, 103, 142, 201, 65, 176, 33, 49, 226, 177, 109, 46, 21, 67, 130,
  1005. 54, 125, 107, 154, 145, 51, 199, 189, 161, 142, 231, 240, 139, 162, 240, 22,
  1006. 231, 86, 79, 106, 92, 47, 146, 156, 36, 207, 71, 33, 2, 244, 221, 71,
  1007. 44, 127, 71, 177, 75, 126, 68, 119, 200, 129, 191, 251, 6, 236, 247, 6,
  1008. 133, 175, 56, 239, 147, 221, 243, 154, 242, 82, 106, 99, 77, 158, 60, 229,
  1009. 2, 42, 24, 174, 27, 198, 14, 204, 246, 251, 141, 31, 114, 163, 29, 147,
  1010. 121, 53, 74, 31, 147, 189, 42, 98, 202, 17, 228, 123, 209, 40, 77, 49,
  1011. 112, 203, 30, 12, 205, 25, 19, 106, 145, 185, 163, 201, 237, 223, 247, 38,
  1012. 33, 105, 243, 117, 92, 179, 204, 248, 160, 90, 73, 126, 2, 41, 213, 204,
  1013. 6, 124, 195, 201, 230, 187, 210, 167, 48, 79, 123, 159, 145, 218, 105, 209,
  1014. 240, 152, 136, 235, 235, 164, 157, 9, 152, 38, 27, 209, 120, 77, 238, 196,
  1015. 240, 233, 10, 241, 90, 67, 12, 79, 0, 43, 58, 27, 83, 199, 190, 182};
  1016. static ushort expected_data[5 * 5] = {
  1017. 120, 100, 151, 101, 130,
  1018. 106, 115, 141, 130, 127,
  1019. 91, 136, 170, 114, 140,
  1020. 104, 122, 131, 147, 133,
  1021. 161, 163, 70, 107, 182
  1022. };
  1023. cv::Mat src(16, 16, CV_16UC1, input_data);
  1024. cv::Mat expected(5, 5, CV_16UC1, expected_data);
  1025. cv::Mat actual(expected.size(), expected.type());
  1026. cv::resize(src, actual, cv::Size(), 0.3, 0.3, INTER_AREA);
  1027. check_resize_area<ushort>(expected, actual, 1.0);
  1028. }
  1029. TEST(Imgproc_resize_area, regression_half_round)
  1030. {
  1031. static uchar input_data[32 * 32];
  1032. for(int i = 0; i < 32 * 32; ++i)
  1033. input_data[i] = (uchar)(i % 2 + 253 + i / (16 * 32));
  1034. static uchar expected_data[16 * 16];
  1035. for(int i = 0; i < 16 * 16; ++i)
  1036. expected_data[i] = (uchar)(254 + i / (16 * 8));
  1037. cv::Mat src(32, 32, CV_8UC1, input_data);
  1038. cv::Mat expected(16, 16, CV_8UC1, expected_data);
  1039. cv::Mat actual(expected.size(), expected.type());
  1040. cv::resize(src, actual, cv::Size(), 0.5, 0.5, INTER_AREA);
  1041. check_resize_area<uchar>(expected, actual, 0.5);
  1042. }
  1043. TEST(Imgproc_resize_area, regression_quarter_round)
  1044. {
  1045. static uchar input_data[32 * 32];
  1046. for(int i = 0; i < 32 * 32; ++i)
  1047. input_data[i] = (uchar)(i % 2 + 253 + i / (16 * 32));
  1048. static uchar expected_data[8 * 8];
  1049. for(int i = 0; i < 8 * 8; ++i)
  1050. expected_data[i] = 254;
  1051. cv::Mat src(32, 32, CV_8UC1, input_data);
  1052. cv::Mat expected(8, 8, CV_8UC1, expected_data);
  1053. cv::Mat actual(expected.size(), expected.type());
  1054. cv::resize(src, actual, cv::Size(), 0.25, 0.25, INTER_AREA);
  1055. check_resize_area<uchar>(expected, actual, 0.5);
  1056. }
  1057. //////////////////////////////////////////////////////////////////////////
  1058. TEST(Imgproc_Resize, accuracy) { CV_ResizeTest test; test.safe_run(); }
  1059. TEST(Imgproc_ResizeExact, accuracy) { CV_ResizeExactTest test; test.safe_run(); }
  1060. TEST(Imgproc_WarpAffine, accuracy) { CV_WarpAffineTest test; test.safe_run(); }
  1061. TEST(Imgproc_WarpPerspective, accuracy) { CV_WarpPerspectiveTest test; test.safe_run(); }
  1062. TEST(Imgproc_Remap, accuracy) { CV_RemapTest test; test.safe_run(); }
  1063. TEST(Imgproc_GetRectSubPix, accuracy) { CV_GetRectSubPixTest test; test.safe_run(); }
  1064. TEST(Imgproc_GetQuadSubPix, accuracy) { CV_GetQuadSubPixTest test; test.safe_run(); }
  1065. //////////////////////////////////////////////////////////////////////////
  1066. template <typename T, typename WT>
  1067. struct IntCast
  1068. {
  1069. T operator() (WT val) const
  1070. {
  1071. return cv::saturate_cast<T>(val >> 2);
  1072. }
  1073. };
  1074. template <typename T, typename WT>
  1075. struct FltCast
  1076. {
  1077. T operator() (WT val) const
  1078. {
  1079. return cv::saturate_cast<T>(val * 0.25);
  1080. }
  1081. };
  1082. template <typename T, typename WT, int one, typename CastOp>
  1083. void resizeArea(const cv::Mat & src, cv::Mat & dst)
  1084. {
  1085. int cn = src.channels();
  1086. CastOp castOp;
  1087. for (int y = 0; y < dst.rows; ++y)
  1088. {
  1089. const T * sptr0 = src.ptr<T>(y << 1);
  1090. const T * sptr1 = src.ptr<T>((y << 1) + 1);
  1091. T * dptr = dst.ptr<T>(y);
  1092. for (int x = 0; x < dst.cols * cn; x += cn)
  1093. {
  1094. int x1 = x << 1;
  1095. for (int c = 0; c < cn; ++c)
  1096. {
  1097. WT sum = WT(sptr0[x1 + c]) + WT(sptr0[x1 + c + cn]);
  1098. sum += WT(sptr1[x1 + c]) + WT(sptr1[x1 + c + cn]) + (WT)(one);
  1099. dptr[x + c] = castOp(sum);
  1100. }
  1101. }
  1102. }
  1103. }
  1104. TEST(Resize, Area_half)
  1105. {
  1106. const int size = 1000;
  1107. int types[] = { CV_8UC1, CV_8UC4,
  1108. CV_16UC1, CV_16UC4,
  1109. CV_16SC1, CV_16SC3, CV_16SC4,
  1110. CV_32FC1, CV_32FC4 };
  1111. cv::RNG rng(17);
  1112. for (int i = 0, _size = sizeof(types) / sizeof(types[0]); i < _size; ++i)
  1113. {
  1114. int type = types[i], depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
  1115. const float eps = depth <= CV_32S ? 0 : 7e-5f;
  1116. SCOPED_TRACE(depth);
  1117. SCOPED_TRACE(cn);
  1118. cv::Mat src(size, size, type), dst_actual(size >> 1, size >> 1, type),
  1119. dst_reference(size >> 1, size >> 1, type);
  1120. rng.fill(src, cv::RNG::UNIFORM, -1000, 1000, true);
  1121. if (depth == CV_8U)
  1122. resizeArea<uchar, ushort, 2, IntCast<uchar, ushort> >(src, dst_reference);
  1123. else if (depth == CV_16U)
  1124. resizeArea<ushort, uint, 2, IntCast<ushort, uint> >(src, dst_reference);
  1125. else if (depth == CV_16S)
  1126. resizeArea<short, int, 2, IntCast<short, int> >(src, dst_reference);
  1127. else if (depth == CV_32F)
  1128. resizeArea<float, float, 0, FltCast<float, float> >(src, dst_reference);
  1129. else
  1130. CV_Assert(0);
  1131. cv::resize(src, dst_actual, dst_actual.size(), 0, 0, cv::INTER_AREA);
  1132. ASSERT_GE(eps, cvtest::norm(dst_reference, dst_actual, cv::NORM_INF));
  1133. }
  1134. }
  1135. TEST(Resize, lanczos4_regression_16192)
  1136. {
  1137. Size src_size(11, 17);
  1138. Size dst_size(11, 153);
  1139. Mat src(src_size, CV_8UC3, Scalar::all(128));
  1140. Mat dst(dst_size, CV_8UC3, Scalar::all(255));
  1141. cv::resize(src, dst, dst_size, 0, 0, INTER_LANCZOS4);
  1142. Mat expected(dst_size, CV_8UC3, Scalar::all(128));
  1143. EXPECT_EQ(cvtest::norm(dst, expected, NORM_INF), 0) << dst(Rect(0,0,8,8));
  1144. }
  1145. TEST(Resize, nearest_regression_15075)
  1146. {
  1147. const int C = 5;
  1148. const int i1 = 5, j1 = 5;
  1149. Size src_size(12, 12);
  1150. Size dst_size(11, 11);
  1151. cv::Mat src = cv::Mat::zeros(src_size, CV_8UC(C)), dst;
  1152. for (int j = 0; j < C; j++)
  1153. src.col(i1).row(j1).data[j] = 1;
  1154. cv::resize(src, dst, dst_size, 0, 0, INTER_NEAREST);
  1155. EXPECT_EQ(C, cvtest::norm(dst, NORM_L1)) << src.size;
  1156. }
  1157. TEST(Imgproc_Warp, multichannel)
  1158. {
  1159. static const int inter_types[] = {INTER_NEAREST, INTER_AREA, INTER_CUBIC,
  1160. INTER_LANCZOS4, INTER_LINEAR};
  1161. static const int inter_n = sizeof(inter_types) / sizeof(int);
  1162. static const int border_types[] = {BORDER_CONSTANT, BORDER_DEFAULT,
  1163. BORDER_REFLECT, BORDER_REPLICATE,
  1164. BORDER_WRAP, BORDER_WRAP};
  1165. static const int border_n = sizeof(border_types) / sizeof(int);
  1166. RNG& rng = theRNG();
  1167. for( int iter = 0; iter < 100; iter++ )
  1168. {
  1169. int inter = inter_types[rng.uniform(0, inter_n)];
  1170. int border = border_types[rng.uniform(0, border_n)];
  1171. int width = rng.uniform(3, 333);
  1172. int height = rng.uniform(3, 333);
  1173. int cn = rng.uniform(1, 15);
  1174. if(inter == INTER_CUBIC || inter == INTER_LANCZOS4)
  1175. cn = rng.uniform(1, 5);
  1176. Mat src(height, width, CV_8UC(cn)), dst;
  1177. //randu(src, 0, 256);
  1178. src.setTo(0.);
  1179. Mat rot = getRotationMatrix2D(Point2f(0.f, 0.f), 1.0, 1.0);
  1180. warpAffine(src, dst, rot, src.size(), inter, border);
  1181. ASSERT_EQ(0.0, cvtest::norm(dst, NORM_INF));
  1182. Mat rot2 = Mat::eye(3, 3, rot.type());
  1183. rot.copyTo(rot2.rowRange(0, 2));
  1184. warpPerspective(src, dst, rot2, src.size(), inter, border);
  1185. ASSERT_EQ(0.0, cvtest::norm(dst, NORM_INF));
  1186. }
  1187. }
  1188. TEST(Imgproc_Warp, regression_19566) // valgrind should detect problem if any
  1189. {
  1190. const Size imgSize(8192, 8);
  1191. Mat inMat = Mat::zeros(imgSize, CV_8UC4);
  1192. Mat outMat = Mat::zeros(imgSize, CV_8UC4);
  1193. warpAffine(
  1194. inMat,
  1195. outMat,
  1196. getRotationMatrix2D(Point2f(imgSize.width / 2.0f, imgSize.height / 2.0f), 45.0, 1.0),
  1197. imgSize,
  1198. INTER_LINEAR,
  1199. cv::BORDER_CONSTANT,
  1200. cv::Scalar(0.0, 0.0, 0.0, 255.0)
  1201. );
  1202. }
  1203. TEST(Imgproc_GetAffineTransform, singularity)
  1204. {
  1205. Point2f A_sample[3];
  1206. A_sample[0] = Point2f(8.f, 9.f);
  1207. A_sample[1] = Point2f(40.f, 41.f);
  1208. A_sample[2] = Point2f(47.f, 48.f);
  1209. Point2f B_sample[3];
  1210. B_sample[0] = Point2f(7.37465f, 11.8295f);
  1211. B_sample[1] = Point2f(15.0113f, 12.8994f);
  1212. B_sample[2] = Point2f(38.9943f, 9.56297f);
  1213. Mat trans = getAffineTransform(A_sample, B_sample);
  1214. ASSERT_EQ(0.0, cvtest::norm(trans, NORM_INF));
  1215. }
  1216. TEST(Imgproc_Remap, DISABLED_memleak)
  1217. {
  1218. Mat src;
  1219. const int N = 400;
  1220. src.create(N, N, CV_8U);
  1221. randu(src, 0, 256);
  1222. Mat map_x, map_y, dst;
  1223. dst.create( src.size(), src.type() );
  1224. map_x.create( src.size(), CV_32FC1 );
  1225. map_y.create( src.size(), CV_32FC1 );
  1226. randu(map_x, 0., N+0.);
  1227. randu(map_y, 0., N+0.);
  1228. for( int iter = 0; iter < 10000; iter++ )
  1229. {
  1230. if(iter % 100 == 0)
  1231. {
  1232. putchar('.');
  1233. fflush(stdout);
  1234. }
  1235. remap(src, dst, map_x, map_y, CV_INTER_LINEAR);
  1236. }
  1237. }
  1238. //** @deprecated */
  1239. TEST(Imgproc_linearPolar, identity)
  1240. {
  1241. const int N = 33;
  1242. Mat in(N, N, CV_8UC3, Scalar(255, 0, 0));
  1243. in(cv::Rect(N/3, N/3, N/3, N/3)).setTo(Scalar::all(255));
  1244. cv::blur(in, in, Size(5, 5));
  1245. cv::blur(in, in, Size(5, 5));
  1246. Mat src = in.clone();
  1247. Mat dst;
  1248. Rect roi = Rect(0, 0, in.cols - ((N+19)/20), in.rows);
  1249. for (int i = 1; i <= 5; i++)
  1250. {
  1251. linearPolar(src, dst,
  1252. Point2f((N-1) * 0.5f, (N-1) * 0.5f), N * 0.5f,
  1253. CV_WARP_FILL_OUTLIERS | CV_INTER_LINEAR | CV_WARP_INVERSE_MAP);
  1254. linearPolar(dst, src,
  1255. Point2f((N-1) * 0.5f, (N-1) * 0.5f), N * 0.5f,
  1256. CV_WARP_FILL_OUTLIERS | CV_INTER_LINEAR);
  1257. double psnr = cvtest::PSNR(in(roi), src(roi));
  1258. EXPECT_LE(25, psnr) << "iteration=" << i;
  1259. }
  1260. #if 0
  1261. Mat all(N*2+2,N*2+2, src.type(), Scalar(0,0,255));
  1262. in.copyTo(all(Rect(0,0,N,N)));
  1263. src.copyTo(all(Rect(0,N+1,N,N)));
  1264. src.copyTo(all(Rect(N+1,0,N,N)));
  1265. dst.copyTo(all(Rect(N+1,N+1,N,N)));
  1266. imwrite("linearPolar.png", all);
  1267. imshow("input", in); imshow("result", dst); imshow("restore", src); imshow("all", all);
  1268. cv::waitKey();
  1269. #endif
  1270. }
  1271. //** @deprecated */
  1272. TEST(Imgproc_logPolar, identity)
  1273. {
  1274. const int N = 33;
  1275. Mat in(N, N, CV_8UC3, Scalar(255, 0, 0));
  1276. in(cv::Rect(N/3, N/3, N/3, N/3)).setTo(Scalar::all(255));
  1277. cv::blur(in, in, Size(5, 5));
  1278. cv::blur(in, in, Size(5, 5));
  1279. Mat src = in.clone();
  1280. Mat dst;
  1281. Rect roi = Rect(0, 0, in.cols - ((N+19)/20), in.rows);
  1282. double M = N/log(N * 0.5f);
  1283. for (int i = 1; i <= 5; i++)
  1284. {
  1285. logPolar(src, dst,
  1286. Point2f((N-1) * 0.5f, (N-1) * 0.5f), M,
  1287. CV_WARP_FILL_OUTLIERS | CV_INTER_LINEAR | CV_WARP_INVERSE_MAP);
  1288. logPolar(dst, src,
  1289. Point2f((N-1) * 0.5f, (N-1) * 0.5f), M,
  1290. CV_WARP_FILL_OUTLIERS | CV_INTER_LINEAR);
  1291. double psnr = cvtest::PSNR(in(roi), src(roi));
  1292. EXPECT_LE(25, psnr) << "iteration=" << i;
  1293. }
  1294. #if 0
  1295. Mat all(N*2+2,N*2+2, src.type(), Scalar(0,0,255));
  1296. in.copyTo(all(Rect(0,0,N,N)));
  1297. src.copyTo(all(Rect(0,N+1,N,N)));
  1298. src.copyTo(all(Rect(N+1,0,N,N)));
  1299. dst.copyTo(all(Rect(N+1,N+1,N,N)));
  1300. imwrite("logPolar.png", all);
  1301. imshow("input", in); imshow("result", dst); imshow("restore", src); imshow("all", all);
  1302. cv::waitKey();
  1303. #endif
  1304. }
  1305. TEST(Imgproc_warpPolar, identity)
  1306. {
  1307. const int N = 33;
  1308. Mat in(N, N, CV_8UC3, Scalar(255, 0, 0));
  1309. in(cv::Rect(N / 3, N / 3, N / 3, N / 3)).setTo(Scalar::all(255));
  1310. cv::blur(in, in, Size(5, 5));
  1311. cv::blur(in, in, Size(5, 5));
  1312. Mat src = in.clone();
  1313. Mat dst;
  1314. Rect roi = Rect(0, 0, in.cols - ((N + 19) / 20), in.rows);
  1315. Point2f center = Point2f((N - 1) * 0.5f, (N - 1) * 0.5f);
  1316. double radius = N * 0.5;
  1317. int flags = CV_WARP_FILL_OUTLIERS | CV_INTER_LINEAR;
  1318. // test linearPolar
  1319. for (int ki = 1; ki <= 5; ki++)
  1320. {
  1321. warpPolar(src, dst, src.size(), center, radius, flags + WARP_POLAR_LINEAR + CV_WARP_INVERSE_MAP);
  1322. warpPolar(dst, src, src.size(), center, radius, flags + WARP_POLAR_LINEAR);
  1323. double psnr = cv::PSNR(in(roi), src(roi));
  1324. EXPECT_LE(25, psnr) << "iteration=" << ki;
  1325. }
  1326. // test logPolar
  1327. src = in.clone();
  1328. for (int ki = 1; ki <= 5; ki++)
  1329. {
  1330. warpPolar(src, dst, src.size(),center, radius, flags + WARP_POLAR_LOG + CV_WARP_INVERSE_MAP );
  1331. warpPolar(dst, src, src.size(),center, radius, flags + WARP_POLAR_LOG);
  1332. double psnr = cv::PSNR(in(roi), src(roi));
  1333. EXPECT_LE(25, psnr) << "iteration=" << ki;
  1334. }
  1335. #if 0
  1336. Mat all(N*2+2,N*2+2, src.type(), Scalar(0,0,255));
  1337. in.copyTo(all(Rect(0,0,N,N)));
  1338. src.copyTo(all(Rect(0,N+1,N,N)));
  1339. src.copyTo(all(Rect(N+1,0,N,N)));
  1340. dst.copyTo(all(Rect(N+1,N+1,N,N)));
  1341. imwrite("linearPolar.png", all);
  1342. imshow("input", in); imshow("result", dst); imshow("restore", src); imshow("all", all);
  1343. cv::waitKey();
  1344. #endif
  1345. }
  1346. }} // namespace
  1347. /* End of file. */