perf_resize.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. typedef tuple<MatType, Size, Size> MatInfo_Size_Size_t;
  7. typedef TestBaseWithParam<MatInfo_Size_Size_t> MatInfo_Size_Size;
  8. typedef tuple<Size,Size> Size_Size_t;
  9. typedef tuple<MatType, Size_Size_t> MatInfo_SizePair_t;
  10. typedef TestBaseWithParam<MatInfo_SizePair_t> MatInfo_SizePair;
  11. #define MATTYPE_NE_VALUES CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4, \
  12. CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4, \
  13. CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4
  14. // For gradient-ish testing of the other matrix formats
  15. template<typename T>
  16. static void fillFPGradient(Mat& img)
  17. {
  18. const int ch = img.channels();
  19. int r, c, i;
  20. for(r=0; r<img.rows; r++)
  21. {
  22. for(c=0; c<img.cols; c++)
  23. {
  24. T vals[] = {(T)r, (T)c, (T)(r*c), (T)(r*c/(r+c+1))};
  25. T *p = (T*)img.ptr(r, c);
  26. for(i=0; i<ch; i++) p[i] = (T)vals[i];
  27. }
  28. }
  29. }
  30. PERF_TEST_P(MatInfo_Size_Size, resizeUpLinear,
  31. testing::Values(
  32. MatInfo_Size_Size_t(CV_8UC1, szVGA, szqHD),
  33. MatInfo_Size_Size_t(CV_8UC2, szVGA, szqHD),
  34. MatInfo_Size_Size_t(CV_8UC3, szVGA, szqHD),
  35. MatInfo_Size_Size_t(CV_8UC4, szVGA, szqHD),
  36. MatInfo_Size_Size_t(CV_8UC1, szVGA, sz720p),
  37. MatInfo_Size_Size_t(CV_8UC2, szVGA, sz720p),
  38. MatInfo_Size_Size_t(CV_8UC3, szVGA, sz720p),
  39. MatInfo_Size_Size_t(CV_8UC4, szVGA, sz720p)
  40. )
  41. )
  42. {
  43. int matType = get<0>(GetParam());
  44. Size from = get<1>(GetParam());
  45. Size to = get<2>(GetParam());
  46. cv::Mat src(from, matType), dst(to, matType);
  47. cvtest::fillGradient(src);
  48. declare.in(src).out(dst);
  49. TEST_CYCLE_MULTIRUN(10) resize(src, dst, to, 0, 0, INTER_LINEAR_EXACT);
  50. #ifdef __ANDROID__
  51. SANITY_CHECK(dst, 5);
  52. #else
  53. SANITY_CHECK(dst, 1 + 1e-6);
  54. #endif
  55. }
  56. PERF_TEST_P(MatInfo_SizePair, resizeUpLinearNonExact,
  57. testing::Combine
  58. (
  59. testing::Values( MATTYPE_NE_VALUES ),
  60. testing::Values( Size_Size_t(szVGA, szqHD), Size_Size_t(szVGA, sz720p) )
  61. )
  62. )
  63. {
  64. int matType = get<0>(GetParam());
  65. Size_Size_t sizes = get<1>(GetParam());
  66. Size from = get<0>(sizes);
  67. Size to = get<1>(sizes);
  68. cv::Mat src(from, matType), dst(to, matType);
  69. switch(src.depth())
  70. {
  71. case CV_8U: cvtest::fillGradient(src); break;
  72. case CV_16U: fillFPGradient<ushort>(src); break;
  73. case CV_32F: fillFPGradient<float>(src); break;
  74. }
  75. declare.in(src).out(dst);
  76. TEST_CYCLE_MULTIRUN(10) resize(src, dst, to, 0, 0, INTER_LINEAR);
  77. SANITY_CHECK_NOTHING();
  78. }
  79. PERF_TEST_P(MatInfo_Size_Size, resizeDownLinear,
  80. testing::Values(
  81. MatInfo_Size_Size_t(CV_8UC1, szVGA, szQVGA),
  82. MatInfo_Size_Size_t(CV_8UC2, szVGA, szQVGA),
  83. MatInfo_Size_Size_t(CV_8UC3, szVGA, szQVGA),
  84. MatInfo_Size_Size_t(CV_8UC4, szVGA, szQVGA),
  85. MatInfo_Size_Size_t(CV_8UC1, szqHD, szVGA),
  86. MatInfo_Size_Size_t(CV_8UC2, szqHD, szVGA),
  87. MatInfo_Size_Size_t(CV_8UC3, szqHD, szVGA),
  88. MatInfo_Size_Size_t(CV_8UC4, szqHD, szVGA),
  89. MatInfo_Size_Size_t(CV_8UC1, sz720p, Size(120 * sz720p.width / sz720p.height, 120)),//face detection min_face_size = 20%
  90. MatInfo_Size_Size_t(CV_8UC2, sz720p, Size(120 * sz720p.width / sz720p.height, 120)),//face detection min_face_size = 20%
  91. MatInfo_Size_Size_t(CV_8UC3, sz720p, Size(120 * sz720p.width / sz720p.height, 120)),//face detection min_face_size = 20%
  92. MatInfo_Size_Size_t(CV_8UC4, sz720p, Size(120 * sz720p.width / sz720p.height, 120)),//face detection min_face_size = 20%
  93. MatInfo_Size_Size_t(CV_8UC1, sz720p, szVGA),
  94. MatInfo_Size_Size_t(CV_8UC2, sz720p, szVGA),
  95. MatInfo_Size_Size_t(CV_8UC3, sz720p, szVGA),
  96. MatInfo_Size_Size_t(CV_8UC4, sz720p, szVGA),
  97. MatInfo_Size_Size_t(CV_8UC1, sz720p, szQVGA),
  98. MatInfo_Size_Size_t(CV_8UC2, sz720p, szQVGA),
  99. MatInfo_Size_Size_t(CV_8UC3, sz720p, szQVGA),
  100. MatInfo_Size_Size_t(CV_8UC4, sz720p, szQVGA)
  101. )
  102. )
  103. {
  104. int matType = get<0>(GetParam());
  105. Size from = get<1>(GetParam());
  106. Size to = get<2>(GetParam());
  107. cv::Mat src(from, matType), dst(to, matType);
  108. cvtest::fillGradient(src);
  109. declare.in(src).out(dst);
  110. TEST_CYCLE_MULTIRUN(10) resize(src, dst, to, 0, 0, INTER_LINEAR_EXACT);
  111. #ifdef __ANDROID__
  112. SANITY_CHECK(dst, 5);
  113. #else
  114. SANITY_CHECK(dst, 1 + 1e-6);
  115. #endif
  116. }
  117. PERF_TEST_P(MatInfo_SizePair, resizeDownLinearNonExact,
  118. testing::Combine
  119. (
  120. testing::Values( MATTYPE_NE_VALUES ),
  121. testing::Values
  122. (
  123. Size_Size_t(szVGA, szQVGA),
  124. Size_Size_t(szqHD, szVGA),
  125. Size_Size_t(sz720p, Size(120 * sz720p.width / sz720p.height, 120)),
  126. Size_Size_t(sz720p, szVGA),
  127. Size_Size_t(sz720p, szQVGA)
  128. )
  129. )
  130. )
  131. {
  132. int matType = get<0>(GetParam());
  133. Size_Size_t sizes = get<1>(GetParam());
  134. Size from = get<0>(sizes);
  135. Size to = get<1>(sizes);
  136. cv::Mat src(from, matType), dst(to, matType);
  137. switch(src.depth())
  138. {
  139. case CV_8U: cvtest::fillGradient(src); break;
  140. case CV_16U: fillFPGradient<ushort>(src); break;
  141. case CV_32F: fillFPGradient<float>(src); break;
  142. }
  143. declare.in(src).out(dst);
  144. TEST_CYCLE_MULTIRUN(10) resize(src, dst, to, 0, 0, INTER_LINEAR);
  145. SANITY_CHECK_NOTHING();
  146. }
  147. typedef tuple<MatType, Size, int> MatInfo_Size_Scale_t;
  148. typedef TestBaseWithParam<MatInfo_Size_Scale_t> MatInfo_Size_Scale;
  149. PERF_TEST_P(MatInfo_Size_Scale, ResizeAreaFast,
  150. testing::Combine(
  151. testing::Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_16UC1, CV_16UC3, CV_16UC4),
  152. testing::Values(szVGA, szqHD, sz720p, sz1080p),
  153. testing::Values(2)
  154. )
  155. )
  156. {
  157. int matType = get<0>(GetParam());
  158. Size from = get<1>(GetParam());
  159. int scale = get<2>(GetParam());
  160. from.width = (from.width/scale)*scale;
  161. from.height = (from.height/scale)*scale;
  162. cv::Mat src(from, matType);
  163. cv::Mat dst(from.height / scale, from.width / scale, matType);
  164. declare.in(src, WARMUP_RNG).out(dst);
  165. int runs = 15;
  166. TEST_CYCLE_MULTIRUN(runs) resize(src, dst, dst.size(), 0, 0, INTER_AREA);
  167. //difference equal to 1 is allowed because of different possible rounding modes: round-to-nearest vs bankers' rounding
  168. SANITY_CHECK(dst, 1);
  169. }
  170. typedef TestBaseWithParam<tuple<MatType, Size, double> > MatInfo_Size_Scale_Area;
  171. PERF_TEST_P(MatInfo_Size_Scale_Area, ResizeArea,
  172. testing::Combine(
  173. testing::Values(CV_8UC1, CV_8UC4),
  174. testing::Values(szVGA, szqHD, sz720p),
  175. testing::Values(2.4, 3.4, 1.3)
  176. )
  177. )
  178. {
  179. int matType = get<0>(GetParam());
  180. Size from = get<1>(GetParam());
  181. double scale = get<2>(GetParam());
  182. cv::Mat src(from, matType);
  183. Size to(cvRound(from.width * scale), cvRound(from.height * scale));
  184. cv::Mat dst(to, matType);
  185. declare.in(src, WARMUP_RNG).out(dst);
  186. declare.time(100);
  187. TEST_CYCLE() resize(src, dst, dst.size(), 0, 0, INTER_AREA);
  188. //difference equal to 1 is allowed because of different possible rounding modes: round-to-nearest vs bankers' rounding
  189. SANITY_CHECK(dst, 1);
  190. }
  191. typedef MatInfo_Size_Scale_Area MatInfo_Size_Scale_NN;
  192. PERF_TEST_P(MatInfo_Size_Scale_NN, ResizeNN,
  193. testing::Combine(
  194. testing::Values(CV_8UC1, CV_8UC2, CV_8UC4),
  195. testing::Values(szVGA, szqHD, sz720p, sz1080p, sz2160p),
  196. testing::Values(2.4, 3.4, 1.3)
  197. )
  198. )
  199. {
  200. int matType = get<0>(GetParam());
  201. Size from = get<1>(GetParam());
  202. double scale = get<2>(GetParam());
  203. cv::Mat src(from, matType);
  204. Size to(cvRound(from.width * scale), cvRound(from.height * scale));
  205. cv::Mat dst(to, matType);
  206. declare.in(src, WARMUP_RNG).out(dst);
  207. declare.time(100);
  208. TEST_CYCLE() resize(src, dst, dst.size(), 0, 0, INTER_NEAREST);
  209. EXPECT_GT(countNonZero(dst.reshape(1)), 0);
  210. SANITY_CHECK_NOTHING();
  211. }
  212. PERF_TEST_P(MatInfo_Size_Scale_NN, ResizeNNExact,
  213. testing::Combine(
  214. testing::Values(CV_8UC1, CV_8UC3, CV_8UC4),
  215. testing::Values(sz720p, sz1080p),
  216. testing::Values(0.25, 0.5, 2.0)
  217. )
  218. )
  219. {
  220. int matType = get<0>(GetParam());
  221. Size from = get<1>(GetParam());
  222. double scale = get<2>(GetParam());
  223. cv::Mat src(from, matType);
  224. Size to(cvRound(from.width * scale), cvRound(from.height * scale));
  225. cv::Mat dst(to, matType);
  226. declare.in(src, WARMUP_RNG).out(dst);
  227. declare.time(100);
  228. TEST_CYCLE() resize(src, dst, dst.size(), 0, 0, INTER_NEAREST_EXACT);
  229. EXPECT_GT(countNonZero(dst.reshape(1)), 0);
  230. SANITY_CHECK_NOTHING();
  231. }
  232. } // namespace