CLAHE Python bindings
[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 //! return true if device supports specified feature and gpu module was built with support the feature.
59 //bool supportFeature(const cv::gpu::DeviceInfo& info, cv::gpu::FeatureSet feature);
60
61 //! return all devices compatible with current gpu module build.
62 //const std::vector<cv::ocl::DeviceInfo>& devices();
63 //! return all devices compatible with current gpu module build which support specified feature.
64 //std::vector<cv::ocl::DeviceInfo> devices(cv::gpu::FeatureSet feature);
65
66 //! read image from testdata folder.
67 cv::Mat readImage(const std::string &fileName, int flags = cv::IMREAD_COLOR);
68 cv::Mat readImageType(const std::string &fname, int type);
69
70 double checkNorm(const cv::Mat &m);
71 double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
72 double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
73
74 #define EXPECT_MAT_NORM(mat, eps) \
75 { \
76     EXPECT_LE(checkNorm(cv::Mat(mat)), eps) \
77 }
78
79 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
80 { \
81    ASSERT_EQ(mat1.type(), mat2.type()); \
82    ASSERT_EQ(mat1.size(), mat2.size()); \
83    EXPECT_LE(checkNorm(cv::Mat(mat1), cv::Mat(mat2)), eps); \
84 }
85 /*
86 #define EXPECT_MAT_NEAR(mat1, mat2, eps,s) \
87 { \
88     ASSERT_EQ(mat1.type(), mat2.type()); \
89     ASSERT_EQ(mat1.size(), mat2.size()); \
90     EXPECT_LE(checkNorm(cv::Mat(mat1), cv::Mat(mat2)), eps)<<s; \
91 }
92 */
93 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
94 { \
95     ASSERT_EQ(mat1.type(), mat2.type()); \
96     ASSERT_EQ(mat1.size(), mat2.size()); \
97     EXPECT_LE(checkSimilarity(cv::Mat(mat1), cv::Mat(mat2)), eps); \
98 }
99
100 namespace cv
101 {
102     namespace ocl
103     {
104         // void PrintTo(const DeviceInfo& info, std::ostream* os);
105     }
106 }
107
108 using perf::MatDepth;
109 using perf::MatType;
110
111 //! return vector with types from specified range.
112 std::vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end);
113
114 //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).
115 const std::vector<MatType> &all_types();
116
117 class Inverse
118 {
119 public:
120     inline Inverse(bool val = false) : val_(val) {}
121
122     inline operator bool() const
123     {
124         return val_;
125     }
126
127 private:
128     bool val_;
129 };
130
131 void PrintTo(const Inverse &useRoi, std::ostream *os);
132
133 enum {FLIP_BOTH = 0, FLIP_X = 1, FLIP_Y = -1};
134 CV_ENUM(FlipCode, FLIP_BOTH, FLIP_X, FLIP_Y)
135
136 CV_ENUM(CmpCode, CMP_EQ, CMP_GT, CMP_GE, CMP_LT, CMP_LE, CMP_NE)
137 CV_ENUM(NormCode, NORM_INF, NORM_L1, NORM_L2, NORM_TYPE_MASK, NORM_RELATIVE, NORM_MINMAX)
138 CV_ENUM(ReduceOp, CV_REDUCE_SUM, CV_REDUCE_AVG, CV_REDUCE_MAX, CV_REDUCE_MIN)
139 CV_ENUM(MorphOp, MORPH_OPEN, MORPH_CLOSE, MORPH_GRADIENT, MORPH_TOPHAT, MORPH_BLACKHAT)
140 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
141 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC)
142 CV_ENUM(Border, BORDER_REFLECT101, BORDER_REPLICATE, BORDER_CONSTANT, BORDER_REFLECT, BORDER_WRAP)
143 CV_ENUM(TemplateMethod, TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED)
144
145 CV_FLAGS(GemmFlags, GEMM_1_T, GEMM_2_T, GEMM_3_T);
146 CV_FLAGS(WarpFlags, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, WARP_INVERSE_MAP)
147 CV_FLAGS(DftFlags, DFT_INVERSE, DFT_SCALE, DFT_ROWS, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT)
148
149 void  run_perf_test();
150
151 #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > >
152
153 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
154
155 #define ALL_DEVICES testing::ValuesIn(devices())
156 #define DEVICES(feature) testing::ValuesIn(devices(feature))
157
158 #define ALL_TYPES testing::ValuesIn(all_types())
159 #define TYPES(depth_start, depth_end, cn_start, cn_end) testing::ValuesIn(types(depth_start, depth_end, cn_start, cn_end))
160
161 #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113), cv::Size(1300, 1300))
162
163 #define DIRECT_INVERSE testing::Values(Inverse(false), Inverse(true))
164
165 #ifndef ALL_DEPTH
166 #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))
167 #endif
168 #define REPEAT   1000
169 #define COUNT_U  0 // count the uploading execution time for ocl mat structures
170 #define COUNT_D  0
171 // the following macro section tests the target function (kernel) performance
172 // upload is the code snippet for converting cv::mat to cv::ocl::oclMat
173 // downloading is the code snippet for converting cv::ocl::oclMat back to cv::mat
174 // change COUNT_U and COUNT_D to take downloading and uploading time into account
175 #define P_TEST_FULL( upload, kernel_call, download ) \
176 { \
177     std::cout<< "\n" #kernel_call "\n----------------------"; \
178     {upload;} \
179     R_TEST( kernel_call, 2 ); \
180     double t = (double)cvGetTickCount(); \
181     R_T( { \
182             if( COUNT_U ) {upload;} \
183             kernel_call; \
184             if( COUNT_D ) {download;} \
185             } ); \
186     t = (double)cvGetTickCount() - t; \
187     std::cout << "runtime is  " << t/((double)cvGetTickFrequency()* 1000.) << "ms" << std::endl; \
188 }
189
190 #define R_T2( test ) \
191 { \
192     std::cout<< "\n" #test "\n----------------------"; \
193     R_TEST( test, 15 ) \
194     clock_t st = clock(); \
195     R_T( test ) \
196     std::cout<< clock() - st << "ms\n"; \
197 }
198 #define R_T( test ) \
199     R_TEST( test, REPEAT )
200 #define R_TEST( test, repeat ) \
201     try{ \
202         for( int i = 0; i < repeat; i ++ ) { test; } \
203     } catch( ... ) { std::cout << "||||| Exception catched! |||||\n"; return; }
204
205 //////// Utility
206
207 #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))
208 #ifndef IMPLEMENT_PARAM_CLASS
209 #define IMPLEMENT_PARAM_CLASS(name, type) \
210     class name \
211     { \
212     public: \
213         name ( type arg = type ()) : val_(arg) {} \
214         operator type () const {return val_;} \
215     private: \
216         type val_; \
217     }; \
218     inline void PrintTo( name param, std::ostream* os) \
219     { \
220         *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
221     }
222
223 IMPLEMENT_PARAM_CLASS(Channels, int)
224 #endif // IMPLEMENT_PARAM_CLASS
225
226 #endif // __OPENCV_TEST_UTILITY_HPP__