Merge pull request #1663 from vpisarev:ocl_experiments3
[profile/ivi/opencv.git] / modules / ocl / perf / perf_bgfg.cpp
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) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Fangfang Bai, fangfang@multicorewareinc.com
19 //    Jin Ma,       jin@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors as is and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46 #include "perf_precomp.hpp"
47
48 using namespace perf;
49 using namespace std;
50 using namespace cv::ocl;
51 using namespace cv;
52 using std::tr1::tuple;
53 using std::tr1::get;
54
55 #if defined(HAVE_XINE)         || \
56     defined(HAVE_GSTREAMER)    || \
57     defined(HAVE_QUICKTIME)    || \
58     defined(HAVE_AVFOUNDATION) || \
59     defined(HAVE_FFMPEG)       || \
60     defined(WIN32)
61
62 #  define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
63 #else
64 #  define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
65 #endif
66
67 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
68
69 static void cvtFrameFmt(vector<Mat>& input, vector<Mat>& output)
70 {
71     for(int i = 0; i< (int)(input.size()); i++)
72     {
73         cvtColor(input[i], output[i], COLOR_RGB2GRAY);
74     }
75 }
76
77 //prepare data for CPU
78 static void prepareData(VideoCapture& cap, int cn, vector<Mat>& frame_buffer)
79 {
80     cv::Mat frame;
81     std::vector<Mat> frame_buffer_init;
82     int nFrame = (int)frame_buffer.size();
83     for(int i = 0; i < nFrame; i++)
84     {
85         cap >> frame;
86         ASSERT_FALSE(frame.empty());
87         frame_buffer_init.push_back(frame);
88     }
89
90     if(cn == 1)
91         cvtFrameFmt(frame_buffer_init, frame_buffer);
92     else
93         frame_buffer = frame_buffer_init;
94 }
95
96 //copy CPU data to GPU
97 static void prepareData(vector<Mat>& frame_buffer, vector<oclMat>& frame_buffer_ocl)
98 {
99     for(int i = 0; i < (int)frame_buffer.size(); i++)
100         frame_buffer_ocl.push_back(cv::ocl::oclMat(frame_buffer[i]));
101 }
102
103 ///////////// MOG ////////////////////////
104
105 typedef tuple<string, int, double> VideoMOGParamType;
106 typedef TestBaseWithParam<VideoMOGParamType> VideoMOGFixture;
107
108 PERF_TEST_P(VideoMOGFixture, MOG,
109             ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
110             ::testing::Values(1, 3),
111             ::testing::Values(0.0, 0.01)))
112 {
113     VideoMOGParamType params = GetParam();
114
115     const string inputFile = perf::TestBase::getDataPath(get<0>(params));
116     const int cn = get<1>(params);
117     const float learningRate = static_cast<float>(get<2>(params));
118
119     const int nFrame = 5;
120
121     Mat foreground_cpu;
122     std::vector<Mat> frame_buffer(nFrame);
123     std::vector<oclMat> frame_buffer_ocl;
124
125     cv::VideoCapture cap(inputFile);
126     ASSERT_TRUE(cap.isOpened());
127
128     prepareData(cap, cn, frame_buffer);
129
130     cv::Mat foreground;
131     cv::ocl::oclMat foreground_d;
132     if(RUN_PLAIN_IMPL)
133     {
134         TEST_CYCLE()
135         {
136             cv::Ptr<cv::BackgroundSubtractorMOG> mog = createBackgroundSubtractorMOG();
137             foreground.release();
138             for (int i = 0; i < nFrame; i++)
139             {
140                 mog->apply(frame_buffer[i], foreground, learningRate);
141             }
142         }
143         SANITY_CHECK(foreground);
144     }
145     else if(RUN_OCL_IMPL)
146     {
147         prepareData(frame_buffer, frame_buffer_ocl);
148         CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
149         OCL_TEST_CYCLE()
150         {
151             cv::ocl::MOG d_mog;
152             foreground_d.release();
153             for (int i = 0; i < nFrame; ++i)
154             {
155                 d_mog(frame_buffer_ocl[i], foreground_d, learningRate);
156             }
157         }
158         foreground_d.download(foreground);
159         SANITY_CHECK(foreground);
160     }
161     else
162         OCL_PERF_ELSE
163 }
164
165 ///////////// MOG2 ////////////////////////
166
167 typedef tuple<string, int> VideoMOG2ParamType;
168 typedef TestBaseWithParam<VideoMOG2ParamType> VideoMOG2Fixture;
169
170 PERF_TEST_P(VideoMOG2Fixture, DISABLED_MOG2, // TODO Disabled: random hungs on buildslave
171             ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
172             ::testing::Values(1, 3)))
173 {
174     VideoMOG2ParamType params = GetParam();
175
176     const string inputFile = perf::TestBase::getDataPath(get<0>(params));
177     const int cn = get<1>(params);
178     int nFrame = 5;
179
180     std::vector<cv::Mat> frame_buffer(nFrame);
181     std::vector<cv::ocl::oclMat> frame_buffer_ocl;
182
183     cv::VideoCapture cap(inputFile);
184     ASSERT_TRUE(cap.isOpened());
185     prepareData(cap, cn, frame_buffer);
186     cv::Mat foreground;
187     cv::ocl::oclMat foreground_d;
188
189     if(RUN_PLAIN_IMPL)
190     {
191         TEST_CYCLE()
192         {
193             cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
194             mog2->setDetectShadows(false);
195             foreground.release();
196
197             for (int i = 0; i < nFrame; i++)
198             {
199                 mog2->apply(frame_buffer[i], foreground);
200             }
201         }
202         SANITY_CHECK(foreground);
203     }
204     else if(RUN_OCL_IMPL)
205     {
206         prepareData(frame_buffer, frame_buffer_ocl);
207         CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
208         OCL_TEST_CYCLE()
209         {
210             cv::ocl::MOG2 d_mog2;
211             foreground_d.release();
212             for (int i = 0; i < nFrame; i++)
213             {
214                 d_mog2(frame_buffer_ocl[i], foreground_d);
215             }
216         }
217         foreground_d.download(foreground);
218         SANITY_CHECK(foreground);
219     }
220     else
221         OCL_PERF_ELSE
222 }
223
224 ///////////// MOG2_GetBackgroundImage //////////////////
225
226 typedef TestBaseWithParam<VideoMOG2ParamType> Video_MOG2GetBackgroundImage;
227
228 PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2,
229             ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
230             ::testing::Values(3)))
231 {
232     VideoMOG2ParamType params = GetParam();
233
234     const string inputFile = perf::TestBase::getDataPath(get<0>(params));
235     const int cn = get<1>(params);
236     int nFrame = 5;
237
238     std::vector<cv::Mat> frame_buffer(nFrame);
239     std::vector<cv::ocl::oclMat> frame_buffer_ocl;
240
241     cv::VideoCapture cap(inputFile);
242     ASSERT_TRUE(cap.isOpened());
243
244     prepareData(cap, cn, frame_buffer);
245
246     cv::Mat foreground;
247     cv::Mat background;
248     cv::ocl::oclMat foreground_d;
249     cv::ocl::oclMat background_d;
250
251     if(RUN_PLAIN_IMPL)
252     {
253         TEST_CYCLE()
254         {
255             cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
256             mog2->setDetectShadows(false);
257             foreground.release();
258             background.release();
259             for (int i = 0; i < nFrame; i++)
260             {
261                 mog2->apply(frame_buffer[i], foreground);
262             }
263             mog2->getBackgroundImage(background);
264         }
265         SANITY_CHECK(background);
266     }
267     else if(RUN_OCL_IMPL)
268     {
269         prepareData(frame_buffer, frame_buffer_ocl);
270         CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
271         OCL_TEST_CYCLE()
272         {
273             cv::ocl::MOG2 d_mog2;
274             foreground_d.release();
275             background_d.release();
276             for (int i = 0; i < nFrame; i++)
277             {
278                 d_mog2(frame_buffer_ocl[i], foreground_d);
279             }
280             d_mog2.getBackgroundImage(background_d);
281         }
282         background_d.download(background);
283         SANITY_CHECK(background);
284     }
285     else
286         OCL_PERF_ELSE
287 }
288
289 #endif