Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / ocl / test / utility.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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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 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.
39 //
40 //M*/
41
42 #ifndef __OPENCV_TEST_UTILITY_HPP__
43 #define __OPENCV_TEST_UTILITY_HPP__
44 #define LOOP_TIMES 1
45 #define MWIDTH 256
46 #define MHEIGHT 256
47 //#define RANDOMROI
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);
55
56 void showDiff(cv::InputArray gold, cv::InputArray actual, double eps);
57
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);
63
64
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);
68
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);
72
73 #define EXPECT_MAT_NORM(mat, eps) \
74 { \
75     EXPECT_LE(checkNorm(cv::Mat(mat)), eps) \
76 }
77
78 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
79 { \
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); \
83 }
84 /*
85 #define EXPECT_MAT_NEAR(mat1, mat2, eps,s) \
86 { \
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; \
90 }
91 */
92 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
93 { \
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); \
97 }
98
99 namespace cv
100 {
101     namespace ocl
102     {
103         // void PrintTo(const DeviceInfo& info, std::ostream* os);
104     }
105 }
106
107 using perf::MatDepth;
108 using perf::MatType;
109
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);
112
113 //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).
114 const std::vector<MatType> &all_types();
115
116 class Inverse
117 {
118 public:
119     inline Inverse(bool val = false) : val_(val) {}
120
121     inline operator bool() const
122     {
123         return val_;
124     }
125
126 private:
127     bool val_;
128 };
129
130 void PrintTo(const Inverse &useRoi, std::ostream *os);
131
132 enum {FLIP_BOTH = 0, FLIP_X = 1, FLIP_Y = -1};
133 CV_ENUM(FlipCode, FLIP_BOTH, FLIP_X, FLIP_Y)
134
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)
143
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)
147
148 void  run_perf_test();
149
150 #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > >
151
152 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
153
154 #define ALL_DEVICES testing::ValuesIn(devices())
155 #define DEVICES(feature) testing::ValuesIn(devices(feature))
156
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))
159
160 #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113), cv::Size(1300, 1300))
161
162 #define DIRECT_INVERSE testing::Values(Inverse(false), Inverse(true))
163
164 #ifndef ALL_DEPTH
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))
166 #endif
167 #define REPEAT   1000
168 #define COUNT_U  0 // count the uploading execution time for ocl mat structures
169 #define COUNT_D  0
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 ) \
175 { \
176     std::cout<< "\n" #kernel_call "\n----------------------"; \
177     {upload;} \
178     R_TEST( kernel_call, 2 ); \
179     double t = (double)cvGetTickCount(); \
180     R_T( { \
181             if( COUNT_U ) {upload;} \
182             kernel_call; \
183             if( COUNT_D ) {download;} \
184             } ); \
185     t = (double)cvGetTickCount() - t; \
186     std::cout << "runtime is  " << t/((double)cvGetTickFrequency()* 1000.) << "ms" << std::endl; \
187 }
188
189 #define R_T2( test ) \
190 { \
191     std::cout<< "\n" #test "\n----------------------"; \
192     R_TEST( test, 15 ) \
193     clock_t st = clock(); \
194     R_T( test ) \
195     std::cout<< clock() - st << "ms\n"; \
196 }
197 #define R_T( test ) \
198     R_TEST( test, REPEAT )
199 #define R_TEST( test, repeat ) \
200     try{ \
201         for( int i = 0; i < repeat; i ++ ) { test; } \
202     } catch( ... ) { std::cout << "||||| Exception catched! |||||\n"; return; }
203
204 //////// Utility
205
206 #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))
207 #ifndef IMPLEMENT_PARAM_CLASS
208 #define IMPLEMENT_PARAM_CLASS(name, type) \
209     class name \
210     { \
211     public: \
212         name ( type arg = type ()) : val_(arg) {} \
213         operator type () const {return val_;} \
214     private: \
215         type val_; \
216     }; \
217     inline void PrintTo( name param, std::ostream* os) \
218     { \
219         *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
220     }
221
222 IMPLEMENT_PARAM_CLASS(Channels, int)
223 #endif // IMPLEMENT_PARAM_CLASS
224
225 #endif // __OPENCV_TEST_UTILITY_HPP__