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 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
27 //
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #include "test_precomp.hpp"
45
46 using namespace cv;
47 using namespace cv::cuda;
48 using namespace cv::cudev;
49 using namespace cvtest;
50
51 typedef ::testing::Types<uchar, ushort, short, int, float> AllTypes;
52 typedef ::testing::Types<short, int, float> SignedTypes;
53
54 ////////////////////////////////////////////////////////////////////////////////
55 // UnaryMinusTest
56
57 template <typename T>
58 class UnaryMinusTest : public ::testing::Test
59 {
60 public:
test_gpumat()61 void test_gpumat()
62 {
63 const Size size = randomSize(100, 400);
64 const int type = DataType<T>::type;
65
66 Mat src = randomMat(size, type);
67
68 GpuMat_<T> d_src(src);
69
70 GpuMat_<T> dst = -d_src;
71
72 Mat dst_gold;
73 src.convertTo(dst_gold, src.depth(), -1);
74
75 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
76 }
77
test_globptr()78 void test_globptr()
79 {
80 const Size size = randomSize(100, 400);
81 const int type = DataType<T>::type;
82
83 Mat src = randomMat(size, type);
84
85 GpuMat_<T> d_src(src);
86 GlobPtrSz<T> d_src_ptr = d_src;
87
88 GpuMat_<T> dst = -d_src_ptr;
89
90 Mat dst_gold;
91 src.convertTo(dst_gold, src.depth(), -1);
92
93 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
94 }
95
test_texptr()96 void test_texptr()
97 {
98 const Size size = randomSize(100, 400);
99 const int type = DataType<T>::type;
100
101 Mat src = randomMat(size, type);
102
103 GpuMat_<T> d_src(src);
104 Texture<T> tex_src(d_src);
105
106 GpuMat_<T> dst = -tex_src;
107
108 Mat dst_gold;
109 src.convertTo(dst_gold, src.depth(), -1);
110
111 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
112 }
113
test_expr()114 void test_expr()
115 {
116 const Size size = randomSize(100, 400);
117 const int type = DataType<T>::type;
118
119 Mat src1 = randomMat(size, type);
120 Mat src2 = randomMat(size, type);
121
122 GpuMat_<T> d_src1(src1), d_src2(src2);
123
124 GpuMat_<T> dst = -(d_src1 + d_src2);
125
126 Mat dst_gold;
127 cv::add(src1, src2, dst_gold);
128 dst_gold.convertTo(dst_gold, dst_gold.depth(), -1);
129
130 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
131 }
132 };
133
134 TYPED_TEST_CASE(UnaryMinusTest, SignedTypes);
135
TYPED_TEST(UnaryMinusTest,GpuMat)136 TYPED_TEST(UnaryMinusTest, GpuMat)
137 {
138 UnaryMinusTest<TypeParam>::test_gpumat();
139 }
140
TYPED_TEST(UnaryMinusTest,GlobPtrSz)141 TYPED_TEST(UnaryMinusTest, GlobPtrSz)
142 {
143 UnaryMinusTest<TypeParam>::test_globptr();
144 }
145
TYPED_TEST(UnaryMinusTest,TexturePtr)146 TYPED_TEST(UnaryMinusTest, TexturePtr)
147 {
148 UnaryMinusTest<TypeParam>::test_texptr();
149 }
150
TYPED_TEST(UnaryMinusTest,Expr)151 TYPED_TEST(UnaryMinusTest, Expr)
152 {
153 UnaryMinusTest<TypeParam>::test_expr();
154 }
155
156 ////////////////////////////////////////////////////////////////////////////////
157 // PlusTest
158
159 template <typename T>
160 class PlusTest : public ::testing::Test
161 {
162 public:
test_gpumat_gpumat()163 void test_gpumat_gpumat()
164 {
165 const Size size = randomSize(100, 400);
166 const int type = DataType<T>::type;
167
168 Mat src1 = randomMat(size, type);
169 Mat src2 = randomMat(size, type);
170
171 GpuMat_<T> d_src1(src1), d_src2(src2);
172
173 GpuMat_<T> dst = d_src1 + d_src2;
174
175 Mat dst_gold;
176 cv::add(src1, src2, dst_gold);
177
178 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
179 }
180
test_texptr_scalar()181 void test_texptr_scalar()
182 {
183 const Size size = randomSize(100, 400);
184 const int type = DataType<T>::type;
185
186 Mat src = randomMat(size, type);
187
188 GpuMat_<T> d_src(src);
189 Texture<T> tex_src(d_src);
190
191 GpuMat_<T> dst = tex_src + static_cast<T>(5);
192
193 Mat dst_gold;
194 cv::add(src, 5, dst_gold);
195
196 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
197 }
198
test_expr_gpumat()199 void test_expr_gpumat()
200 {
201 const Size size = randomSize(100, 400);
202 const int type = DataType<T>::type;
203
204 Mat src1 = randomMat(size, type);
205 Mat src2 = randomMat(size, type);
206 Mat src3 = randomMat(size, type);
207
208 GpuMat_<T> d_src1(src1), d_src2(src2), d_src3(src3);
209
210 GpuMat_<T> dst = d_src1 + d_src2 + d_src3;
211
212 Mat dst_gold;
213 cv::add(src1, src2, dst_gold);
214 cv::add(dst_gold, src3, dst_gold);
215
216 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
217 }
218
test_scalar_expr()219 void test_scalar_expr()
220 {
221 const Size size = randomSize(100, 400);
222 const int type = DataType<T>::type;
223
224 Mat src1 = randomMat(size, type);
225 Mat src2 = randomMat(size, type);
226
227 GpuMat_<T> d_src1(src1), d_src2(src2);
228
229 GpuMat_<T> dst = static_cast<T>(5) + (d_src1 + d_src2);
230
231 Mat dst_gold;
232 cv::add(src1, src2, dst_gold);
233 cv::add(dst_gold, 5, dst_gold);
234
235 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
236 }
237 };
238
239 TYPED_TEST_CASE(PlusTest, AllTypes);
240
TYPED_TEST(PlusTest,GpuMat_GpuMat)241 TYPED_TEST(PlusTest, GpuMat_GpuMat)
242 {
243 PlusTest<TypeParam>::test_gpumat_gpumat();
244 }
245
TYPED_TEST(PlusTest,TexturePtr_Scalar)246 TYPED_TEST(PlusTest, TexturePtr_Scalar)
247 {
248 PlusTest<TypeParam>::test_texptr_scalar();
249 }
250
TYPED_TEST(PlusTest,Expr_GpuMat)251 TYPED_TEST(PlusTest, Expr_GpuMat)
252 {
253 PlusTest<TypeParam>::test_expr_gpumat();
254 }
255
TYPED_TEST(PlusTest,Scalar_Expr)256 TYPED_TEST(PlusTest, Scalar_Expr)
257 {
258 PlusTest<TypeParam>::test_scalar_expr();
259 }
260
261 ////////////////////////////////////////////////////////////////////////////////
262 // MinusTest
263
264 template <typename T>
265 class MinusTest : public ::testing::Test
266 {
267 public:
test_gpumat_gpumat()268 void test_gpumat_gpumat()
269 {
270 const Size size = randomSize(100, 400);
271 const int type = DataType<T>::type;
272
273 Mat src1 = randomMat(size, type);
274 Mat src2 = randomMat(size, type);
275
276 GpuMat_<T> d_src1(src1), d_src2(src2);
277
278 GpuMat_<T> dst = d_src1 - d_src2;
279
280 Mat dst_gold;
281 cv::subtract(src1, src2, dst_gold);
282
283 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
284 }
285
test_texptr_scalar()286 void test_texptr_scalar()
287 {
288 const Size size = randomSize(100, 400);
289 const int type = DataType<T>::type;
290
291 Mat src = randomMat(size, type);
292
293 GpuMat_<T> d_src(src);
294 Texture<T> tex_src(d_src);
295
296 GpuMat_<T> dst = tex_src - static_cast<T>(5);
297
298 Mat dst_gold;
299 cv::subtract(src, 5, dst_gold);
300
301 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
302 }
303
test_expr_gpumat()304 void test_expr_gpumat()
305 {
306 const Size size = randomSize(100, 400);
307 const int type = DataType<T>::type;
308
309 Mat src1 = randomMat(size, type);
310 Mat src2 = randomMat(size, type);
311 Mat src3 = randomMat(size, type);
312
313 GpuMat_<T> d_src1(src1), d_src2(src2), d_src3(src3);
314
315 GpuMat_<T> dst = (d_src1 + d_src2) - d_src3;
316
317 Mat dst_gold;
318 cv::add(src1, src2, dst_gold);
319 cv::subtract(dst_gold, src3, dst_gold);
320
321 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
322 }
323
test_scalar_expr()324 void test_scalar_expr()
325 {
326 const Size size = randomSize(100, 400);
327 const int type = DataType<T>::type;
328
329 Mat src1 = randomMat(size, type);
330 Mat src2 = randomMat(size, type);
331
332 GpuMat_<T> d_src1(src1), d_src2(src2);
333
334 GpuMat_<T> dst = static_cast<T>(5) - (d_src1 + d_src2);
335
336 Mat dst_gold;
337 cv::add(src1, src2, dst_gold);
338 cv::subtract(5, dst_gold, dst_gold);
339
340 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
341 }
342 };
343
344 TYPED_TEST_CASE(MinusTest, SignedTypes);
345
TYPED_TEST(MinusTest,GpuMat_GpuMat)346 TYPED_TEST(MinusTest, GpuMat_GpuMat)
347 {
348 MinusTest<TypeParam>::test_gpumat_gpumat();
349 }
350
TYPED_TEST(MinusTest,TexturePtr_Scalar)351 TYPED_TEST(MinusTest, TexturePtr_Scalar)
352 {
353 MinusTest<TypeParam>::test_texptr_scalar();
354 }
355
TYPED_TEST(MinusTest,Expr_GpuMat)356 TYPED_TEST(MinusTest, Expr_GpuMat)
357 {
358 MinusTest<TypeParam>::test_expr_gpumat();
359 }
360
TYPED_TEST(MinusTest,Scalar_Expr)361 TYPED_TEST(MinusTest, Scalar_Expr)
362 {
363 MinusTest<TypeParam>::test_scalar_expr();
364 }
365
366 ////////////////////////////////////////////////////////////////////////////////
367 // AbsDiffTest
368
369 template <typename T>
370 class AbsDiffTest : public ::testing::Test
371 {
372 public:
test_accuracy()373 void test_accuracy()
374 {
375 const Size size = randomSize(100, 400);
376 const int type = DataType<T>::type;
377
378 Mat src1 = randomMat(size, type);
379 Mat src2 = randomMat(size, type);
380
381 GpuMat_<T> d_src1(src1), d_src2(src2);
382
383 GpuMat_<T> dst1 = absdiff_(d_src1, d_src2);
384 GpuMat_<T> dst2 = abs_(d_src1 - d_src2);
385
386 EXPECT_MAT_NEAR(dst1, dst2, 0.0);
387 }
388 };
389
390 TYPED_TEST_CASE(AbsDiffTest, SignedTypes);
391
TYPED_TEST(AbsDiffTest,Accuracy)392 TYPED_TEST(AbsDiffTest, Accuracy)
393 {
394 AbsDiffTest<TypeParam>::test_accuracy();
395 }
396