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 
43 #include "perf_precomp.hpp"
44 
45 using namespace std;
46 using namespace testing;
47 using namespace perf;
48 
49 #if defined(HAVE_XINE)         || \
50     defined(HAVE_GSTREAMER)    || \
51     defined(HAVE_QUICKTIME)    || \
52     defined(HAVE_QTKIT)        || \
53     defined(HAVE_AVFOUNDATION) || \
54     defined(HAVE_FFMPEG)       || \
55     defined(WIN32) /* assume that we have ffmpeg */
56 
57 #  define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
58 #else
59 #  define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
60 #endif
61 
62 //////////////////////////////////////////////////////
63 // MOG
64 
65 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
66 
67 DEF_PARAM_TEST(Video_Cn_LearningRate, string, MatCn, double);
68 
69 PERF_TEST_P(Video_Cn_LearningRate, MOG,
70             Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
71                     CUDA_CHANNELS_1_3_4,
72                     Values(0.0, 0.01)))
73 {
74     const int numIters = 10;
75 
76     const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
77     const int cn = GET_PARAM(1);
78     const float learningRate = static_cast<float>(GET_PARAM(2));
79 
80     cv::VideoCapture cap(inputFile);
81     ASSERT_TRUE(cap.isOpened());
82 
83     cv::Mat frame;
84 
85     cap >> frame;
86     ASSERT_FALSE(frame.empty());
87 
88     if (cn != 3)
89     {
90         cv::Mat temp;
91         if (cn == 1)
92             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
93         else
94             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
95         cv::swap(temp, frame);
96     }
97 
98     if (PERF_RUN_CUDA())
99     {
100         cv::Ptr<cv::BackgroundSubtractor> d_mog = cv::cuda::createBackgroundSubtractorMOG();
101 
102         cv::cuda::GpuMat d_frame(frame);
103         cv::cuda::GpuMat foreground;
104 
105         d_mog->apply(d_frame, foreground, learningRate);
106 
107         int i = 0;
108 
109         // collect performance data
110         for (; i < numIters; ++i)
111         {
112             cap >> frame;
113             ASSERT_FALSE(frame.empty());
114 
115             if (cn != 3)
116             {
117                 cv::Mat temp;
118                 if (cn == 1)
119                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
120                 else
121                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
122                 cv::swap(temp, frame);
123             }
124 
125             d_frame.upload(frame);
126 
127             startTimer();
128             if(!next())
129                 break;
130 
131             d_mog->apply(d_frame, foreground, learningRate);
132 
133             stopTimer();
134         }
135 
136         // process last frame in sequence to get data for sanity test
137         for (; i < numIters; ++i)
138         {
139             cap >> frame;
140             ASSERT_FALSE(frame.empty());
141 
142             if (cn != 3)
143             {
144                 cv::Mat temp;
145                 if (cn == 1)
146                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
147                 else
148                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
149                 cv::swap(temp, frame);
150             }
151 
152             d_frame.upload(frame);
153 
154             d_mog->apply(d_frame, foreground, learningRate);
155         }
156 
157         CUDA_SANITY_CHECK(foreground);
158     }
159     else
160     {
161         FAIL_NO_CPU();
162     }
163 }
164 
165 #endif
166 
167 //////////////////////////////////////////////////////
168 // MOG2
169 
170 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
171 
172 DEF_PARAM_TEST(Video_Cn, string, int);
173 
174 PERF_TEST_P(Video_Cn, DISABLED_MOG2,
175             Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
176                     CUDA_CHANNELS_1_3_4))
177 {
178     const int numIters = 10;
179 
180     const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
181     const int cn = GET_PARAM(1);
182 
183     cv::VideoCapture cap(inputFile);
184     ASSERT_TRUE(cap.isOpened());
185 
186     cv::Mat frame;
187 
188     cap >> frame;
189     ASSERT_FALSE(frame.empty());
190 
191     if (cn != 3)
192     {
193         cv::Mat temp;
194         if (cn == 1)
195             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
196         else
197             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
198         cv::swap(temp, frame);
199     }
200 
201     if (PERF_RUN_CUDA())
202     {
203         cv::Ptr<cv::BackgroundSubtractorMOG2> d_mog2 = cv::cuda::createBackgroundSubtractorMOG2();
204         d_mog2->setDetectShadows(false);
205 
206         cv::cuda::GpuMat d_frame(frame);
207         cv::cuda::GpuMat foreground;
208 
209         d_mog2->apply(d_frame, foreground);
210 
211         int i = 0;
212 
213         // collect performance data
214         for (; i < numIters; ++i)
215         {
216             cap >> frame;
217             ASSERT_FALSE(frame.empty());
218 
219             if (cn != 3)
220             {
221                 cv::Mat temp;
222                 if (cn == 1)
223                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
224                 else
225                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
226                 cv::swap(temp, frame);
227             }
228 
229             d_frame.upload(frame);
230 
231             startTimer();
232             if(!next())
233                 break;
234 
235             d_mog2->apply(d_frame, foreground);
236 
237             stopTimer();
238         }
239 
240         // process last frame in sequence to get data for sanity test
241         for (; i < numIters; ++i)
242         {
243             cap >> frame;
244             ASSERT_FALSE(frame.empty());
245 
246             if (cn != 3)
247             {
248                 cv::Mat temp;
249                 if (cn == 1)
250                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
251                 else
252                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
253                 cv::swap(temp, frame);
254             }
255 
256             d_frame.upload(frame);
257 
258             d_mog2->apply(d_frame, foreground);
259         }
260 
261         CUDA_SANITY_CHECK(foreground);
262     }
263     else
264     {
265         cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::createBackgroundSubtractorMOG2();
266         mog2->setDetectShadows(false);
267 
268         cv::Mat foreground;
269 
270         mog2->apply(frame, foreground);
271 
272         int i = 0;
273 
274         // collect performance data
275         for (; i < numIters; ++i)
276         {
277             cap >> frame;
278             ASSERT_FALSE(frame.empty());
279 
280             if (cn != 3)
281             {
282                 cv::Mat temp;
283                 if (cn == 1)
284                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
285                 else
286                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
287                 cv::swap(temp, frame);
288             }
289 
290             startTimer();
291             if(!next())
292                 break;
293 
294             mog2->apply(frame, foreground);
295 
296             stopTimer();
297         }
298 
299         // process last frame in sequence to get data for sanity test
300         for (; i < numIters; ++i)
301         {
302             cap >> frame;
303             ASSERT_FALSE(frame.empty());
304 
305             if (cn != 3)
306             {
307                 cv::Mat temp;
308                 if (cn == 1)
309                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
310                 else
311                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
312                 cv::swap(temp, frame);
313             }
314 
315             mog2->apply(frame, foreground);
316         }
317 
318         CPU_SANITY_CHECK(foreground);
319     }
320 }
321 
322 #endif
323 
324 //////////////////////////////////////////////////////
325 // MOG2GetBackgroundImage
326 
327 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
328 
329 PERF_TEST_P(Video_Cn, MOG2GetBackgroundImage,
330             Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
331                     CUDA_CHANNELS_1_3_4))
332 {
333     const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
334     const int cn = GET_PARAM(1);
335 
336     cv::VideoCapture cap(inputFile);
337     ASSERT_TRUE(cap.isOpened());
338 
339     cv::Mat frame;
340 
341     if (PERF_RUN_CUDA())
342     {
343         cv::Ptr<cv::BackgroundSubtractor> d_mog2 = cv::cuda::createBackgroundSubtractorMOG2();
344 
345         cv::cuda::GpuMat d_frame;
346         cv::cuda::GpuMat d_foreground;
347 
348         for (int i = 0; i < 10; ++i)
349         {
350             cap >> frame;
351             ASSERT_FALSE(frame.empty());
352 
353             if (cn != 3)
354             {
355                 cv::Mat temp;
356                 if (cn == 1)
357                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
358                 else
359                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
360                 cv::swap(temp, frame);
361             }
362 
363             d_frame.upload(frame);
364 
365             d_mog2->apply(d_frame, d_foreground);
366         }
367 
368         cv::cuda::GpuMat background;
369 
370         TEST_CYCLE() d_mog2->getBackgroundImage(background);
371 
372         CUDA_SANITY_CHECK(background, 1);
373     }
374     else
375     {
376         cv::Ptr<cv::BackgroundSubtractor> mog2 = cv::createBackgroundSubtractorMOG2();
377         cv::Mat foreground;
378 
379         for (int i = 0; i < 10; ++i)
380         {
381             cap >> frame;
382             ASSERT_FALSE(frame.empty());
383 
384             if (cn != 3)
385             {
386                 cv::Mat temp;
387                 if (cn == 1)
388                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
389                 else
390                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
391                 cv::swap(temp, frame);
392             }
393 
394             mog2->apply(frame, foreground);
395         }
396 
397         cv::Mat background;
398 
399         TEST_CYCLE() mog2->getBackgroundImage(background);
400 
401         CPU_SANITY_CHECK(background);
402     }
403 }
404 
405 #endif
406