perf_bgsegm.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. //////////////////////////////////////////////////////
  45. // MOG
  46. #ifdef HAVE_VIDEO_INPUT
  47. DEF_PARAM_TEST(Video_Cn_LearningRate, string, MatCn, double);
  48. PERF_TEST_P(Video_Cn_LearningRate, MOG,
  49. Combine(Values("cv/video/768x576.avi", "cv/video/1920x1080.avi"),
  50. CUDA_CHANNELS_1_3_4,
  51. Values(0.0, 0.01)))
  52. {
  53. const int numIters = 10;
  54. const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
  55. const int cn = GET_PARAM(1);
  56. const float learningRate = static_cast<float>(GET_PARAM(2));
  57. cv::VideoCapture cap(inputFile);
  58. ASSERT_TRUE(cap.isOpened());
  59. cv::Mat frame;
  60. cap >> frame;
  61. ASSERT_FALSE(frame.empty());
  62. if (cn != 3)
  63. {
  64. cv::Mat temp;
  65. if (cn == 1)
  66. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  67. else
  68. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  69. cv::swap(temp, frame);
  70. }
  71. if (PERF_RUN_CUDA())
  72. {
  73. cv::Ptr<cv::BackgroundSubtractor> d_mog = cv::cuda::createBackgroundSubtractorMOG();
  74. cv::cuda::GpuMat d_frame(frame);
  75. cv::cuda::GpuMat foreground;
  76. d_mog->apply(d_frame, foreground, learningRate);
  77. int i = 0;
  78. // collect performance data
  79. for (; i < numIters; ++i)
  80. {
  81. cap >> frame;
  82. ASSERT_FALSE(frame.empty());
  83. if (cn != 3)
  84. {
  85. cv::Mat temp;
  86. if (cn == 1)
  87. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  88. else
  89. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  90. cv::swap(temp, frame);
  91. }
  92. d_frame.upload(frame);
  93. startTimer();
  94. if(!next())
  95. break;
  96. d_mog->apply(d_frame, foreground, learningRate);
  97. stopTimer();
  98. }
  99. // process last frame in sequence to get data for sanity test
  100. for (; i < numIters; ++i)
  101. {
  102. cap >> frame;
  103. ASSERT_FALSE(frame.empty());
  104. if (cn != 3)
  105. {
  106. cv::Mat temp;
  107. if (cn == 1)
  108. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  109. else
  110. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  111. cv::swap(temp, frame);
  112. }
  113. d_frame.upload(frame);
  114. d_mog->apply(d_frame, foreground, learningRate);
  115. }
  116. CUDA_SANITY_CHECK(foreground);
  117. }
  118. else
  119. {
  120. FAIL_NO_CPU();
  121. }
  122. }
  123. #endif
  124. //////////////////////////////////////////////////////
  125. // MOG2
  126. #ifdef HAVE_VIDEO_INPUT
  127. DEF_PARAM_TEST(Video_Cn, string, int);
  128. PERF_TEST_P(Video_Cn, DISABLED_MOG2,
  129. Combine(Values("cv/video/768x576.avi", "cv/video/1920x1080.avi"),
  130. CUDA_CHANNELS_1_3_4))
  131. {
  132. const int numIters = 10;
  133. const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
  134. const int cn = GET_PARAM(1);
  135. cv::VideoCapture cap(inputFile);
  136. ASSERT_TRUE(cap.isOpened());
  137. cv::Mat frame;
  138. cap >> frame;
  139. ASSERT_FALSE(frame.empty());
  140. if (cn != 3)
  141. {
  142. cv::Mat temp;
  143. if (cn == 1)
  144. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  145. else
  146. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  147. cv::swap(temp, frame);
  148. }
  149. if (PERF_RUN_CUDA())
  150. {
  151. cv::Ptr<cv::BackgroundSubtractorMOG2> d_mog2 = cv::cuda::createBackgroundSubtractorMOG2();
  152. d_mog2->setDetectShadows(false);
  153. cv::cuda::GpuMat d_frame(frame);
  154. cv::cuda::GpuMat foreground;
  155. d_mog2->apply(d_frame, foreground);
  156. int i = 0;
  157. // collect performance data
  158. for (; i < numIters; ++i)
  159. {
  160. cap >> frame;
  161. ASSERT_FALSE(frame.empty());
  162. if (cn != 3)
  163. {
  164. cv::Mat temp;
  165. if (cn == 1)
  166. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  167. else
  168. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  169. cv::swap(temp, frame);
  170. }
  171. d_frame.upload(frame);
  172. startTimer();
  173. if(!next())
  174. break;
  175. d_mog2->apply(d_frame, foreground);
  176. stopTimer();
  177. }
  178. // process last frame in sequence to get data for sanity test
  179. for (; i < numIters; ++i)
  180. {
  181. cap >> frame;
  182. ASSERT_FALSE(frame.empty());
  183. if (cn != 3)
  184. {
  185. cv::Mat temp;
  186. if (cn == 1)
  187. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  188. else
  189. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  190. cv::swap(temp, frame);
  191. }
  192. d_frame.upload(frame);
  193. d_mog2->apply(d_frame, foreground);
  194. }
  195. CUDA_SANITY_CHECK(foreground);
  196. }
  197. else
  198. {
  199. cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::createBackgroundSubtractorMOG2();
  200. mog2->setDetectShadows(false);
  201. cv::Mat foreground;
  202. mog2->apply(frame, foreground);
  203. int i = 0;
  204. // collect performance data
  205. for (; i < numIters; ++i)
  206. {
  207. cap >> frame;
  208. ASSERT_FALSE(frame.empty());
  209. if (cn != 3)
  210. {
  211. cv::Mat temp;
  212. if (cn == 1)
  213. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  214. else
  215. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  216. cv::swap(temp, frame);
  217. }
  218. startTimer();
  219. if(!next())
  220. break;
  221. mog2->apply(frame, foreground);
  222. stopTimer();
  223. }
  224. // process last frame in sequence to get data for sanity test
  225. for (; i < numIters; ++i)
  226. {
  227. cap >> frame;
  228. ASSERT_FALSE(frame.empty());
  229. if (cn != 3)
  230. {
  231. cv::Mat temp;
  232. if (cn == 1)
  233. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  234. else
  235. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  236. cv::swap(temp, frame);
  237. }
  238. mog2->apply(frame, foreground);
  239. }
  240. CPU_SANITY_CHECK(foreground);
  241. }
  242. }
  243. #endif
  244. //////////////////////////////////////////////////////
  245. // MOG2GetBackgroundImage
  246. #ifdef HAVE_VIDEO_INPUT
  247. PERF_TEST_P(Video_Cn, MOG2GetBackgroundImage,
  248. Combine(Values("cv/video/768x576.avi", "cv/video/1920x1080.avi"),
  249. CUDA_CHANNELS_1_3_4))
  250. {
  251. const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
  252. const int cn = GET_PARAM(1);
  253. cv::VideoCapture cap(inputFile);
  254. ASSERT_TRUE(cap.isOpened());
  255. cv::Mat frame;
  256. if (PERF_RUN_CUDA())
  257. {
  258. cv::Ptr<cv::BackgroundSubtractor> d_mog2 = cv::cuda::createBackgroundSubtractorMOG2();
  259. cv::cuda::GpuMat d_frame;
  260. cv::cuda::GpuMat d_foreground;
  261. for (int i = 0; i < 10; ++i)
  262. {
  263. cap >> frame;
  264. ASSERT_FALSE(frame.empty());
  265. if (cn != 3)
  266. {
  267. cv::Mat temp;
  268. if (cn == 1)
  269. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  270. else
  271. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  272. cv::swap(temp, frame);
  273. }
  274. d_frame.upload(frame);
  275. d_mog2->apply(d_frame, d_foreground);
  276. }
  277. cv::cuda::GpuMat background;
  278. TEST_CYCLE() d_mog2->getBackgroundImage(background);
  279. CUDA_SANITY_CHECK(background, 1);
  280. }
  281. else
  282. {
  283. cv::Ptr<cv::BackgroundSubtractor> mog2 = cv::createBackgroundSubtractorMOG2();
  284. cv::Mat foreground;
  285. for (int i = 0; i < 10; ++i)
  286. {
  287. cap >> frame;
  288. ASSERT_FALSE(frame.empty());
  289. if (cn != 3)
  290. {
  291. cv::Mat temp;
  292. if (cn == 1)
  293. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  294. else
  295. cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
  296. cv::swap(temp, frame);
  297. }
  298. mog2->apply(frame, foreground);
  299. }
  300. cv::Mat background;
  301. TEST_CYCLE() mog2->getBackgroundImage(background);
  302. CPU_SANITY_CHECK(background);
  303. }
  304. }
  305. #endif
  306. }} // namespace