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__
48 int randomInt(int minVal, int maxVal);
49 double randomDouble(double minVal, double maxVal);
50 //std::string generateVarList(int first,...);
51 std::string generateVarList(int &p1, int &p2);
52 cv::Size randomSize(int minVal, int maxVal);
53 cv::Scalar randomScalar(double minVal, double maxVal);
54 cv::Mat randomMat(cv::Size size, int type, double minVal = 0.0, double maxVal = 255.0);
56 void showDiff(cv::InputArray gold, cv::InputArray actual, double eps);
58 // This function test if gpu_rst matches cpu_rst.
59 // If the two vectors are not equal, it will return the difference in vector size
60 // Else it will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
61 // The smaller, the better matched
62 double checkRectSimilarity(cv::Size sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
65 //! read image from testdata folder.
66 cv::Mat readImage(const std::string &fileName, int flags = cv::IMREAD_COLOR);
67 cv::Mat readImageType(const std::string &fname, int type);
69 double checkNorm(const cv::Mat &m);
70 double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
71 double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
73 #define EXPECT_MAT_NORM(mat, eps) \
75 EXPECT_LE(checkNorm(cv::Mat(mat)), eps) \
78 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
80 ASSERT_EQ(mat1.type(), mat2.type()); \
81 ASSERT_EQ(mat1.size(), mat2.size()); \
82 EXPECT_LE(checkNorm(cv::Mat(mat1), cv::Mat(mat2)), eps); \
85 #define EXPECT_MAT_NEAR(mat1, mat2, eps,s) \
87 ASSERT_EQ(mat1.type(), mat2.type()); \
88 ASSERT_EQ(mat1.size(), mat2.size()); \
89 EXPECT_LE(checkNorm(cv::Mat(mat1), cv::Mat(mat2)), eps)<<s; \
92 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
94 ASSERT_EQ(mat1.type(), mat2.type()); \
95 ASSERT_EQ(mat1.size(), mat2.size()); \
96 EXPECT_LE(checkSimilarity(cv::Mat(mat1), cv::Mat(mat2)), eps); \
103 // void PrintTo(const DeviceInfo& info, std::ostream* os);
107 using perf::MatDepth;
110 //! return vector with types from specified range.
111 std::vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end);
113 //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).
114 const std::vector<MatType> &all_types();
119 inline Inverse(bool val = false) : val_(val) {}
121 inline operator bool() const
130 void PrintTo(const Inverse &useRoi, std::ostream *os);
132 enum {FLIP_BOTH = 0, FLIP_X = 1, FLIP_Y = -1};
133 CV_ENUM(FlipCode, FLIP_BOTH, FLIP_X, FLIP_Y)
135 CV_ENUM(CmpCode, CMP_EQ, CMP_GT, CMP_GE, CMP_LT, CMP_LE, CMP_NE)
136 CV_ENUM(NormCode, NORM_INF, NORM_L1, NORM_L2, NORM_TYPE_MASK, NORM_RELATIVE, NORM_MINMAX)
137 CV_ENUM(ReduceOp, CV_REDUCE_SUM, CV_REDUCE_AVG, CV_REDUCE_MAX, CV_REDUCE_MIN)
138 CV_ENUM(MorphOp, MORPH_OPEN, MORPH_CLOSE, MORPH_GRADIENT, MORPH_TOPHAT, MORPH_BLACKHAT)
139 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
140 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC)
141 CV_ENUM(Border, BORDER_REFLECT101, BORDER_REPLICATE, BORDER_CONSTANT, BORDER_REFLECT, BORDER_WRAP)
142 CV_ENUM(TemplateMethod, TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED)
144 CV_FLAGS(GemmFlags, GEMM_1_T, GEMM_2_T, GEMM_3_T);
145 CV_FLAGS(WarpFlags, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, WARP_INVERSE_MAP)
146 CV_FLAGS(DftFlags, DFT_INVERSE, DFT_SCALE, DFT_ROWS, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT)
148 void run_perf_test();
150 #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > >
152 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
154 #define ALL_DEVICES testing::ValuesIn(devices())
155 #define DEVICES(feature) testing::ValuesIn(devices(feature))
157 #define ALL_TYPES testing::ValuesIn(all_types())
158 #define TYPES(depth_start, depth_end, cn_start, cn_end) testing::ValuesIn(types(depth_start, depth_end, cn_start, cn_end))
160 #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113), cv::Size(1300, 1300))
162 #define DIRECT_INVERSE testing::Values(Inverse(false), Inverse(true))
165 #define ALL_DEPTH testing::Values(MatDepth(CV_8U), MatDepth(CV_8S), MatDepth(CV_16U), MatDepth(CV_16S), MatDepth(CV_32S), MatDepth(CV_32F), MatDepth(CV_64F))
168 #define COUNT_U 0 // count the uploading execution time for ocl mat structures
170 // the following macro section tests the target function (kernel) performance
171 // upload is the code snippet for converting cv::mat to cv::ocl::oclMat
172 // downloading is the code snippet for converting cv::ocl::oclMat back to cv::mat
173 // change COUNT_U and COUNT_D to take downloading and uploading time into account
174 #define P_TEST_FULL( upload, kernel_call, download ) \
176 std::cout<< "\n" #kernel_call "\n----------------------"; \
178 R_TEST( kernel_call, 2 ); \
179 double t = (double)cvGetTickCount(); \
181 if( COUNT_U ) {upload;} \
183 if( COUNT_D ) {download;} \
185 t = (double)cvGetTickCount() - t; \
186 std::cout << "runtime is " << t/((double)cvGetTickFrequency()* 1000.) << "ms" << std::endl; \
189 #define R_T2( test ) \
191 std::cout<< "\n" #test "\n----------------------"; \
193 clock_t st = clock(); \
195 std::cout<< clock() - st << "ms\n"; \
197 #define R_T( test ) \
198 R_TEST( test, REPEAT )
199 #define R_TEST( test, repeat ) \
201 for( int i = 0; i < repeat; i ++ ) { test; } \
202 } catch( ... ) { std::cout << "||||| Exception catched! |||||\n"; return; }
206 #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))
207 #ifndef IMPLEMENT_PARAM_CLASS
208 #define IMPLEMENT_PARAM_CLASS(name, type) \
212 name ( type arg = type ()) : val_(arg) {} \
213 operator type () const {return val_;} \
217 inline void PrintTo( name param, std::ostream* os) \
219 *os << #name << "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
222 IMPLEMENT_PARAM_CLASS(Channels, int)
223 #endif // IMPLEMENT_PARAM_CLASS
225 #endif // __OPENCV_TEST_UTILITY_HPP__