perf_optflowpyrlk.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "perf_precomp.hpp"
  2. namespace opencv_test { namespace {
  3. using namespace perf;
  4. typedef tuple<std::string, int, int, tuple<int,int>, int> Path_Idx_Cn_NPoints_WSize_t;
  5. typedef TestBaseWithParam<Path_Idx_Cn_NPoints_WSize_t> Path_Idx_Cn_NPoints_WSize;
  6. void FormTrackingPointsArray(vector<Point2f>& points, int width, int height, int nPointsX, int nPointsY)
  7. {
  8. int stepX = width / nPointsX;
  9. int stepY = height / nPointsY;
  10. if (stepX < 1 || stepY < 1) FAIL() << "Specified points number is too big";
  11. points.clear();
  12. points.reserve(nPointsX * nPointsY);
  13. for( int x = stepX / 2; x < width; x += stepX )
  14. {
  15. for( int y = stepY / 2; y < height; y += stepY )
  16. {
  17. Point2f pt(static_cast<float>(x), static_cast<float>(y));
  18. points.push_back(pt);
  19. }
  20. }
  21. }
  22. PERF_TEST_P(Path_Idx_Cn_NPoints_WSize, OpticalFlowPyrLK_full, testing::Combine(
  23. testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.png"),
  24. testing::Range(1, 3),
  25. testing::Values(1, 3, 4),
  26. testing::Values(make_tuple(9, 9), make_tuple(15, 15)),
  27. testing::Values(7, 11)
  28. )
  29. )
  30. {
  31. string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));
  32. string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));
  33. Mat img1 = imread(filename1);
  34. Mat img2 = imread(filename2);
  35. if (img1.empty()) FAIL() << "Unable to load source image " << filename1;
  36. if (img2.empty()) FAIL() << "Unable to load source image " << filename2;
  37. int cn = get<2>(GetParam());
  38. int nPointsX = std::min(get<0>(get<3>(GetParam())), img1.cols);
  39. int nPointsY = std::min(get<1>(get<3>(GetParam())), img1.rows);
  40. int winSize = get<4>(GetParam());
  41. int maxLevel = 2;
  42. TermCriteria criteria(TermCriteria::COUNT|TermCriteria::EPS, 7, 0.001);
  43. int flags = 0;
  44. double minEigThreshold = 1e-4;
  45. Mat frame1, frame2;
  46. switch(cn)
  47. {
  48. case 1:
  49. cvtColor(img1, frame1, COLOR_BGR2GRAY, cn);
  50. cvtColor(img2, frame2, COLOR_BGR2GRAY, cn);
  51. break;
  52. case 3:
  53. frame1 = img1;
  54. frame2 = img2;
  55. break;
  56. case 4:
  57. cvtColor(img1, frame1, COLOR_BGR2BGRA, cn);
  58. cvtColor(img2, frame2, COLOR_BGR2BGRA, cn);
  59. break;
  60. default:
  61. FAIL() << "Unexpected number of channels: " << cn;
  62. }
  63. vector<Point2f> inPoints;
  64. vector<Point2f> outPoints;
  65. vector<uchar> status;
  66. vector<float> err;
  67. FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);
  68. outPoints.resize(inPoints.size());
  69. status.resize(inPoints.size());
  70. err.resize(inPoints.size());
  71. declare.in(frame1, frame2, inPoints).out(outPoints);
  72. TEST_CYCLE_N(30)
  73. {
  74. calcOpticalFlowPyrLK(frame1, frame2, inPoints, outPoints, status, err,
  75. Size(winSize, winSize), maxLevel, criteria,
  76. flags, minEigThreshold);
  77. }
  78. SANITY_CHECK_NOTHING();
  79. }
  80. typedef tuple<std::string, int, tuple<int, int>, int> Path_Idx_NPoints_WSize_t;
  81. typedef TestBaseWithParam<Path_Idx_NPoints_WSize_t> Path_Idx_NPoints_WSize;
  82. PERF_TEST_P(Path_Idx_NPoints_WSize, DISABLED_OpticalFlowPyrLK_ovx, testing::Combine(
  83. testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.png"),
  84. testing::Range(1, 3),
  85. testing::Values(make_tuple(9, 9), make_tuple(15, 15)),
  86. testing::Values(7, 11)
  87. )
  88. )
  89. {
  90. string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));
  91. string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));
  92. Mat img1 = imread(filename1);
  93. Mat img2 = imread(filename2);
  94. if (img1.empty()) FAIL() << "Unable to load source image " << filename1;
  95. if (img2.empty()) FAIL() << "Unable to load source image " << filename2;
  96. int nPointsX = std::min(get<0>(get<2>(GetParam())), img1.cols);
  97. int nPointsY = std::min(get<1>(get<2>(GetParam())), img1.rows);
  98. int winSize = get<3>(GetParam());
  99. int maxLevel = 2;
  100. TermCriteria criteria(TermCriteria::COUNT|TermCriteria::EPS, 7, 0.001);
  101. int flags = 0;
  102. double minEigThreshold = 1e-4;
  103. Mat frame1, frame2;
  104. cvtColor(img1, frame1, COLOR_BGR2GRAY, 1);
  105. cvtColor(img2, frame2, COLOR_BGR2GRAY, 1);
  106. vector<Point2f> inPoints;
  107. vector<Point2f> outPoints;
  108. vector<uchar> status;
  109. FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);
  110. outPoints.resize(inPoints.size());
  111. status.resize(inPoints.size());
  112. declare.in(frame1, frame2, inPoints).out(outPoints);
  113. TEST_CYCLE_N(30)
  114. {
  115. calcOpticalFlowPyrLK(frame1, frame2, inPoints, outPoints, status, cv::noArray(),
  116. Size(winSize, winSize), maxLevel, criteria,
  117. flags, minEigThreshold);
  118. }
  119. SANITY_CHECK_NOTHING();
  120. }
  121. typedef tuple<std::string, int, int, tuple<int,int>, int, bool> Path_Idx_Cn_NPoints_WSize_Deriv_t;
  122. typedef TestBaseWithParam<Path_Idx_Cn_NPoints_WSize_Deriv_t> Path_Idx_Cn_NPoints_WSize_Deriv;
  123. PERF_TEST_P(Path_Idx_Cn_NPoints_WSize_Deriv, OpticalFlowPyrLK_self, testing::Combine(
  124. testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.png"),
  125. testing::Range(1, 3),
  126. testing::Values(1, 3, 4),
  127. testing::Values(make_tuple(9, 9), make_tuple(15, 15)),
  128. testing::Values(7, 11),
  129. testing::Bool()
  130. )
  131. )
  132. {
  133. string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));
  134. string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));
  135. Mat img1 = imread(filename1);
  136. Mat img2 = imread(filename2);
  137. if (img1.empty()) FAIL() << "Unable to load source image " << filename1;
  138. if (img2.empty()) FAIL() << "Unable to load source image " << filename2;
  139. int cn = get<2>(GetParam());
  140. int nPointsX = std::min(get<0>(get<3>(GetParam())), img1.cols);
  141. int nPointsY = std::min(get<1>(get<3>(GetParam())), img1.rows);
  142. int winSize = get<4>(GetParam());
  143. bool withDerivatives = get<5>(GetParam());
  144. int maxLevel = 2;
  145. TermCriteria criteria(TermCriteria::COUNT|TermCriteria::EPS, 7, 0.001);
  146. int flags = 0;
  147. double minEigThreshold = 1e-4;
  148. Mat frame1, frame2;
  149. switch(cn)
  150. {
  151. case 1:
  152. cvtColor(img1, frame1, COLOR_BGR2GRAY, cn);
  153. cvtColor(img2, frame2, COLOR_BGR2GRAY, cn);
  154. break;
  155. case 3:
  156. frame1 = img1;
  157. frame2 = img2;
  158. break;
  159. case 4:
  160. cvtColor(img1, frame1, COLOR_BGR2BGRA, cn);
  161. cvtColor(img2, frame2, COLOR_BGR2BGRA, cn);
  162. break;
  163. default:
  164. FAIL() << "Unexpected number of channels: " << cn;
  165. }
  166. vector<Point2f> inPoints;
  167. vector<Point2f> outPoints;
  168. vector<uchar> status;
  169. vector<float> err;
  170. FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);
  171. outPoints.resize(inPoints.size());
  172. status.resize(inPoints.size());
  173. err.resize(inPoints.size());
  174. std::vector<Mat> pyramid1, pyramid2;
  175. maxLevel = buildOpticalFlowPyramid(frame1, pyramid1, Size(winSize, winSize), maxLevel, withDerivatives);
  176. maxLevel = buildOpticalFlowPyramid(frame2, pyramid2, Size(winSize, winSize), maxLevel, withDerivatives);
  177. declare.in(pyramid1, pyramid2, inPoints).out(outPoints);
  178. declare.time(400);
  179. int runs = 3;
  180. TEST_CYCLE_MULTIRUN(runs)
  181. {
  182. calcOpticalFlowPyrLK(pyramid1, pyramid2, inPoints, outPoints, status, err,
  183. Size(winSize, winSize), maxLevel, criteria,
  184. flags, minEigThreshold);
  185. }
  186. SANITY_CHECK_NOTHING();
  187. }
  188. CV_ENUM(PyrBorderMode, BORDER_DEFAULT, BORDER_TRANSPARENT)
  189. typedef tuple<std::string, int, bool, PyrBorderMode, bool> Path_Win_Deriv_Border_Reuse_t;
  190. typedef TestBaseWithParam<Path_Win_Deriv_Border_Reuse_t> Path_Win_Deriv_Border_Reuse;
  191. PERF_TEST_P(Path_Win_Deriv_Border_Reuse, OpticalFlowPyrLK_pyr, testing::Combine(
  192. testing::Values<std::string>("cv/optflow/frames/720p_01.png"),
  193. testing::Values(7, 11),
  194. testing::Bool(),
  195. PyrBorderMode::all(),
  196. testing::Bool()
  197. )
  198. )
  199. {
  200. string filename = getDataPath(get<0>(GetParam()));
  201. Mat img = imread(filename);
  202. Size winSize(get<1>(GetParam()), get<1>(GetParam()));
  203. bool withDerivatives = get<2>(GetParam());
  204. int derivBorder = get<3>(GetParam());
  205. int pyrBorder = derivBorder;
  206. if(derivBorder != BORDER_TRANSPARENT)
  207. {
  208. derivBorder = BORDER_CONSTANT;
  209. pyrBorder = BORDER_REFLECT_101;
  210. }
  211. bool tryReuseInputImage = get<4>(GetParam());
  212. std::vector<Mat> pyramid;
  213. img.adjustROI(winSize.height, winSize.height, winSize.width, winSize.width);
  214. int maxLevel = buildOpticalFlowPyramid(img, pyramid, winSize, 1000, withDerivatives, BORDER_CONSTANT, BORDER_CONSTANT, tryReuseInputImage);
  215. declare.in(img).out(pyramid);
  216. TEST_CYCLE()
  217. {
  218. buildOpticalFlowPyramid(img, pyramid, winSize, maxLevel, withDerivatives, pyrBorder, derivBorder, tryReuseInputImage);
  219. }
  220. size_t expected_layers = ((size_t)maxLevel + 1) * (withDerivatives ? 2 : 1);
  221. ASSERT_EQ(expected_layers, pyramid.size());
  222. SANITY_CHECK_NOTHING();
  223. }
  224. }} // namespace