perf_optflow.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "perf_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. typedef pair<string, string> pair_string;
  45. DEF_PARAM_TEST_1(ImagePair, pair_string);
  46. //////////////////////////////////////////////////////
  47. // BroxOpticalFlow
  48. PERF_TEST_P(ImagePair, BroxOpticalFlow,
  49. Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
  50. {
  51. declare.time(300);
  52. cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
  53. ASSERT_FALSE(frame0.empty());
  54. cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
  55. ASSERT_FALSE(frame1.empty());
  56. frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);
  57. frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);
  58. if (PERF_RUN_CUDA())
  59. {
  60. const cv::cuda::GpuMat d_frame0(frame0);
  61. const cv::cuda::GpuMat d_frame1(frame1);
  62. cv::cuda::GpuMat flow;
  63. cv::Ptr<cv::cuda::BroxOpticalFlow> d_alg =
  64. cv::cuda::BroxOpticalFlow::create(0.197 /*alpha*/, 50.0 /*gamma*/, 0.8 /*scale_factor*/,
  65. 10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);
  66. TEST_CYCLE() d_alg->calc(d_frame0, d_frame1, flow);
  67. cv::cuda::GpuMat flows[2];
  68. cv::cuda::split(flow, flows);
  69. cv::cuda::GpuMat u = flows[0];
  70. cv::cuda::GpuMat v = flows[1];
  71. CUDA_SANITY_CHECK(u, 1e-1);
  72. CUDA_SANITY_CHECK(v, 1e-1);
  73. }
  74. else
  75. {
  76. FAIL_NO_CPU();
  77. }
  78. }
  79. //////////////////////////////////////////////////////
  80. // PyrLKOpticalFlowSparse
  81. DEF_PARAM_TEST(ImagePair_Gray_NPts_WinSz_Levels_Iters, pair_string, bool, int, int, int, int);
  82. PERF_TEST_P(ImagePair_Gray_NPts_WinSz_Levels_Iters, PyrLKOpticalFlowSparse,
  83. Combine(Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")),
  84. Bool(),
  85. Values(8000),
  86. Values(21),
  87. Values(1, 3),
  88. Values(1, 30)))
  89. {
  90. declare.time(20.0);
  91. const pair_string imagePair = GET_PARAM(0);
  92. const bool useGray = GET_PARAM(1);
  93. const int points = GET_PARAM(2);
  94. const int winSize = GET_PARAM(3);
  95. const int levels = GET_PARAM(4);
  96. const int iters = GET_PARAM(5);
  97. cv::Mat frame0 = readImage(imagePair.first, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
  98. ASSERT_FALSE(frame0.empty());
  99. cv::Mat frame1 = readImage(imagePair.second, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
  100. ASSERT_FALSE(frame1.empty());
  101. cv::Mat gray_frame;
  102. if (useGray)
  103. gray_frame = frame0;
  104. else
  105. cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY);
  106. cv::Mat pts;
  107. cv::goodFeaturesToTrack(gray_frame, pts, points, 0.01, 0.0);
  108. frame0.convertTo(frame0, CV_32F);
  109. frame1.convertTo(frame1, CV_32F);
  110. if(!useGray)
  111. {
  112. cv::cvtColor(frame0, frame0, cv::COLOR_BGR2BGRA);
  113. cv::cvtColor(frame1, frame1, cv::COLOR_BGR2BGRA);
  114. }
  115. if (PERF_RUN_CUDA())
  116. {
  117. const cv::cuda::GpuMat d_pts(pts.reshape(2, 1));
  118. cv::Ptr<cv::cuda::SparsePyrLKOpticalFlow> d_pyrLK =
  119. cv::cuda::SparsePyrLKOpticalFlow::create(cv::Size(winSize, winSize),
  120. levels - 1,
  121. iters);
  122. const cv::cuda::GpuMat d_frame0(frame0);
  123. const cv::cuda::GpuMat d_frame1(frame1);
  124. cv::cuda::GpuMat nextPts;
  125. cv::cuda::GpuMat status;
  126. TEST_CYCLE() d_pyrLK->calc(d_frame0, d_frame1, d_pts, nextPts, status);
  127. CUDA_SANITY_CHECK(nextPts);
  128. CUDA_SANITY_CHECK(status);
  129. }
  130. else
  131. {
  132. cv::Mat nextPts;
  133. cv::Mat status;
  134. TEST_CYCLE()
  135. {
  136. cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, cv::noArray(),
  137. cv::Size(winSize, winSize), levels - 1,
  138. cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, iters, 0.01));
  139. }
  140. CPU_SANITY_CHECK(nextPts);
  141. CPU_SANITY_CHECK(status);
  142. }
  143. }
  144. //////////////////////////////////////////////////////
  145. // PyrLKOpticalFlowDense
  146. DEF_PARAM_TEST(ImagePair_WinSz_Levels_Iters, pair_string, int, int, int);
  147. PERF_TEST_P(ImagePair_WinSz_Levels_Iters, PyrLKOpticalFlowDense,
  148. Combine(Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")),
  149. Values(3, 5, 7, 9, 13, 17, 21),
  150. Values(1, 3),
  151. Values(1, 10)))
  152. {
  153. declare.time(30);
  154. const pair_string imagePair = GET_PARAM(0);
  155. const int winSize = GET_PARAM(1);
  156. const int levels = GET_PARAM(2);
  157. const int iters = GET_PARAM(3);
  158. const cv::Mat frame0 = readImage(imagePair.first, cv::IMREAD_GRAYSCALE);
  159. ASSERT_FALSE(frame0.empty());
  160. const cv::Mat frame1 = readImage(imagePair.second, cv::IMREAD_GRAYSCALE);
  161. ASSERT_FALSE(frame1.empty());
  162. if (PERF_RUN_CUDA())
  163. {
  164. const cv::cuda::GpuMat d_frame0(frame0);
  165. const cv::cuda::GpuMat d_frame1(frame1);
  166. cv::cuda::GpuMat flow;
  167. cv::Ptr<cv::cuda::DensePyrLKOpticalFlow> d_pyrLK =
  168. cv::cuda::DensePyrLKOpticalFlow::create(cv::Size(winSize, winSize),
  169. levels - 1,
  170. iters);
  171. TEST_CYCLE() d_pyrLK->calc(d_frame0, d_frame1, flow);
  172. cv::cuda::GpuMat flows[2];
  173. cv::cuda::split(flow, flows);
  174. cv::cuda::GpuMat u = flows[0];
  175. cv::cuda::GpuMat v = flows[1];
  176. // Sanity test fails on Maxwell and CUDA 7.0
  177. SANITY_CHECK_NOTHING();
  178. }
  179. else
  180. {
  181. FAIL_NO_CPU();
  182. }
  183. }
  184. //////////////////////////////////////////////////////
  185. // FarnebackOpticalFlow
  186. PERF_TEST_P(ImagePair, FarnebackOpticalFlow,
  187. Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
  188. {
  189. declare.time(10);
  190. const cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
  191. ASSERT_FALSE(frame0.empty());
  192. const cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
  193. ASSERT_FALSE(frame1.empty());
  194. const int numLevels = 5;
  195. const double pyrScale = 0.5;
  196. const int winSize = 13;
  197. const int numIters = 10;
  198. const int polyN = 5;
  199. const double polySigma = 1.1;
  200. const int flags = 0;
  201. if (PERF_RUN_CUDA())
  202. {
  203. const cv::cuda::GpuMat d_frame0(frame0);
  204. const cv::cuda::GpuMat d_frame1(frame1);
  205. cv::cuda::GpuMat flow;
  206. cv::Ptr<cv::cuda::FarnebackOpticalFlow> d_farneback =
  207. cv::cuda::FarnebackOpticalFlow::create(numLevels, pyrScale, false, winSize,
  208. numIters, polyN, polySigma, flags);
  209. TEST_CYCLE() d_farneback->calc(d_frame0, d_frame1, flow);
  210. cv::cuda::GpuMat flows[2];
  211. cv::cuda::split(flow, flows);
  212. cv::cuda::GpuMat u = flows[0];
  213. cv::cuda::GpuMat v = flows[1];
  214. CUDA_SANITY_CHECK(u, 1e-4);
  215. CUDA_SANITY_CHECK(v, 1e-4);
  216. }
  217. else
  218. {
  219. cv::Mat flow;
  220. TEST_CYCLE() cv::calcOpticalFlowFarneback(frame0, frame1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
  221. CPU_SANITY_CHECK(flow);
  222. }
  223. }
  224. //////////////////////////////////////////////////////
  225. // OpticalFlowDual_TVL1
  226. PERF_TEST_P(ImagePair, OpticalFlowDual_TVL1,
  227. Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
  228. {
  229. declare.time(20);
  230. const cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
  231. ASSERT_FALSE(frame0.empty());
  232. const cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
  233. ASSERT_FALSE(frame1.empty());
  234. if (PERF_RUN_CUDA())
  235. {
  236. const cv::cuda::GpuMat d_frame0(frame0);
  237. const cv::cuda::GpuMat d_frame1(frame1);
  238. cv::cuda::GpuMat flow;
  239. cv::Ptr<cv::cuda::OpticalFlowDual_TVL1> d_alg =
  240. cv::cuda::OpticalFlowDual_TVL1::create();
  241. TEST_CYCLE() d_alg->calc(d_frame0, d_frame1, flow);
  242. cv::cuda::GpuMat flows[2];
  243. cv::cuda::split(flow, flows);
  244. cv::cuda::GpuMat u = flows[0];
  245. cv::cuda::GpuMat v = flows[1];
  246. CUDA_SANITY_CHECK(u, 1e-1);
  247. CUDA_SANITY_CHECK(v, 1e-1);
  248. }
  249. else
  250. {
  251. cv::Mat flow;
  252. cv::Ptr<cv::optflow::DualTVL1OpticalFlow> alg = cv::optflow::createOptFlow_DualTVL1();
  253. alg->setMedianFiltering(1);
  254. alg->setInnerIterations(1);
  255. alg->setOuterIterations(300);
  256. TEST_CYCLE() alg->calc(frame0, frame1, flow);
  257. CPU_SANITY_CHECK(flow);
  258. }
  259. }
  260. //////////////////////////////////////////////////////
  261. // NvidiaOpticalFlow_1_0
  262. PERF_TEST_P(ImagePair, NvidiaOpticalFlow_1_0,
  263. Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
  264. {
  265. declare.time(10);
  266. const cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
  267. ASSERT_FALSE(frame0.empty());
  268. const cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
  269. ASSERT_FALSE(frame1.empty());
  270. Stream inputStream;
  271. Stream outputStream;
  272. if (PERF_RUN_CUDA())
  273. {
  274. const cv::cuda::GpuMat d_frame0(frame0);
  275. const cv::cuda::GpuMat d_frame1(frame1);
  276. cv::cuda::GpuMat d_flow;
  277. cv::Ptr<cv::cuda::NvidiaOpticalFlow_1_0> d_nvof;
  278. try
  279. {
  280. d_nvof = cv::cuda::NvidiaOpticalFlow_1_0::create(frame0.size(),
  281. cv::cuda::NvidiaOpticalFlow_1_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_FAST,
  282. false, false, false, 0, inputStream, outputStream);
  283. }
  284. catch (const cv::Exception& e)
  285. {
  286. if(e.code == Error::StsBadFunc || e.code == Error::StsBadArg || e.code == Error::StsNullPtr)
  287. throw SkipTestException("Current configuration is not supported");
  288. throw;
  289. }
  290. TEST_CYCLE() d_nvof->calc(d_frame0, d_frame1, d_flow);
  291. cv::cuda::GpuMat flow[2];
  292. cv::cuda::split(d_flow, flow);
  293. cv::cuda::GpuMat u = flow[0];
  294. cv::cuda::GpuMat v = flow[1];
  295. CUDA_SANITY_CHECK(u, 1e-10);
  296. CUDA_SANITY_CHECK(v, 1e-10);
  297. d_nvof->collectGarbage();
  298. }
  299. }
  300. //////////////////////////////////////////////////////
  301. // NvidiaOpticalFlow_2_0
  302. PERF_TEST_P(ImagePair, NvidiaOpticalFlow_2_0,
  303. Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
  304. {
  305. declare.time(10);
  306. const cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
  307. ASSERT_FALSE(frame0.empty());
  308. const cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
  309. ASSERT_FALSE(frame1.empty());
  310. const cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE outGridSize
  311. = cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE::NV_OF_OUTPUT_VECTOR_GRID_SIZE_1;
  312. const cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE hintGridSize
  313. = cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE::NV_OF_HINT_VECTOR_GRID_SIZE_1;
  314. Stream inputStream;
  315. Stream outputStream;
  316. if (PERF_RUN_CUDA())
  317. {
  318. const cv::cuda::GpuMat d_frame0(frame0);
  319. const cv::cuda::GpuMat d_frame1(frame1);
  320. cv::cuda::GpuMat d_flow;
  321. cv::Ptr<cv::cuda::NvidiaOpticalFlow_2_0> d_nvof;
  322. try
  323. {
  324. d_nvof = cv::cuda::NvidiaOpticalFlow_2_0::create(frame0.size(),
  325. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_FAST, outGridSize, hintGridSize,
  326. false, false, false, 0, inputStream, outputStream);
  327. }
  328. catch (const cv::Exception& e)
  329. {
  330. if (e.code == Error::StsBadFunc || e.code == Error::StsBadArg || e.code == Error::StsNullPtr)
  331. throw SkipTestException("Current configuration is not supported");
  332. throw;
  333. }
  334. TEST_CYCLE() d_nvof->calc(d_frame0, d_frame1, d_flow);
  335. cv::cuda::GpuMat flow[2];
  336. cv::cuda::split(d_flow, flow);
  337. cv::cuda::GpuMat u = flow[0];
  338. cv::cuda::GpuMat v = flow[1];
  339. CUDA_SANITY_CHECK(u, 1e-10);
  340. CUDA_SANITY_CHECK(v, 1e-10);
  341. d_nvof->collectGarbage();
  342. }
  343. }
  344. }} // namespace