b18cb17dfb3c35f7fcbca624db3a4f3e3582b222
[profile/ivi/opencv.git] / modules / gpu / perf / perf_video.cpp
1 #include "perf_precomp.hpp"
2
3 using namespace std;
4 using namespace testing;
5
6 namespace cv
7 {
8     template<> void Ptr<CvBGStatModel>::delete_obj()
9     {
10         cvReleaseBGStatModel(&obj);
11     }
12 }
13
14 namespace {
15
16 //////////////////////////////////////////////////////
17 // BroxOpticalFlow
18
19 typedef pair<string, string> pair_string;
20
21 DEF_PARAM_TEST_1(ImagePair, pair_string);
22
23 PERF_TEST_P(ImagePair, Video_BroxOpticalFlow,
24     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
25 {
26     declare.time(10);
27
28     cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
29     ASSERT_FALSE(frame0.empty());
30
31     cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
32     ASSERT_FALSE(frame1.empty());
33
34     frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);
35     frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);
36
37     if (PERF_RUN_GPU())
38     {
39         cv::gpu::GpuMat d_frame0(frame0);
40         cv::gpu::GpuMat d_frame1(frame1);
41         cv::gpu::GpuMat d_u;
42         cv::gpu::GpuMat d_v;
43
44         cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,
45                                         10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);
46
47         d_flow(d_frame0, d_frame1, d_u, d_v);
48
49         TEST_CYCLE()
50         {
51             d_flow(d_frame0, d_frame1, d_u, d_v);
52         }
53
54         GPU_SANITY_CHECK(d_u);
55         GPU_SANITY_CHECK(d_v);
56     }
57     else
58     {
59         FAIL() << "No such CPU implementation analogy";
60     }
61 }
62
63 //////////////////////////////////////////////////////
64 // InterpolateFrames
65
66 PERF_TEST_P(ImagePair, Video_InterpolateFrames,
67     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
68 {
69     cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
70     ASSERT_FALSE(frame0.empty());
71
72     cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
73     ASSERT_FALSE(frame1.empty());
74
75     frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);
76     frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);
77
78     if (PERF_RUN_GPU())
79     {
80         cv::gpu::GpuMat d_frame0(frame0);
81         cv::gpu::GpuMat d_frame1(frame1);
82         cv::gpu::GpuMat d_fu, d_fv;
83         cv::gpu::GpuMat d_bu, d_bv;
84
85         cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,
86                                         10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);
87
88         d_flow(d_frame0, d_frame1, d_fu, d_fv);
89         d_flow(d_frame1, d_frame0, d_bu, d_bv);
90
91         cv::gpu::GpuMat d_newFrame;
92         cv::gpu::GpuMat d_buf;
93
94         cv::gpu::interpolateFrames(d_frame0, d_frame1, d_fu, d_fv, d_bu, d_bv, 0.5f, d_newFrame, d_buf);
95
96         TEST_CYCLE()
97         {
98             cv::gpu::interpolateFrames(d_frame0, d_frame1, d_fu, d_fv, d_bu, d_bv, 0.5f, d_newFrame, d_buf);
99         }
100
101         GPU_SANITY_CHECK(d_fu);
102         GPU_SANITY_CHECK(d_fv);
103         GPU_SANITY_CHECK(d_bu);
104         GPU_SANITY_CHECK(d_bv);
105     }
106     else
107     {
108         FAIL() << "No such CPU implementation analogy";
109     }
110 }
111
112 //////////////////////////////////////////////////////
113 // CreateOpticalFlowNeedleMap
114
115 PERF_TEST_P(ImagePair, Video_CreateOpticalFlowNeedleMap,
116     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
117 {
118     cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
119     ASSERT_FALSE(frame0.empty());
120
121     cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
122     ASSERT_FALSE(frame1.empty());
123
124     frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);
125     frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);
126
127     if (PERF_RUN_GPU())
128     {
129         cv::gpu::GpuMat d_frame0(frame0);
130         cv::gpu::GpuMat d_frame1(frame1);
131         cv::gpu::GpuMat d_u;
132         cv::gpu::GpuMat d_v;
133
134         cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,
135                                         10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);
136
137         d_flow(d_frame0, d_frame1, d_u, d_v);
138
139         cv::gpu::GpuMat d_vertex, d_colors;
140
141         cv::gpu::createOpticalFlowNeedleMap(d_u, d_v, d_vertex, d_colors);
142
143         TEST_CYCLE()
144         {
145             cv::gpu::createOpticalFlowNeedleMap(d_u, d_v, d_vertex, d_colors);
146         }
147
148         GPU_SANITY_CHECK(d_vertex);
149         GPU_SANITY_CHECK(d_colors);
150     }
151     else
152     {
153         FAIL() << "No such CPU implementation analogy";
154     }
155 }
156
157 //////////////////////////////////////////////////////
158 // GoodFeaturesToTrack
159
160 DEF_PARAM_TEST(Image_MinDistance, string, double);
161
162 PERF_TEST_P(Image_MinDistance, Video_GoodFeaturesToTrack,
163     Combine(Values<string>("gpu/perf/aloe.png"), Values(0.0, 3.0)))
164 {
165     string fileName = GET_PARAM(0);
166     double minDistance = GET_PARAM(1);
167
168     cv::Mat image = readImage(fileName, cv::IMREAD_GRAYSCALE);
169     ASSERT_FALSE(image.empty());
170
171     if (PERF_RUN_GPU())
172     {
173         cv::gpu::GoodFeaturesToTrackDetector_GPU d_detector(8000, 0.01, minDistance);
174
175         cv::gpu::GpuMat d_image(image);
176         cv::gpu::GpuMat d_pts;
177
178         d_detector(d_image, d_pts);
179
180         TEST_CYCLE()
181         {
182             d_detector(d_image, d_pts);
183         }
184
185         GPU_SANITY_CHECK(d_pts);
186     }
187     else
188     {
189         cv::Mat pts;
190
191         cv::goodFeaturesToTrack(image, pts, 8000, 0.01, minDistance);
192
193         TEST_CYCLE()
194         {
195             cv::goodFeaturesToTrack(image, pts, 8000, 0.01, minDistance);
196         }
197
198         CPU_SANITY_CHECK(pts);
199     }
200 }
201
202 //////////////////////////////////////////////////////
203 // PyrLKOpticalFlowSparse
204
205 DEF_PARAM_TEST(ImagePair_Gray_NPts_WinSz_Levels_Iters, pair_string, bool, int, int, int, int);
206
207 PERF_TEST_P(ImagePair_Gray_NPts_WinSz_Levels_Iters, Video_PyrLKOpticalFlowSparse, Combine(
208     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")),
209     Bool(),
210     Values(1000, 2000, 4000, 8000),
211     Values(9, 13, 17, 21),
212     Values(1, 2, 3),
213     Values(1, 10, 30)))
214 {
215     declare.time(20.0);
216
217     pair_string imagePair = GET_PARAM(0);
218     bool useGray = GET_PARAM(1);
219     int points = GET_PARAM(2);
220     int winSize = GET_PARAM(3);
221     int levels = GET_PARAM(4);
222     int iters = GET_PARAM(5);
223
224     cv::Mat frame0 = readImage(imagePair.first, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
225     ASSERT_FALSE(frame0.empty());
226
227     cv::Mat frame1 = readImage(imagePair.second, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
228     ASSERT_FALSE(frame1.empty());
229
230     cv::Mat gray_frame;
231     if (useGray)
232         gray_frame = frame0;
233     else
234         cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY);
235
236     cv::Mat pts;
237     cv::goodFeaturesToTrack(gray_frame, pts, points, 0.01, 0.0);
238
239     if (PERF_RUN_GPU())
240     {
241         cv::gpu::GpuMat d_pts(pts.reshape(2, 1));
242
243         cv::gpu::PyrLKOpticalFlow d_pyrLK;
244         d_pyrLK.winSize = cv::Size(winSize, winSize);
245         d_pyrLK.maxLevel = levels - 1;
246         d_pyrLK.iters = iters;
247
248         cv::gpu::GpuMat d_frame0(frame0);
249         cv::gpu::GpuMat d_frame1(frame1);
250         cv::gpu::GpuMat d_nextPts;
251         cv::gpu::GpuMat d_status;
252
253         d_pyrLK.sparse(d_frame0, d_frame1, d_pts, d_nextPts, d_status);
254
255         TEST_CYCLE()
256         {
257             d_pyrLK.sparse(d_frame0, d_frame1, d_pts, d_nextPts, d_status);
258         }
259
260         GPU_SANITY_CHECK(d_status);
261     }
262     else
263     {
264         cv::Mat nextPts;
265         cv::Mat status;
266
267         cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, cv::noArray(),
268                                  cv::Size(winSize, winSize), levels - 1,
269                                  cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, iters, 0.01));
270
271         TEST_CYCLE()
272         {
273             cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, cv::noArray(),
274                                      cv::Size(winSize, winSize), levels - 1,
275                                      cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, iters, 0.01));
276         }
277
278         CPU_SANITY_CHECK(status);
279     }
280 }
281
282 //////////////////////////////////////////////////////
283 // PyrLKOpticalFlowDense
284
285 DEF_PARAM_TEST(ImagePair_WinSz_Levels_Iters, pair_string, int, int, int);
286
287 PERF_TEST_P(ImagePair_WinSz_Levels_Iters, Video_PyrLKOpticalFlowDense, Combine(
288     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")),
289     Values(3, 5, 7, 9, 13, 17, 21),
290     Values(1, 2, 3),
291     Values(1, 10)))
292 {
293     declare.time(30);
294
295     pair_string imagePair = GET_PARAM(0);
296     int winSize = GET_PARAM(1);
297     int levels = GET_PARAM(2);
298     int iters = GET_PARAM(3);
299
300     cv::Mat frame0 = readImage(imagePair.first, cv::IMREAD_GRAYSCALE);
301     ASSERT_FALSE(frame0.empty());
302
303     cv::Mat frame1 = readImage(imagePair.second, cv::IMREAD_GRAYSCALE);
304     ASSERT_FALSE(frame1.empty());
305
306     if (PERF_RUN_GPU())
307     {
308         cv::gpu::GpuMat d_frame0(frame0);
309         cv::gpu::GpuMat d_frame1(frame1);
310         cv::gpu::GpuMat d_u;
311         cv::gpu::GpuMat d_v;
312
313         cv::gpu::PyrLKOpticalFlow d_pyrLK;
314         d_pyrLK.winSize = cv::Size(winSize, winSize);
315         d_pyrLK.maxLevel = levels - 1;
316         d_pyrLK.iters = iters;
317
318         d_pyrLK.dense(d_frame0, d_frame1, d_u, d_v);
319
320         TEST_CYCLE()
321         {
322             d_pyrLK.dense(d_frame0, d_frame1, d_u, d_v);
323         }
324
325         GPU_SANITY_CHECK(d_u);
326         GPU_SANITY_CHECK(d_v);
327     }
328     else
329     {
330         FAIL() << "No such CPU implementation analogy";
331     }
332 }
333
334 //////////////////////////////////////////////////////
335 // FarnebackOpticalFlow
336
337 PERF_TEST_P(ImagePair, Video_FarnebackOpticalFlow,
338     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
339 {
340     declare.time(10);
341
342     cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
343     ASSERT_FALSE(frame0.empty());
344
345     cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
346     ASSERT_FALSE(frame1.empty());
347
348     int numLevels = 5;
349     double pyrScale = 0.5;
350     int winSize = 13;
351     int numIters = 10;
352     int polyN = 5;
353     double polySigma = 1.1;
354     int flags = 0;
355
356     if (PERF_RUN_GPU())
357     {
358         cv::gpu::GpuMat d_frame0(frame0);
359         cv::gpu::GpuMat d_frame1(frame1);
360         cv::gpu::GpuMat d_u;
361         cv::gpu::GpuMat d_v;
362
363         cv::gpu::FarnebackOpticalFlow d_farneback;
364         d_farneback.numLevels = numLevels;
365         d_farneback.pyrScale = pyrScale;
366         d_farneback.winSize = winSize;
367         d_farneback.numIters = numIters;
368         d_farneback.polyN = polyN;
369         d_farneback.polySigma = polySigma;
370         d_farneback.flags = flags;
371
372         d_farneback(d_frame0, d_frame1, d_u, d_v);
373
374         TEST_CYCLE()
375         {
376             d_farneback(d_frame0, d_frame1, d_u, d_v);
377         }
378
379         GPU_SANITY_CHECK(d_u);
380         GPU_SANITY_CHECK(d_v);
381     }
382     else
383     {
384         cv::Mat flow;
385
386         cv::calcOpticalFlowFarneback(frame0, frame1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
387
388         TEST_CYCLE()
389         {
390             cv::calcOpticalFlowFarneback(frame0, frame1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
391         }
392
393         CPU_SANITY_CHECK(flow);
394     }
395 }
396
397 //////////////////////////////////////////////////////
398 // FGDStatModel
399
400 DEF_PARAM_TEST_1(Video, string);
401
402 PERF_TEST_P(Video, DISABLED_Video_FGDStatModel, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
403 {
404     declare.time(60);
405
406     string inputFile = perf::TestBase::getDataPath(GetParam());
407
408     cv::VideoCapture cap(inputFile);
409     ASSERT_TRUE(cap.isOpened());
410
411     cv::Mat frame;
412     cap >> frame;
413     ASSERT_FALSE(frame.empty());
414
415     if (PERF_RUN_GPU())
416     {
417         cv::gpu::GpuMat d_frame(frame);
418
419         cv::gpu::FGDStatModel d_model(4);
420         d_model.create(d_frame);
421
422         for (int i = 0; i < 10; ++i)
423         {
424             cap >> frame;
425             ASSERT_FALSE(frame.empty());
426
427             d_frame.upload(frame);
428
429             startTimer(); next();
430             d_model.update(d_frame);
431             stopTimer();
432         }
433     }
434     else
435     {
436         IplImage ipl_frame = frame;
437         cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame));
438
439         for (int i = 0; i < 10; ++i)
440         {
441             cap >> frame;
442             ASSERT_FALSE(frame.empty());
443
444             ipl_frame = frame;
445
446             startTimer(); next();
447             cvUpdateBGStatModel(&ipl_frame, model);
448             stopTimer();
449         }
450     }
451 }
452
453 //////////////////////////////////////////////////////
454 // MOG
455
456 DEF_PARAM_TEST(Video_Cn_LearningRate, string, MatCn, double);
457
458 PERF_TEST_P(Video_Cn_LearningRate, DISABLED_Video_MOG,
459     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4, Values(0.0, 0.01)))
460 {
461     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
462     int cn = GET_PARAM(1);
463     float learningRate = static_cast<float>(GET_PARAM(2));
464
465     cv::VideoCapture cap(inputFile);
466     ASSERT_TRUE(cap.isOpened());
467
468     cv::Mat frame;
469
470     cap >> frame;
471     ASSERT_FALSE(frame.empty());
472
473     if (cn != 3)
474     {
475         cv::Mat temp;
476         if (cn == 1)
477             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
478         else
479             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
480         cv::swap(temp, frame);
481     }
482
483     if (PERF_RUN_GPU())
484     {
485         cv::gpu::GpuMat d_frame(frame);
486         cv::gpu::MOG_GPU d_mog;
487         cv::gpu::GpuMat d_foreground;
488
489         d_mog(d_frame, d_foreground, learningRate);
490
491         for (int i = 0; i < 10; ++i)
492         {
493             cap >> frame;
494             ASSERT_FALSE(frame.empty());
495
496             if (cn != 3)
497             {
498                 cv::Mat temp;
499                 if (cn == 1)
500                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
501                 else
502                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
503                 cv::swap(temp, frame);
504             }
505
506             d_frame.upload(frame);
507
508             startTimer(); next();
509             d_mog(d_frame, d_foreground, learningRate);
510             stopTimer();
511         }
512     }
513     else
514     {
515         cv::BackgroundSubtractorMOG mog;
516         cv::Mat foreground;
517
518         mog(frame, foreground, learningRate);
519
520         for (int i = 0; i < 10; ++i)
521         {
522             cap >> frame;
523             ASSERT_FALSE(frame.empty());
524
525             if (cn != 3)
526             {
527                 cv::Mat temp;
528                 if (cn == 1)
529                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
530                 else
531                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
532                 cv::swap(temp, frame);
533             }
534
535             startTimer(); next();
536             mog(frame, foreground, learningRate);
537             stopTimer();
538         }
539     }
540 }
541
542 //////////////////////////////////////////////////////
543 // MOG2
544
545 DEF_PARAM_TEST(Video_Cn, string, int);
546
547 PERF_TEST_P(Video_Cn, DISABLED_Video_MOG2,
548     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))
549 {
550     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
551     int cn = GET_PARAM(1);
552
553     cv::VideoCapture cap(inputFile);
554     ASSERT_TRUE(cap.isOpened());
555
556     cv::Mat frame;
557
558     cap >> frame;
559     ASSERT_FALSE(frame.empty());
560
561     if (cn != 3)
562     {
563         cv::Mat temp;
564         if (cn == 1)
565             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
566         else
567             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
568         cv::swap(temp, frame);
569     }
570
571     if (PERF_RUN_GPU())
572     {
573         cv::gpu::GpuMat d_frame(frame);
574         cv::gpu::MOG2_GPU d_mog2;
575         cv::gpu::GpuMat d_foreground;
576
577         d_mog2(d_frame, d_foreground);
578
579         for (int i = 0; i < 10; ++i)
580         {
581             cap >> frame;
582             ASSERT_FALSE(frame.empty());
583
584             if (cn != 3)
585             {
586                 cv::Mat temp;
587                 if (cn == 1)
588                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
589                 else
590                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
591                 cv::swap(temp, frame);
592             }
593
594             d_frame.upload(frame);
595
596             startTimer(); next();
597             d_mog2(d_frame, d_foreground);
598             stopTimer();
599         }
600     }
601     else
602     {
603         cv::BackgroundSubtractorMOG2 mog2;
604         cv::Mat foreground;
605
606         mog2(frame, foreground);
607
608         for (int i = 0; i < 10; ++i)
609         {
610             cap >> frame;
611             ASSERT_FALSE(frame.empty());
612
613             if (cn != 3)
614             {
615                 cv::Mat temp;
616                 if (cn == 1)
617                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
618                 else
619                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
620                 cv::swap(temp, frame);
621             }
622
623             startTimer(); next();
624             mog2(frame, foreground);
625             stopTimer();
626         }
627     }
628 }
629
630 //////////////////////////////////////////////////////
631 // MOG2GetBackgroundImage
632
633 PERF_TEST_P(Video_Cn, Video_MOG2GetBackgroundImage,
634     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))
635 {
636     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
637     int cn = GET_PARAM(1);
638
639     cv::VideoCapture cap(inputFile);
640     ASSERT_TRUE(cap.isOpened());
641
642     cv::Mat frame;
643
644     if (PERF_RUN_GPU())
645     {
646         cv::gpu::GpuMat d_frame;
647         cv::gpu::MOG2_GPU d_mog2;
648         cv::gpu::GpuMat d_foreground;
649
650         for (int i = 0; i < 10; ++i)
651         {
652             cap >> frame;
653             ASSERT_FALSE(frame.empty());
654
655             if (cn != 3)
656             {
657                 cv::Mat temp;
658                 if (cn == 1)
659                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
660                 else
661                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
662                 cv::swap(temp, frame);
663             }
664
665             d_frame.upload(frame);
666
667             d_mog2(d_frame, d_foreground);
668         }
669
670         cv::gpu::GpuMat d_background;
671         d_mog2.getBackgroundImage(d_background);
672
673         TEST_CYCLE()
674         {
675             d_mog2.getBackgroundImage(d_background);
676         }
677
678         GPU_SANITY_CHECK(d_background);
679     }
680     else
681     {
682         cv::BackgroundSubtractorMOG2 mog2;
683         cv::Mat foreground;
684
685         for (int i = 0; i < 10; ++i)
686         {
687             cap >> frame;
688             ASSERT_FALSE(frame.empty());
689
690             if (cn != 3)
691             {
692                 cv::Mat temp;
693                 if (cn == 1)
694                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
695                 else
696                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
697                 cv::swap(temp, frame);
698             }
699
700             mog2(frame, foreground);
701         }
702
703         cv::Mat background;
704         mog2.getBackgroundImage(background);
705
706         TEST_CYCLE()
707         {
708             mog2.getBackgroundImage(background);
709         }
710
711         CPU_SANITY_CHECK(background);
712     }
713 }
714
715 //////////////////////////////////////////////////////
716 // VIBE
717
718 PERF_TEST_P(Video_Cn, DISABLED_Video_VIBE,
719     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))
720 {
721     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
722     int cn = GET_PARAM(1);
723
724     cv::VideoCapture cap(inputFile);
725     ASSERT_TRUE(cap.isOpened());
726
727     cv::Mat frame;
728     cap >> frame;
729     ASSERT_FALSE(frame.empty());
730
731     if (cn != 3)
732     {
733         cv::Mat temp;
734         if (cn == 1)
735             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
736         else
737             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
738         cv::swap(temp, frame);
739     }
740
741     if (PERF_RUN_GPU())
742     {
743         cv::gpu::GpuMat d_frame(frame);
744         cv::gpu::VIBE_GPU d_vibe;
745         cv::gpu::GpuMat d_foreground;
746
747         d_vibe(d_frame, d_foreground);
748
749         for (int i = 0; i < 10; ++i)
750         {
751             cap >> frame;
752             ASSERT_FALSE(frame.empty());
753
754             if (cn != 3)
755             {
756                 cv::Mat temp;
757                 if (cn == 1)
758                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
759                 else
760                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
761                 cv::swap(temp, frame);
762             }
763
764             d_frame.upload(frame);
765
766             startTimer(); next();
767             d_vibe(d_frame, d_foreground);
768             stopTimer();
769         }
770     }
771     else
772     {
773         FAIL() << "No such CPU implementation analogy";
774     }
775 }
776
777 //////////////////////////////////////////////////////
778 // GMG
779
780 DEF_PARAM_TEST(Video_Cn_MaxFeatures, string, MatCn, int);
781
782 PERF_TEST_P(Video_Cn_MaxFeatures, DISABLED_Video_GMG,
783     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4, Values(20, 40, 60)))
784 {
785     std::string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
786     int cn = GET_PARAM(1);
787     int maxFeatures = GET_PARAM(2);
788
789     cv::VideoCapture cap(inputFile);
790     ASSERT_TRUE(cap.isOpened());
791
792     cv::Mat frame;
793     cap >> frame;
794     ASSERT_FALSE(frame.empty());
795
796     if (cn != 3)
797     {
798         cv::Mat temp;
799         if (cn == 1)
800             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
801         else
802             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
803         cv::swap(temp, frame);
804     }
805
806     if (PERF_RUN_GPU())
807     {
808         cv::gpu::GpuMat d_frame(frame);
809         cv::gpu::GpuMat d_fgmask;
810
811         cv::gpu::GMG_GPU d_gmg;
812         d_gmg.maxFeatures = maxFeatures;
813
814         d_gmg(d_frame, d_fgmask);
815
816         for (int i = 0; i < 150; ++i)
817         {
818             cap >> frame;
819             if (frame.empty())
820             {
821                 cap.release();
822                 cap.open(inputFile);
823                 cap >> frame;
824             }
825
826             if (cn != 3)
827             {
828                 cv::Mat temp;
829                 if (cn == 1)
830                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
831                 else
832                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
833                 cv::swap(temp, frame);
834             }
835
836             d_frame.upload(frame);
837
838             startTimer(); next();
839             d_gmg(d_frame, d_fgmask);
840             stopTimer();
841         }
842     }
843     else
844     {
845         cv::Mat fgmask;
846         cv::Mat zeros(frame.size(), CV_8UC1, cv::Scalar::all(0));
847
848         cv::BackgroundSubtractorGMG gmg;
849         gmg.set("maxFeatures", maxFeatures);
850         gmg.initialize(frame.size(), 0.0, 255.0);
851
852         gmg(frame, fgmask);
853
854         for (int i = 0; i < 150; ++i)
855         {
856             cap >> frame;
857             if (frame.empty())
858             {
859                 cap.release();
860                 cap.open(inputFile);
861                 cap >> frame;
862             }
863
864             if (cn != 3)
865             {
866                 cv::Mat temp;
867                 if (cn == 1)
868                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
869                 else
870                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
871                 cv::swap(temp, frame);
872             }
873
874             startTimer(); next();
875             gmg(frame, fgmask);
876             stopTimer();
877         }
878     }
879 }
880
881 //////////////////////////////////////////////////////
882 // VideoWriter
883
884 PERF_TEST_P(Video, DISABLED_Video_VideoWriter, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
885 {
886     declare.time(30);
887
888     string inputFile = perf::TestBase::getDataPath(GetParam());
889     string outputFile = cv::tempfile(".avi");
890
891     const double FPS = 25.0;
892
893     cv::VideoCapture reader(inputFile);
894     ASSERT_TRUE( reader.isOpened() );
895
896     cv::Mat frame;
897
898     if (PERF_RUN_GPU())
899     {
900         cv::gpu::VideoWriter_GPU d_writer;
901
902         cv::gpu::GpuMat d_frame;
903
904         for (int i = 0; i < 10; ++i)
905         {
906             reader >> frame;
907             ASSERT_FALSE(frame.empty());
908
909             d_frame.upload(frame);
910
911             if (!d_writer.isOpened())
912                 d_writer.open(outputFile, frame.size(), FPS);
913
914             startTimer(); next();
915             d_writer.write(d_frame);
916             stopTimer();
917         }
918     }
919     else
920     {
921         cv::VideoWriter writer;
922
923         for (int i = 0; i < 10; ++i)
924         {
925             reader >> frame;
926             ASSERT_FALSE(frame.empty());
927
928             if (!writer.isOpened())
929                 writer.open(outputFile, CV_FOURCC('X', 'V', 'I', 'D'), FPS, frame.size());
930
931             startTimer(); next();
932             writer.write(frame);
933             stopTimer();
934         }
935     }
936 }
937
938 //////////////////////////////////////////////////////
939 // VideoReader
940
941 PERF_TEST_P(Video, Video_VideoReader, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
942 {
943     declare.time(20);
944
945     string inputFile = perf::TestBase::getDataPath(GetParam());
946
947     if (PERF_RUN_GPU())
948     {
949         cv::gpu::VideoReader_GPU d_reader(inputFile);
950         ASSERT_TRUE( d_reader.isOpened() );
951
952         cv::gpu::GpuMat d_frame;
953
954         d_reader.read(d_frame);
955
956         TEST_CYCLE_N(10)
957         {
958             d_reader.read(d_frame);
959         }
960
961         GPU_SANITY_CHECK(d_frame);
962     }
963     else
964     {
965         cv::VideoCapture reader(inputFile);
966         ASSERT_TRUE( reader.isOpened() );
967
968         cv::Mat frame;
969
970         reader >> frame;
971
972         TEST_CYCLE_N(10)
973         {
974             reader >> frame;
975         }
976
977         CPU_SANITY_CHECK(frame);
978     }
979 }
980
981 } // namespace