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, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
25 //
26 // * The name of the copyright holders may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the OpenCV Foundation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #ifndef __OPENCV_TS_OCL_TEST_HPP__
43 #define __OPENCV_TS_OCL_TEST_HPP__
44
45 #include "opencv2/opencv_modules.hpp"
46
47 #include "opencv2/ts.hpp"
48
49 #include "opencv2/imgcodecs.hpp"
50 #include "opencv2/videoio.hpp"
51 #include "opencv2/highgui.hpp"
52 #include "opencv2/imgproc.hpp"
53 #include "opencv2/imgproc/types_c.h"
54 #include "opencv2/core/ocl.hpp"
55
56 namespace cvtest {
57 namespace ocl {
58
59 using namespace cv;
60 using namespace testing;
61
ToUMat(const std::vector<Mat> & src)62 inline std::vector<UMat> ToUMat(const std::vector<Mat>& src)
63 {
64 std::vector<UMat> dst;
65 dst.resize(src.size());
66 for (size_t i = 0; i < src.size(); ++i)
67 {
68 src[i].copyTo(dst[i]);
69 }
70 return dst;
71 }
72
ToUMat(const Mat & src)73 inline UMat ToUMat(const Mat& src)
74 {
75 UMat dst;
76 src.copyTo(dst);
77 return dst;
78 }
79
ToUMat(InputArray src)80 inline UMat ToUMat(InputArray src)
81 {
82 UMat dst;
83 src.getMat().copyTo(dst);
84 return dst;
85 }
86
87 extern int test_loop_times;
88
89 #define MAX_VALUE 357
90
91 #define EXPECT_MAT_NORM(mat, eps) \
92 do \
93 { \
94 EXPECT_LE(TestUtils::checkNorm1(mat), eps) \
95 } while ((void)0, 0)
96
97 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
98 do \
99 { \
100 ASSERT_EQ(mat1.type(), mat2.type()); \
101 ASSERT_EQ(mat1.size(), mat2.size()); \
102 EXPECT_LE(TestUtils::checkNorm2(mat1, mat2), eps) \
103 << "Size: " << mat1.size() << std::endl; \
104 } while ((void)0, 0)
105
106 #define EXPECT_MAT_NEAR_RELATIVE(mat1, mat2, eps) \
107 do \
108 { \
109 ASSERT_EQ(mat1.type(), mat2.type()); \
110 ASSERT_EQ(mat1.size(), mat2.size()); \
111 EXPECT_LE(TestUtils::checkNormRelative(mat1, mat2), eps) \
112 << "Size: " << mat1.size() << std::endl; \
113 } while ((void)0, 0)
114
115 #define EXPECT_MAT_N_DIFF(mat1, mat2, num) \
116 do \
117 { \
118 ASSERT_EQ(mat1.type(), mat2.type()); \
119 ASSERT_EQ(mat1.size(), mat2.size()); \
120 Mat diff; \
121 absdiff(mat1, mat2, diff); \
122 EXPECT_LE(countNonZero(diff.reshape(1)), num) \
123 << "Size: " << mat1.size() << std::endl; \
124 } while ((void)0, 0)
125
126 #define OCL_EXPECT_MATS_NEAR(name, eps) \
127 do \
128 { \
129 ASSERT_EQ(name ## _roi.type(), u ## name ## _roi.type()); \
130 ASSERT_EQ(name ## _roi.size(), u ## name ## _roi.size()); \
131 EXPECT_LE(TestUtils::checkNorm2(name ## _roi, u ## name ## _roi), eps) \
132 << "Size: " << name ## _roi.size() << std::endl; \
133 Point _offset; \
134 Size _wholeSize; \
135 u ## name ## _roi.locateROI(_wholeSize, _offset); \
136 Mat _mask(name.size(), CV_8UC1, Scalar::all(255)); \
137 _mask(Rect(_offset, name ## _roi.size())).setTo(Scalar::all(0)); \
138 ASSERT_EQ(name.type(), u ## name.type()); \
139 ASSERT_EQ(name.size(), u ## name.size()); \
140 EXPECT_LE(TestUtils::checkNorm2(name, u ## name, _mask), eps) \
141 << "Size: " << name ## _roi.size() << std::endl; \
142 } while ((void)0, 0)
143
144 #define OCL_EXPECT_MATS_NEAR_RELATIVE(name, eps) \
145 do \
146 { \
147 ASSERT_EQ(name ## _roi.type(), u ## name ## _roi.type()); \
148 ASSERT_EQ(name ## _roi.size(), u ## name ## _roi.size()); \
149 EXPECT_LE(TestUtils::checkNormRelative(name ## _roi, u ## name ## _roi), eps) \
150 << "Size: " << name ## _roi.size() << std::endl; \
151 Point _offset; \
152 Size _wholeSize; \
153 name ## _roi.locateROI(_wholeSize, _offset); \
154 Mat _mask(name.size(), CV_8UC1, Scalar::all(255)); \
155 _mask(Rect(_offset, name ## _roi.size())).setTo(Scalar::all(0)); \
156 ASSERT_EQ(name.type(), u ## name.type()); \
157 ASSERT_EQ(name.size(), u ## name.size()); \
158 EXPECT_LE(TestUtils::checkNormRelative(name, u ## name, _mask), eps) \
159 << "Size: " << name ## _roi.size() << std::endl; \
160 } while ((void)0, 0)
161
162 //for sparse matrix
163 #define OCL_EXPECT_MATS_NEAR_RELATIVE_SPARSE(name, eps) \
164 do \
165 { \
166 ASSERT_EQ(name ## _roi.type(), u ## name ## _roi.type()); \
167 ASSERT_EQ(name ## _roi.size(), u ## name ## _roi.size()); \
168 EXPECT_LE(TestUtils::checkNormRelativeSparse(name ## _roi, u ## name ## _roi), eps) \
169 << "Size: " << name ## _roi.size() << std::endl; \
170 Point _offset; \
171 Size _wholeSize; \
172 name ## _roi.locateROI(_wholeSize, _offset); \
173 Mat _mask(name.size(), CV_8UC1, Scalar::all(255)); \
174 _mask(Rect(_offset, name ## _roi.size())).setTo(Scalar::all(0)); \
175 ASSERT_EQ(name.type(), u ## name.type()); \
176 ASSERT_EQ(name.size(), u ## name.size()); \
177 EXPECT_LE(TestUtils::checkNormRelativeSparse(name, u ## name, _mask), eps) \
178 << "Size: " << name ## _roi.size() << std::endl; \
179 } while ((void)0, 0)
180
181 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
182 do \
183 { \
184 ASSERT_EQ(mat1.type(), mat2.type()); \
185 ASSERT_EQ(mat1.size(), mat2.size()); \
186 EXPECT_LE(checkSimilarity(mat1, mat2), eps) \
187 << "Size: " << mat1.size() << std::endl; \
188 } while ((void)0, 0)
189
190 using perf::MatDepth;
191 using perf::MatType;
192
193 #define OCL_RNG_SEED 123456
194
195 struct CV_EXPORTS TestUtils
196 {
197 cv::RNG rng;
198
TestUtilscvtest::ocl::TestUtils199 TestUtils()
200 {
201 rng = cv::RNG(OCL_RNG_SEED);
202 }
203
randomIntcvtest::ocl::TestUtils204 int randomInt(int minVal, int maxVal)
205 {
206 return rng.uniform(minVal, maxVal);
207 }
208
randomDoublecvtest::ocl::TestUtils209 double randomDouble(double minVal, double maxVal)
210 {
211 return rng.uniform(minVal, maxVal);
212 }
213
randomDoubleLogcvtest::ocl::TestUtils214 double randomDoubleLog(double minVal, double maxVal)
215 {
216 double logMin = log((double)minVal + 1);
217 double logMax = log((double)maxVal + 1);
218 double pow = rng.uniform(logMin, logMax);
219 double v = exp(pow) - 1;
220 CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal)));
221 return v;
222 }
223
randomSizecvtest::ocl::TestUtils224 Size randomSize(int minVal, int maxVal)
225 {
226 #if 1
227 return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal));
228 #else
229 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
230 #endif
231 }
232
randomSizecvtest::ocl::TestUtils233 Size randomSize(int minValX, int maxValX, int minValY, int maxValY)
234 {
235 #if 1
236 return cv::Size((int)randomDoubleLog(minValX, maxValX), (int)randomDoubleLog(minValY, maxValY));
237 #else
238 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
239 #endif
240 }
241
randomScalarcvtest::ocl::TestUtils242 Scalar randomScalar(double minVal, double maxVal)
243 {
244 return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
245 }
246
randomMatcvtest::ocl::TestUtils247 Mat randomMat(Size size, int type, double minVal, double maxVal, bool useRoi = false)
248 {
249 RNG dataRng(rng.next());
250 return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi);
251 }
252
253 struct Border
254 {
255 int top, bot, lef, rig;
256 };
257
randomBordercvtest::ocl::TestUtils258 Border randomBorder(int minValue = 0, int maxValue = MAX_VALUE)
259 {
260 Border border = {
261 (int)randomDoubleLog(minValue, maxValue),
262 (int)randomDoubleLog(minValue, maxValue),
263 (int)randomDoubleLog(minValue, maxValue),
264 (int)randomDoubleLog(minValue, maxValue)
265 };
266 return border;
267 }
268
randomSubMatcvtest::ocl::TestUtils269 void randomSubMat(Mat& whole, Mat& subMat, const Size& roiSize, const Border& border, int type, double minVal, double maxVal)
270 {
271 Size wholeSize = Size(roiSize.width + border.lef + border.rig, roiSize.height + border.top + border.bot);
272 whole = randomMat(wholeSize, type, minVal, maxVal, false);
273 subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
274 }
275
276 // If the two vectors are not equal, it will return the difference in vector size
277 // Else it will return (total diff of each 1 and 2 rects covered pixels)/(total 1 rects covered pixels)
278 // The smaller, the better matched
279 static double checkRectSimilarity(const cv::Size & sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
280
281 //! read image from testdata folder.
282 static cv::Mat readImage(const String &fileName, int flags = cv::IMREAD_COLOR);
283 static cv::Mat readImageType(const String &fname, int type);
284
285 static double checkNorm1(InputArray m, InputArray mask = noArray());
286 static double checkNorm2(InputArray m1, InputArray m2, InputArray mask = noArray());
287 static double checkSimilarity(InputArray m1, InputArray m2);
288 static void showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow);
289
checkNormRelativecvtest::ocl::TestUtils290 static inline double checkNormRelative(InputArray m1, InputArray m2, InputArray mask = noArray())
291 {
292 return cvtest::norm(m1.getMat(), m2.getMat(), cv::NORM_INF, mask) /
293 std::max((double)std::numeric_limits<float>::epsilon(),
294 (double)std::max(cvtest::norm(m1.getMat(), cv::NORM_INF), cvtest::norm(m2.getMat(), cv::NORM_INF)));
295 }
296
checkNormRelativeSparsecvtest::ocl::TestUtils297 static inline double checkNormRelativeSparse(InputArray m1, InputArray m2, InputArray mask = noArray())
298 {
299 double norm_inf = cvtest::norm(m1.getMat(), m2.getMat(), cv::NORM_INF, mask);
300 double norm_rel = norm_inf /
301 std::max((double)std::numeric_limits<float>::epsilon(),
302 (double)std::max(cvtest::norm(m1.getMat(), cv::NORM_INF), cvtest::norm(m2.getMat(), cv::NORM_INF)));
303 return std::min(norm_inf, norm_rel);
304 }
305
306 };
307
308 #define TEST_DECLARE_INPUT_PARAMETER(name) Mat name, name ## _roi; UMat u ## name, u ## name ## _roi
309 #define TEST_DECLARE_OUTPUT_PARAMETER(name) TEST_DECLARE_INPUT_PARAMETER(name)
310
311 #define UMAT_UPLOAD_INPUT_PARAMETER(name) \
312 do \
313 { \
314 name.copyTo(u ## name); \
315 Size _wholeSize; Point ofs; name ## _roi.locateROI(_wholeSize, ofs); \
316 u ## name ## _roi = u ## name(Rect(ofs.x, ofs.y, name ## _roi.size().width, name ## _roi.size().height)); \
317 } while ((void)0, 0)
318
319 #define UMAT_UPLOAD_OUTPUT_PARAMETER(name) UMAT_UPLOAD_INPUT_PARAMETER(name)
320
321 template <typename T>
322 struct CV_EXPORTS TSTestWithParam : public TestUtils, public ::testing::TestWithParam<T>
323 {
324
325 };
326
327 #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > >
328
329 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
330
331 #ifndef IMPLEMENT_PARAM_CLASS
332 #define IMPLEMENT_PARAM_CLASS(name, type) \
333 class name \
334 { \
335 public: \
336 name ( type arg = type ()) : val_(arg) {} \
337 operator type () const {return val_;} \
338 private: \
339 type val_; \
340 }; \
341 inline void PrintTo( name param, std::ostream* os) \
342 { \
343 *os << #name << "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
344 }
345
346 IMPLEMENT_PARAM_CLASS(Channels, int)
347 #endif // IMPLEMENT_PARAM_CLASS
348
349 #define OCL_TEST_P TEST_P
350 #define OCL_TEST_F(name, ...) typedef name OCL_##name; TEST_F(OCL_##name, __VA_ARGS__)
351 #define OCL_TEST(name, ...) TEST(OCL_##name, __VA_ARGS__)
352
353 #define OCL_OFF(fn) cv::ocl::setUseOpenCL(false); fn
354 #define OCL_ON(fn) cv::ocl::setUseOpenCL(true); fn
355
356 #define OCL_ALL_DEPTHS Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F)
357 #define OCL_ALL_CHANNELS Values(1, 2, 3, 4)
358
359 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA)
360 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
361 CV_ENUM(BorderType, BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101)
362
363 #define OCL_INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) \
364 INSTANTIATE_TEST_CASE_P(OCL_ ## prefix, test_case_name, generator)
365
366 } } // namespace cvtest::ocl
367
368 #endif // __OPENCV_TS_OCL_TEST_HPP__
369