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