cab07fd9ac7236259c4f3954e67fb1b2ae471cdf
[profile/ivi/opencv.git] / modules / ts / include / opencv2 / ts / ocl_test.hpp
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 "cvconfig.h" // to get definition of HAVE_OPENCL
46 #include "opencv2/opencv_modules.hpp"
47
48 #ifdef HAVE_OPENCL
49
50 #include "opencv2/ts.hpp"
51
52 #include "opencv2/highgui.hpp"
53 #include "opencv2/imgproc.hpp"
54 #include "opencv2/imgproc/types_c.h"
55 #include "opencv2/core/ocl.hpp"
56
57 namespace cvtest {
58 namespace ocl {
59
60 using namespace cv;
61 using namespace testing;
62
63 namespace traits {
64
65 template <typename T>
66 struct GetMatForRead
67 {
68 };
69 template <>
70 struct GetMatForRead<Mat>
71 {
72     static const Mat get(const Mat& m) { return m; }
73 };
74
75 template <>
76 struct GetMatForRead<UMat>
77 {
78     static const Mat get(const UMat& m) { return m.getMat(ACCESS_READ); }
79 };
80
81 template <>
82 struct GetMatForRead<MatExpr>
83 {
84     static const Mat get(const MatExpr& m) { return m; }
85 };
86
87 } // namespace traits
88
89 template <typename T>
90 const Mat getMatForRead(const T& mat)
91 {
92     return traits::GetMatForRead<T>::get(mat);
93 }
94
95 extern int test_loop_times;
96
97 #define MAX_VALUE 357
98
99 #define EXPECT_MAT_NORM(mat, eps) \
100 { \
101     EXPECT_LE(TestUtils::checkNorm(mat), eps) \
102 }
103
104 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
105 { \
106     ASSERT_EQ(mat1.type(), mat2.type()); \
107     ASSERT_EQ(mat1.size(), mat2.size()); \
108     EXPECT_LE(TestUtils::checkNorm(mat1, mat2), eps) \
109         << "Size: " << mat1.size() << std::endl; \
110 }
111
112 #define EXPECT_MAT_NEAR_RELATIVE(mat1, mat2, eps) \
113 { \
114     ASSERT_EQ(mat1.type(), mat2.type()); \
115     ASSERT_EQ(mat1.size(), mat2.size()); \
116     EXPECT_LE(TestUtils::checkNormRelative(mat1, mat2), eps) \
117         << "Size: " << mat1.size() << std::endl; \
118 }
119
120 #define OCL_EXPECT_MATS_NEAR(name, eps) \
121 { \
122     EXPECT_MAT_NEAR(name ## _roi, u ## name ## _roi, eps); \
123     int nextValue = rng.next(); \
124     RNG dataRng1(nextValue), dataRng2(nextValue); \
125     dataRng1.fill(name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
126     dataRng2.fill(u ## name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
127     EXPECT_MAT_NEAR(name, u ## name, 0/*FLT_EPSILON*/); \
128 }
129
130 #define OCL_EXPECT_MATS_NEAR_RELATIVE(name, eps) \
131 { \
132     EXPECT_MAT_NEAR_RELATIVE(name ## _roi, u ## name ## _roi, eps); \
133     int nextValue = rng.next(); \
134     RNG dataRng1(nextValue), dataRng2(nextValue); \
135     dataRng1.fill(name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
136     dataRng2.fill(u ## name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
137     EXPECT_MAT_NEAR_RELATIVE(name, u ## name, 0/*FLT_EPSILON*/); \
138 }
139
140 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
141 { \
142     ASSERT_EQ(mat1.type(), mat2.type()); \
143     ASSERT_EQ(mat1.size(), mat2.size()); \
144     EXPECT_LE(checkSimilarity(mat1, mat2), eps) \
145         << "Size: " << mat1.size() << std::endl; \
146 }
147
148 using perf::MatDepth;
149 using perf::MatType;
150
151 #define OCL_RNG_SEED 123456
152
153 struct CV_EXPORTS TestUtils
154 {
155     cv::RNG rng;
156
157     TestUtils()
158     {
159         rng = cv::RNG(OCL_RNG_SEED);
160     }
161
162     int randomInt(int minVal, int maxVal)
163     {
164         return rng.uniform(minVal, maxVal);
165     }
166
167     double randomDouble(double minVal, double maxVal)
168     {
169         return rng.uniform(minVal, maxVal);
170     }
171
172     double randomDoubleLog(double minVal, double maxVal)
173     {
174         double logMin = log((double)minVal + 1);
175         double logMax = log((double)maxVal + 1);
176         double pow = rng.uniform(logMin, logMax);
177         double v = exp(pow) - 1;
178         CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal)));
179         return v;
180     }
181
182     Size randomSize(int minVal, int maxVal)
183     {
184 #if 1
185         return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal));
186 #else
187         return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
188 #endif
189     }
190
191     Size randomSize(int minValX, int maxValX, int minValY, int maxValY)
192     {
193 #if 1
194         return cv::Size((int)randomDoubleLog(minValX, maxValX), (int)randomDoubleLog(minValY, maxValY));
195 #else
196         return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
197 #endif
198     }
199
200     Scalar randomScalar(double minVal, double maxVal)
201     {
202         return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
203     }
204
205     Mat randomMat(Size size, int type, double minVal, double maxVal, bool useRoi = false)
206     {
207         RNG dataRng(rng.next());
208         return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi);
209     }
210
211     struct Border
212     {
213         int top, bot, lef, rig;
214     };
215
216     Border randomBorder(int minValue = 0, int maxValue = MAX_VALUE)
217     {
218         Border border = {
219                 (int)randomDoubleLog(minValue, maxValue),
220                 (int)randomDoubleLog(minValue, maxValue),
221                 (int)randomDoubleLog(minValue, maxValue),
222                 (int)randomDoubleLog(minValue, maxValue)
223         };
224         return border;
225     }
226
227     void randomSubMat(Mat& whole, Mat& subMat, const Size& roiSize, const Border& border, int type, double minVal, double maxVal)
228     {
229         Size wholeSize = Size(roiSize.width + border.lef + border.rig, roiSize.height + border.top + border.bot);
230         whole = randomMat(wholeSize, type, minVal, maxVal, false);
231         subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
232     }
233
234     // If the two vectors are not equal, it will return the difference in vector size
235     // Else it will return (total diff of each 1 and 2 rects covered pixels)/(total 1 rects covered pixels)
236     // The smaller, the better matched
237     static double checkRectSimilarity(cv::Size sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
238
239     //! read image from testdata folder.
240
241     static cv::Mat readImage(const String &fileName, int flags = cv::IMREAD_COLOR);
242     static cv::Mat readImageType(const String &fname, int type);
243
244     static double checkNorm(const cv::Mat &m);
245     static double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
246     static double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
247     static inline double checkNormRelative(const Mat &m1, const Mat &m2)
248     {
249         return cv::norm(m1, m2, cv::NORM_INF) /
250                 std::max((double)std::numeric_limits<float>::epsilon(),
251                          (double)std::max(cv::norm(m1, cv::NORM_INF), norm(m2, cv::NORM_INF)));
252     }
253     static void showDiff(const Mat& src, const Mat& gold, const Mat& actual, double eps, bool alwaysShow = false);
254
255     template <typename T1>
256     static double checkNorm(const T1& m)
257     {
258         return checkNorm(getMatForRead(m));
259     }
260     template <typename T1, typename T2>
261     static double checkNorm(const T1& m1, const T2& m2)
262     {
263         return checkNorm(getMatForRead(m1), getMatForRead(m2));
264     }
265     template <typename T1, typename T2>
266     static double checkSimilarity(const T1& m1, const T2& m2)
267     {
268         return checkSimilarity(getMatForRead(m1), getMatForRead(m2));
269     }
270     template <typename T1, typename T2>
271     static inline double checkNormRelative(const T1& m1, const T2& m2)
272     {
273         const Mat _m1 = getMatForRead(m1);
274         const Mat _m2 = getMatForRead(m2);
275         return checkNormRelative(_m1, _m2);
276     }
277
278     template <typename T1, typename T2, typename T3>
279     static void showDiff(const T1& src, const T2& gold, const T3& actual, double eps, bool alwaysShow = false)
280     {
281         const Mat _src = getMatForRead(src);
282         const Mat _gold = getMatForRead(gold);
283         const Mat _actual = getMatForRead(actual);
284         showDiff(_src, _gold, _actual, eps, alwaysShow);
285     }
286 };
287
288 #define TEST_DECLARE_INPUT_PARAMETER(name) Mat name, name ## _roi; UMat u ## name, u ## name ## _roi;
289 #define TEST_DECLARE_OUTPUT_PARAMETER(name) TEST_DECLARE_INPUT_PARAMETER(name)
290
291 #define UMAT_UPLOAD_INPUT_PARAMETER(name) \
292 { \
293     name.copyTo(u ## name); \
294     Size _wholeSize; Point ofs; name ## _roi.locateROI(_wholeSize, ofs); \
295     u ## name ## _roi = u ## name(Rect(ofs.x, ofs.y, name ## _roi.size().width, name ## _roi.size().height)); \
296 }
297 #define UMAT_UPLOAD_OUTPUT_PARAMETER(name) UMAT_UPLOAD_INPUT_PARAMETER(name)
298
299 template <typename T>
300 struct CV_EXPORTS TSTestWithParam : public TestUtils, public ::testing::TestWithParam<T>
301 {
302
303 };
304
305 #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > >
306
307 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
308
309 #ifndef IMPLEMENT_PARAM_CLASS
310 #define IMPLEMENT_PARAM_CLASS(name, type) \
311     class name \
312     { \
313     public: \
314         name ( type arg = type ()) : val_(arg) {} \
315         operator type () const {return val_;} \
316     private: \
317         type val_; \
318     }; \
319     inline void PrintTo( name param, std::ostream* os) \
320     { \
321         *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
322     }
323
324 IMPLEMENT_PARAM_CLASS(Channels, int)
325 #endif // IMPLEMENT_PARAM_CLASS
326
327 #define OCL_TEST_P TEST_P
328 #define OCL_TEST_F(name, ...) typedef name OCL_##name; TEST_F(OCL_##name, __VA_ARGS__)
329 #define OCL_TEST(name, ...) TEST(OCL_##name, __VA_ARGS__)
330
331 #define OCL_OFF(fn) cv::ocl::setUseOpenCL(false); fn
332 #define OCL_ON(fn) cv::ocl::setUseOpenCL(true); fn
333
334 #define OCL_ALL_DEPTHS Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F)
335 #define OCL_ALL_CHANNELS Values(1, 2, 3, 4)
336
337 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA)
338 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
339 CV_ENUM(BorderType, BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101)
340
341 #define OCL_INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) \
342     INSTANTIATE_TEST_CASE_P(OCL_ ## prefix, test_case_name, generator)
343
344 }} // namespace cvtest::ocl
345
346 #endif // HAVE_OPENCL
347
348 #endif // __OPENCV_TS_OCL_TEST_HPP__