catching OpenCL double not supported exceptions
[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         learningRate = GET_PARAM(1);
89         useRoi = GET_PARAM(2);
90     }
91 };
92
93 OCL_TEST_P(mog, Update)
94 {
95     std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
96     cv::VideoCapture cap(inputFile);
97     ASSERT_TRUE(cap.isOpened());
98
99     cv::Mat frame;
100     cap >> frame;
101     ASSERT_FALSE(frame.empty());
102
103     cv::ocl::MOG mog;
104     cv::ocl::oclMat foreground = createMat_ocl(rng, frame.size(), CV_8UC1, useRoi);
105
106     cv::BackgroundSubtractorMOG mog_gold;
107     cv::Mat foreground_gold;
108
109     for (int i = 0; i < 10; ++i)
110     {
111         cap >> frame;
112         ASSERT_FALSE(frame.empty());
113
114         if (useGray)
115         {
116             cv::Mat temp;
117             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
118             cv::swap(temp, frame);
119         }
120
121         mog(loadMat_ocl(rng, frame, useRoi), foreground, (float)learningRate);
122
123         mog_gold(frame, foreground_gold, learningRate);
124
125         EXPECT_MAT_NEAR(foreground_gold, foreground, 0.0);
126     }
127 }
128 INSTANTIATE_TEST_CASE_P(OCL_Video, mog, testing::Combine(
129     testing::Values(UseGray(false), UseGray(true)),
130     testing::Values(LearningRate(0.0), LearningRate(0.01)),
131     Values(true, false)));
132
133 //////////////////////////////////////////////////////
134 // MOG2
135
136 namespace
137 {
138     IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
139 }
140
141 PARAM_TEST_CASE(mog2, UseGray, DetectShadow, bool)
142 {
143     bool useGray;
144     bool detectShadow;
145     bool useRoi;
146     virtual void SetUp()
147     {
148         useGray = GET_PARAM(0);
149         detectShadow = GET_PARAM(1);
150         useRoi = GET_PARAM(2);
151     }
152 };
153
154 OCL_TEST_P(mog2, Update)
155 {
156     std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
157     cv::VideoCapture cap(inputFile);
158     ASSERT_TRUE(cap.isOpened());
159
160     cv::Mat frame;
161     cap >> frame;
162     ASSERT_FALSE(frame.empty());
163
164     cv::ocl::MOG2 mog2;
165     mog2.bShadowDetection = detectShadow;
166     cv::ocl::oclMat foreground = createMat_ocl(rng, frame.size(), CV_8UC1, useRoi);
167
168     cv::BackgroundSubtractorMOG2 mog2_gold;
169     mog2_gold.set("detectShadows", detectShadow);
170     cv::Mat foreground_gold;
171
172     for (int i = 0; i < 10; ++i)
173     {
174         cap >> frame;
175         ASSERT_FALSE(frame.empty());
176
177         if (useGray)
178         {
179             cv::Mat temp;
180             cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
181             cv::swap(temp, frame);
182         }
183
184         mog2(loadMat_ocl(rng, frame, useRoi), foreground);
185
186         mog2_gold(frame, foreground_gold);
187
188         if (detectShadow)
189             EXPECT_MAT_SIMILAR(foreground_gold, foreground, 15e-3)
190         else
191             EXPECT_MAT_NEAR(foreground_gold, foreground, 0)
192     }
193 }
194
195 OCL_TEST_P(mog2, getBackgroundImage)
196 {
197     if (useGray)
198         return;
199
200     std::string inputFile = string(cvtest::TS::ptr()->get_data_path()) + "gpu/video/768x576.avi";
201     cv::VideoCapture cap(inputFile);
202     ASSERT_TRUE(cap.isOpened());
203
204     cv::Mat frame;
205
206     cv::ocl::MOG2 mog2;
207     mog2.bShadowDetection = detectShadow;
208     cv::ocl::oclMat foreground;
209
210     cv::BackgroundSubtractorMOG2 mog2_gold;
211     mog2_gold.set("detectShadows", detectShadow);
212     cv::Mat foreground_gold;
213
214     for (int i = 0; i < 10; ++i)
215     {
216         cap >> frame;
217         ASSERT_FALSE(frame.empty());
218
219         mog2(loadMat_ocl(rng, frame, useRoi), foreground);
220
221         mog2_gold(frame, foreground_gold);
222     }
223
224     cv::ocl::oclMat background = createMat_ocl(rng, frame.size(), frame.type(), useRoi);
225     mog2.getBackgroundImage(background);
226
227     cv::Mat background_gold;
228     mog2_gold.getBackgroundImage(background_gold);
229
230     EXPECT_MAT_NEAR(background_gold, background, 1.0);
231 }
232
233 INSTANTIATE_TEST_CASE_P(OCL_Video, mog2, testing::Combine(
234     testing::Values(UseGray(true), UseGray(false)),
235     testing::Values(DetectShadow(true), DetectShadow(false)),
236     Values(true, false)));
237
238 #endif
239
240 #endif