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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 Intel Corporation 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_TEST_UTILITY_HPP__
43 #define __OPENCV_TEST_UTILITY_HPP__
55 //void showDiff(cv::InputArray gold, cv::InputArray actual, double eps);
57 cv::ocl::oclMat createMat_ocl(cv::RNG& rng, Size size, int type, bool useRoi);
58 cv::ocl::oclMat loadMat_ocl(cv::RNG& rng, const Mat& m, bool useRoi);
60 // This function test if gpu_rst matches cpu_rst.
61 // If the two vectors are not equal, it will return the difference in vector size
62 // Else it will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
63 // The smaller, the better matched
64 double checkRectSimilarity(cv::Size sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
67 //! read image from testdata folder.
68 cv::Mat readImage(const std::string &fileName, int flags = cv::IMREAD_COLOR);
69 cv::Mat readImageType(const std::string &fname, int type);
71 double checkNorm(const cv::Mat &m);
72 double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
73 double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
75 #define EXPECT_MAT_NORM(mat, eps) \
77 EXPECT_LE(checkNorm(cv::Mat(mat)), eps) \
80 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
82 ASSERT_EQ(mat1.type(), mat2.type()); \
83 ASSERT_EQ(mat1.size(), mat2.size()); \
84 EXPECT_LE(checkNorm(cv::Mat(mat1), cv::Mat(mat2)), eps); \
87 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
89 ASSERT_EQ(mat1.type(), mat2.type()); \
90 ASSERT_EQ(mat1.size(), mat2.size()); \
91 EXPECT_LE(checkSimilarity(cv::Mat(mat1), cv::Mat(mat2)), eps); \
98 //! return vector with types from specified range.
99 std::vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end);
101 //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).
102 const std::vector<MatType> &all_types();
107 inline Inverse(bool val = false) : val_(val) {}
109 inline operator bool() const
118 void PrintTo(const Inverse &useRoi, std::ostream *os);
120 #define OCL_RNG_SEED 123456
122 template <typename T>
123 struct TSTestWithParam : public ::testing::TestWithParam<T>
129 rng = cv::RNG(OCL_RNG_SEED);
132 int randomInt(int minVal, int maxVal)
134 return rng.uniform(minVal, maxVal);
137 double randomDouble(double minVal, double maxVal)
139 return rng.uniform(minVal, maxVal);
142 double randomDoubleLog(double minVal, double maxVal)
144 double logMin = log((double)minVal + 1);
145 double logMax = log((double)maxVal + 1);
146 double pow = rng.uniform(logMin, logMax);
147 double v = exp(pow) - 1;
148 CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal)));
152 Size randomSize(int minVal, int maxVal)
155 return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal));
157 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
161 Size randomSize(int minValX, int maxValX, int minValY, int maxValY)
164 return cv::Size(randomDoubleLog(minValX, maxValX), randomDoubleLog(minValY, maxValY));
166 return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
170 Scalar randomScalar(double minVal, double maxVal)
172 return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
175 Mat randomMat(Size size, int type, double minVal, double maxVal, bool useRoi = false)
177 RNG dataRng(rng.next());
178 return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi);
183 int top, bot, lef, rig;
186 Border randomBorder(int minValue = 0, int maxValue = MAX_VALUE)
189 (int)randomDoubleLog(minValue, maxValue),
190 (int)randomDoubleLog(minValue, maxValue),
191 (int)randomDoubleLog(minValue, maxValue),
192 (int)randomDoubleLog(minValue, maxValue)
197 void randomSubMat(Mat& whole, Mat& subMat, const Size& roiSize, const Border& border, int type, double minVal, double maxVal)
199 Size wholeSize = Size(roiSize.width + border.lef + border.rig, roiSize.height + border.top + border.bot);
200 whole = randomMat(wholeSize, type, minVal, maxVal, false);
201 subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
204 void generateOclMat(cv::ocl::oclMat& whole, cv::ocl::oclMat& subMat, const Mat& wholeMat, const Size& roiSize, const Border& border)
207 subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
211 #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > >
213 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
215 #define ALL_TYPES testing::ValuesIn(all_types())
216 #define TYPES(depth_start, depth_end, cn_start, cn_end) testing::ValuesIn(types(depth_start, depth_end, cn_start, cn_end))
218 #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113), cv::Size(1300, 1300))
220 #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))
221 #ifndef IMPLEMENT_PARAM_CLASS
222 #define IMPLEMENT_PARAM_CLASS(name, type) \
226 name ( type arg = type ()) : val_(arg) {} \
227 operator type () const {return val_;} \
231 inline void PrintTo( name param, std::ostream* os) \
233 *os << #name << "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
236 IMPLEMENT_PARAM_CLASS(Channels, int)
237 #endif // IMPLEMENT_PARAM_CLASS
239 } // namespace cvtest
241 enum {FLIP_BOTH = 0, FLIP_X = 1, FLIP_Y = -1};
242 CV_ENUM(FlipCode, FLIP_BOTH, FLIP_X, FLIP_Y)
244 CV_ENUM(CmpCode, CMP_EQ, CMP_GT, CMP_GE, CMP_LT, CMP_LE, CMP_NE)
245 CV_ENUM(NormCode, NORM_INF, NORM_L1, NORM_L2, NORM_TYPE_MASK, NORM_RELATIVE, NORM_MINMAX)
246 CV_ENUM(ReduceOp, CV_REDUCE_SUM, CV_REDUCE_AVG, CV_REDUCE_MAX, CV_REDUCE_MIN)
247 CV_ENUM(MorphOp, MORPH_OPEN, MORPH_CLOSE, MORPH_GRADIENT, MORPH_TOPHAT, MORPH_BLACKHAT)
248 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
249 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC)
250 CV_ENUM(Border, BORDER_REFLECT101, BORDER_REPLICATE, BORDER_CONSTANT, BORDER_REFLECT, BORDER_WRAP)
251 CV_ENUM(TemplateMethod, TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED)
253 CV_FLAGS(GemmFlags, GEMM_1_T, GEMM_2_T, GEMM_3_T);
254 CV_FLAGS(WarpFlags, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, WARP_INVERSE_MAP)
255 CV_FLAGS(DftFlags, DFT_INVERSE, DFT_SCALE, DFT_ROWS, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT)
257 # define OCL_TEST_P(test_case_name, test_name) \
258 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : \
259 public test_case_name { \
261 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() { } \
262 virtual void TestBody(); \
263 void OCLTestBody(); \
265 static int AddToRegistry() \
267 ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \
268 GetTestCasePatternHolder<test_case_name>(\
269 #test_case_name, __FILE__, __LINE__)->AddTestPattern(\
272 new ::testing::internal::TestMetaFactory< \
273 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>()); \
277 static int gtest_registering_dummy_; \
278 GTEST_DISALLOW_COPY_AND_ASSIGN_(\
279 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)); \
282 int GTEST_TEST_CLASS_NAME_(test_case_name, \
283 test_name)::gtest_registering_dummy_ = \
284 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \
286 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() \
292 catch (const cv::Exception & ex) \
294 if (ex.code != CV_OpenCLDoubleNotSupported) \
297 std::cout << "Test skipped (selected device does not support double)" << std::endl; \
301 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::OCLTestBody()
303 #endif // __OPENCV_TEST_UTILITY_HPP__