test_imgwarp_strict.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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. void __wrap_printf_func(const char* fmt, ...)
  44. {
  45. va_list args;
  46. va_start(args, fmt);
  47. char buffer[256];
  48. vsnprintf (buffer, sizeof(buffer), fmt, args);
  49. cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, buffer);
  50. va_end(args);
  51. }
  52. #define PRINT_TO_LOG __wrap_printf_func
  53. #define SHOW_IMAGE
  54. #undef SHOW_IMAGE
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. // ImageWarpBaseTest
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. class CV_ImageWarpBaseTest :
  59. public cvtest::BaseTest
  60. {
  61. public:
  62. enum { cell_size = 10 };
  63. CV_ImageWarpBaseTest();
  64. virtual ~CV_ImageWarpBaseTest();
  65. virtual void run(int);
  66. protected:
  67. virtual void generate_test_data();
  68. virtual void run_func() = 0;
  69. virtual void run_reference_func() = 0;
  70. virtual float get_success_error_level(int _interpolation, int _depth) const;
  71. virtual void validate_results() const;
  72. virtual void prepare_test_data_for_reference_func();
  73. Size randSize(RNG& rng) const;
  74. String interpolation_to_string(int inter_type) const;
  75. int interpolation;
  76. Mat src;
  77. Mat dst;
  78. Mat reference_dst;
  79. };
  80. CV_ImageWarpBaseTest::CV_ImageWarpBaseTest() :
  81. BaseTest(), interpolation(-1),
  82. src(), dst(), reference_dst()
  83. {
  84. test_case_count = 40;
  85. ts->set_failed_test_info(cvtest::TS::OK);
  86. }
  87. CV_ImageWarpBaseTest::~CV_ImageWarpBaseTest()
  88. {
  89. }
  90. String CV_ImageWarpBaseTest::interpolation_to_string(int inter) const
  91. {
  92. bool inverse = (inter & WARP_INVERSE_MAP) != 0;
  93. inter &= ~WARP_INVERSE_MAP;
  94. String str;
  95. if (inter == INTER_NEAREST)
  96. str = "INTER_NEAREST";
  97. else if (inter == INTER_LINEAR)
  98. str = "INTER_LINEAR";
  99. else if (inter == INTER_LINEAR_EXACT)
  100. str = "INTER_LINEAR_EXACT";
  101. else if (inter == INTER_AREA)
  102. str = "INTER_AREA";
  103. else if (inter == INTER_CUBIC)
  104. str = "INTER_CUBIC";
  105. else if (inter == INTER_LANCZOS4)
  106. str = "INTER_LANCZOS4";
  107. else if (inter == INTER_LANCZOS4 + 1)
  108. str = "INTER_AREA_FAST";
  109. if (inverse)
  110. str += " | WARP_INVERSE_MAP";
  111. return str.empty() ? "Unsupported/Unknown interpolation type" : str;
  112. }
  113. Size CV_ImageWarpBaseTest::randSize(RNG& rng) const
  114. {
  115. Size size;
  116. size.width = static_cast<int>(std::exp(rng.uniform(1.0f, 7.0f)));
  117. size.height = static_cast<int>(std::exp(rng.uniform(1.0f, 7.0f)));
  118. return size;
  119. }
  120. void CV_ImageWarpBaseTest::generate_test_data()
  121. {
  122. RNG& rng = ts->get_rng();
  123. // generating the src matrix structure
  124. Size ssize = randSize(rng), dsize;
  125. int depth = rng.uniform(0, CV_64F);
  126. while (depth == CV_8S || depth == CV_32S)
  127. depth = rng.uniform(0, CV_64F);
  128. int cn = rng.uniform(1, 4);
  129. src.create(ssize, CV_MAKE_TYPE(depth, cn));
  130. // generating the src matrix
  131. int x, y;
  132. if (cvtest::randInt(rng) % 2)
  133. {
  134. for (y = 0; y < ssize.height; y += cell_size)
  135. for (x = 0; x < ssize.width; x += cell_size)
  136. rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
  137. std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), cv::FILLED);
  138. }
  139. else
  140. {
  141. src = Scalar::all(255);
  142. for (y = cell_size; y < src.rows; y += cell_size)
  143. line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
  144. for (x = cell_size; x < src.cols; x += cell_size)
  145. line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
  146. }
  147. // generating an interpolation type
  148. interpolation = rng.uniform(0, cv::INTER_LANCZOS4 + 1);
  149. // generating the dst matrix structure
  150. double scale_x, scale_y;
  151. if (interpolation == INTER_AREA)
  152. {
  153. bool area_fast = rng.uniform(0., 1.) > 0.5;
  154. if (area_fast)
  155. {
  156. scale_x = rng.uniform(2, 5);
  157. scale_y = rng.uniform(2, 5);
  158. }
  159. else
  160. {
  161. scale_x = rng.uniform(1.0, 3.0);
  162. scale_y = rng.uniform(1.0, 3.0);
  163. }
  164. }
  165. else
  166. {
  167. scale_x = rng.uniform(0.4, 4.0);
  168. scale_y = rng.uniform(0.4, 4.0);
  169. }
  170. CV_Assert(scale_x > 0.0f && scale_y > 0.0f);
  171. dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
  172. dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
  173. dst = Mat::zeros(dsize, src.type());
  174. reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
  175. scale_x = src.cols / static_cast<double>(dst.cols);
  176. scale_y = src.rows / static_cast<double>(dst.rows);
  177. if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
  178. interpolation = INTER_LINEAR;
  179. }
  180. void CV_ImageWarpBaseTest::run(int)
  181. {
  182. for (int i = 0; i < test_case_count; ++i)
  183. {
  184. generate_test_data();
  185. run_func();
  186. run_reference_func();
  187. if (ts->get_err_code() < 0)
  188. break;
  189. validate_results();
  190. if (ts->get_err_code() < 0)
  191. break;
  192. ts->update_context(this, i, true);
  193. }
  194. ts->set_gtest_status();
  195. }
  196. float CV_ImageWarpBaseTest::get_success_error_level(int _interpolation, int) const
  197. {
  198. if (_interpolation == INTER_CUBIC)
  199. return 1.0f;
  200. else if (_interpolation == INTER_LANCZOS4)
  201. return 1.0f;
  202. else if (_interpolation == INTER_NEAREST)
  203. return 255.0f; // FIXIT: check is not reliable for Black/White (0/255) images
  204. else if (_interpolation == INTER_AREA)
  205. return 2.0f;
  206. else
  207. return 1.0f;
  208. }
  209. void CV_ImageWarpBaseTest::validate_results() const
  210. {
  211. Mat _dst;
  212. dst.convertTo(_dst, reference_dst.depth());
  213. Size dsize = dst.size(), ssize = src.size();
  214. int cn = _dst.channels();
  215. dsize.width *= cn;
  216. float t = get_success_error_level(interpolation & INTER_MAX, dst.depth());
  217. for (int dy = 0; dy < dsize.height; ++dy)
  218. {
  219. const float* rD = reference_dst.ptr<float>(dy);
  220. const float* D = _dst.ptr<float>(dy);
  221. for (int dx = 0; dx < dsize.width; ++dx)
  222. if (fabs(rD[dx] - D[dx]) > t &&
  223. // fabs(rD[dx] - D[dx]) < 250.0f &&
  224. rD[dx] <= 255.0f && D[dx] <= 255.0f && rD[dx] >= 0.0f && D[dx] >= 0.0f)
  225. {
  226. PRINT_TO_LOG("\nNorm of the difference: %lf\n", cvtest::norm(reference_dst, _dst, NORM_INF));
  227. PRINT_TO_LOG("Error in (dx, dy): (%d, %d)\n", dx / cn + 1, dy + 1);
  228. PRINT_TO_LOG("Tuple (rD, D): (%f, %f)\n", rD[dx], D[dx]);
  229. PRINT_TO_LOG("Dsize: (%d, %d)\n", dsize.width / cn, dsize.height);
  230. PRINT_TO_LOG("Ssize: (%d, %d)\n", src.cols, src.rows);
  231. double scale_x = static_cast<double>(ssize.width) / dsize.width;
  232. double scale_y = static_cast<double>(ssize.height) / dsize.height;
  233. bool area_fast = interpolation == INTER_AREA &&
  234. fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
  235. fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
  236. if (area_fast)
  237. {
  238. scale_y = cvRound(scale_y);
  239. scale_x = cvRound(scale_x);
  240. }
  241. PRINT_TO_LOG("Interpolation: %s\n", interpolation_to_string(area_fast ? INTER_LANCZOS4 + 1 : interpolation).c_str());
  242. PRINT_TO_LOG("Scale (x, y): (%lf, %lf)\n", scale_x, scale_y);
  243. PRINT_TO_LOG("Elemsize: %d\n", src.elemSize1());
  244. PRINT_TO_LOG("Channels: %d\n", cn);
  245. #ifdef SHOW_IMAGE
  246. const std::string w1("OpenCV impl (run func)"), w2("Reference func"), w3("Src image"), w4("Diff");
  247. namedWindow(w1, cv::WINDOW_KEEPRATIO);
  248. namedWindow(w2, cv::WINDOW_KEEPRATIO);
  249. namedWindow(w3, cv::WINDOW_KEEPRATIO);
  250. namedWindow(w4, cv::WINDOW_KEEPRATIO);
  251. Mat diff;
  252. absdiff(reference_dst, _dst, diff);
  253. imshow(w1, dst);
  254. imshow(w2, reference_dst);
  255. imshow(w3, src);
  256. imshow(w4, diff);
  257. waitKey();
  258. #endif
  259. const int radius = 3;
  260. int rmin = MAX(dy - radius, 0), rmax = MIN(dy + radius, dsize.height);
  261. int cmin = MAX(dx / cn - radius, 0), cmax = MIN(dx / cn + radius, dsize.width);
  262. std::cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
  263. std::cout << "reference result:\n" << reference_dst(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
  264. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  265. return;
  266. }
  267. }
  268. }
  269. void CV_ImageWarpBaseTest::prepare_test_data_for_reference_func()
  270. {
  271. if (src.depth() != CV_32F)
  272. {
  273. Mat tmp;
  274. src.convertTo(tmp, CV_32F);
  275. src = tmp;
  276. }
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  279. // Resize
  280. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  281. class CV_Resize_Test :
  282. public CV_ImageWarpBaseTest
  283. {
  284. public:
  285. CV_Resize_Test();
  286. virtual ~CV_Resize_Test();
  287. protected:
  288. virtual void generate_test_data();
  289. virtual void run_func();
  290. virtual void run_reference_func();
  291. private:
  292. double scale_x;
  293. double scale_y;
  294. bool area_fast;
  295. void resize_generic();
  296. void resize_area();
  297. double getWeight(double a, double b, int x);
  298. typedef std::vector<std::pair<int, double> > dim;
  299. void generate_buffer(double scale, dim& _dim);
  300. void resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim);
  301. };
  302. CV_Resize_Test::CV_Resize_Test() :
  303. CV_ImageWarpBaseTest(), scale_x(),
  304. scale_y(), area_fast(false)
  305. {
  306. }
  307. CV_Resize_Test::~CV_Resize_Test()
  308. {
  309. }
  310. namespace
  311. {
  312. void interpolateLinear(float x, float* coeffs)
  313. {
  314. coeffs[0] = 1.f - x;
  315. coeffs[1] = x;
  316. }
  317. void interpolateCubic(float x, float* coeffs)
  318. {
  319. const float A = -0.75f;
  320. coeffs[0] = ((A*(x + 1) - 5*A)*(x + 1) + 8*A)*(x + 1) - 4*A;
  321. coeffs[1] = ((A + 2)*x - (A + 3))*x*x + 1;
  322. coeffs[2] = ((A + 2)*(1 - x) - (A + 3))*(1 - x)*(1 - x) + 1;
  323. coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
  324. }
  325. void interpolateLanczos4(float x, float* coeffs)
  326. {
  327. static const double s45 = 0.70710678118654752440084436210485;
  328. static const double cs[][2]=
  329. {{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45}, {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
  330. if( x < FLT_EPSILON )
  331. {
  332. for( int i = 0; i < 8; i++ )
  333. coeffs[i] = 0;
  334. coeffs[3] = 1;
  335. return;
  336. }
  337. float sum = 0;
  338. double y0=-(x+3)*CV_PI*0.25, s0 = sin(y0), c0=cos(y0);
  339. for(int i = 0; i < 8; i++ )
  340. {
  341. double y = -(x+3-i)*CV_PI*0.25;
  342. coeffs[i] = (float)((cs[i][0]*s0 + cs[i][1]*c0)/(y*y));
  343. sum += coeffs[i];
  344. }
  345. sum = 1.f/sum;
  346. for(int i = 0; i < 8; i++ )
  347. coeffs[i] *= sum;
  348. }
  349. typedef void (*interpolate_method)(float x, float* coeffs);
  350. interpolate_method inter_array[] = { &interpolateLinear, &interpolateCubic, &interpolateLanczos4 };
  351. }
  352. void CV_Resize_Test::generate_test_data()
  353. {
  354. RNG& rng = ts->get_rng();
  355. // generating the src matrix structure
  356. Size ssize = randSize(rng), dsize;
  357. int depth = rng.uniform(0, CV_64F);
  358. while (depth == CV_8S || depth == CV_32S)
  359. depth = rng.uniform(0, CV_64F);
  360. int cn = rng.uniform(1, 4);
  361. src.create(ssize, CV_MAKE_TYPE(depth, cn));
  362. // generating the src matrix
  363. int x, y;
  364. if (cvtest::randInt(rng) % 2)
  365. {
  366. for (y = 0; y < ssize.height; y += cell_size)
  367. for (x = 0; x < ssize.width; x += cell_size)
  368. rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
  369. std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), cv::FILLED);
  370. }
  371. else
  372. {
  373. src = Scalar::all(255);
  374. for (y = cell_size; y < src.rows; y += cell_size)
  375. line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
  376. for (x = cell_size; x < src.cols; x += cell_size)
  377. line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
  378. }
  379. // generating an interpolation type
  380. interpolation = rng.uniform(0, cv::INTER_MAX - 1);
  381. // generating the dst matrix structure
  382. if (interpolation == INTER_AREA)
  383. {
  384. area_fast = rng.uniform(0., 1.) > 0.5;
  385. if (area_fast)
  386. {
  387. scale_x = rng.uniform(2, 5);
  388. scale_y = rng.uniform(2, 5);
  389. }
  390. else
  391. {
  392. scale_x = rng.uniform(1.0, 3.0);
  393. scale_y = rng.uniform(1.0, 3.0);
  394. }
  395. }
  396. else
  397. {
  398. scale_x = rng.uniform(0.4, 4.0);
  399. scale_y = rng.uniform(0.4, 4.0);
  400. }
  401. CV_Assert(scale_x > 0.0f && scale_y > 0.0f);
  402. dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
  403. dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
  404. dst = Mat::zeros(dsize, src.type());
  405. reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
  406. scale_x = src.cols / static_cast<double>(dst.cols);
  407. scale_y = src.rows / static_cast<double>(dst.rows);
  408. if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
  409. interpolation = INTER_LINEAR_EXACT;
  410. if (interpolation == INTER_LINEAR_EXACT && (depth == CV_32F || depth == CV_64F))
  411. interpolation = INTER_LINEAR;
  412. area_fast = interpolation == INTER_AREA &&
  413. fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
  414. fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
  415. if (area_fast)
  416. {
  417. scale_x = cvRound(scale_x);
  418. scale_y = cvRound(scale_y);
  419. }
  420. }
  421. void CV_Resize_Test::run_func()
  422. {
  423. cv::resize(src, dst, dst.size(), 0, 0, interpolation);
  424. }
  425. void CV_Resize_Test::run_reference_func()
  426. {
  427. CV_ImageWarpBaseTest::prepare_test_data_for_reference_func();
  428. if (interpolation == INTER_AREA)
  429. resize_area();
  430. else
  431. resize_generic();
  432. }
  433. double CV_Resize_Test::getWeight(double a, double b, int x)
  434. {
  435. double w = std::min(static_cast<double>(x + 1), b) - std::max(static_cast<double>(x), a);
  436. CV_Assert(w >= 0);
  437. return w;
  438. }
  439. void CV_Resize_Test::resize_area()
  440. {
  441. Size ssize = src.size(), dsize = reference_dst.size();
  442. CV_Assert(!ssize.empty() && !dsize.empty());
  443. int cn = src.channels();
  444. CV_Assert(scale_x >= 1.0 && scale_y >= 1.0);
  445. double fsy0 = 0, fsy1 = scale_y;
  446. for (int dy = 0; dy < dsize.height; ++dy)
  447. {
  448. float* yD = reference_dst.ptr<float>(dy);
  449. int isy0 = cvFloor(fsy0), isy1 = std::min(cvFloor(fsy1), ssize.height - 1);
  450. CV_Assert(isy1 <= ssize.height && isy0 < ssize.height);
  451. double fsx0 = 0, fsx1 = scale_x;
  452. for (int dx = 0; dx < dsize.width; ++dx)
  453. {
  454. float* xyD = yD + cn * dx;
  455. int isx0 = cvFloor(fsx0), isx1 = std::min(ssize.width - 1, cvFloor(fsx1));
  456. CV_Assert(isx1 <= ssize.width);
  457. CV_Assert(isx0 < ssize.width);
  458. // for each pixel of dst
  459. for (int r = 0; r < cn; ++r)
  460. {
  461. xyD[r] = 0.0f;
  462. double area = 0.0;
  463. for (int sy = isy0; sy <= isy1; ++sy)
  464. {
  465. const float* yS = src.ptr<float>(sy);
  466. for (int sx = isx0; sx <= isx1; ++sx)
  467. {
  468. double wy = getWeight(fsy0, fsy1, sy);
  469. double wx = getWeight(fsx0, fsx1, sx);
  470. double w = wx * wy;
  471. xyD[r] += static_cast<float>(yS[sx * cn + r] * w);
  472. area += w;
  473. }
  474. }
  475. CV_Assert(area != 0);
  476. // norming pixel
  477. xyD[r] = static_cast<float>(xyD[r] / area);
  478. }
  479. fsx1 = std::min((fsx0 = fsx1) + scale_x, static_cast<double>(ssize.width));
  480. }
  481. fsy1 = std::min((fsy0 = fsy1) + scale_y, static_cast<double>(ssize.height));
  482. }
  483. }
  484. // for interpolation type : INTER_LINEAR, INTER_LINEAR_EXACT, INTER_CUBIC, INTER_LANCZOS4
  485. void CV_Resize_Test::resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim)
  486. {
  487. Size dsize = _dst.size();
  488. int cn = _dst.channels();
  489. float* yD = _dst.ptr<float>(dy);
  490. if (interpolation == INTER_NEAREST)
  491. {
  492. const float* yS = _src.ptr<float>(dy);
  493. for (int dx = 0; dx < dsize.width; ++dx)
  494. {
  495. int isx = _dim[dx].first;
  496. const float* xyS = yS + isx * cn;
  497. float* xyD = yD + dx * cn;
  498. for (int r = 0; r < cn; ++r)
  499. xyD[r] = xyS[r];
  500. }
  501. }
  502. else if (interpolation == INTER_LINEAR || interpolation == INTER_LINEAR_EXACT || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4)
  503. {
  504. interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : interpolation == INTER_LINEAR_EXACT ? 5 : 1)];
  505. size_t elemsize = _src.elemSize();
  506. int ofs = 0, ksize = 2;
  507. if (interpolation == INTER_CUBIC)
  508. ofs = 1, ksize = 4;
  509. else if (interpolation == INTER_LANCZOS4)
  510. ofs = 3, ksize = 8;
  511. Mat _extended_src_row(1, _src.cols + ksize * 2, _src.type());
  512. const uchar* srow = _src.ptr(dy);
  513. memcpy(_extended_src_row.ptr() + elemsize * ksize, srow, _src.step);
  514. for (int k = 0; k < ksize; ++k)
  515. {
  516. memcpy(_extended_src_row.ptr() + k * elemsize, srow, elemsize);
  517. memcpy(_extended_src_row.ptr() + (ksize + k) * elemsize + _src.step, srow + _src.step - elemsize, elemsize);
  518. }
  519. for (int dx = 0; dx < dsize.width; ++dx)
  520. {
  521. int isx = _dim[dx].first;
  522. double fsx = _dim[dx].second;
  523. float *xyD = yD + dx * cn;
  524. const float* xyS = _extended_src_row.ptr<float>(0) + (isx + ksize - ofs) * cn;
  525. float w[8];
  526. inter_func(static_cast<float>(fsx), w);
  527. for (int r = 0; r < cn; ++r)
  528. {
  529. xyD[r] = 0;
  530. for (int k = 0; k < ksize; ++k)
  531. xyD[r] += w[k] * xyS[k * cn + r];
  532. }
  533. }
  534. }
  535. else
  536. CV_Assert(0);
  537. }
  538. void CV_Resize_Test::generate_buffer(double scale, dim& _dim)
  539. {
  540. size_t length = _dim.size();
  541. for (size_t dx = 0; dx < length; ++dx)
  542. {
  543. double fsx = scale * (dx + 0.5) - 0.5;
  544. int isx = cvFloor(fsx);
  545. _dim[dx] = std::make_pair(isx, fsx - isx);
  546. }
  547. }
  548. void CV_Resize_Test::resize_generic()
  549. {
  550. Size dsize = reference_dst.size(), ssize = src.size();
  551. CV_Assert(!dsize.empty() && !ssize.empty());
  552. dim dims[] = { dim(dsize.width), dim(dsize.height) };
  553. if (interpolation == INTER_NEAREST)
  554. {
  555. for (int dx = 0; dx < dsize.width; ++dx)
  556. dims[0][dx].first = std::min(cvFloor(dx * scale_x), ssize.width - 1);
  557. for (int dy = 0; dy < dsize.height; ++dy)
  558. dims[1][dy].first = std::min(cvFloor(dy * scale_y), ssize.height - 1);
  559. }
  560. else
  561. {
  562. generate_buffer(scale_x, dims[0]);
  563. generate_buffer(scale_y, dims[1]);
  564. }
  565. Mat tmp(ssize.height, dsize.width, reference_dst.type());
  566. for (int dy = 0; dy < tmp.rows; ++dy)
  567. resize_1d(src, tmp, dy, dims[0]);
  568. cv::Mat tmp_t(tmp.cols, tmp.rows, tmp.type());
  569. cvtest::transpose(tmp, tmp_t);
  570. cv::Mat reference_dst_t(reference_dst.cols, reference_dst.rows, reference_dst.type());
  571. cvtest::transpose(reference_dst, reference_dst_t);
  572. for (int dy = 0; dy < tmp_t.rows; ++dy)
  573. resize_1d(tmp_t, reference_dst_t, dy, dims[1]);
  574. cvtest::transpose(reference_dst_t, reference_dst);
  575. }
  576. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  577. // remap
  578. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  579. class CV_Remap_Test :
  580. public CV_ImageWarpBaseTest
  581. {
  582. public:
  583. CV_Remap_Test();
  584. virtual ~CV_Remap_Test();
  585. private:
  586. typedef void (CV_Remap_Test::*remap_func)(const Mat&, Mat&);
  587. protected:
  588. virtual void generate_test_data();
  589. virtual void prepare_test_data_for_reference_func();
  590. virtual void run_func();
  591. virtual void run_reference_func();
  592. Mat mapx, mapy;
  593. int borderType;
  594. Scalar borderValue;
  595. remap_func funcs[2];
  596. private:
  597. void remap_nearest(const Mat&, Mat&);
  598. void remap_generic(const Mat&, Mat&);
  599. void convert_maps();
  600. const char* borderType_to_string() const;
  601. virtual void validate_results() const;
  602. };
  603. CV_Remap_Test::CV_Remap_Test() :
  604. CV_ImageWarpBaseTest(), borderType(-1)
  605. {
  606. funcs[0] = &CV_Remap_Test::remap_nearest;
  607. funcs[1] = &CV_Remap_Test::remap_generic;
  608. }
  609. CV_Remap_Test::~CV_Remap_Test()
  610. {
  611. }
  612. void CV_Remap_Test::generate_test_data()
  613. {
  614. CV_ImageWarpBaseTest::generate_test_data();
  615. RNG& rng = ts->get_rng();
  616. borderType = rng.uniform(1, BORDER_WRAP);
  617. borderValue = Scalar::all(rng.uniform(0, 255));
  618. // generating the mapx, mapy matrices
  619. static const int mapx_types[] = { CV_16SC2, CV_32FC1, CV_32FC2 };
  620. mapx.create(dst.size(), mapx_types[rng.uniform(0, sizeof(mapx_types) / sizeof(int))]);
  621. mapy.release();
  622. const int n = std::min(std::min(src.cols, src.rows) / 10 + 1, 2);
  623. float _n = 0; //static_cast<float>(-n);
  624. switch (mapx.type())
  625. {
  626. case CV_16SC2:
  627. {
  628. MatIterator_<Vec2s> begin_x = mapx.begin<Vec2s>(), end_x = mapx.end<Vec2s>();
  629. for ( ; begin_x != end_x; ++begin_x)
  630. {
  631. (*begin_x)[0] = static_cast<short>(rng.uniform(static_cast<int>(_n), std::max(src.cols + n - 1, 0)));
  632. (*begin_x)[1] = static_cast<short>(rng.uniform(static_cast<int>(_n), std::max(src.rows + n - 1, 0)));
  633. }
  634. if (interpolation != INTER_NEAREST)
  635. {
  636. static const int mapy_types[] = { CV_16UC1, CV_16SC1 };
  637. mapy.create(dst.size(), mapy_types[rng.uniform(0, sizeof(mapy_types) / sizeof(int))]);
  638. switch (mapy.type())
  639. {
  640. case CV_16UC1:
  641. {
  642. MatIterator_<ushort> begin_y = mapy.begin<ushort>(), end_y = mapy.end<ushort>();
  643. for ( ; begin_y != end_y; ++begin_y)
  644. *begin_y = static_cast<ushort>(rng.uniform(0, 1024));
  645. }
  646. break;
  647. case CV_16SC1:
  648. {
  649. MatIterator_<short> begin_y = mapy.begin<short>(), end_y = mapy.end<short>();
  650. for ( ; begin_y != end_y; ++begin_y)
  651. *begin_y = static_cast<short>(rng.uniform(0, 1024));
  652. }
  653. break;
  654. }
  655. }
  656. }
  657. break;
  658. case CV_32FC1:
  659. {
  660. mapy.create(dst.size(), CV_32FC1);
  661. float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
  662. fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
  663. MatIterator_<float> begin_x = mapx.begin<float>(), end_x = mapx.end<float>();
  664. MatIterator_<float> begin_y = mapy.begin<float>();
  665. for ( ; begin_x != end_x; ++begin_x, ++begin_y)
  666. {
  667. *begin_x = rng.uniform(_n, fscols);
  668. *begin_y = rng.uniform(_n, fsrows);
  669. }
  670. }
  671. break;
  672. case CV_32FC2:
  673. {
  674. float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
  675. fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
  676. int width = mapx.cols << 1;
  677. for (int y = 0; y < mapx.rows; ++y)
  678. {
  679. float * ptr = mapx.ptr<float>(y);
  680. for (int x = 0; x < width; x += 2)
  681. {
  682. ptr[x] = rng.uniform(_n, fscols);
  683. ptr[x + 1] = rng.uniform(_n, fsrows);
  684. }
  685. }
  686. }
  687. break;
  688. default:
  689. CV_Assert(0);
  690. break;
  691. }
  692. }
  693. void CV_Remap_Test::run_func()
  694. {
  695. remap(src, dst, mapx, mapy, interpolation, borderType, borderValue);
  696. }
  697. void CV_Remap_Test::convert_maps()
  698. {
  699. if (mapx.type() != CV_16SC2)
  700. convertMaps(mapx.clone(), mapy.clone(), mapx, mapy, CV_16SC2, interpolation == INTER_NEAREST);
  701. else if (interpolation != INTER_NEAREST)
  702. if (mapy.type() != CV_16UC1)
  703. mapy.clone().convertTo(mapy, CV_16UC1);
  704. if (interpolation == INTER_NEAREST)
  705. mapy = Mat();
  706. CV_Assert(((interpolation == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16UC1 ||
  707. mapy.type() == CV_16SC1) && mapx.type() == CV_16SC2);
  708. }
  709. const char* CV_Remap_Test::borderType_to_string() const
  710. {
  711. if (borderType == BORDER_CONSTANT)
  712. return "BORDER_CONSTANT";
  713. if (borderType == BORDER_REPLICATE)
  714. return "BORDER_REPLICATE";
  715. if (borderType == BORDER_REFLECT)
  716. return "BORDER_REFLECT";
  717. if (borderType == BORDER_WRAP)
  718. return "BORDER_WRAP";
  719. if (borderType == BORDER_REFLECT_101)
  720. return "BORDER_REFLECT_101";
  721. return "Unsupported/Unknown border type";
  722. }
  723. void CV_Remap_Test::prepare_test_data_for_reference_func()
  724. {
  725. CV_ImageWarpBaseTest::prepare_test_data_for_reference_func();
  726. convert_maps();
  727. }
  728. void CV_Remap_Test::run_reference_func()
  729. {
  730. prepare_test_data_for_reference_func();
  731. if (interpolation == INTER_AREA)
  732. interpolation = INTER_LINEAR;
  733. int index = interpolation == INTER_NEAREST ? 0 : 1;
  734. (this->*funcs[index])(src, reference_dst);
  735. }
  736. void CV_Remap_Test::remap_nearest(const Mat& _src, Mat& _dst)
  737. {
  738. CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
  739. CV_Assert(mapx.type() == CV_16SC2 && mapy.empty());
  740. Size ssize = _src.size(), dsize = _dst.size();
  741. CV_Assert(!ssize.empty() && !dsize.empty());
  742. int cn = _src.channels();
  743. for (int dy = 0; dy < dsize.height; ++dy)
  744. {
  745. const short* yM = mapx.ptr<short>(dy);
  746. float* yD = _dst.ptr<float>(dy);
  747. for (int dx = 0; dx < dsize.width; ++dx)
  748. {
  749. float* xyD = yD + cn * dx;
  750. int sx = yM[dx * 2], sy = yM[dx * 2 + 1];
  751. if (sx >= 0 && sx < ssize.width && sy >= 0 && sy < ssize.height)
  752. {
  753. const float *xyS = _src.ptr<float>(sy) + sx * cn;
  754. for (int r = 0; r < cn; ++r)
  755. xyD[r] = xyS[r];
  756. }
  757. else if (borderType != BORDER_TRANSPARENT)
  758. {
  759. if (borderType == BORDER_CONSTANT)
  760. for (int r = 0; r < cn; ++r)
  761. xyD[r] = saturate_cast<float>(borderValue[r]);
  762. else
  763. {
  764. sx = borderInterpolate(sx, ssize.width, borderType);
  765. sy = borderInterpolate(sy, ssize.height, borderType);
  766. CV_Assert(sx >= 0 && sy >= 0 && sx < ssize.width && sy < ssize.height);
  767. const float *xyS = _src.ptr<float>(sy) + sx * cn;
  768. for (int r = 0; r < cn; ++r)
  769. xyD[r] = xyS[r];
  770. }
  771. }
  772. }
  773. }
  774. }
  775. void CV_Remap_Test::remap_generic(const Mat& _src, Mat& _dst)
  776. {
  777. CV_Assert(mapx.type() == CV_16SC2 && mapy.type() == CV_16UC1);
  778. int ksize = 2;
  779. if (interpolation == INTER_CUBIC)
  780. ksize = 4;
  781. else if (interpolation == INTER_LANCZOS4)
  782. ksize = 8;
  783. else if (interpolation != INTER_LINEAR)
  784. CV_Assert(0);
  785. int ofs = (ksize / 2) - 1;
  786. CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
  787. Size ssize = _src.size(), dsize = _dst.size();
  788. int cn = _src.channels(), width1 = std::max(ssize.width - ksize + 1, 0),
  789. height1 = std::max(ssize.height - ksize + 1, 0);
  790. float ix[8], w[16];
  791. interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
  792. for (int dy = 0; dy < dsize.height; ++dy)
  793. {
  794. const short* yMx = mapx.ptr<short>(dy);
  795. const ushort* yMy = mapy.ptr<ushort>(dy);
  796. float* yD = _dst.ptr<float>(dy);
  797. for (int dx = 0; dx < dsize.width; ++dx)
  798. {
  799. float* xyD = yD + dx * cn;
  800. float sx = yMx[dx * 2], sy = yMx[dx * 2 + 1];
  801. int isx = cvFloor(sx), isy = cvFloor(sy);
  802. inter_func((yMy[dx] & (INTER_TAB_SIZE - 1)) / static_cast<float>(INTER_TAB_SIZE), w);
  803. inter_func(((yMy[dx] >> INTER_BITS) & (INTER_TAB_SIZE - 1)) / static_cast<float>(INTER_TAB_SIZE), w + ksize);
  804. isx -= ofs;
  805. isy -= ofs;
  806. if (isx >= 0 && isx < width1 && isy >= 0 && isy < height1)
  807. {
  808. for (int r = 0; r < cn; ++r)
  809. {
  810. for (int y = 0; y < ksize; ++y)
  811. {
  812. const float* xyS = _src.ptr<float>(isy + y) + isx * cn;
  813. ix[y] = 0;
  814. for (int i = 0; i < ksize; ++i)
  815. ix[y] += w[i] * xyS[i * cn + r];
  816. }
  817. xyD[r] = 0;
  818. for (int i = 0; i < ksize; ++i)
  819. xyD[r] += w[ksize + i] * ix[i];
  820. }
  821. }
  822. else if (borderType != BORDER_TRANSPARENT)
  823. {
  824. int ar_x[8], ar_y[8];
  825. for (int k = 0; k < ksize; k++)
  826. {
  827. ar_x[k] = borderInterpolate(isx + k, ssize.width, borderType) * cn;
  828. ar_y[k] = borderInterpolate(isy + k, ssize.height, borderType);
  829. }
  830. for (int r = 0; r < cn; r++)
  831. {
  832. xyD[r] = 0;
  833. for (int i = 0; i < ksize; ++i)
  834. {
  835. ix[i] = 0;
  836. if (ar_y[i] >= 0)
  837. {
  838. const float* yS = _src.ptr<float>(ar_y[i]);
  839. for (int j = 0; j < ksize; ++j)
  840. ix[i] += saturate_cast<float>((ar_x[j] >= 0 ? yS[ar_x[j] + r] : borderValue[r]) * w[j]);
  841. }
  842. else
  843. for (int j = 0; j < ksize; ++j)
  844. ix[i] += saturate_cast<float>(borderValue[r] * w[j]);
  845. }
  846. for (int i = 0; i < ksize; ++i)
  847. xyD[r] += saturate_cast<float>(w[ksize + i] * ix[i]);
  848. }
  849. }
  850. }
  851. }
  852. }
  853. void CV_Remap_Test::validate_results() const
  854. {
  855. CV_ImageWarpBaseTest::validate_results();
  856. if (cvtest::TS::ptr()->get_err_code() == cvtest::TS::FAIL_BAD_ACCURACY)
  857. {
  858. PRINT_TO_LOG("BorderType: %s\n", borderType_to_string());
  859. PRINT_TO_LOG("BorderValue: (%f, %f, %f, %f)\n",
  860. borderValue[0], borderValue[1], borderValue[2], borderValue[3]);
  861. }
  862. }
  863. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  864. // warpAffine
  865. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  866. class CV_WarpAffine_Test :
  867. public CV_Remap_Test
  868. {
  869. public:
  870. CV_WarpAffine_Test();
  871. virtual ~CV_WarpAffine_Test();
  872. protected:
  873. virtual void generate_test_data();
  874. virtual float get_success_error_level(int _interpolation, int _depth) const;
  875. virtual void run_func();
  876. virtual void run_reference_func();
  877. Mat M;
  878. private:
  879. void warpAffine(const Mat&, Mat&);
  880. };
  881. CV_WarpAffine_Test::CV_WarpAffine_Test() :
  882. CV_Remap_Test()
  883. {
  884. }
  885. CV_WarpAffine_Test::~CV_WarpAffine_Test()
  886. {
  887. }
  888. void CV_WarpAffine_Test::generate_test_data()
  889. {
  890. CV_Remap_Test::generate_test_data();
  891. RNG& rng = ts->get_rng();
  892. // generating the M 2x3 matrix
  893. static const int depths[] = { CV_32FC1, CV_64FC1 };
  894. // generating 2d matrix
  895. M = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f),
  896. rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
  897. int depth = depths[rng.uniform(0, sizeof(depths) / sizeof(depths[0]))];
  898. if (M.depth() != depth)
  899. {
  900. Mat tmp;
  901. M.convertTo(tmp, depth);
  902. M = tmp;
  903. }
  904. // warp_matrix is inverse
  905. if (rng.uniform(0., 1.) > 0)
  906. interpolation |= cv::WARP_INVERSE_MAP;
  907. }
  908. void CV_WarpAffine_Test::run_func()
  909. {
  910. cv::warpAffine(src, dst, M, dst.size(), interpolation, borderType, borderValue);
  911. }
  912. float CV_WarpAffine_Test::get_success_error_level(int _interpolation, int _depth) const
  913. {
  914. return _depth == CV_8U ? 0 : CV_ImageWarpBaseTest::get_success_error_level(_interpolation, _depth);
  915. }
  916. void CV_WarpAffine_Test::run_reference_func()
  917. {
  918. Mat tmp = Mat::zeros(dst.size(), dst.type());
  919. warpAffine(src, tmp);
  920. tmp.convertTo(reference_dst, reference_dst.depth());
  921. }
  922. void CV_WarpAffine_Test::warpAffine(const Mat& _src, Mat& _dst)
  923. {
  924. Size dsize = _dst.size();
  925. CV_Assert(!_src.empty());
  926. CV_Assert(!dsize.empty());
  927. CV_Assert(_src.type() == _dst.type());
  928. Mat tM;
  929. M.convertTo(tM, CV_64F);
  930. int inter = interpolation & INTER_MAX;
  931. if (inter == INTER_AREA)
  932. inter = INTER_LINEAR;
  933. mapx.create(dsize, CV_16SC2);
  934. if (inter != INTER_NEAREST)
  935. mapy.create(dsize, CV_16SC1);
  936. else
  937. mapy = Mat();
  938. if (!(interpolation & cv::WARP_INVERSE_MAP))
  939. invertAffineTransform(tM.clone(), tM);
  940. const int AB_BITS = MAX(10, (int)INTER_BITS);
  941. const int AB_SCALE = 1 << AB_BITS;
  942. int round_delta = (inter == INTER_NEAREST) ? AB_SCALE / 2 : (AB_SCALE / INTER_TAB_SIZE / 2);
  943. const softdouble* data_tM = tM.ptr<softdouble>(0);
  944. for (int dy = 0; dy < dsize.height; ++dy)
  945. {
  946. short* yM = mapx.ptr<short>(dy);
  947. for (int dx = 0; dx < dsize.width; ++dx, yM += 2)
  948. {
  949. int v1 = saturate_cast<int>(saturate_cast<int>(data_tM[0] * dx * AB_SCALE) +
  950. saturate_cast<int>((data_tM[1] * dy + data_tM[2]) * AB_SCALE) + round_delta),
  951. v2 = saturate_cast<int>(saturate_cast<int>(data_tM[3] * dx * AB_SCALE) +
  952. saturate_cast<int>((data_tM[4] * dy + data_tM[5]) * AB_SCALE) + round_delta);
  953. v1 >>= AB_BITS - INTER_BITS;
  954. v2 >>= AB_BITS - INTER_BITS;
  955. yM[0] = saturate_cast<short>(v1 >> INTER_BITS);
  956. yM[1] = saturate_cast<short>(v2 >> INTER_BITS);
  957. if (inter != INTER_NEAREST)
  958. mapy.ptr<short>(dy)[dx] = ((v2 & (INTER_TAB_SIZE - 1)) * INTER_TAB_SIZE + (v1 & (INTER_TAB_SIZE - 1)));
  959. }
  960. }
  961. CV_Assert(mapx.type() == CV_16SC2 && ((inter == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16SC1));
  962. cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
  963. }
  964. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  965. // warpPerspective
  966. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  967. class CV_WarpPerspective_Test :
  968. public CV_WarpAffine_Test
  969. {
  970. public:
  971. CV_WarpPerspective_Test();
  972. virtual ~CV_WarpPerspective_Test();
  973. protected:
  974. virtual void generate_test_data();
  975. virtual float get_success_error_level(int _interpolation, int _depth) const;
  976. virtual void run_func();
  977. virtual void run_reference_func();
  978. private:
  979. void warpPerspective(const Mat&, Mat&);
  980. };
  981. CV_WarpPerspective_Test::CV_WarpPerspective_Test() :
  982. CV_WarpAffine_Test()
  983. {
  984. }
  985. CV_WarpPerspective_Test::~CV_WarpPerspective_Test()
  986. {
  987. }
  988. void CV_WarpPerspective_Test::generate_test_data()
  989. {
  990. CV_Remap_Test::generate_test_data();
  991. // generating the M 3x3 matrix
  992. RNG& rng = ts->get_rng();
  993. float cols = static_cast<float>(src.cols), rows = static_cast<float>(src.rows);
  994. Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
  995. Point2f dp[] = { Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
  996. Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
  997. Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
  998. Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)) };
  999. M = getPerspectiveTransform(sp, dp);
  1000. static const int depths[] = { CV_32F, CV_64F };
  1001. int depth = depths[rng.uniform(0, 2)];
  1002. M.clone().convertTo(M, depth);
  1003. }
  1004. void CV_WarpPerspective_Test::run_func()
  1005. {
  1006. cv::warpPerspective(src, dst, M, dst.size(), interpolation, borderType, borderValue);
  1007. }
  1008. float CV_WarpPerspective_Test::get_success_error_level(int _interpolation, int _depth) const
  1009. {
  1010. return CV_ImageWarpBaseTest::get_success_error_level(_interpolation, _depth);
  1011. }
  1012. void CV_WarpPerspective_Test::run_reference_func()
  1013. {
  1014. Mat tmp = Mat::zeros(dst.size(), dst.type());
  1015. warpPerspective(src, tmp);
  1016. tmp.convertTo(reference_dst, reference_dst.depth());
  1017. }
  1018. void CV_WarpPerspective_Test::warpPerspective(const Mat& _src, Mat& _dst)
  1019. {
  1020. Size ssize = _src.size(), dsize = _dst.size();
  1021. CV_Assert(!ssize.empty());
  1022. CV_Assert(!dsize.empty());
  1023. CV_Assert(_src.type() == _dst.type());
  1024. if (M.depth() != CV_64F)
  1025. {
  1026. Mat tmp;
  1027. M.convertTo(tmp, CV_64F);
  1028. M = tmp;
  1029. }
  1030. if (!(interpolation & cv::WARP_INVERSE_MAP))
  1031. {
  1032. Mat tmp;
  1033. invert(M, tmp);
  1034. M = tmp;
  1035. }
  1036. int inter = interpolation & INTER_MAX;
  1037. if (inter == INTER_AREA)
  1038. inter = INTER_LINEAR;
  1039. mapx.create(dsize, CV_16SC2);
  1040. if (inter != INTER_NEAREST)
  1041. mapy.create(dsize, CV_16SC1);
  1042. else
  1043. mapy = Mat();
  1044. double* tM = M.ptr<double>(0);
  1045. for (int dy = 0; dy < dsize.height; ++dy)
  1046. {
  1047. short* yMx = mapx.ptr<short>(dy);
  1048. for (int dx = 0; dx < dsize.width; ++dx, yMx += 2)
  1049. {
  1050. double den = tM[6] * dx + tM[7] * dy + tM[8];
  1051. den = den ? 1.0 / den : 0.0;
  1052. if (inter == INTER_NEAREST)
  1053. {
  1054. yMx[0] = saturate_cast<short>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
  1055. yMx[1] = saturate_cast<short>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
  1056. continue;
  1057. }
  1058. den *= static_cast<double>(INTER_TAB_SIZE);
  1059. int v0 = saturate_cast<int>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
  1060. int v1 = saturate_cast<int>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
  1061. yMx[0] = saturate_cast<short>(v0 >> INTER_BITS);
  1062. yMx[1] = saturate_cast<short>(v1 >> INTER_BITS);
  1063. mapy.ptr<short>(dy)[dx] = saturate_cast<short>((v1 & (INTER_TAB_SIZE - 1)) *
  1064. INTER_TAB_SIZE + (v0 & (INTER_TAB_SIZE - 1)));
  1065. }
  1066. }
  1067. CV_Assert(mapx.type() == CV_16SC2 && ((inter == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16SC1));
  1068. cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
  1069. }
  1070. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  1071. // Tests
  1072. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  1073. TEST(Imgproc_Resize_Test, accuracy) { CV_Resize_Test test; test.safe_run(); }
  1074. TEST(Imgproc_Remap_Test, accuracy) { CV_Remap_Test test; test.safe_run(); }
  1075. TEST(Imgproc_WarpAffine_Test, accuracy) { CV_WarpAffine_Test test; test.safe_run(); }
  1076. TEST(Imgproc_WarpPerspective_Test, accuracy) { CV_WarpPerspective_Test test; test.safe_run(); }
  1077. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  1078. #ifdef OPENCV_TEST_BIGDATA
  1079. CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_LINEAR_EXACT, INTER_CUBIC, INTER_AREA)
  1080. class Imgproc_Resize :
  1081. public ::testing::TestWithParam<Interpolation>
  1082. {
  1083. public:
  1084. virtual void SetUp()
  1085. {
  1086. inter = GetParam();
  1087. }
  1088. protected:
  1089. int inter;
  1090. };
  1091. TEST_P(Imgproc_Resize, BigSize)
  1092. {
  1093. cv::Mat src(46342, 46342, CV_8UC3, cv::Scalar::all(10)), dst;
  1094. ASSERT_FALSE(src.empty());
  1095. ASSERT_NO_THROW(cv::resize(src, dst, cv::Size(), 0.5, 0.5, inter));
  1096. }
  1097. INSTANTIATE_TEST_CASE_P(Imgproc, Imgproc_Resize, Interpolation::all());
  1098. #endif
  1099. }} // namespace