gapi_planar_test.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. //
  5. // Copyright (C) 2019 Intel Corporation
  6. #include "test_precomp.hpp"
  7. #include <opencv2/gapi/cpu/gcpukernel.hpp>
  8. namespace opencv_test
  9. {
  10. G_TYPED_KERNEL(GResize3c3p, <GMatP(GMat,Size,int)>, "test.resize3c3p") {
  11. static GMatDesc outMeta(GMatDesc in, Size sz, int) {
  12. GAPI_Assert(in.depth == CV_8U);
  13. GAPI_Assert(in.chan == 3);
  14. GAPI_Assert(in.planar == false);
  15. return in.withSize(sz).asPlanar();
  16. }
  17. };
  18. G_TYPED_KERNEL(GResize3p3p, <GMatP(GMatP,Size,int)>, "test.resize3p3p") {
  19. static GMatDesc outMeta(GMatDesc in, Size sz, int) {
  20. GAPI_Assert(in.depth == CV_8U);
  21. GAPI_Assert(in.chan == 3);
  22. GAPI_Assert(in.planar);
  23. return in.withSize(sz);
  24. }
  25. };
  26. static GMatDesc NV12toRGBoutMeta(GMatDesc inY, GMatDesc inUV)
  27. {
  28. GAPI_Assert(inY.depth == CV_8U);
  29. GAPI_Assert(inUV.depth == CV_8U);
  30. GAPI_Assert(inY.chan == 1);
  31. GAPI_Assert(inY.planar == false);
  32. GAPI_Assert(inUV.chan == 2);
  33. GAPI_Assert(inUV.planar == false);
  34. GAPI_Assert(inY.size.width == 2 * inUV.size.width);
  35. GAPI_Assert(inY.size.height == 2 * inUV.size.height);
  36. return inY.withType(CV_8U, 3);
  37. }
  38. G_TYPED_KERNEL(GNV12toRGBp, <GMatP(GMat,GMat)>, "test.nv12torgbp") {
  39. static GMatDesc outMeta(GMatDesc inY, GMatDesc inUV) {
  40. return NV12toRGBoutMeta(inY, inUV).asPlanar();
  41. }
  42. };
  43. static void toPlanar(const cv::Mat& in, cv::Mat& out)
  44. {
  45. GAPI_Assert(out.depth() == in.depth());
  46. GAPI_Assert(out.channels() == 1);
  47. GAPI_Assert(in.channels() == 3);
  48. GAPI_Assert(out.cols == in.cols);
  49. GAPI_Assert(out.rows == 3*in.rows);
  50. std::vector<cv::Mat> outs(3);
  51. for (int i = 0; i < 3; i++) {
  52. outs[i] = out(cv::Rect(0, i*in.rows, in.cols, in.rows));
  53. }
  54. cv::split(in, outs);
  55. }
  56. GAPI_OCV_KERNEL(OCVResize3c3p, GResize3c3p)
  57. {
  58. static void run(const cv::Mat& in, cv::Size out_sz, int interp, cv::Mat& out)
  59. {
  60. cv::Mat resized_mat;
  61. cv::resize(in, resized_mat, out_sz, 0, 0, interp);
  62. std::vector<cv::Mat> outs(3);
  63. for (int i = 0; i < 3; i++) {
  64. outs[i] = out(cv::Rect(0, i*out_sz.height, out_sz.width, out_sz.height));
  65. }
  66. cv::split(resized_mat, outs);
  67. }
  68. };
  69. GAPI_OCV_KERNEL(OCVResize3p3p, GResize3p3p)
  70. {
  71. static void run(const cv::Mat& in, cv::Size out_sz, int interp, cv::Mat& out)
  72. {
  73. std::vector<cv::Mat> ins(3);
  74. std::vector<cv::Mat> outs(3);
  75. int inH = in.rows / 3;
  76. int inW = in.cols;
  77. int outH = out.rows / 3;
  78. int outW = out.cols;
  79. for (int i = 0; i < 3; i++) {
  80. ins [i] = in(cv::Rect(0, i*inH, inW, inH));
  81. outs[i] = out(cv::Rect(0, i*outH, outW, outH));
  82. cv::resize(ins[i], outs[i], out_sz, 0, 0, interp);
  83. }
  84. }
  85. };
  86. GAPI_OCV_KERNEL(OCVNV12toRGBp, GNV12toRGBp)
  87. {
  88. static void run(const cv::Mat& inY, const cv::Mat& inUV, cv::Mat& out)
  89. {
  90. cv::Mat rgb;
  91. cv::cvtColorTwoPlane(inY, inUV, rgb, cv::COLOR_YUV2RGB_NV12);
  92. toPlanar(rgb, out);
  93. }
  94. };
  95. struct PlanarTest : public TestWithParam <std::pair<cv::Size, cv::Size>> {};
  96. TEST_P(PlanarTest, Resize3c3p)
  97. {
  98. cv::Size in_sz, out_sz;
  99. std::tie(in_sz, out_sz) = GetParam();
  100. int interp = cv::INTER_NEAREST;
  101. cv::Mat in_mat = cv::Mat(in_sz, CV_8UC3);
  102. cv::randn(in_mat, cv::Scalar::all(127.0f), cv::Scalar::all(40.f));
  103. cv::Mat out_mat = cv::Mat::zeros(out_sz.height*3, out_sz.width, CV_8UC1);
  104. cv::Mat out_mat_ocv = cv::Mat::zeros(out_sz.height*3, out_sz.width, CV_8UC1);
  105. cv::GMat in;
  106. auto out = GResize3c3p::on(in, out_sz, interp);
  107. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  108. auto pkg = cv::gapi::kernels<OCVResize3c3p>();
  109. c.apply(cv::gin(in_mat), cv::gout(out_mat), cv::compile_args(pkg));
  110. cv::Mat resized_mat;
  111. cv::resize(in_mat, resized_mat, out_sz, 0, 0, interp);
  112. toPlanar(resized_mat, out_mat_ocv);
  113. EXPECT_EQ(0, cvtest::norm(out_mat, out_mat_ocv, NORM_INF));
  114. }
  115. TEST_P(PlanarTest, Resize3p3p)
  116. {
  117. cv::Size in_sz, out_sz;
  118. std::tie(in_sz, out_sz) = GetParam();
  119. int interp = cv::INTER_NEAREST;
  120. cv::Mat in_mat = cv::Mat(cv::Size{in_sz.width, in_sz.height*3}, CV_8UC1);
  121. cv::randn(in_mat, cv::Scalar::all(127.0f), cv::Scalar::all(40.f));
  122. cv::Mat out_mat = cv::Mat::zeros(out_sz.height*3, out_sz.width, CV_8UC1);
  123. cv::Mat out_mat_ocv = cv::Mat::zeros(out_sz.height*3, out_sz.width, CV_8UC1);
  124. cv::GMatP in;
  125. auto out = GResize3p3p::on(in, out_sz, interp);
  126. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  127. auto pkg = cv::gapi::kernels<OCVResize3p3p>();
  128. c.compile(cv::descr_of(in_mat).asPlanar(3), cv::compile_args(pkg))
  129. (cv::gin(in_mat), cv::gout(out_mat));
  130. for (int i = 0; i < 3; i++) {
  131. const cv::Mat in_mat_roi = in_mat(cv::Rect(0, i*in_sz.height, in_sz.width, in_sz.height));
  132. cv::Mat out_mat_roi = out_mat_ocv(cv::Rect(0, i*out_sz.height, out_sz.width, out_sz.height));
  133. cv::resize(in_mat_roi, out_mat_roi, out_sz, 0, 0, interp);
  134. }
  135. EXPECT_EQ(0, cvtest::norm(out_mat, out_mat_ocv, NORM_INF));
  136. }
  137. TEST_P(PlanarTest, Pipeline)
  138. {
  139. cv::Size in_sz, out_sz;
  140. std::tie(in_sz, out_sz) = GetParam();
  141. int interp = cv::INTER_NEAREST;
  142. cv::Mat in_mat = cv::Mat(cv::Size{in_sz.width, in_sz.height*3/2}, CV_8UC1);
  143. cv::randn(in_mat, cv::Scalar::all(127.0f), cv::Scalar::all(40.f));
  144. cv::Size uv_sz(in_sz.width / 2, in_sz.height / 2);
  145. cv::Mat y_mat = cv::Mat(in_sz, CV_8UC1, in_mat.data);
  146. cv::Mat uv_mat = cv::Mat(uv_sz, CV_8UC2, in_mat.data + in_mat.step1() * in_sz.height);
  147. cv::Mat out_mat = cv::Mat::zeros(out_sz.height*3, out_sz.width, CV_8UC1);
  148. cv::Mat out_mat_ocv = cv::Mat::zeros(out_sz.height*3, out_sz.width, CV_8UC1);
  149. cv::GMat inY, inUV;
  150. auto out = GResize3p3p::on(GNV12toRGBp::on(inY, inUV), out_sz, interp);
  151. cv::GComputation c(cv::GIn(inY, inUV), cv::GOut(out));
  152. auto pkg = cv::gapi::kernels<OCVNV12toRGBp, OCVResize3p3p>();
  153. c.apply(cv::gin(y_mat, uv_mat), cv::gout(out_mat), cv::compile_args(pkg));
  154. cv::Mat rgb, resized_mat;
  155. cv::cvtColorTwoPlane(y_mat, uv_mat, rgb, cv::COLOR_YUV2RGB_NV12);
  156. cv::resize(rgb, resized_mat, out_sz, 0, 0, interp);
  157. toPlanar(resized_mat, out_mat_ocv);
  158. EXPECT_EQ(0, cvtest::norm(out_mat, out_mat_ocv, NORM_INF));
  159. }
  160. INSTANTIATE_TEST_CASE_P(Sanity, PlanarTest,
  161. Values(std::make_pair(cv::Size{8, 8}, cv::Size{4, 4})
  162. ,std::make_pair(cv::Size{960, 540}, cv::Size{224, 224})
  163. ,std::make_pair(cv::Size{64, 64}, cv::Size{224, 224})
  164. ));
  165. } // namespace opencv_test