1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #ifndef __OPENCV_TS_OCL_TEST_HPP__
43 #define __OPENCV_TS_OCL_TEST_HPP__
45 #include "opencv2/opencv_modules.hpp"
47 #include "opencv2/ts.hpp"
49 #include "opencv2/highgui.hpp"
50 #include "opencv2/imgproc.hpp"
51 #include "opencv2/imgproc/types_c.h"
52 #include "opencv2/core/ocl.hpp"
58 using namespace testing;
60 extern int test_loop_times;
64 #define EXPECT_MAT_NORM(mat, eps) \
66 EXPECT_LE(TestUtils::checkNorm(mat), eps) \
69 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
71 ASSERT_EQ(mat1.type(), mat2.type()); \
72 ASSERT_EQ(mat1.size(), mat2.size()); \
73 EXPECT_LE(TestUtils::checkNorm(mat1, mat2), eps) \
74 << "Size: " << mat1.size() << std::endl; \
77 #define EXPECT_MAT_NEAR_RELATIVE(mat1, mat2, eps) \
79 ASSERT_EQ(mat1.type(), mat2.type()); \
80 ASSERT_EQ(mat1.size(), mat2.size()); \
81 EXPECT_LE(TestUtils::checkNormRelative(mat1, mat2), eps) \
82 << "Size: " << mat1.size() << std::endl; \
85 #define OCL_EXPECT_MATS_NEAR(name, eps) \
87 EXPECT_MAT_NEAR(name ## _roi, u ## name ## _roi, eps); \
88 int nextValue = rng.next(); \
89 RNG dataRng1(nextValue), dataRng2(nextValue); \
90 dataRng1.fill(name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
91 dataRng2.fill(u ## name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
92 EXPECT_MAT_NEAR(name, u ## name, 0/*FLT_EPSILON*/); \
95 #define OCL_EXPECT_MATS_NEAR_RELATIVE(name, eps) \
97 EXPECT_MAT_NEAR_RELATIVE(name ## _roi, u ## name ## _roi, eps); \
98 int nextValue = rng.next(); \
99 RNG dataRng1(nextValue), dataRng2(nextValue); \
100 dataRng1.fill(name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
101 dataRng2.fill(u ## name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
102 EXPECT_MAT_NEAR_RELATIVE(name, u ## name, 0/*FLT_EPSILON*/); \
105 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
107 ASSERT_EQ(mat1.type(), mat2.type()); \
108 ASSERT_EQ(mat1.size(), mat2.size()); \
109 EXPECT_LE(checkSimilarity(mat1, mat2), eps) \
110 << "Size: " << mat1.size() << std::endl; \
113 using perf::MatDepth;
116 #define OCL_RNG_SEED 123456
118 struct CV_EXPORTS TestUtils
124 rng = cv::RNG(OCL_RNG_SEED);
127 int randomInt(int minVal, int maxVal)
129 return rng.uniform(minVal, maxVal);
132 double randomDouble(double minVal, double maxVal)
134 return rng.uniform(minVal, maxVal);
137 double randomDoubleLog(double minVal, double maxVal)
139 double logMin = log((double)minVal + 1);
140 double logMax = log((double)maxVal + 1);
141 double pow = rng.uniform(logMin, logMax);
142 double v = exp(pow) - 1;
143 CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal)));
147 Size randomSize(int minVal, int maxVal)
150 return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal));
152 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
156 Size randomSize(int minValX, int maxValX, int minValY, int maxValY)
159 return cv::Size((int)randomDoubleLog(minValX, maxValX), (int)randomDoubleLog(minValY, maxValY));
161 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
165 Scalar randomScalar(double minVal, double maxVal)
167 return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
170 Mat randomMat(Size size, int type, double minVal, double maxVal, bool useRoi = false)
172 RNG dataRng(rng.next());
173 return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi);
178 int top, bot, lef, rig;
181 Border randomBorder(int minValue = 0, int maxValue = MAX_VALUE)
184 (int)randomDoubleLog(minValue, maxValue),
185 (int)randomDoubleLog(minValue, maxValue),
186 (int)randomDoubleLog(minValue, maxValue),
187 (int)randomDoubleLog(minValue, maxValue)
192 void randomSubMat(Mat& whole, Mat& subMat, const Size& roiSize, const Border& border, int type, double minVal, double maxVal)
194 Size wholeSize = Size(roiSize.width + border.lef + border.rig, roiSize.height + border.top + border.bot);
195 whole = randomMat(wholeSize, type, minVal, maxVal, false);
196 subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
199 // If the two vectors are not equal, it will return the difference in vector size
200 // Else it will return (total diff of each 1 and 2 rects covered pixels)/(total 1 rects covered pixels)
201 // The smaller, the better matched
202 static double checkRectSimilarity(const cv::Size & sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
204 //! read image from testdata folder.
205 static cv::Mat readImage(const String &fileName, int flags = cv::IMREAD_COLOR);
206 static cv::Mat readImageType(const String &fname, int type);
208 static double checkNorm(InputArray m);
209 static double checkNorm(InputArray m1, InputArray m2);
210 static double checkSimilarity(InputArray m1, InputArray m2);
211 static void showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow);
213 static inline double checkNormRelative(InputArray m1, InputArray m2)
215 return cv::norm(m1.getMat(), m2.getMat(), cv::NORM_INF) /
216 std::max((double)std::numeric_limits<float>::epsilon(),
217 (double)std::max(cv::norm(m1.getMat(), cv::NORM_INF), norm(m2.getMat(), cv::NORM_INF)));
221 #define TEST_DECLARE_INPUT_PARAMETER(name) Mat name, name ## _roi; UMat u ## name, u ## name ## _roi;
222 #define TEST_DECLARE_OUTPUT_PARAMETER(name) TEST_DECLARE_INPUT_PARAMETER(name)
224 #define UMAT_UPLOAD_INPUT_PARAMETER(name) \
226 name.copyTo(u ## name); \
227 Size _wholeSize; Point ofs; name ## _roi.locateROI(_wholeSize, ofs); \
228 u ## name ## _roi = u ## name(Rect(ofs.x, ofs.y, name ## _roi.size().width, name ## _roi.size().height)); \
230 #define UMAT_UPLOAD_OUTPUT_PARAMETER(name) UMAT_UPLOAD_INPUT_PARAMETER(name)
232 template <typename T>
233 struct CV_EXPORTS TSTestWithParam : public TestUtils, public ::testing::TestWithParam<T>
238 #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > >
240 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
242 #ifndef IMPLEMENT_PARAM_CLASS
243 #define IMPLEMENT_PARAM_CLASS(name, type) \
247 name ( type arg = type ()) : val_(arg) {} \
248 operator type () const {return val_;} \
252 inline void PrintTo( name param, std::ostream* os) \
254 *os << #name << "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
257 IMPLEMENT_PARAM_CLASS(Channels, int)
258 #endif // IMPLEMENT_PARAM_CLASS
260 #define OCL_TEST_P TEST_P
261 #define OCL_TEST_F(name, ...) typedef name OCL_##name; TEST_F(OCL_##name, __VA_ARGS__)
262 #define OCL_TEST(name, ...) TEST(OCL_##name, __VA_ARGS__)
264 #define OCL_OFF(fn) cv::ocl::setUseOpenCL(false); fn
265 #define OCL_ON(fn) cv::ocl::setUseOpenCL(true); fn
267 #define OCL_ALL_DEPTHS Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F)
268 #define OCL_ALL_CHANNELS Values(1, 2, 3, 4)
270 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA)
271 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
272 CV_ENUM(BorderType, BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101)
274 #define OCL_INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) \
275 INSTANTIATE_TEST_CASE_P(OCL_ ## prefix, test_case_name, generator)
277 } } // namespace cvtest::ocl
279 #endif // __OPENCV_TS_OCL_TEST_HPP__