test_precomp.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_TEST_PRECOMP_HPP
  5. #define OPENCV_TEST_PRECOMP_HPP
  6. #include <chrono>
  7. #include <opencv2/core.hpp>
  8. #include <opencv2/ts.hpp>
  9. #include <opencv2/ts/ocl_test.hpp> // OCL_ON, OCL_OFF
  10. #include <opencv2/imgcodecs.hpp>
  11. #include <opencv2/quality.hpp>
  12. #include <opencv2/quality/quality_utils.hpp>
  13. namespace opencv_test
  14. {
  15. namespace quality_test
  16. {
  17. const cv::String
  18. dataDir = "cv/optflow/"
  19. , testfile1a = dataDir + "rock_1.bmp"
  20. , testfile1b = dataDir + "rock_2.bmp"
  21. , testfile2a = dataDir + "RubberWhale1.png"
  22. , testfile2b = dataDir + "RubberWhale2.png"
  23. ;
  24. const cv::Scalar
  25. MSE_EXPECTED_1 = { 2136.0525 } // matlab: immse('rock_1.bmp', 'rock_2.bmp') == 2.136052552083333e+03
  26. , MSE_EXPECTED_2 = { 92.8235, 109.4104, 121.4 } // matlab: immse('rubberwhale1.png', 'rubberwhale2.png') == {92.8235, 109.4104, 121.4}
  27. ;
  28. inline cv::Mat get_testfile(const cv::String& path, int flags = IMREAD_UNCHANGED )
  29. {
  30. auto full_path = TS::ptr()->get_data_path() + path;
  31. auto result = cv::imread( full_path, flags );
  32. if (result.empty())
  33. CV_Error(cv::Error::StsObjectNotFound, "Cannot find file: " + full_path );
  34. return result;
  35. }
  36. inline cv::Mat get_testfile_1a() { return get_testfile(testfile1a, IMREAD_GRAYSCALE); }
  37. inline cv::Mat get_testfile_1b() { return get_testfile(testfile1b, IMREAD_GRAYSCALE); }
  38. inline cv::Mat get_testfile_2a() { return get_testfile(testfile2a); }
  39. inline cv::Mat get_testfile_2b() { return get_testfile(testfile2b); }
  40. const double QUALITY_ERR_TOLERANCE = .002 // allowed margin of error
  41. ;
  42. inline void quality_expect_near( const cv::Scalar& a, const cv::Scalar& b, double err_tolerance = QUALITY_ERR_TOLERANCE)
  43. {
  44. for (int i = 0; i < a.rows; ++i)
  45. {
  46. if (std::isinf(a(i)))
  47. EXPECT_EQ(a(i), b(i));
  48. else
  49. EXPECT_NEAR(a(i), b(i), err_tolerance);
  50. }
  51. }
  52. template <typename TMat>
  53. inline void check_quality_map( const TMat& mat, const bool expect_empty = false )
  54. {
  55. EXPECT_EQ( mat.empty(), expect_empty );
  56. if ( !expect_empty )
  57. {
  58. EXPECT_GT(mat.rows, 0);
  59. EXPECT_GT(mat.cols, 0);
  60. }
  61. }
  62. // execute quality test for a pair of images
  63. template <typename TMat>
  64. inline void quality_test(cv::Ptr<quality::QualityBase> ptr, const TMat& cmp, const Scalar& expected, const bool quality_map_expected = true, const bool empty_expected = false )
  65. {
  66. cv::Mat qMat = {};
  67. cv::UMat qUMat = {};
  68. // quality map should return empty in initial state
  69. ptr->getQualityMap(qMat);
  70. EXPECT_TRUE( qMat.empty() );
  71. // compute quality, check result
  72. quality_expect_near( expected, ptr->compute(cmp));
  73. if (empty_expected)
  74. EXPECT_TRUE(ptr->empty());
  75. else
  76. EXPECT_FALSE(ptr->empty());
  77. // getQualityMap to Mat, UMat
  78. ptr->getQualityMap(qMat);
  79. ptr->getQualityMap(qUMat);
  80. // check them
  81. check_quality_map(qMat, !quality_map_expected);
  82. check_quality_map(qUMat, !quality_map_expected);
  83. // reset algorithm, should now be empty
  84. ptr->clear();
  85. EXPECT_TRUE(ptr->empty());
  86. }
  87. /* A/B test benchmarking for development purposes */
  88. /*
  89. template <typename Fn>
  90. inline void quality_performance_test( const char* name, Fn&& op )
  91. {
  92. const auto exec_test = [&]()
  93. {
  94. const int NRUNS = 100;
  95. const auto start_t = std::chrono::high_resolution_clock::now();
  96. for (int i = 0; i < NRUNS; ++i)
  97. op();
  98. const auto end_t = std::chrono::high_resolution_clock::now();
  99. std::cout << name << " performance (OCL=" << cv::ocl::useOpenCL() << "): " << (double)(std::chrono::duration_cast<std::chrono::milliseconds>(end_t - start_t).count()) / (double)NRUNS << "ms\n";
  100. };
  101. // only run tests in NDEBUG mode
  102. #ifdef NDEBUG
  103. OCL_OFF(exec_test());
  104. OCL_ON(exec_test());
  105. #endif
  106. }
  107. */
  108. }
  109. }
  110. #endif