Merge pull request #1449 from SpecLad:dc1394-095
[profile/ivi/opencv.git] / modules / ocl / test / test_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-2013, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Jin Ma, jin@multicorewareinc.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other oclMaterials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors "as is" and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45
46 #include "test_precomp.hpp"
47
48 #ifdef HAVE_OPENCL
49
50 using namespace cv;
51 using namespace cv::ocl;
52 using namespace cvtest;
53 using namespace testing;
54 using namespace std;
55
56 #if defined(HAVE_XINE)         || \
57     defined(HAVE_GSTREAMER)    || \
58     defined(HAVE_QUICKTIME)    || \
59     defined(HAVE_AVFOUNDATION) || \
60     defined(HAVE_FFMPEG)       || \
61     defined(WIN32)
62
63 #  define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
64 #else
65 #  define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
66 #endif
67
68 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
69
70 //////////////////////////////////////////////////////
71 // MOG
72
73 namespace
74 {
75     IMPLEMENT_PARAM_CLASS(UseGray, bool)
76     IMPLEMENT_PARAM_CLASS(LearningRate, double)
77 }
78
79 PARAM_TEST_CASE(mog, UseGray, LearningRate, bool)
80 {
81     bool useGray;
82     double learningRate;
83     bool useRoi;
84
85     virtual void SetUp()
86     {
87         useGray = GET_PARAM(0);
88
89         learningRate = GET_PARAM(1);
90
91         useRoi = GET_PARAM(2);
92     }
93 };
94
95 TEST_P(mog, Update)
96 {
97     std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
98     cv::VideoCapture cap(inputFile);
99     ASSERT_TRUE(cap.isOpened());
100
101     cv::Mat frame;
102     cap >> frame;
103     ASSERT_FALSE(frame.empty());
104
105     cv::ocl::MOG mog;
106     cv::ocl::oclMat foreground = createMat_ocl(frame.size(), CV_8UC1, useRoi);
107
108     cv::BackgroundSubtractorMOG mog_gold;
109     cv::Mat foreground_gold;
110
111     for (int i = 0; i < 10; ++i)
112     {
113         cap >> frame;
114         ASSERT_FALSE(frame.empty());
115
116         if (useGray)
117         {
118             cv::Mat temp;
119             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
120             cv::swap(temp, frame);
121         }
122
123         mog(loadMat_ocl(frame, useRoi), foreground, (float)learningRate);
124
125         mog_gold(frame, foreground_gold, learningRate);
126
127         EXPECT_MAT_NEAR(foreground_gold, foreground, 0.0);
128     }
129 }
130 INSTANTIATE_TEST_CASE_P(OCL_Video, mog, testing::Combine(
131     testing::Values(UseGray(false), UseGray(true)),
132     testing::Values(LearningRate(0.0), LearningRate(0.01)),
133     Values(true, false)));
134
135 //////////////////////////////////////////////////////
136 // MOG2
137
138 namespace
139 {
140     IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
141 }
142
143 PARAM_TEST_CASE(mog2, UseGray, DetectShadow, bool)
144 {
145     bool useGray;
146     bool detectShadow;
147     bool useRoi;
148     virtual void SetUp()
149     {
150         useGray = GET_PARAM(0);
151         detectShadow = GET_PARAM(1);
152         useRoi = GET_PARAM(2);
153     }
154 };
155
156 TEST_P(mog2, Update)
157 {
158     std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
159     cv::VideoCapture cap(inputFile);
160     ASSERT_TRUE(cap.isOpened());
161
162     cv::Mat frame;
163     cap >> frame;
164     ASSERT_FALSE(frame.empty());
165
166     cv::ocl::MOG2 mog2;
167     mog2.bShadowDetection = detectShadow;
168     cv::ocl::oclMat foreground = createMat_ocl(frame.size(), CV_8UC1, useRoi);
169
170     cv::BackgroundSubtractorMOG2 mog2_gold;
171     mog2_gold.set("detectShadows", detectShadow);
172     cv::Mat foreground_gold;
173
174     for (int i = 0; i < 10; ++i)
175     {
176         cap >> frame;
177         ASSERT_FALSE(frame.empty());
178
179         if (useGray)
180         {
181             cv::Mat temp;
182             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
183             cv::swap(temp, frame);
184         }
185
186         mog2(loadMat_ocl(frame, useRoi), foreground);
187
188         mog2_gold(frame, foreground_gold);
189
190         if (detectShadow)
191             EXPECT_MAT_SIMILAR(foreground_gold, foreground, 15e-3)
192         else
193             EXPECT_MAT_NEAR(foreground_gold, foreground, 0)
194     }
195 }
196
197 TEST_P(mog2, getBackgroundImage)
198 {
199     if (useGray)
200         return;
201
202     std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
203     cv::VideoCapture cap(inputFile);
204     ASSERT_TRUE(cap.isOpened());
205
206     cv::Mat frame;
207
208     cv::ocl::MOG2 mog2;
209     mog2.bShadowDetection = detectShadow;
210     cv::ocl::oclMat foreground;
211
212     cv::BackgroundSubtractorMOG2 mog2_gold;
213     mog2_gold.set("detectShadows", detectShadow);
214     cv::Mat foreground_gold;
215
216     for (int i = 0; i < 10; ++i)
217     {
218         cap >> frame;
219         ASSERT_FALSE(frame.empty());
220
221         mog2(loadMat_ocl(frame, useRoi), foreground);
222
223         mog2_gold(frame, foreground_gold);
224     }
225
226     cv::ocl::oclMat background = createMat_ocl(frame.size(), frame.type(), useRoi);
227     mog2.getBackgroundImage(background);
228
229     cv::Mat background_gold;
230     mog2_gold.getBackgroundImage(background_gold);
231
232     EXPECT_MAT_NEAR(background_gold, background, 1.0);
233 }
234
235 INSTANTIATE_TEST_CASE_P(OCL_Video, mog2, testing::Combine(
236     testing::Values(UseGray(true), UseGray(false)),
237     testing::Values(DetectShadow(true), DetectShadow(false)),
238     Values(true, false)));
239
240 #endif
241
242 #endif