test_precomp.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "opencv2/ts.hpp"
  8. #include "opencv2/videoio.hpp"
  9. #include "opencv2/videoio/registry.hpp"
  10. #include "opencv2/imgproc/imgproc_c.h"
  11. #include "opencv2/core/private.hpp"
  12. namespace cv {
  13. static inline
  14. std::ostream& operator<<(std::ostream& out, const VideoCaptureAPIs& api)
  15. {
  16. out << cv::videoio_registry::getBackendName(api); return out;
  17. }
  18. static inline
  19. std::ostream& operator<<(std::ostream& out, const VideoAccelerationType& va_type)
  20. {
  21. struct {
  22. VideoAccelerationType va_type;
  23. const char* str;
  24. } va_types[] = {
  25. {VIDEO_ACCELERATION_ANY, "ANY"},
  26. {VIDEO_ACCELERATION_NONE, "NONE"},
  27. {VIDEO_ACCELERATION_D3D11, "D3D11"},
  28. {VIDEO_ACCELERATION_VAAPI, "VAAPI"},
  29. {VIDEO_ACCELERATION_MFX, "MFX"},
  30. };
  31. for (const auto& va : va_types) {
  32. if (va_type == va.va_type) {
  33. out << va.str;
  34. return out;
  35. }
  36. }
  37. out << cv::format("UNKNOWN(0x%ux)", static_cast<unsigned int>(va_type));
  38. return out;
  39. }
  40. static inline void PrintTo(const cv::VideoCaptureAPIs& api, std::ostream* os)
  41. {
  42. *os << cv::videoio_registry::getBackendName(api);
  43. }
  44. } // namespace
  45. inline std::string fourccToString(int fourcc)
  46. {
  47. return cv::format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
  48. }
  49. inline int fourccFromString(const std::string &fourcc)
  50. {
  51. if (fourcc.size() != 4) return 0;
  52. return cv::VideoWriter::fourcc(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
  53. }
  54. inline void generateFrame(int i, int FRAME_COUNT, cv::Mat & frame)
  55. {
  56. using namespace cv;
  57. using namespace std;
  58. int offset = (((i * 5) % FRAME_COUNT) - FRAME_COUNT / 2) * (frame.cols / 2) / FRAME_COUNT;
  59. frame(cv::Rect(0, 0, frame.cols / 2 + offset, frame.rows)) = Scalar(255, 255, 255);
  60. frame(cv::Rect(frame.cols / 2 + offset, 0, frame.cols - frame.cols / 2 - offset, frame.rows)) = Scalar(0, 0, 0);
  61. ostringstream buf; buf << "Frame " << setw(2) << setfill('0') << i + 1;
  62. int baseLine = 0;
  63. Size box = getTextSize(buf.str(), FONT_HERSHEY_COMPLEX, 2, 5, &baseLine);
  64. putText(frame, buf.str(), Point((frame.cols - box.width) / 2, (frame.rows - box.height) / 2 + baseLine),
  65. FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 5, LINE_AA);
  66. Point p(i * frame.cols / (FRAME_COUNT - 1), i * frame.rows / (FRAME_COUNT - 1));
  67. circle(frame, p, 50, Scalar(200, 25, 55), 8, LINE_AA);
  68. #if 0
  69. imshow("frame", frame);
  70. waitKey();
  71. #endif
  72. }
  73. class BunnyParameters
  74. {
  75. public:
  76. inline static int getWidth() { return 672; };
  77. inline static int getHeight() { return 384; };
  78. inline static int getFps() { return 24; };
  79. inline static double getTime() { return 5.21; };
  80. inline static int getCount() { return cvRound(getFps() * getTime()); };
  81. inline static std::string getFilename(const std::string &ext)
  82. {
  83. return cvtest::TS::ptr()->get_data_path() + "video/big_buck_bunny" + ext;
  84. }
  85. };
  86. static inline bool isBackendAvailable(cv::VideoCaptureAPIs api, const std::vector<cv::VideoCaptureAPIs>& api_list)
  87. {
  88. for (size_t i = 0; i < api_list.size(); i++)
  89. {
  90. if (api_list[i] == api)
  91. return true;
  92. }
  93. return false;
  94. }
  95. #endif