test_precomp.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <sstream>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include "opencv2/ts.hpp"
  10. #include "opencv2/ts/ocl_test.hpp"
  11. #include "opencv2/videoio.hpp"
  12. #include "opencv2/videoio/registry.hpp"
  13. #include "opencv2/core/private.hpp"
  14. #include "opencv2/core/utils/configuration.private.hpp"
  15. namespace cv {
  16. static inline
  17. std::ostream& operator<<(std::ostream& out, const VideoCaptureAPIs& api)
  18. {
  19. out << cv::videoio_registry::getBackendName(api); return out;
  20. }
  21. static inline
  22. std::ostream& operator<<(std::ostream& out, const VideoAccelerationType& va_type)
  23. {
  24. struct {
  25. VideoAccelerationType va_type;
  26. const char* str;
  27. } va_types[] = {
  28. {VIDEO_ACCELERATION_ANY, "ANY"},
  29. {VIDEO_ACCELERATION_NONE, "NONE"},
  30. {VIDEO_ACCELERATION_D3D11, "D3D11"},
  31. {VIDEO_ACCELERATION_VAAPI, "VAAPI"},
  32. {VIDEO_ACCELERATION_MFX, "MFX"},
  33. {VIDEO_ACCELERATION_DRM, "DRM"},
  34. };
  35. for (const auto& va : va_types) {
  36. if (va_type == va.va_type) {
  37. out << va.str;
  38. return out;
  39. }
  40. }
  41. out << cv::format("UNKNOWN(0x%ux)", static_cast<unsigned int>(va_type));
  42. return out;
  43. }
  44. static inline void PrintTo(const cv::VideoCaptureAPIs& api, std::ostream* os)
  45. {
  46. *os << cv::videoio_registry::getBackendName(api);
  47. }
  48. } // namespace
  49. inline std::string fourccToString(int fourcc)
  50. {
  51. return cv::format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
  52. }
  53. inline std::string fourccToStringSafe(int fourcc)
  54. {
  55. std::string res = fourccToString(fourcc);
  56. // TODO: return hex values for invalid characters
  57. std::transform(res.begin(), res.end(), res.begin(),
  58. [](char c) -> char { return (c >= '0' && c <= 'z') ? c : (c == ' ' ? '_' : 'x'); });
  59. return res;
  60. }
  61. inline int fourccFromString(const std::string &fourcc)
  62. {
  63. if (fourcc.size() != 4) return 0;
  64. return cv::VideoWriter::fourcc(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
  65. }
  66. inline std::string extToStringSafe(const std::string & ext)
  67. {
  68. std::string res;
  69. const bool start_with_dot = (ext.size() > 0) && (ext[0] == '.');
  70. std::transform(start_with_dot ? ext.begin() + 1 : ext.begin(), ext.end(), std::back_inserter(res),
  71. [](char c) -> char { return (c >= '0' && c <= 'z') ? c : ((c == ' ' || c == '.') ? '_' : 'x'); });
  72. return res;
  73. }
  74. inline std::string getExtensionSafe(const std::string & fname)
  75. {
  76. std::string fext(std::find(fname.begin(), fname.end(), '.'), fname.end());
  77. if (fext.size() == 0)
  78. return std::string("NOEXT");
  79. else
  80. return extToStringSafe(fext);
  81. }
  82. inline std::string getBackendNameSafe(const cv::VideoCaptureAPIs & api)
  83. {
  84. const std::string res = cv::videoio_registry::getBackendName(api);
  85. if (res.substr(0, 7) == "Unknown")
  86. {
  87. std::ostringstream os; os << "BACKEND_" << (size_t)api; return os.str();
  88. }
  89. else
  90. {
  91. return res;
  92. }
  93. }
  94. inline void generateFrame(int i, int FRAME_COUNT, cv::Mat & frame)
  95. {
  96. using namespace cv;
  97. using namespace std;
  98. int offset = (((i * 5) % FRAME_COUNT) - FRAME_COUNT / 2) * (frame.cols / 2) / FRAME_COUNT;
  99. frame(cv::Rect(0, 0, frame.cols / 2 + offset, frame.rows)) = Scalar(255, 255, 255);
  100. frame(cv::Rect(frame.cols / 2 + offset, 0, frame.cols - frame.cols / 2 - offset, frame.rows)) = Scalar(0, 0, 0);
  101. ostringstream buf; buf << "Frame " << setw(2) << setfill('0') << i + 1;
  102. int baseLine = 0;
  103. Size box = getTextSize(buf.str(), FONT_HERSHEY_COMPLEX, 2, 5, &baseLine);
  104. putText(frame, buf.str(), Point((frame.cols - box.width) / 2, (frame.rows - box.height) / 2 + baseLine),
  105. FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 5, LINE_AA);
  106. Point p(i * frame.cols / (FRAME_COUNT - 1), i * frame.rows / (FRAME_COUNT - 1));
  107. circle(frame, p, 50, Scalar(200, 25, 55), 8, LINE_AA);
  108. #if 0
  109. imshow("frame", frame);
  110. waitKey();
  111. #endif
  112. }
  113. class BunnyParameters
  114. {
  115. public:
  116. inline static int getWidth() { return 672; }
  117. inline static int getHeight() { return 384; }
  118. inline static int getFps() { return 24; }
  119. inline static double getTime() { return 5.21; }
  120. inline static int getCount() { return cvRound(getFps() * getTime()); }
  121. inline static std::string getFilename(const std::string &ext)
  122. {
  123. return cvtest::TS::ptr()->get_data_path() + "video/big_buck_bunny" + ext;
  124. }
  125. };
  126. static inline bool isBackendAvailable(cv::VideoCaptureAPIs api, const std::vector<cv::VideoCaptureAPIs>& api_list)
  127. {
  128. for (size_t i = 0; i < api_list.size(); i++)
  129. {
  130. if (api_list[i] == api)
  131. return true;
  132. }
  133. return false;
  134. }
  135. #endif