perf_warp.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "perf_precomp.hpp"
  5. namespace opencv_test {
  6. enum{HALF_SIZE=0, UPSIDE_DOWN, REFLECTION_X, REFLECTION_BOTH};
  7. CV_ENUM(BorderMode, BORDER_CONSTANT, BORDER_REPLICATE)
  8. CV_ENUM(InterType, INTER_NEAREST, INTER_LINEAR)
  9. CV_ENUM(RemapMode, HALF_SIZE, UPSIDE_DOWN, REFLECTION_X, REFLECTION_BOTH)
  10. typedef TestBaseWithParam< tuple<Size, InterType, BorderMode> > TestWarpAffine;
  11. typedef TestBaseWithParam< tuple<Size, InterType, BorderMode> > TestWarpPerspective;
  12. typedef TestBaseWithParam< tuple<Size, InterType, BorderMode, MatType> > TestWarpPerspectiveNear_t;
  13. typedef TestBaseWithParam< tuple<MatType, Size, InterType, BorderMode, RemapMode> > TestRemap;
  14. void update_map(const Mat& src, Mat& map_x, Mat& map_y, const int remapMode );
  15. PERF_TEST_P( TestWarpAffine, WarpAffine,
  16. Combine(
  17. Values( szVGA, sz720p, sz1080p ),
  18. InterType::all(),
  19. BorderMode::all()
  20. )
  21. )
  22. {
  23. Size sz, szSrc(512, 512);
  24. int borderMode, interType;
  25. sz = get<0>(GetParam());
  26. interType = get<1>(GetParam());
  27. borderMode = get<2>(GetParam());
  28. Scalar borderColor = Scalar::all(150);
  29. Mat src(szSrc,CV_8UC4), dst(sz, CV_8UC4);
  30. cvtest::fillGradient(src);
  31. if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
  32. Mat warpMat = getRotationMatrix2D(Point2f(src.cols/2.f, src.rows/2.f), 30., 2.2);
  33. declare.in(src).out(dst);
  34. TEST_CYCLE() warpAffine( src, dst, warpMat, sz, interType, borderMode, borderColor );
  35. #ifdef __ANDROID__
  36. SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10);
  37. #else
  38. SANITY_CHECK(dst, 1);
  39. #endif
  40. }
  41. PERF_TEST_P(TestWarpAffine, DISABLED_WarpAffine_ovx,
  42. Combine(
  43. Values(szVGA, sz720p, sz1080p),
  44. InterType::all(),
  45. BorderMode::all()
  46. )
  47. )
  48. {
  49. Size sz, szSrc(512, 512);
  50. int borderMode, interType;
  51. sz = get<0>(GetParam());
  52. interType = get<1>(GetParam());
  53. borderMode = get<2>(GetParam());
  54. Scalar borderColor = Scalar::all(150);
  55. Mat src(szSrc, CV_8UC1), dst(sz, CV_8UC1);
  56. cvtest::fillGradient(src);
  57. if (borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
  58. Mat warpMat = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f), 30., 2.2);
  59. declare.in(src).out(dst);
  60. TEST_CYCLE() warpAffine(src, dst, warpMat, sz, interType, borderMode, borderColor);
  61. #ifdef __ANDROID__
  62. SANITY_CHECK(dst, interType == INTER_LINEAR ? 5 : 10);
  63. #else
  64. SANITY_CHECK(dst, 1);
  65. #endif
  66. }
  67. PERF_TEST_P( TestWarpPerspective, WarpPerspective,
  68. Combine(
  69. Values( szVGA, sz720p, sz1080p ),
  70. InterType::all(),
  71. BorderMode::all()
  72. )
  73. )
  74. {
  75. Size sz, szSrc(512, 512);
  76. int borderMode, interType;
  77. sz = get<0>(GetParam());
  78. interType = get<1>(GetParam());
  79. borderMode = get<2>(GetParam());
  80. Scalar borderColor = Scalar::all(150);
  81. Mat src(szSrc,CV_8UC4), dst(sz, CV_8UC4);
  82. cvtest::fillGradient(src);
  83. if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
  84. Mat rotMat = getRotationMatrix2D(Point2f(src.cols/2.f, src.rows/2.f), 30., 2.2);
  85. Mat warpMat(3, 3, CV_64FC1);
  86. for(int r=0; r<2; r++)
  87. for(int c=0; c<3; c++)
  88. warpMat.at<double>(r, c) = rotMat.at<double>(r, c);
  89. warpMat.at<double>(2, 0) = .3/sz.width;
  90. warpMat.at<double>(2, 1) = .3/sz.height;
  91. warpMat.at<double>(2, 2) = 1;
  92. declare.in(src).out(dst);
  93. TEST_CYCLE() warpPerspective( src, dst, warpMat, sz, interType, borderMode, borderColor );
  94. #ifdef __ANDROID__
  95. SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10);
  96. #else
  97. SANITY_CHECK(dst, 1);
  98. #endif
  99. }
  100. PERF_TEST_P(TestWarpPerspective, DISABLED_WarpPerspective_ovx,
  101. Combine(
  102. Values(szVGA, sz720p, sz1080p),
  103. InterType::all(),
  104. BorderMode::all()
  105. )
  106. )
  107. {
  108. Size sz, szSrc(512, 512);
  109. int borderMode, interType;
  110. sz = get<0>(GetParam());
  111. interType = get<1>(GetParam());
  112. borderMode = get<2>(GetParam());
  113. Scalar borderColor = Scalar::all(150);
  114. Mat src(szSrc, CV_8UC1), dst(sz, CV_8UC1);
  115. cvtest::fillGradient(src);
  116. if (borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
  117. Mat rotMat = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f), 30., 2.2);
  118. Mat warpMat(3, 3, CV_64FC1);
  119. for (int r = 0; r<2; r++)
  120. for (int c = 0; c<3; c++)
  121. warpMat.at<double>(r, c) = rotMat.at<double>(r, c);
  122. warpMat.at<double>(2, 0) = .3 / sz.width;
  123. warpMat.at<double>(2, 1) = .3 / sz.height;
  124. warpMat.at<double>(2, 2) = 1;
  125. declare.in(src).out(dst);
  126. TEST_CYCLE() warpPerspective(src, dst, warpMat, sz, interType, borderMode, borderColor);
  127. #ifdef __ANDROID__
  128. SANITY_CHECK(dst, interType == INTER_LINEAR ? 5 : 10);
  129. #else
  130. SANITY_CHECK(dst, 1);
  131. #endif
  132. }
  133. PERF_TEST_P( TestWarpPerspectiveNear_t, WarpPerspectiveNear,
  134. Combine(
  135. Values( Size(640,480), Size(1920,1080), Size(2592,1944) ),
  136. InterType::all(),
  137. BorderMode::all(),
  138. Values( CV_8UC1, CV_8UC4 )
  139. )
  140. )
  141. {
  142. Size size;
  143. int borderMode, interType, type;
  144. size = get<0>(GetParam());
  145. interType = get<1>(GetParam());
  146. borderMode = get<2>(GetParam());
  147. type = get<3>(GetParam());
  148. Scalar borderColor = Scalar::all(150);
  149. Mat src(size, type), dst(size, type);
  150. cvtest::fillGradient(src);
  151. if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
  152. int shift = static_cast<int>(src.cols*0.04);
  153. Mat srcVertices = (Mat_<Vec2f>(1, 4) << Vec2f(0, 0),
  154. Vec2f(static_cast<float>(size.width-1), 0),
  155. Vec2f(static_cast<float>(size.width-1), static_cast<float>(size.height-1)),
  156. Vec2f(0, static_cast<float>(size.height-1)));
  157. Mat dstVertices = (Mat_<Vec2f>(1, 4) << Vec2f(0, static_cast<float>(shift)),
  158. Vec2f(static_cast<float>(size.width-shift/2), 0),
  159. Vec2f(static_cast<float>(size.width-shift), static_cast<float>(size.height-shift)),
  160. Vec2f(static_cast<float>(shift/2), static_cast<float>(size.height-1)));
  161. Mat warpMat = getPerspectiveTransform(srcVertices, dstVertices);
  162. declare.in(src).out(dst);
  163. declare.time(100);
  164. TEST_CYCLE()
  165. {
  166. warpPerspective( src, dst, warpMat, size, interType, borderMode, borderColor );
  167. }
  168. #ifdef __ANDROID__
  169. SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10);
  170. #else
  171. SANITY_CHECK(dst, 1);
  172. #endif
  173. }
  174. PERF_TEST_P( TestRemap, remap,
  175. Combine(
  176. Values( CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1 ),
  177. Values( szVGA, sz1080p ),
  178. InterType::all(),
  179. BorderMode::all(),
  180. RemapMode::all()
  181. )
  182. )
  183. {
  184. int type = get<0>(GetParam());
  185. Size size = get<1>(GetParam());
  186. int interpolationType = get<2>(GetParam());
  187. int borderMode = get<3>(GetParam());
  188. int remapMode = get<4>(GetParam());
  189. unsigned int height = size.height;
  190. unsigned int width = size.width;
  191. Mat source(height, width, type);
  192. Mat destination;
  193. Mat map_x(height, width, CV_32F);
  194. Mat map_y(height, width, CV_32F);
  195. declare.in(source, WARMUP_RNG);
  196. update_map(source, map_x, map_y, remapMode);
  197. TEST_CYCLE()
  198. {
  199. remap(source, destination, map_x, map_y, interpolationType, borderMode);
  200. }
  201. SANITY_CHECK_NOTHING();
  202. }
  203. void update_map(const Mat& src, Mat& map_x, Mat& map_y, const int remapMode )
  204. {
  205. for( int j = 0; j < src.rows; j++ )
  206. {
  207. for( int i = 0; i < src.cols; i++ )
  208. {
  209. switch( remapMode )
  210. {
  211. case HALF_SIZE:
  212. if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
  213. {
  214. map_x.at<float>(j,i) = 2*( i - src.cols*0.25f ) + 0.5f ;
  215. map_y.at<float>(j,i) = 2*( j - src.rows*0.25f ) + 0.5f ;
  216. }
  217. else
  218. {
  219. map_x.at<float>(j,i) = 0 ;
  220. map_y.at<float>(j,i) = 0 ;
  221. }
  222. break;
  223. case UPSIDE_DOWN:
  224. map_x.at<float>(j,i) = static_cast<float>(i) ;
  225. map_y.at<float>(j,i) = static_cast<float>(src.rows - j) ;
  226. break;
  227. case REFLECTION_X:
  228. map_x.at<float>(j,i) = static_cast<float>(src.cols - i) ;
  229. map_y.at<float>(j,i) = static_cast<float>(j) ;
  230. break;
  231. case REFLECTION_BOTH:
  232. map_x.at<float>(j,i) = static_cast<float>(src.cols - i) ;
  233. map_y.at<float>(j,i) = static_cast<float>(src.rows - j) ;
  234. break;
  235. } // end of switch
  236. }
  237. }
  238. }
  239. PERF_TEST(Transform, getPerspectiveTransform_1000)
  240. {
  241. unsigned int size = 8;
  242. Mat source(1, size/2, CV_32FC2);
  243. Mat destination(1, size/2, CV_32FC2);
  244. Mat transformCoefficient;
  245. declare.in(source, destination, WARMUP_RNG);
  246. PERF_SAMPLE_BEGIN()
  247. for (int i = 0; i < 1000; i++)
  248. {
  249. transformCoefficient = getPerspectiveTransform(source, destination);
  250. }
  251. PERF_SAMPLE_END()
  252. SANITY_CHECK_NOTHING();
  253. }
  254. PERF_TEST(Transform, getPerspectiveTransform_QR_1000)
  255. {
  256. unsigned int size = 8;
  257. Mat source(1, size/2, CV_32FC2);
  258. Mat destination(1, size/2, CV_32FC2);
  259. Mat transformCoefficient;
  260. declare.in(source, destination, WARMUP_RNG);
  261. PERF_SAMPLE_BEGIN()
  262. for (int i = 0; i < 1000; i++)
  263. {
  264. transformCoefficient = getPerspectiveTransform(source, destination, DECOMP_QR);
  265. }
  266. PERF_SAMPLE_END()
  267. SANITY_CHECK_NOTHING();
  268. }
  269. } // namespace