Blame view

3rdparty/opencv-4.5.4/modules/gapi/test/gapi_frame_tests.cpp 5.7 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  //
  // Copyright (C) 2020 Intel Corporation
  
  #include "test_precomp.hpp"
  
  #include <opencv2/gapi/media.hpp>
  
  ////////////////////////////////////////////////////////////////////////////////
  // cv::GFrame tests
  
  namespace opencv_test {
  
  G_API_OP(GBlurFrame, <GMat(GFrame)>, "test.blur_frame") {
      static GMatDesc outMeta(GFrameDesc in) {
          return cv::GMatDesc(CV_8U,3,in.size);
      }
  };
  
  GAPI_OCV_KERNEL(OCVBlurFrame, GBlurFrame) {
      static void run(const cv::MediaFrame &in, cv::Mat& out) {
          GAPI_Assert(in.desc().fmt == cv::MediaFormat::BGR);
          cv::MediaFrame::View view = in.access(cv::MediaFrame::Access::R);
          cv::blur(cv::Mat(in.desc().size, CV_8UC3, view.ptr[0], view.stride[0]),
                   out,
                   cv::Size{3,3});
      }
  };
  
  ////////////////////////////////////////////////////////////////////////////////
  // cv::MediaFrame tests
  namespace {
  class TestMediaBGR final: public cv::MediaFrame::IAdapter {
      cv::Mat m_mat;
      using Cb = cv::MediaFrame::View::Callback;
      Cb m_cb;
  
  public:
      explicit TestMediaBGR(cv::Mat m, Cb cb = [](){})
          : m_mat(m), m_cb(cb) {
      }
      cv::GFrameDesc meta() const override {
          return cv::GFrameDesc{cv::MediaFormat::BGR, cv::Size(m_mat.cols, m_mat.rows)};
      }
      cv::MediaFrame::View access(cv::MediaFrame::Access) override {
          cv::MediaFrame::View::Ptrs pp = { m_mat.ptr(), nullptr, nullptr, nullptr };
          cv::MediaFrame::View::Strides ss = { m_mat.step, 0u, 0u, 0u };
          return cv::MediaFrame::View(std::move(pp), std::move(ss), Cb{m_cb});
      }
  };
  
  class TestMediaNV12 final: public cv::MediaFrame::IAdapter {
      cv::Mat m_y;
      cv::Mat m_uv;
  public:
      TestMediaNV12(cv::Mat y, cv::Mat uv) : m_y(y), m_uv(uv) {
      }
      cv::GFrameDesc meta() const override {
          return cv::GFrameDesc{cv::MediaFormat::NV12, cv::Size(m_y.cols, m_y.rows)};
      }
      cv::MediaFrame::View access(cv::MediaFrame::Access) override {
          cv::MediaFrame::View::Ptrs pp = {
              m_y.ptr(), m_uv.ptr(), nullptr, nullptr
          };
          cv::MediaFrame::View::Strides ss = {
              m_y.step, m_uv.step, 0u, 0u
          };
          return cv::MediaFrame::View(std::move(pp), std::move(ss));
      }
  };
  } // anonymous namespace
  
  struct MediaFrame_Test: public ::testing::Test {
      using M = cv::Mat;
      using MF = cv::MediaFrame;
      MF frame;
  };
  
  struct MediaFrame_BGR: public MediaFrame_Test {
      M bgr;
      MediaFrame_BGR()
          : bgr(M::eye(240, 320, CV_8UC3)) {
          cv::randn(bgr, cv::Scalar::all(127.0f), cv::Scalar::all(40.f));
          frame = MF::Create<TestMediaBGR>(bgr);
      }
  };
  
  TEST_F(MediaFrame_BGR, Meta) {
      auto meta = frame.desc();
      EXPECT_EQ(cv::MediaFormat::BGR, meta.fmt);
      EXPECT_EQ(cv::Size(320,240),    meta.size);
  }
  
  TEST_F(MediaFrame_BGR, Access) {
      cv::MediaFrame::View view1 = frame.access(cv::MediaFrame::Access::R);
      EXPECT_EQ(bgr.ptr(), view1.ptr[0]);
      EXPECT_EQ(bgr.step,  view1.stride[0]);
  
      cv::MediaFrame::View view2 = frame.access(cv::MediaFrame::Access::R);
      EXPECT_EQ(bgr.ptr(), view2.ptr[0]);
      EXPECT_EQ(bgr.step,  view2.stride[0]);
  }
  
  TEST_F(MediaFrame_BGR, Input) {
      // Run the OpenCV code
      cv::Mat out_mat_ocv, out_mat_gapi;
      cv::blur(bgr, out_mat_ocv, cv::Size{3,3});
  
      // Run the G-API code
      cv::GFrame in;
      cv::GMat out = GBlurFrame::on(in);
      cv::GComputation(cv::GIn(in), cv::GOut(out))
          .apply(cv::gin(frame),
                 cv::gout(out_mat_gapi),
                 cv::compile_args(cv::gapi::kernels<OCVBlurFrame>()));
  
      // Compare
      EXPECT_EQ(0, cvtest::norm(out_mat_ocv, out_mat_gapi, NORM_INF));
  }
  
  struct MediaFrame_NV12: public MediaFrame_Test {
      cv::Size sz;
      cv::Mat buf, y, uv;
      MediaFrame_NV12()
          : sz {320, 240}
          , buf(M::eye(sz.height*3/2, sz.width, CV_8UC1))
          , y  (buf.rowRange(0, sz.height))
          , uv (buf.rowRange(sz.height, sz.height*3/2)) {
          frame = MF::Create<TestMediaNV12>(y, uv);
      }
  };
  
  TEST_F(MediaFrame_NV12, Meta) {
      auto meta = frame.desc();
      EXPECT_EQ(cv::MediaFormat::NV12, meta.fmt);
      EXPECT_EQ(cv::Size(320,240),     meta.size);
  }
  
  TEST_F(MediaFrame_NV12, Access) {
      cv::MediaFrame::View view1 = frame.access(cv::MediaFrame::Access::R);
      EXPECT_EQ(y. ptr(), view1.ptr   [0]);
      EXPECT_EQ(y. step,  view1.stride[0]);
      EXPECT_EQ(uv.ptr(), view1.ptr   [1]);
      EXPECT_EQ(uv.step,  view1.stride[1]);
  
      cv::MediaFrame::View view2 = frame.access(cv::MediaFrame::Access::R);
      EXPECT_EQ(y. ptr(), view2.ptr   [0]);
      EXPECT_EQ(y. step,  view2.stride[0]);
      EXPECT_EQ(uv.ptr(), view2.ptr   [1]);
      EXPECT_EQ(uv.step,  view2.stride[1]);
  }
  
  TEST(MediaFrame, Callback) {
      int counter = 0;
      cv::Mat bgr = cv::Mat::eye(240, 320, CV_8UC3);
      cv::MediaFrame frame = cv::MediaFrame::Create<TestMediaBGR>(bgr, [&counter](){counter++;});
  
      // Test that the callback (in this case, incrementing the counter)
      // is called only on View destruction.
      EXPECT_EQ(0, counter);
      {
          cv::MediaFrame::View v1 = frame.access(cv::MediaFrame::Access::R);
          EXPECT_EQ(0, counter);
      }
      EXPECT_EQ(1, counter);
      {
          cv::MediaFrame::View v1 = frame.access(cv::MediaFrame::Access::R);
          EXPECT_EQ(1, counter);
          cv::MediaFrame::View v2 = frame.access(cv::MediaFrame::Access::W);
          EXPECT_EQ(1, counter);
      }
      EXPECT_EQ(3, counter);
  }
  
  TEST(MediaFrame, blobParams) {
      cv::Mat bgr = cv::Mat::eye(240, 320, CV_8UC3);
      cv::MediaFrame frame = cv::MediaFrame::Create<TestMediaBGR>(bgr);
  
      EXPECT_NO_THROW(frame.blobParams());
  }
  
  } // namespace opencv_test