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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45 #ifdef HAVE_CUDA
46
47 using namespace cvtest;
48
49 #if defined(HAVE_XINE) || \
50 defined(HAVE_GSTREAMER) || \
51 defined(HAVE_QUICKTIME) || \
52 defined(HAVE_QTKIT) || \
53 defined(HAVE_AVFOUNDATION) || \
54 defined(HAVE_FFMPEG) || \
55 defined(WIN32) /* assume that we have ffmpeg */
56
57 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
58 #else
59 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
60 #endif
61
62 //////////////////////////////////////////////////////
63 // MOG2
64
65 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
66
67 namespace
68 {
69 IMPLEMENT_PARAM_CLASS(UseGray, bool)
70 IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
71 }
72
PARAM_TEST_CASE(MOG2,cv::cuda::DeviceInfo,std::string,UseGray,DetectShadow,UseRoi)73 PARAM_TEST_CASE(MOG2, cv::cuda::DeviceInfo, std::string, UseGray, DetectShadow, UseRoi)
74 {
75 cv::cuda::DeviceInfo devInfo;
76 std::string inputFile;
77 bool useGray;
78 bool detectShadow;
79 bool useRoi;
80
81 virtual void SetUp()
82 {
83 devInfo = GET_PARAM(0);
84 cv::cuda::setDevice(devInfo.deviceID());
85
86 inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "video/" + GET_PARAM(1);
87 useGray = GET_PARAM(2);
88 detectShadow = GET_PARAM(3);
89 useRoi = GET_PARAM(4);
90 }
91 };
92
CUDA_TEST_P(MOG2,Update)93 CUDA_TEST_P(MOG2, Update)
94 {
95 cv::VideoCapture cap(inputFile);
96 ASSERT_TRUE(cap.isOpened());
97
98 cv::Mat frame;
99 cap >> frame;
100 ASSERT_FALSE(frame.empty());
101
102 cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::cuda::createBackgroundSubtractorMOG2();
103 mog2->setDetectShadows(detectShadow);
104 cv::cuda::GpuMat foreground = createMat(frame.size(), CV_8UC1, useRoi);
105
106 cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2();
107 mog2_gold->setDetectShadows(detectShadow);
108 cv::Mat foreground_gold;
109
110 for (int i = 0; i < 10; ++i)
111 {
112 cap >> frame;
113 ASSERT_FALSE(frame.empty());
114
115 if (useGray)
116 {
117 cv::Mat temp;
118 cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
119 cv::swap(temp, frame);
120 }
121
122 mog2->apply(loadMat(frame, useRoi), foreground);
123
124 mog2_gold->apply(frame, foreground_gold);
125
126 if (detectShadow)
127 {
128 ASSERT_MAT_SIMILAR(foreground_gold, foreground, 1e-2);
129 }
130 else
131 {
132 ASSERT_MAT_NEAR(foreground_gold, foreground, 0);
133 }
134 }
135 }
136
CUDA_TEST_P(MOG2,getBackgroundImage)137 CUDA_TEST_P(MOG2, getBackgroundImage)
138 {
139 if (useGray)
140 return;
141
142 cv::VideoCapture cap(inputFile);
143 ASSERT_TRUE(cap.isOpened());
144
145 cv::Mat frame;
146
147 cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::cuda::createBackgroundSubtractorMOG2();
148 mog2->setDetectShadows(detectShadow);
149 cv::cuda::GpuMat foreground;
150
151 cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2();
152 mog2_gold->setDetectShadows(detectShadow);
153 cv::Mat foreground_gold;
154
155 for (int i = 0; i < 10; ++i)
156 {
157 cap >> frame;
158 ASSERT_FALSE(frame.empty());
159
160 mog2->apply(loadMat(frame, useRoi), foreground);
161
162 mog2_gold->apply(frame, foreground_gold);
163 }
164
165 cv::cuda::GpuMat background = createMat(frame.size(), frame.type(), useRoi);
166 mog2->getBackgroundImage(background);
167
168 cv::Mat background_gold;
169 mog2_gold->getBackgroundImage(background_gold);
170
171 ASSERT_MAT_NEAR(background_gold, background, 1);
172 }
173
174 INSTANTIATE_TEST_CASE_P(CUDA_BgSegm, MOG2, testing::Combine(
175 ALL_DEVICES,
176 testing::Values(std::string("768x576.avi")),
177 testing::Values(UseGray(true), UseGray(false)),
178 testing::Values(DetectShadow(true), DetectShadow(false)),
179 WHOLE_SUBMAT));
180
181 #endif
182
183 #endif // HAVE_CUDA
184