f509b786ad59102e3032a1d66d5488a4e0f3fbf0
[profile/ivi/opencv.git] / modules / gpu / test / utility.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #ifndef __OPENCV_TEST_UTILITY_HPP__\r
43 #define __OPENCV_TEST_UTILITY_HPP__\r
44 \r
45 #include <vector>\r
46 #include <string>\r
47 #include "opencv2/core/core.hpp"\r
48 #include "opencv2/highgui/highgui.hpp"\r
49 #include "opencv2/gpu/gpu.hpp"\r
50 #include "opencv2/ts/ts.hpp"\r
51 #include "opencv2/ts/ts_perf.hpp"\r
52 \r
53 //////////////////////////////////////////////////////////////////////\r
54 // random generators\r
55 \r
56 int randomInt(int minVal, int maxVal);\r
57 double randomDouble(double minVal, double maxVal);\r
58 cv::Size randomSize(int minVal, int maxVal);\r
59 cv::Scalar randomScalar(double minVal, double maxVal);\r
60 cv::Mat randomMat(cv::Size size, int type, double minVal = 0.0, double maxVal = 255.0);\r
61 \r
62 //////////////////////////////////////////////////////////////////////\r
63 // GpuMat create\r
64 \r
65 cv::gpu::GpuMat createMat(cv::Size size, int type, bool useRoi = false);\r
66 cv::gpu::GpuMat loadMat(const cv::Mat& m, bool useRoi = false);\r
67 \r
68 //////////////////////////////////////////////////////////////////////\r
69 // Image load\r
70 \r
71 //! read image from testdata folder\r
72 cv::Mat readImage(const std::string& fileName, int flags = cv::IMREAD_COLOR);\r
73 \r
74 //! read image from testdata folder and convert it to specified type\r
75 cv::Mat readImageType(const std::string& fname, int type);\r
76 \r
77 //////////////////////////////////////////////////////////////////////\r
78 // Gpu devices\r
79 \r
80 //! return true if device supports specified feature and gpu module was built with support the feature.\r
81 bool supportFeature(const cv::gpu::DeviceInfo& info, cv::gpu::FeatureSet feature);\r
82 \r
83 class DeviceManager\r
84 {\r
85 public:\r
86     static DeviceManager& instance();\r
87 \r
88     void load(int i);\r
89     void loadAll();\r
90 \r
91     const std::vector<cv::gpu::DeviceInfo>& values() const { return devices_; }\r
92 \r
93 private:\r
94     std::vector<cv::gpu::DeviceInfo> devices_;\r
95 };\r
96 \r
97 #define ALL_DEVICES testing::ValuesIn(DeviceManager::instance().values())\r
98 \r
99 //////////////////////////////////////////////////////////////////////\r
100 // Additional assertion\r
101 \r
102 cv::Mat getMat(cv::InputArray arr);\r
103 \r
104 double checkNorm(cv::InputArray m1, cv::InputArray m2);\r
105 \r
106 void minMaxLocGold(const cv::Mat& src, double* minVal_, double* maxVal_ = 0, cv::Point* minLoc_ = 0, cv::Point* maxLoc_ = 0, const cv::Mat& mask = cv::Mat());\r
107 \r
108 testing::AssertionResult assertMatNear(const char* expr1, const char* expr2, const char* eps_expr, cv::InputArray m1, cv::InputArray m2, double eps);\r
109 \r
110 #define EXPECT_MAT_NEAR(m1, m2, eps) EXPECT_PRED_FORMAT3(assertMatNear, m1, m2, eps)\r
111 #define ASSERT_MAT_NEAR(m1, m2, eps) ASSERT_PRED_FORMAT3(assertMatNear, m1, m2, eps)\r
112 \r
113 #define EXPECT_SCALAR_NEAR(s1, s2, eps) \\r
114     { \\r
115         EXPECT_NEAR(s1[0], s2[0], eps); \\r
116         EXPECT_NEAR(s1[1], s2[1], eps); \\r
117         EXPECT_NEAR(s1[2], s2[2], eps); \\r
118         EXPECT_NEAR(s1[3], s2[3], eps); \\r
119     }\r
120 #define ASSERT_SCALAR_NEAR(s1, s2, eps) \\r
121     { \\r
122         ASSERT_NEAR(s1[0], s2[0], eps); \\r
123         ASSERT_NEAR(s1[1], s2[1], eps); \\r
124         ASSERT_NEAR(s1[2], s2[2], eps); \\r
125         ASSERT_NEAR(s1[3], s2[3], eps); \\r
126     }\r
127 \r
128 #define EXPECT_POINT2_NEAR(p1, p2, eps) \\r
129     { \\r
130         EXPECT_NEAR(p1.x, p2.x, eps); \\r
131         EXPECT_NEAR(p1.y, p2.y, eps); \\r
132     }\r
133 #define ASSERT_POINT2_NEAR(p1, p2, eps) \\r
134     { \\r
135         ASSERT_NEAR(p1.x, p2.x, eps); \\r
136         ASSERT_NEAR(p1.y, p2.y, eps); \\r
137     }\r
138 \r
139 #define EXPECT_POINT3_NEAR(p1, p2, eps) \\r
140     { \\r
141         EXPECT_NEAR(p1.x, p2.x, eps); \\r
142         EXPECT_NEAR(p1.y, p2.y, eps); \\r
143         EXPECT_NEAR(p1.z, p2.z, eps); \\r
144     }\r
145 #define ASSERT_POINT3_NEAR(p1, p2, eps) \\r
146     { \\r
147         ASSERT_NEAR(p1.x, p2.x, eps); \\r
148         ASSERT_NEAR(p1.y, p2.y, eps); \\r
149         ASSERT_NEAR(p1.z, p2.z, eps); \\r
150     }\r
151 \r
152 double checkSimilarity(cv::InputArray m1, cv::InputArray m2);\r
153 \r
154 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \\r
155     { \\r
156         ASSERT_EQ(mat1.type(), mat2.type()); \\r
157         ASSERT_EQ(mat1.size(), mat2.size()); \\r
158         EXPECT_LE(checkSimilarity(mat1, mat2), eps); \\r
159     }\r
160 #define ASSERT_MAT_SIMILAR(mat1, mat2, eps) \\r
161     { \\r
162         ASSERT_EQ(mat1.type(), mat2.type()); \\r
163         ASSERT_EQ(mat1.size(), mat2.size()); \\r
164         ASSERT_LE(checkSimilarity(mat1, mat2), eps); \\r
165     }\r
166 \r
167 //////////////////////////////////////////////////////////////////////\r
168 // Helper structs for value-parameterized tests\r
169 \r
170 #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > >\r
171 #define GET_PARAM(k) std::tr1::get< k >(GetParam())\r
172 \r
173 namespace cv { namespace gpu\r
174 {\r
175     void PrintTo(const DeviceInfo& info, std::ostream* os);\r
176 }}\r
177 \r
178 #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113))\r
179 \r
180 // Depth\r
181 \r
182 using perf::MatDepth;\r
183 \r
184 //! return vector with depths from specified range.\r
185 std::vector<MatDepth> depths(int depth_start, int depth_end);\r
186 \r
187 #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))\r
188 #define DEPTHS(depth_start, depth_end) testing::ValuesIn(depths(depth_start, depth_end))\r
189 #define DEPTH_PAIRS testing::Values(std::make_pair(MatDepth(CV_8U), MatDepth(CV_8U)),   \\r
190                                     std::make_pair(MatDepth(CV_8U), MatDepth(CV_16U)),  \\r
191                                     std::make_pair(MatDepth(CV_8U), MatDepth(CV_16S)),  \\r
192                                     std::make_pair(MatDepth(CV_8U), MatDepth(CV_32S)),  \\r
193                                     std::make_pair(MatDepth(CV_8U), MatDepth(CV_32F)),  \\r
194                                     std::make_pair(MatDepth(CV_8U), MatDepth(CV_64F)),  \\r
195                                                                                         \\r
196                                     std::make_pair(MatDepth(CV_16U), MatDepth(CV_16U)), \\r
197                                     std::make_pair(MatDepth(CV_16U), MatDepth(CV_32S)), \\r
198                                     std::make_pair(MatDepth(CV_16U), MatDepth(CV_32F)), \\r
199                                     std::make_pair(MatDepth(CV_16U), MatDepth(CV_64F)), \\r
200                                                                                         \\r
201                                     std::make_pair(MatDepth(CV_16S), MatDepth(CV_16S)), \\r
202                                     std::make_pair(MatDepth(CV_16S), MatDepth(CV_32S)), \\r
203                                     std::make_pair(MatDepth(CV_16S), MatDepth(CV_32F)), \\r
204                                     std::make_pair(MatDepth(CV_16S), MatDepth(CV_64F)), \\r
205                                                                                         \\r
206                                     std::make_pair(MatDepth(CV_32S), MatDepth(CV_32S)), \\r
207                                     std::make_pair(MatDepth(CV_32S), MatDepth(CV_32F)), \\r
208                                     std::make_pair(MatDepth(CV_32S), MatDepth(CV_64F)), \\r
209                                                                                         \\r
210                                     std::make_pair(MatDepth(CV_32F), MatDepth(CV_32F)), \\r
211                                     std::make_pair(MatDepth(CV_32F), MatDepth(CV_64F)), \\r
212                                                                                         \\r
213                                     std::make_pair(MatDepth(CV_64F), MatDepth(CV_64F)))\r
214 \r
215 // Type\r
216 \r
217 using perf::MatType;\r
218 \r
219 //! return vector with types from specified range.\r
220 std::vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end);\r
221 \r
222 //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).\r
223 const std::vector<MatType>& all_types();\r
224 \r
225 #define ALL_TYPES testing::ValuesIn(all_types())\r
226 #define TYPES(depth_start, depth_end, cn_start, cn_end) testing::ValuesIn(types(depth_start, depth_end, cn_start, cn_end))\r
227 \r
228 // ROI\r
229 \r
230 class UseRoi\r
231 {\r
232 public:\r
233     inline UseRoi(bool val = false) : val_(val) {}\r
234 \r
235     inline operator bool() const { return val_; }\r
236 \r
237 private:\r
238     bool val_;\r
239 };\r
240 \r
241 void PrintTo(const UseRoi& useRoi, std::ostream* os);\r
242 \r
243 #define WHOLE testing::Values(UseRoi(false))\r
244 #define SUBMAT testing::Values(UseRoi(true))\r
245 #define WHOLE_SUBMAT testing::Values(UseRoi(false), UseRoi(true))\r
246 \r
247 // Direct/Inverse\r
248 \r
249 class Inverse\r
250 {\r
251 public:\r
252     inline Inverse(bool val = false) : val_(val) {}\r
253 \r
254     inline operator bool() const { return val_; }\r
255 \r
256 private:\r
257     bool val_;\r
258 };\r
259 void PrintTo(const Inverse& useRoi, std::ostream* os);\r
260 #define DIRECT_INVERSE testing::Values(Inverse(false), Inverse(true))\r
261 \r
262 // Param class\r
263 \r
264 #define IMPLEMENT_PARAM_CLASS(name, type) \\r
265     class name \\r
266     { \\r
267     public: \\r
268         name ( type arg = type ()) : val_(arg) {} \\r
269         operator type () const {return val_;} \\r
270     private: \\r
271         type val_; \\r
272     }; \\r
273     inline void PrintTo( name param, std::ostream* os) \\r
274     { \\r
275         *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \\r
276     }\r
277 \r
278 IMPLEMENT_PARAM_CLASS(Channels, int)\r
279 \r
280 #define ALL_CHANNELS testing::Values(Channels(1), Channels(2), Channels(3), Channels(4))\r
281 #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))\r
282 \r
283 // Flags and enums\r
284 \r
285 CV_ENUM(NormCode, cv::NORM_INF, cv::NORM_L1, cv::NORM_L2, cv::NORM_TYPE_MASK, cv::NORM_RELATIVE, cv::NORM_MINMAX)\r
286 \r
287 CV_ENUM(Interpolation, cv::INTER_NEAREST, cv::INTER_LINEAR, cv::INTER_CUBIC, cv::INTER_AREA)\r
288 \r
289 CV_ENUM(BorderType, cv::BORDER_REFLECT101, cv::BORDER_REPLICATE, cv::BORDER_CONSTANT, cv::BORDER_REFLECT, cv::BORDER_WRAP)\r
290 #define ALL_BORDER_TYPES testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_CONSTANT), BorderType(cv::BORDER_REFLECT), BorderType(cv::BORDER_WRAP))\r
291 \r
292 CV_FLAGS(WarpFlags, cv::INTER_NEAREST, cv::INTER_LINEAR, cv::INTER_CUBIC, cv::WARP_INVERSE_MAP)\r
293 \r
294 //////////////////////////////////////////////////////////////////////\r
295 // Other\r
296 \r
297 void showDiff(cv::InputArray gold, cv::InputArray actual, double eps);\r
298 \r
299 #endif // __OPENCV_TEST_UTILITY_HPP__\r