b228580fdea8c8cc74e93f0b26e59d3b8fadbd50
[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 // OpticalFlowDual_TVL1
399
400 PERF_TEST_P(ImagePair, Video_OpticalFlowDual_TVL1,
401     Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))
402 {
403     declare.time(20);
404
405     cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
406     ASSERT_FALSE(frame0.empty());
407
408     cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);
409     ASSERT_FALSE(frame1.empty());
410
411     if (PERF_RUN_GPU())
412     {
413         cv::gpu::GpuMat d_frame0(frame0);
414         cv::gpu::GpuMat d_frame1(frame1);
415         cv::gpu::GpuMat d_flowx;
416         cv::gpu::GpuMat d_flowy;
417
418         cv::gpu::OpticalFlowDual_TVL1_GPU d_alg;
419
420         d_alg(d_frame0, d_frame1, d_flowx, d_flowy);
421
422         TEST_CYCLE()
423         {
424             d_alg(d_frame0, d_frame1, d_flowx, d_flowy);
425         }
426
427         GPU_SANITY_CHECK(d_flowx);
428         GPU_SANITY_CHECK(d_flowy);
429     }
430     else
431     {
432         cv::Mat flow;
433
434         cv::OpticalFlowDual_TVL1 alg;
435
436         alg(frame0, frame1, flow);
437
438         TEST_CYCLE()
439         {
440             alg(frame0, frame1, flow);
441         }
442
443         CPU_SANITY_CHECK(flow);
444     }
445 }
446
447 //////////////////////////////////////////////////////
448 // FGDStatModel
449
450 DEF_PARAM_TEST_1(Video, string);
451
452 PERF_TEST_P(Video, DISABLED_Video_FGDStatModel, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
453 {
454     declare.time(60);
455
456     string inputFile = perf::TestBase::getDataPath(GetParam());
457
458     cv::VideoCapture cap(inputFile);
459     ASSERT_TRUE(cap.isOpened());
460
461     cv::Mat frame;
462     cap >> frame;
463     ASSERT_FALSE(frame.empty());
464
465     if (PERF_RUN_GPU())
466     {
467         cv::gpu::GpuMat d_frame(frame);
468
469         cv::gpu::FGDStatModel d_model(4);
470         d_model.create(d_frame);
471
472         for (int i = 0; i < 10; ++i)
473         {
474             cap >> frame;
475             ASSERT_FALSE(frame.empty());
476
477             d_frame.upload(frame);
478
479             startTimer(); next();
480             d_model.update(d_frame);
481             stopTimer();
482         }
483     }
484     else
485     {
486         IplImage ipl_frame = frame;
487         cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame));
488
489         for (int i = 0; i < 10; ++i)
490         {
491             cap >> frame;
492             ASSERT_FALSE(frame.empty());
493
494             ipl_frame = frame;
495
496             startTimer(); next();
497             cvUpdateBGStatModel(&ipl_frame, model);
498             stopTimer();
499         }
500     }
501 }
502
503 //////////////////////////////////////////////////////
504 // MOG
505
506 DEF_PARAM_TEST(Video_Cn_LearningRate, string, MatCn, double);
507
508 PERF_TEST_P(Video_Cn_LearningRate, DISABLED_Video_MOG,
509     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4, Values(0.0, 0.01)))
510 {
511     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
512     int cn = GET_PARAM(1);
513     float learningRate = static_cast<float>(GET_PARAM(2));
514
515     cv::VideoCapture cap(inputFile);
516     ASSERT_TRUE(cap.isOpened());
517
518     cv::Mat frame;
519
520     cap >> frame;
521     ASSERT_FALSE(frame.empty());
522
523     if (cn != 3)
524     {
525         cv::Mat temp;
526         if (cn == 1)
527             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
528         else
529             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
530         cv::swap(temp, frame);
531     }
532
533     if (PERF_RUN_GPU())
534     {
535         cv::gpu::GpuMat d_frame(frame);
536         cv::gpu::MOG_GPU d_mog;
537         cv::gpu::GpuMat d_foreground;
538
539         d_mog(d_frame, d_foreground, learningRate);
540
541         for (int i = 0; i < 10; ++i)
542         {
543             cap >> frame;
544             ASSERT_FALSE(frame.empty());
545
546             if (cn != 3)
547             {
548                 cv::Mat temp;
549                 if (cn == 1)
550                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
551                 else
552                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
553                 cv::swap(temp, frame);
554             }
555
556             d_frame.upload(frame);
557
558             startTimer(); next();
559             d_mog(d_frame, d_foreground, learningRate);
560             stopTimer();
561         }
562     }
563     else
564     {
565         cv::BackgroundSubtractorMOG mog;
566         cv::Mat foreground;
567
568         mog(frame, foreground, learningRate);
569
570         for (int i = 0; i < 10; ++i)
571         {
572             cap >> frame;
573             ASSERT_FALSE(frame.empty());
574
575             if (cn != 3)
576             {
577                 cv::Mat temp;
578                 if (cn == 1)
579                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
580                 else
581                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
582                 cv::swap(temp, frame);
583             }
584
585             startTimer(); next();
586             mog(frame, foreground, learningRate);
587             stopTimer();
588         }
589     }
590 }
591
592 //////////////////////////////////////////////////////
593 // MOG2
594
595 DEF_PARAM_TEST(Video_Cn, string, int);
596
597 PERF_TEST_P(Video_Cn, DISABLED_Video_MOG2,
598     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))
599 {
600     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
601     int cn = GET_PARAM(1);
602
603     cv::VideoCapture cap(inputFile);
604     ASSERT_TRUE(cap.isOpened());
605
606     cv::Mat frame;
607
608     cap >> frame;
609     ASSERT_FALSE(frame.empty());
610
611     if (cn != 3)
612     {
613         cv::Mat temp;
614         if (cn == 1)
615             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
616         else
617             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
618         cv::swap(temp, frame);
619     }
620
621     if (PERF_RUN_GPU())
622     {
623         cv::gpu::GpuMat d_frame(frame);
624         cv::gpu::MOG2_GPU d_mog2;
625         cv::gpu::GpuMat d_foreground;
626
627         d_mog2(d_frame, d_foreground);
628
629         for (int i = 0; i < 10; ++i)
630         {
631             cap >> frame;
632             ASSERT_FALSE(frame.empty());
633
634             if (cn != 3)
635             {
636                 cv::Mat temp;
637                 if (cn == 1)
638                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
639                 else
640                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
641                 cv::swap(temp, frame);
642             }
643
644             d_frame.upload(frame);
645
646             startTimer(); next();
647             d_mog2(d_frame, d_foreground);
648             stopTimer();
649         }
650     }
651     else
652     {
653         cv::BackgroundSubtractorMOG2 mog2;
654         cv::Mat foreground;
655
656         mog2(frame, foreground);
657
658         for (int i = 0; i < 10; ++i)
659         {
660             cap >> frame;
661             ASSERT_FALSE(frame.empty());
662
663             if (cn != 3)
664             {
665                 cv::Mat temp;
666                 if (cn == 1)
667                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
668                 else
669                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
670                 cv::swap(temp, frame);
671             }
672
673             startTimer(); next();
674             mog2(frame, foreground);
675             stopTimer();
676         }
677     }
678 }
679
680 //////////////////////////////////////////////////////
681 // MOG2GetBackgroundImage
682
683 PERF_TEST_P(Video_Cn, Video_MOG2GetBackgroundImage,
684     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))
685 {
686     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
687     int cn = GET_PARAM(1);
688
689     cv::VideoCapture cap(inputFile);
690     ASSERT_TRUE(cap.isOpened());
691
692     cv::Mat frame;
693
694     if (PERF_RUN_GPU())
695     {
696         cv::gpu::GpuMat d_frame;
697         cv::gpu::MOG2_GPU d_mog2;
698         cv::gpu::GpuMat d_foreground;
699
700         for (int i = 0; i < 10; ++i)
701         {
702             cap >> frame;
703             ASSERT_FALSE(frame.empty());
704
705             if (cn != 3)
706             {
707                 cv::Mat temp;
708                 if (cn == 1)
709                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
710                 else
711                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
712                 cv::swap(temp, frame);
713             }
714
715             d_frame.upload(frame);
716
717             d_mog2(d_frame, d_foreground);
718         }
719
720         cv::gpu::GpuMat d_background;
721         d_mog2.getBackgroundImage(d_background);
722
723         TEST_CYCLE()
724         {
725             d_mog2.getBackgroundImage(d_background);
726         }
727
728         GPU_SANITY_CHECK(d_background);
729     }
730     else
731     {
732         cv::BackgroundSubtractorMOG2 mog2;
733         cv::Mat foreground;
734
735         for (int i = 0; i < 10; ++i)
736         {
737             cap >> frame;
738             ASSERT_FALSE(frame.empty());
739
740             if (cn != 3)
741             {
742                 cv::Mat temp;
743                 if (cn == 1)
744                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
745                 else
746                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
747                 cv::swap(temp, frame);
748             }
749
750             mog2(frame, foreground);
751         }
752
753         cv::Mat background;
754         mog2.getBackgroundImage(background);
755
756         TEST_CYCLE()
757         {
758             mog2.getBackgroundImage(background);
759         }
760
761         CPU_SANITY_CHECK(background);
762     }
763 }
764
765 //////////////////////////////////////////////////////
766 // VIBE
767
768 PERF_TEST_P(Video_Cn, DISABLED_Video_VIBE,
769     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))
770 {
771     string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
772     int cn = GET_PARAM(1);
773
774     cv::VideoCapture cap(inputFile);
775     ASSERT_TRUE(cap.isOpened());
776
777     cv::Mat frame;
778     cap >> frame;
779     ASSERT_FALSE(frame.empty());
780
781     if (cn != 3)
782     {
783         cv::Mat temp;
784         if (cn == 1)
785             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
786         else
787             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
788         cv::swap(temp, frame);
789     }
790
791     if (PERF_RUN_GPU())
792     {
793         cv::gpu::GpuMat d_frame(frame);
794         cv::gpu::VIBE_GPU d_vibe;
795         cv::gpu::GpuMat d_foreground;
796
797         d_vibe(d_frame, d_foreground);
798
799         for (int i = 0; i < 10; ++i)
800         {
801             cap >> frame;
802             ASSERT_FALSE(frame.empty());
803
804             if (cn != 3)
805             {
806                 cv::Mat temp;
807                 if (cn == 1)
808                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
809                 else
810                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
811                 cv::swap(temp, frame);
812             }
813
814             d_frame.upload(frame);
815
816             startTimer(); next();
817             d_vibe(d_frame, d_foreground);
818             stopTimer();
819         }
820     }
821     else
822     {
823         FAIL() << "No such CPU implementation analogy";
824     }
825 }
826
827 //////////////////////////////////////////////////////
828 // GMG
829
830 DEF_PARAM_TEST(Video_Cn_MaxFeatures, string, MatCn, int);
831
832 PERF_TEST_P(Video_Cn_MaxFeatures, DISABLED_Video_GMG,
833     Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4, Values(20, 40, 60)))
834 {
835     std::string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
836     int cn = GET_PARAM(1);
837     int maxFeatures = GET_PARAM(2);
838
839     cv::VideoCapture cap(inputFile);
840     ASSERT_TRUE(cap.isOpened());
841
842     cv::Mat frame;
843     cap >> frame;
844     ASSERT_FALSE(frame.empty());
845
846     if (cn != 3)
847     {
848         cv::Mat temp;
849         if (cn == 1)
850             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
851         else
852             cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
853         cv::swap(temp, frame);
854     }
855
856     if (PERF_RUN_GPU())
857     {
858         cv::gpu::GpuMat d_frame(frame);
859         cv::gpu::GpuMat d_fgmask;
860
861         cv::gpu::GMG_GPU d_gmg;
862         d_gmg.maxFeatures = maxFeatures;
863
864         d_gmg(d_frame, d_fgmask);
865
866         for (int i = 0; i < 150; ++i)
867         {
868             cap >> frame;
869             if (frame.empty())
870             {
871                 cap.release();
872                 cap.open(inputFile);
873                 cap >> frame;
874             }
875
876             if (cn != 3)
877             {
878                 cv::Mat temp;
879                 if (cn == 1)
880                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
881                 else
882                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
883                 cv::swap(temp, frame);
884             }
885
886             d_frame.upload(frame);
887
888             startTimer(); next();
889             d_gmg(d_frame, d_fgmask);
890             stopTimer();
891         }
892     }
893     else
894     {
895         cv::Mat fgmask;
896         cv::Mat zeros(frame.size(), CV_8UC1, cv::Scalar::all(0));
897
898         cv::BackgroundSubtractorGMG gmg;
899         gmg.set("maxFeatures", maxFeatures);
900         gmg.initialize(frame.size(), 0.0, 255.0);
901
902         gmg(frame, fgmask);
903
904         for (int i = 0; i < 150; ++i)
905         {
906             cap >> frame;
907             if (frame.empty())
908             {
909                 cap.release();
910                 cap.open(inputFile);
911                 cap >> frame;
912             }
913
914             if (cn != 3)
915             {
916                 cv::Mat temp;
917                 if (cn == 1)
918                     cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
919                 else
920                     cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);
921                 cv::swap(temp, frame);
922             }
923
924             startTimer(); next();
925             gmg(frame, fgmask);
926             stopTimer();
927         }
928     }
929 }
930
931 //////////////////////////////////////////////////////
932 // VideoWriter
933
934 PERF_TEST_P(Video, DISABLED_Video_VideoWriter, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
935 {
936     declare.time(30);
937
938     string inputFile = perf::TestBase::getDataPath(GetParam());
939     string outputFile = cv::tempfile(".avi");
940
941     const double FPS = 25.0;
942
943     cv::VideoCapture reader(inputFile);
944     ASSERT_TRUE( reader.isOpened() );
945
946     cv::Mat frame;
947
948     if (PERF_RUN_GPU())
949     {
950         cv::gpu::VideoWriter_GPU d_writer;
951
952         cv::gpu::GpuMat d_frame;
953
954         for (int i = 0; i < 10; ++i)
955         {
956             reader >> frame;
957             ASSERT_FALSE(frame.empty());
958
959             d_frame.upload(frame);
960
961             if (!d_writer.isOpened())
962                 d_writer.open(outputFile, frame.size(), FPS);
963
964             startTimer(); next();
965             d_writer.write(d_frame);
966             stopTimer();
967         }
968     }
969     else
970     {
971         cv::VideoWriter writer;
972
973         for (int i = 0; i < 10; ++i)
974         {
975             reader >> frame;
976             ASSERT_FALSE(frame.empty());
977
978             if (!writer.isOpened())
979                 writer.open(outputFile, CV_FOURCC('X', 'V', 'I', 'D'), FPS, frame.size());
980
981             startTimer(); next();
982             writer.write(frame);
983             stopTimer();
984         }
985     }
986 }
987
988 //////////////////////////////////////////////////////
989 // VideoReader
990
991 PERF_TEST_P(Video, Video_VideoReader, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
992 {
993     declare.time(20);
994
995     string inputFile = perf::TestBase::getDataPath(GetParam());
996
997     if (PERF_RUN_GPU())
998     {
999         cv::gpu::VideoReader_GPU d_reader(inputFile);
1000         ASSERT_TRUE( d_reader.isOpened() );
1001
1002         cv::gpu::GpuMat d_frame;
1003
1004         d_reader.read(d_frame);
1005
1006         TEST_CYCLE_N(10)
1007         {
1008             d_reader.read(d_frame);
1009         }
1010
1011         GPU_SANITY_CHECK(d_frame);
1012     }
1013     else
1014     {
1015         cv::VideoCapture reader(inputFile);
1016         ASSERT_TRUE( reader.isOpened() );
1017
1018         cv::Mat frame;
1019
1020         reader >> frame;
1021
1022         TEST_CYCLE_N(10)
1023         {
1024             reader >> frame;
1025         }
1026
1027         CPU_SANITY_CHECK(frame);
1028     }
1029 }
1030
1031 } // namespace