gapi_gcomputation_tests.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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) 2018 Intel Corporation
  6. #include "test_precomp.hpp"
  7. #include <opencv2/gapi/s11n.hpp>
  8. #include <opencv2/gapi/cpu/gcpukernel.hpp>
  9. #include <ade/util/zip_range.hpp>
  10. namespace opencv_test
  11. {
  12. namespace
  13. {
  14. G_TYPED_KERNEL(CustomResize, <cv::GMat(cv::GMat, cv::Size, double, double, int)>, "org.opencv.customk.resize")
  15. {
  16. static cv::GMatDesc outMeta(cv::GMatDesc in, cv::Size sz, double fx, double fy, int) {
  17. if (sz.width != 0 && sz.height != 0)
  18. {
  19. return in.withSize(sz);
  20. }
  21. else
  22. {
  23. GAPI_Assert(fx != 0. && fy != 0.);
  24. return in.withSize
  25. (cv::Size(static_cast<int>(std::round(in.size.width * fx)),
  26. static_cast<int>(std::round(in.size.height * fy))));
  27. }
  28. }
  29. };
  30. GAPI_OCV_KERNEL(CustomResizeImpl, CustomResize)
  31. {
  32. static void run(const cv::Mat& in, cv::Size sz, double fx, double fy, int interp, cv::Mat &out)
  33. {
  34. cv::resize(in, out, sz, fx, fy, interp);
  35. }
  36. };
  37. struct GComputationApplyTest: public ::testing::Test
  38. {
  39. cv::GMat in;
  40. cv::Mat in_mat;
  41. cv::Mat out_mat;
  42. cv::GComputation m_c;
  43. GComputationApplyTest() : in_mat(300, 300, CV_8UC1),
  44. m_c(cv::GIn(in), cv::GOut(CustomResize::on(in, cv::Size(100, 100),
  45. 0.0, 0.0, cv::INTER_LINEAR)))
  46. {
  47. }
  48. };
  49. struct GComputationVectorMatsAsOutput: public ::testing::Test
  50. {
  51. cv::Mat in_mat;
  52. cv::GComputation m_c;
  53. std::vector<cv::Mat> ref_mats;
  54. GComputationVectorMatsAsOutput() : in_mat(300, 300, CV_8UC3),
  55. m_c([&](){
  56. cv::GMat in;
  57. cv::GMat out[3];
  58. std::tie(out[0], out[1], out[2]) = cv::gapi::split3(in);
  59. return cv::GComputation({in}, {out[0], out[1], out[2]});
  60. })
  61. {
  62. cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255));
  63. cv::split(in_mat, ref_mats);
  64. }
  65. void run(std::vector<cv::Mat>& out_mats)
  66. {
  67. m_c.apply({in_mat}, out_mats);
  68. }
  69. void check(const std::vector<cv::Mat>& out_mats)
  70. {
  71. for (const auto it : ade::util::zip(ref_mats, out_mats))
  72. {
  73. const auto& ref_mat = std::get<0>(it);
  74. const auto& out_mat = std::get<1>(it);
  75. EXPECT_EQ(0, cvtest::norm(ref_mat, out_mat, NORM_INF));
  76. }
  77. }
  78. };
  79. struct GComputationPythonApplyTest: public ::testing::Test
  80. {
  81. cv::Size sz;
  82. MatType type;
  83. cv::Mat in_mat1, in_mat2, out_mat_ocv;
  84. cv::GComputation m_c;
  85. GComputationPythonApplyTest() : sz(cv::Size(300,300)), type(CV_8UC1),
  86. in_mat1(sz, type), in_mat2(sz, type), out_mat_ocv(sz, type),
  87. m_c([&](){
  88. cv::GMat in1, in2;
  89. cv::GMat out = in1 + in2;
  90. return cv::GComputation(cv::GIn(in1, in2), cv::GOut(out));
  91. })
  92. {
  93. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  94. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  95. out_mat_ocv = in_mat1 + in_mat2;
  96. }
  97. };
  98. }
  99. TEST_F(GComputationPythonApplyTest, WithoutSerialization)
  100. {
  101. auto output = m_c.apply(cv::detail::ExtractArgsCallback{[this](const cv::GTypesInfo& info)
  102. {
  103. GAPI_Assert(info[0].shape == cv::GShape::GMAT);
  104. GAPI_Assert(info[1].shape == cv::GShape::GMAT);
  105. return cv::GRunArgs{in_mat1, in_mat2};
  106. }
  107. });
  108. EXPECT_EQ(1u, output.size());
  109. const auto& out_mat_gapi = cv::util::get<cv::Mat>(output[0]);
  110. EXPECT_EQ(0, cvtest::norm(out_mat_ocv, out_mat_gapi, NORM_INF));
  111. }
  112. TEST_F(GComputationPythonApplyTest, WithSerialization)
  113. {
  114. auto p = cv::gapi::serialize(m_c);
  115. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  116. auto output = c.apply(cv::detail::ExtractArgsCallback{[this](const cv::GTypesInfo& info)
  117. {
  118. GAPI_Assert(info[0].shape == cv::GShape::GMAT);
  119. GAPI_Assert(info[1].shape == cv::GShape::GMAT);
  120. return cv::GRunArgs{in_mat1, in_mat2};
  121. }
  122. });
  123. EXPECT_EQ(1u, output.size());
  124. const auto& out_mat_gapi = cv::util::get<cv::Mat>(output[0]);
  125. EXPECT_EQ(0, cvtest::norm(out_mat_ocv, out_mat_gapi, NORM_INF));
  126. }
  127. TEST_F(GComputationApplyTest, ThrowDontPassCustomKernel)
  128. {
  129. EXPECT_THROW(m_c.apply(in_mat, out_mat), std::logic_error);
  130. }
  131. TEST_F(GComputationApplyTest, NoThrowPassCustomKernel)
  132. {
  133. const auto pkg = cv::gapi::kernels<CustomResizeImpl>();
  134. ASSERT_NO_THROW(m_c.apply(in_mat, out_mat, cv::compile_args(pkg)));
  135. }
  136. TEST_F(GComputationVectorMatsAsOutput, OutputAllocated)
  137. {
  138. std::vector<cv::Mat> out_mats(3);
  139. for (auto& out_mat : out_mats)
  140. {
  141. out_mat.create(in_mat.size(), CV_8UC1);
  142. }
  143. run(out_mats);
  144. check(out_mats);
  145. }
  146. TEST_F(GComputationVectorMatsAsOutput, OutputNotAllocated)
  147. {
  148. std::vector<cv::Mat> out_mats(3);
  149. run(out_mats);
  150. check(out_mats);
  151. }
  152. TEST_F(GComputationVectorMatsAsOutput, OutputAllocatedWithInvalidMeta)
  153. {
  154. std::vector<cv::Mat> out_mats(3);
  155. for (auto& out_mat : out_mats)
  156. {
  157. out_mat.create(in_mat.size() / 2, CV_8UC1);
  158. }
  159. run(out_mats);
  160. check(out_mats);
  161. }
  162. } // namespace opencv_test