test_imgwarp_strict.cpp 42 KB

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