perf_warp.cpp 10 KB

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