Home | History | Annotate | Download | only in ts
      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_CUDA_TEST_UTILITY_HPP__
     44 #define __OPENCV_CUDA_TEST_UTILITY_HPP__
     45 
     46 #include <stdexcept>
     47 #include "cvconfig.h"
     48 #include "opencv2/core.hpp"
     49 #include "opencv2/core/cuda.hpp"
     50 #include "opencv2/imgcodecs.hpp"
     51 #include "opencv2/highgui.hpp"
     52 #include "opencv2/imgproc.hpp"
     53 #include "opencv2/ts.hpp"
     54 
     55 namespace cvtest
     56 {
     57     //////////////////////////////////////////////////////////////////////
     58     // random generators
     59 
     60     CV_EXPORTS int randomInt(int minVal, int maxVal);
     61     CV_EXPORTS double randomDouble(double minVal, double maxVal);
     62     CV_EXPORTS cv::Size randomSize(int minVal, int maxVal);
     63     CV_EXPORTS cv::Scalar randomScalar(double minVal, double maxVal);
     64     CV_EXPORTS cv::Mat randomMat(cv::Size size, int type, double minVal = 0.0, double maxVal = 255.0);
     65 
     66     //////////////////////////////////////////////////////////////////////
     67     // GpuMat create
     68 
     69     CV_EXPORTS cv::cuda::GpuMat createMat(cv::Size size, int type, bool useRoi = false);
     70     CV_EXPORTS cv::cuda::GpuMat loadMat(const cv::Mat& m, bool useRoi = false);
     71 
     72     //////////////////////////////////////////////////////////////////////
     73     // Image load
     74 
     75     //! read image from testdata folder
     76     CV_EXPORTS cv::Mat readImage(const std::string& fileName, int flags = cv::IMREAD_COLOR);
     77 
     78     //! read image from testdata folder and convert it to specified type
     79     CV_EXPORTS cv::Mat readImageType(const std::string& fname, int type);
     80 
     81     //////////////////////////////////////////////////////////////////////
     82     // Gpu devices
     83 
     84     //! return true if device supports specified feature and gpu module was built with support the feature.
     85     CV_EXPORTS bool supportFeature(const cv::cuda::DeviceInfo& info, cv::cuda::FeatureSet feature);
     86 
     87     class CV_EXPORTS DeviceManager
     88     {
     89     public:
     90         static DeviceManager& instance();
     91 
     92         void load(int i);
     93         void loadAll();
     94 
     95         const std::vector<cv::cuda::DeviceInfo>& values() const { return devices_; }
     96 
     97     private:
     98         std::vector<cv::cuda::DeviceInfo> devices_;
     99     };
    100 
    101     #define ALL_DEVICES testing::ValuesIn(cvtest::DeviceManager::instance().values())
    102 
    103     //////////////////////////////////////////////////////////////////////
    104     // Additional assertion
    105 
    106     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());
    107 
    108     CV_EXPORTS cv::Mat getMat(cv::InputArray arr);
    109 
    110     CV_EXPORTS testing::AssertionResult assertMatNear(const char* expr1, const char* expr2, const char* eps_expr, cv::InputArray m1, cv::InputArray m2, double eps);
    111 
    112     #define EXPECT_MAT_NEAR(m1, m2, eps) EXPECT_PRED_FORMAT3(cvtest::assertMatNear, m1, m2, eps)
    113     #define ASSERT_MAT_NEAR(m1, m2, eps) ASSERT_PRED_FORMAT3(cvtest::assertMatNear, m1, m2, eps)
    114 
    115     #define EXPECT_SCALAR_NEAR(s1, s2, eps) \
    116         { \
    117             EXPECT_NEAR(s1[0], s2[0], eps); \
    118             EXPECT_NEAR(s1[1], s2[1], eps); \
    119             EXPECT_NEAR(s1[2], s2[2], eps); \
    120             EXPECT_NEAR(s1[3], s2[3], eps); \
    121         }
    122     #define ASSERT_SCALAR_NEAR(s1, s2, eps) \
    123         { \
    124             ASSERT_NEAR(s1[0], s2[0], eps); \
    125             ASSERT_NEAR(s1[1], s2[1], eps); \
    126             ASSERT_NEAR(s1[2], s2[2], eps); \
    127             ASSERT_NEAR(s1[3], s2[3], eps); \
    128         }
    129 
    130     #define EXPECT_POINT2_NEAR(p1, p2, eps) \
    131         { \
    132             EXPECT_NEAR(p1.x, p2.x, eps); \
    133             EXPECT_NEAR(p1.y, p2.y, eps); \
    134         }
    135     #define ASSERT_POINT2_NEAR(p1, p2, eps) \
    136         { \
    137             ASSERT_NEAR(p1.x, p2.x, eps); \
    138             ASSERT_NEAR(p1.y, p2.y, eps); \
    139         }
    140 
    141     #define EXPECT_POINT3_NEAR(p1, p2, eps) \
    142         { \
    143             EXPECT_NEAR(p1.x, p2.x, eps); \
    144             EXPECT_NEAR(p1.y, p2.y, eps); \
    145             EXPECT_NEAR(p1.z, p2.z, eps); \
    146         }
    147     #define ASSERT_POINT3_NEAR(p1, p2, eps) \
    148         { \
    149             ASSERT_NEAR(p1.x, p2.x, eps); \
    150             ASSERT_NEAR(p1.y, p2.y, eps); \
    151             ASSERT_NEAR(p1.z, p2.z, eps); \
    152         }
    153 
    154     CV_EXPORTS double checkSimilarity(cv::InputArray m1, cv::InputArray m2);
    155 
    156     #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
    157         { \
    158             ASSERT_EQ(mat1.type(), mat2.type()); \
    159             ASSERT_EQ(mat1.size(), mat2.size()); \
    160             EXPECT_LE(checkSimilarity(mat1, mat2), eps); \
    161         }
    162     #define ASSERT_MAT_SIMILAR(mat1, mat2, eps) \
    163         { \
    164             ASSERT_EQ(mat1.type(), mat2.type()); \
    165             ASSERT_EQ(mat1.size(), mat2.size()); \
    166             ASSERT_LE(checkSimilarity(mat1, mat2), eps); \
    167         }
    168 
    169     //////////////////////////////////////////////////////////////////////
    170     // Helper structs for value-parameterized tests
    171 
    172     #define CUDA_TEST_P(test_case_name, test_name) \
    173       class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \
    174           : public test_case_name { \
    175        public: \
    176         GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {} \
    177         virtual void TestBody(); \
    178        private: \
    179         void UnsafeTestBody(); \
    180         static int AddToRegistry() { \
    181           ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \
    182               GetTestCasePatternHolder<test_case_name>(\
    183                   #test_case_name, __FILE__, __LINE__)->AddTestPattern(\
    184                       #test_case_name, \
    185                       #test_name, \
    186                       new ::testing::internal::TestMetaFactory< \
    187                           GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>()); \
    188           return 0; \
    189         } \
    190         static int gtest_registering_dummy_; \
    191         GTEST_DISALLOW_COPY_AND_ASSIGN_(\
    192             GTEST_TEST_CLASS_NAME_(test_case_name, test_name)); \
    193       }; \
    194       int GTEST_TEST_CLASS_NAME_(test_case_name, \
    195                                  test_name)::gtest_registering_dummy_ = \
    196           GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \
    197       void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() \
    198       { \
    199         try \
    200         { \
    201           UnsafeTestBody(); \
    202         } \
    203         catch (...) \
    204         { \
    205           cv::cuda::resetDevice(); \
    206           throw; \
    207         } \
    208       } \
    209       void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::UnsafeTestBody()
    210 
    211     #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > >
    212     #define GET_PARAM(k) std::tr1::get< k >(GetParam())
    213 
    214     #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113))
    215 
    216     // Depth
    217 
    218     using perf::MatDepth;
    219 
    220     #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))
    221 
    222     #define DEPTH_PAIRS testing::Values(std::make_pair(MatDepth(CV_8U), MatDepth(CV_8U)),   \
    223                                         std::make_pair(MatDepth(CV_8U), MatDepth(CV_16U)),  \
    224                                         std::make_pair(MatDepth(CV_8U), MatDepth(CV_16S)),  \
    225                                         std::make_pair(MatDepth(CV_8U), MatDepth(CV_32S)),  \
    226                                         std::make_pair(MatDepth(CV_8U), MatDepth(CV_32F)),  \
    227                                         std::make_pair(MatDepth(CV_8U), MatDepth(CV_64F)),  \
    228                                                                                             \
    229                                         std::make_pair(MatDepth(CV_16U), MatDepth(CV_16U)), \
    230                                         std::make_pair(MatDepth(CV_16U), MatDepth(CV_32S)), \
    231                                         std::make_pair(MatDepth(CV_16U), MatDepth(CV_32F)), \
    232                                         std::make_pair(MatDepth(CV_16U), MatDepth(CV_64F)), \
    233                                                                                             \
    234                                         std::make_pair(MatDepth(CV_16S), MatDepth(CV_16S)), \
    235                                         std::make_pair(MatDepth(CV_16S), MatDepth(CV_32S)), \
    236                                         std::make_pair(MatDepth(CV_16S), MatDepth(CV_32F)), \
    237                                         std::make_pair(MatDepth(CV_16S), MatDepth(CV_64F)), \
    238                                                                                             \
    239                                         std::make_pair(MatDepth(CV_32S), MatDepth(CV_32S)), \
    240                                         std::make_pair(MatDepth(CV_32S), MatDepth(CV_32F)), \
    241                                         std::make_pair(MatDepth(CV_32S), MatDepth(CV_64F)), \
    242                                                                                             \
    243                                         std::make_pair(MatDepth(CV_32F), MatDepth(CV_32F)), \
    244                                         std::make_pair(MatDepth(CV_32F), MatDepth(CV_64F)), \
    245                                                                                             \
    246                                         std::make_pair(MatDepth(CV_64F), MatDepth(CV_64F)))
    247 
    248     // Type
    249 
    250     using perf::MatType;
    251 
    252     //! return vector with types from specified range.
    253     CV_EXPORTS std::vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end);
    254 
    255     //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).
    256     CV_EXPORTS const std::vector<MatType>& all_types();
    257 
    258     #define ALL_TYPES testing::ValuesIn(all_types())
    259     #define TYPES(depth_start, depth_end, cn_start, cn_end) testing::ValuesIn(types(depth_start, depth_end, cn_start, cn_end))
    260 
    261     // ROI
    262 
    263     class UseRoi
    264     {
    265     public:
    266         inline UseRoi(bool val = false) : val_(val) {}
    267 
    268         inline operator bool() const { return val_; }
    269 
    270     private:
    271         bool val_;
    272     };
    273 
    274     CV_EXPORTS void PrintTo(const UseRoi& useRoi, std::ostream* os);
    275 
    276     #define WHOLE_SUBMAT testing::Values(UseRoi(false), UseRoi(true))
    277 
    278     // Direct/Inverse
    279 
    280     class Inverse
    281     {
    282     public:
    283         inline Inverse(bool val = false) : val_(val) {}
    284 
    285         inline operator bool() const { return val_; }
    286 
    287     private:
    288         bool val_;
    289     };
    290 
    291     CV_EXPORTS void PrintTo(const Inverse& useRoi, std::ostream* os);
    292 
    293     #define DIRECT_INVERSE testing::Values(Inverse(false), Inverse(true))
    294 
    295     // Param class
    296 
    297     #define IMPLEMENT_PARAM_CLASS(name, type) \
    298         class name \
    299         { \
    300         public: \
    301             name ( type arg = type ()) : val_(arg) {} \
    302             operator type () const {return val_;} \
    303         private: \
    304             type val_; \
    305         }; \
    306         inline void PrintTo( name param, std::ostream* os) \
    307         { \
    308             *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
    309         }
    310 
    311     IMPLEMENT_PARAM_CLASS(Channels, int)
    312 
    313     #define ALL_CHANNELS testing::Values(Channels(1), Channels(2), Channels(3), Channels(4))
    314     #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))
    315 
    316     // Flags and enums
    317 
    318     CV_ENUM(NormCode, NORM_INF, NORM_L1, NORM_L2, NORM_TYPE_MASK, NORM_RELATIVE, NORM_MINMAX)
    319 
    320     CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA)
    321 
    322     CV_ENUM(BorderType, BORDER_REFLECT101, BORDER_REPLICATE, BORDER_CONSTANT, BORDER_REFLECT, BORDER_WRAP)
    323     #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))
    324 
    325     CV_FLAGS(WarpFlags, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, WARP_INVERSE_MAP)
    326 
    327     //////////////////////////////////////////////////////////////////////
    328     // Features2D
    329 
    330     CV_EXPORTS testing::AssertionResult assertKeyPointsEquals(const char* gold_expr, const char* actual_expr, std::vector<cv::KeyPoint>& gold, std::vector<cv::KeyPoint>& actual);
    331 
    332     #define ASSERT_KEYPOINTS_EQ(gold, actual) EXPECT_PRED_FORMAT2(assertKeyPointsEquals, gold, actual)
    333 
    334     CV_EXPORTS int getMatchedPointsCount(std::vector<cv::KeyPoint>& gold, std::vector<cv::KeyPoint>& actual);
    335     CV_EXPORTS int getMatchedPointsCount(const std::vector<cv::KeyPoint>& keypoints1, const std::vector<cv::KeyPoint>& keypoints2, const std::vector<cv::DMatch>& matches);
    336 
    337     //////////////////////////////////////////////////////////////////////
    338     // Other
    339 
    340     CV_EXPORTS void dumpImage(const std::string& fileName, const cv::Mat& image);
    341     CV_EXPORTS void showDiff(cv::InputArray gold, cv::InputArray actual, double eps);
    342 
    343     CV_EXPORTS void parseCudaDeviceOptions(int argc, char **argv);
    344     CV_EXPORTS void printCudaInfo();
    345 }
    346 
    347 namespace cv { namespace cuda
    348 {
    349     CV_EXPORTS void PrintTo(const DeviceInfo& info, std::ostream* os);
    350 }}
    351 
    352 #ifdef HAVE_CUDA
    353 
    354 #define CV_CUDA_TEST_MAIN(resourcesubdir) \
    355     CV_TEST_MAIN(resourcesubdir, cvtest::parseCudaDeviceOptions(argc, argv), cvtest::printCudaInfo(), cv::setUseOptimized(false))
    356 
    357 #else // HAVE_CUDA
    358 
    359 #define CV_CUDA_TEST_MAIN(resourcesubdir) \
    360     int main() \
    361     { \
    362         printf("OpenCV was built without CUDA support\n"); \
    363         return 0; \
    364     }
    365 
    366 #endif // HAVE_CUDA
    367 
    368 
    369 #endif // __OPENCV_CUDA_TEST_UTILITY_HPP__
    370