test_gstreamer.cpp 6.5 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. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. typedef tuple< string, Size, Size, int > Param;
  7. typedef testing::TestWithParam< Param > videoio_gstreamer;
  8. TEST_P(videoio_gstreamer, read_check)
  9. {
  10. if (!videoio_registry::hasBackend(CAP_GSTREAMER))
  11. throw SkipTestException("GStreamer backend was not found");
  12. string format = get<0>(GetParam());
  13. Size frame_size = get<1>(GetParam());
  14. Size mat_size = get<2>(GetParam());
  15. int convertToRGB = get<3>(GetParam());
  16. int count_frames = 10;
  17. std::ostringstream pipeline;
  18. pipeline << "videotestsrc pattern=ball num-buffers=" << count_frames << " ! " << format;
  19. pipeline << ", width=" << frame_size.width << ", height=" << frame_size.height << " ! appsink";
  20. VideoCapture cap;
  21. ASSERT_NO_THROW(cap.open(pipeline.str(), CAP_GSTREAMER));
  22. ASSERT_TRUE(cap.isOpened());
  23. Mat buffer, decode_frame, gray_frame, rgb_frame;
  24. for (int i = 0; i < count_frames; ++i)
  25. {
  26. cap >> buffer;
  27. decode_frame = (format == "jpegenc ! image/jpeg") ? imdecode(buffer, IMREAD_UNCHANGED) : buffer;
  28. EXPECT_EQ(mat_size, decode_frame.size());
  29. cvtColor(decode_frame, rgb_frame, convertToRGB);
  30. cvtColor(rgb_frame, gray_frame, COLOR_RGB2GRAY);
  31. if (gray_frame.depth() == CV_16U)
  32. {
  33. gray_frame.convertTo(gray_frame, CV_8U, 255.0/65535);
  34. }
  35. vector<Vec3f> circles;
  36. HoughCircles(gray_frame, circles, HOUGH_GRADIENT, 1, gray_frame.rows/16, 100, 30, 1, 30 );
  37. if (circles.size() == 1)
  38. {
  39. EXPECT_NEAR(18.5, circles[0][2], 1.0);
  40. }
  41. else
  42. {
  43. ADD_FAILURE() << "Found " << circles.size() << " on frame " << i ;
  44. }
  45. }
  46. {
  47. Mat frame;
  48. cap >> frame;
  49. EXPECT_TRUE(frame.empty());
  50. }
  51. cap.release();
  52. ASSERT_FALSE(cap.isOpened());
  53. }
  54. static const Param test_data[] = {
  55. make_tuple("video/x-raw, format=BGR" , Size(640, 480), Size(640, 480), COLOR_BGR2RGB),
  56. make_tuple("video/x-raw, format=BGRA" , Size(640, 480), Size(640, 480), COLOR_BGRA2RGB),
  57. make_tuple("video/x-raw, format=RGBA" , Size(640, 480), Size(640, 480), COLOR_RGBA2RGB),
  58. make_tuple("video/x-raw, format=BGRx" , Size(640, 480), Size(640, 480), COLOR_BGRA2RGB),
  59. make_tuple("video/x-raw, format=RGBx" , Size(640, 480), Size(640, 480), COLOR_RGBA2RGB),
  60. make_tuple("video/x-raw, format=GRAY8", Size(640, 480), Size(640, 480), COLOR_GRAY2RGB),
  61. make_tuple("video/x-raw, format=UYVY" , Size(640, 480), Size(640, 480), COLOR_YUV2RGB_UYVY),
  62. make_tuple("video/x-raw, format=YUY2" , Size(640, 480), Size(640, 480), COLOR_YUV2RGB_YUY2),
  63. make_tuple("video/x-raw, format=YVYU" , Size(640, 480), Size(640, 480), COLOR_YUV2RGB_YVYU),
  64. make_tuple("video/x-raw, format=NV12" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_NV12),
  65. make_tuple("video/x-raw, format=NV21" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_NV21),
  66. make_tuple("video/x-raw, format=YV12" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_YV12),
  67. make_tuple("video/x-raw, format=I420" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_I420),
  68. make_tuple("video/x-bayer" , Size(640, 480), Size(640, 480), COLOR_BayerBG2RGB),
  69. make_tuple("jpegenc ! image/jpeg" , Size(640, 480), Size(640, 480), COLOR_BGR2RGB),
  70. // unaligned cases, strides information must be used
  71. make_tuple("video/x-raw, format=BGR" , Size(322, 242), Size(322, 242), COLOR_BGR2RGB),
  72. make_tuple("video/x-raw, format=GRAY8", Size(322, 242), Size(322, 242), COLOR_GRAY2RGB),
  73. make_tuple("video/x-raw, format=NV12" , Size(322, 242), Size(322, 363), COLOR_YUV2RGB_NV12),
  74. make_tuple("video/x-raw, format=NV21" , Size(322, 242), Size(322, 363), COLOR_YUV2RGB_NV21),
  75. make_tuple("video/x-raw, format=YV12" , Size(322, 242), Size(322, 363), COLOR_YUV2RGB_YV12),
  76. make_tuple("video/x-raw, format=I420" , Size(322, 242), Size(322, 363), COLOR_YUV2RGB_I420),
  77. // 16 bit
  78. make_tuple("video/x-raw, format=GRAY16_LE", Size(640, 480), Size(640, 480), COLOR_GRAY2RGB),
  79. make_tuple("video/x-raw, format=GRAY16_BE", Size(640, 480), Size(640, 480), COLOR_GRAY2RGB),
  80. };
  81. INSTANTIATE_TEST_CASE_P(videoio, videoio_gstreamer, testing::ValuesIn(test_data));
  82. TEST(videoio_gstreamer, unsupported_pipeline)
  83. {
  84. if (!videoio_registry::hasBackend(CAP_GSTREAMER))
  85. throw SkipTestException("GStreamer backend was not found");
  86. // could not link videoconvert0 to matroskamux0, matroskamux0 can't handle caps video/x-raw, format=(string)RGBA
  87. std::string pipeline = "appsrc ! videoconvert ! video/x-raw, format=(string)RGBA ! matroskamux ! filesink location=test.mkv";
  88. Size frame_size(640, 480);
  89. VideoWriter writer;
  90. EXPECT_NO_THROW(writer.open(pipeline, CAP_GSTREAMER, 0/*fourcc*/, 30/*fps*/, frame_size, true));
  91. EXPECT_FALSE(writer.isOpened());
  92. // no frames
  93. EXPECT_NO_THROW(writer.release());
  94. }
  95. TEST(videoio_gstreamer, gray16_writing)
  96. {
  97. if (!videoio_registry::hasBackend(CAP_GSTREAMER))
  98. throw SkipTestException("GStreamer backend was not found");
  99. Size frame_size(320, 240);
  100. // generate a noise frame
  101. Mat frame = Mat(frame_size, CV_16U);
  102. randu(frame, 0, 65535);
  103. // generate a temp filename, and fix path separators to how GStreamer expects them
  104. cv::String temp_file = cv::tempfile(".raw");
  105. std::replace(temp_file.begin(), temp_file.end(), '\\', '/');
  106. // write noise frame to file using GStreamer
  107. std::ostringstream writer_pipeline;
  108. writer_pipeline << "appsrc ! filesink location=" << temp_file;
  109. std::vector<int> params {
  110. VIDEOWRITER_PROP_IS_COLOR, 0/*false*/,
  111. VIDEOWRITER_PROP_DEPTH, CV_16U
  112. };
  113. VideoWriter writer;
  114. ASSERT_NO_THROW(writer.open(writer_pipeline.str(), CAP_GSTREAMER, 0/*fourcc*/, 30/*fps*/, frame_size, params));
  115. ASSERT_TRUE(writer.isOpened());
  116. ASSERT_NO_THROW(writer.write(frame));
  117. ASSERT_NO_THROW(writer.release());
  118. // read noise frame back in
  119. Mat written_frame(frame_size, CV_16U);
  120. std::ifstream fs(temp_file, std::ios::in | std::ios::binary);
  121. fs.read((char*)written_frame.ptr(0), frame_size.width * frame_size.height * 2);
  122. ASSERT_TRUE(fs);
  123. fs.close();
  124. // compare to make sure it's identical
  125. EXPECT_EQ(0, cv::norm(frame, written_frame, NORM_INF));
  126. // remove temp file
  127. EXPECT_EQ(0, remove(temp_file.c_str()));
  128. }
  129. }} // namespace