1 #include "../test_precomp.hpp"
2 #include "opencv2/ts/ocl_test.hpp"
3
4 #ifdef HAVE_OPENCL
5
6 #if defined(HAVE_XINE) || \
7 defined(HAVE_GSTREAMER) || \
8 defined(HAVE_QUICKTIME) || \
9 defined(HAVE_AVFOUNDATION) || \
10 defined(HAVE_FFMPEG) || \
11 defined(WIN32)
12
13 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 1
14 #else
15 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
16 #endif
17
18 #if BUILD_WITH_VIDEO_INPUT_SUPPORT
19
20 namespace cvtest {
21 namespace ocl {
22
23 //////////////////////////Mog2_Update///////////////////////////////////
24
25 namespace
26 {
27 IMPLEMENT_PARAM_CLASS(UseGray, bool)
28 IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
29 }
30
PARAM_TEST_CASE(Mog2_Update,UseGray,DetectShadow)31 PARAM_TEST_CASE(Mog2_Update, UseGray, DetectShadow)
32 {
33 bool useGray;
34 bool detectShadow;
35 virtual void SetUp()
36 {
37 useGray = GET_PARAM(0);
38 detectShadow = GET_PARAM(1);
39 }
40 };
41
OCL_TEST_P(Mog2_Update,Accuracy)42 OCL_TEST_P(Mog2_Update, Accuracy)
43 {
44 string inputFile = string(TS::ptr()->get_data_path()) + "video/768x576.avi";
45 VideoCapture cap(inputFile);
46 ASSERT_TRUE(cap.isOpened());
47
48 Ptr<BackgroundSubtractorMOG2> mog2_cpu = createBackgroundSubtractorMOG2();
49 Ptr<BackgroundSubtractorMOG2> mog2_ocl = createBackgroundSubtractorMOG2();
50
51 mog2_cpu->setDetectShadows(detectShadow);
52 mog2_ocl->setDetectShadows(detectShadow);
53
54 Mat frame, foreground;
55 UMat u_foreground;
56
57 for (int i = 0; i < 10; ++i)
58 {
59 cap >> frame;
60 ASSERT_FALSE(frame.empty());
61
62 if (useGray)
63 {
64 Mat temp;
65 cvtColor(frame, temp, COLOR_BGR2GRAY);
66 swap(temp, frame);
67 }
68
69 OCL_OFF(mog2_cpu->apply(frame, foreground));
70 OCL_ON (mog2_ocl->apply(frame, u_foreground));
71
72 if (detectShadow)
73 EXPECT_MAT_SIMILAR(foreground, u_foreground, 15e-3);
74 else
75 EXPECT_MAT_NEAR(foreground, u_foreground, 0);
76 }
77 }
78
79 //////////////////////////Mog2_getBackgroundImage///////////////////////////////////
80
PARAM_TEST_CASE(Mog2_getBackgroundImage,DetectShadow)81 PARAM_TEST_CASE(Mog2_getBackgroundImage, DetectShadow)
82 {
83 bool detectShadow;
84 virtual void SetUp()
85 {
86 detectShadow = GET_PARAM(0);
87 }
88 };
89
OCL_TEST_P(Mog2_getBackgroundImage,Accuracy)90 OCL_TEST_P(Mog2_getBackgroundImage, Accuracy)
91 {
92 string inputFile = string(TS::ptr()->get_data_path()) + "video/768x576.avi";
93 VideoCapture cap(inputFile);
94 ASSERT_TRUE(cap.isOpened());
95
96 Ptr<BackgroundSubtractorMOG2> mog2_cpu = createBackgroundSubtractorMOG2();
97 Ptr<BackgroundSubtractorMOG2> mog2_ocl = createBackgroundSubtractorMOG2();
98
99 mog2_cpu->setDetectShadows(detectShadow);
100 mog2_ocl->setDetectShadows(detectShadow);
101
102 Mat frame, foreground;
103 UMat u_foreground;
104
105 for (int i = 0; i < 10; ++i)
106 {
107 cap >> frame;
108 ASSERT_FALSE(frame.empty());
109
110 OCL_OFF(mog2_cpu->apply(frame, foreground));
111 OCL_ON (mog2_ocl->apply(frame, u_foreground));
112 }
113
114 Mat background;
115 OCL_OFF(mog2_cpu->getBackgroundImage(background));
116
117 UMat u_background;
118 OCL_ON (mog2_ocl->getBackgroundImage(u_background));
119
120 EXPECT_MAT_NEAR(background, u_background, 1.0);
121 }
122
123 ///////////////////////////////////////////////////////////////////////////////////////////
124
125 OCL_INSTANTIATE_TEST_CASE_P(OCL_Video, Mog2_Update, Combine(
126 Values(UseGray(true), UseGray(false)),
127 Values(DetectShadow(true), DetectShadow(false)))
128 );
129
130 OCL_INSTANTIATE_TEST_CASE_P(OCL_Video, Mog2_getBackgroundImage, (Values(DetectShadow(true), DetectShadow(false)))
131 );
132
133 }}// namespace cvtest::ocl
134
135 #endif
136 #endif
137