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