1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Fangfang Bai, fangfang@multicorewareinc.com
19 // Jin Ma, jin@multicorewareinc.com
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
24 // * Redistribution's of source code must retain the above copyright notice,
25 // this list of conditions and the following disclaimer.
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.
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.
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.
46 #include "perf_precomp.hpp"
50 using namespace cv::ocl;
52 using std::tr1::tuple;
55 #if defined(HAVE_XINE) || \
56 defined(HAVE_GSTREAMER) || \
57 defined(HAVE_QUICKTIME) || \
58 defined(HAVE_AVFOUNDATION) || \
59 defined(HAVE_FFMPEG) || \
62 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
64 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
67 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
69 static void cvtFrameFmt(vector<Mat>& input, vector<Mat>& output)
71 for(int i = 0; i< (int)(input.size()); i++)
73 cvtColor(input[i], output[i], COLOR_RGB2GRAY);
77 //prepare data for CPU
78 static void prepareData(VideoCapture& cap, int cn, vector<Mat>& frame_buffer)
81 std::vector<Mat> frame_buffer_init;
82 int nFrame = (int)frame_buffer.size();
83 for(int i = 0; i < nFrame; i++)
86 ASSERT_FALSE(frame.empty());
87 frame_buffer_init.push_back(frame);
91 cvtFrameFmt(frame_buffer_init, frame_buffer);
93 frame_buffer = frame_buffer_init;
96 //copy CPU data to GPU
97 static void prepareData(vector<Mat>& frame_buffer, vector<oclMat>& frame_buffer_ocl)
99 for(int i = 0; i < (int)frame_buffer.size(); i++)
100 frame_buffer_ocl.push_back(cv::ocl::oclMat(frame_buffer[i]));
103 ///////////// MOG ////////////////////////
105 typedef tuple<string, int, double> VideoMOGParamType;
106 typedef TestBaseWithParam<VideoMOGParamType> VideoMOGFixture;
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)))
113 VideoMOGParamType params = GetParam();
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));
119 const int nFrame = 5;
122 std::vector<Mat> frame_buffer(nFrame);
123 std::vector<oclMat> frame_buffer_ocl;
125 cv::VideoCapture cap(inputFile);
126 ASSERT_TRUE(cap.isOpened());
128 prepareData(cap, cn, frame_buffer);
131 cv::ocl::oclMat foreground_d;
136 cv::Ptr<cv::BackgroundSubtractorMOG> mog = createBackgroundSubtractorMOG();
137 foreground.release();
138 for (int i = 0; i < nFrame; i++)
140 mog->apply(frame_buffer[i], foreground, learningRate);
143 SANITY_CHECK(foreground);
145 else if(RUN_OCL_IMPL)
147 prepareData(frame_buffer, frame_buffer_ocl);
148 CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
152 foreground_d.release();
153 for (int i = 0; i < nFrame; ++i)
155 d_mog(frame_buffer_ocl[i], foreground_d, learningRate);
158 foreground_d.download(foreground);
159 SANITY_CHECK(foreground);
165 ///////////// MOG2 ////////////////////////
167 typedef tuple<string, int> VideoMOG2ParamType;
168 typedef TestBaseWithParam<VideoMOG2ParamType> VideoMOG2Fixture;
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)))
174 VideoMOG2ParamType params = GetParam();
176 const string inputFile = perf::TestBase::getDataPath(get<0>(params));
177 const int cn = get<1>(params);
180 std::vector<cv::Mat> frame_buffer(nFrame);
181 std::vector<cv::ocl::oclMat> frame_buffer_ocl;
183 cv::VideoCapture cap(inputFile);
184 ASSERT_TRUE(cap.isOpened());
185 prepareData(cap, cn, frame_buffer);
187 cv::ocl::oclMat foreground_d;
193 cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2();
194 mog2->setDetectShadows(false);
195 foreground.release();
197 for (int i = 0; i < nFrame; i++)
199 mog2->apply(frame_buffer[i], foreground);
202 SANITY_CHECK(foreground);
204 else if(RUN_OCL_IMPL)
206 prepareData(frame_buffer, frame_buffer_ocl);
207 CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
210 cv::ocl::MOG2 d_mog2;
211 foreground_d.release();
212 for (int i = 0; i < nFrame; i++)
214 d_mog2(frame_buffer_ocl[i], foreground_d);
217 foreground_d.download(foreground);
218 SANITY_CHECK(foreground);
224 ///////////// MOG2_GetBackgroundImage //////////////////
226 typedef TestBaseWithParam<VideoMOG2ParamType> Video_MOG2GetBackgroundImage;
228 PERF_TEST_P(Video_MOG2GetBackgroundImage, MOG2,
229 ::testing::Combine(::testing::Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"),
230 ::testing::Values(3)))
232 VideoMOG2ParamType params = GetParam();
234 const string inputFile = perf::TestBase::getDataPath(get<0>(params));
235 const int cn = get<1>(params);
238 std::vector<cv::Mat> frame_buffer(nFrame);
239 std::vector<cv::ocl::oclMat> frame_buffer_ocl;
241 cv::VideoCapture cap(inputFile);
242 ASSERT_TRUE(cap.isOpened());
244 prepareData(cap, cn, frame_buffer);
248 cv::ocl::oclMat foreground_d;
249 cv::ocl::oclMat background_d;
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++)
261 mog2->apply(frame_buffer[i], foreground);
263 mog2->getBackgroundImage(background);
265 SANITY_CHECK(background);
267 else if(RUN_OCL_IMPL)
269 prepareData(frame_buffer, frame_buffer_ocl);
270 CV_Assert((int)(frame_buffer_ocl.size()) == nFrame);
273 cv::ocl::MOG2 d_mog2;
274 foreground_d.release();
275 background_d.release();
276 for (int i = 0; i < nFrame; i++)
278 d_mog2(frame_buffer_ocl[i], foreground_d);
280 d_mog2.getBackgroundImage(background_d);
282 background_d.download(background);
283 SANITY_CHECK(background);