From a9f5c19239a6b15d9ce9cd273cfb3146c53fd908 Mon Sep 17 00:00:00 2001 From: Orest Chura Date: Tue, 28 Jul 2020 14:20:36 +0300 Subject: [PATCH] Merge pull request #17871 from OrestChura:oc/typed_GArray_GMat * Added overload for `GArray` ProtoParam in `gtyped.hpp` * Tests+compile_args - added tests for GArray as an input and an output of GComputationT - added possibility to give the compile_args to GComputationT.apply() * Fix win errors --- modules/gapi/include/opencv2/gapi/gtyped.hpp | 21 ++++- modules/gapi/test/gapi_typed_tests.cpp | 120 ++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/gtyped.hpp b/modules/gapi/include/opencv2/gapi/gtyped.hpp index 1ce6201..6fe52a6 100644 --- a/modules/gapi/include/opencv2/gapi/gtyped.hpp +++ b/modules/gapi/include/opencv2/gapi/gtyped.hpp @@ -26,6 +26,7 @@ namespace detail template<> struct ProtoToParam { using type = cv::Mat; }; template<> struct ProtoToParam { using type = cv::Scalar; }; template struct ProtoToParam > { using type = std::vector; }; + template<> struct ProtoToParam> { using type = std::vector; }; template struct ProtoToParam > { using type = U; }; template using ProtoToParamT = typename ProtoToParam::type; @@ -133,11 +134,19 @@ public: } void apply(detail::ProtoToParamT... inArgs, + detail::ProtoToParamT &outArg, + GCompileArgs &&args) + { + m_comp.apply(cv::gin(inArgs...), cv::gout(outArg), std::move(args)); + } + + void apply(detail::ProtoToParamT... inArgs, detail::ProtoToParamT &outArg) { - m_comp.apply(cv::gin(inArgs...), cv::gout(outArg)); + apply(inArgs..., outArg, GCompileArgs()); } + GCompiledT compile(detail::ProtoToMetaT... inDescs) { GMetaArgs inMetas = { GMetaArg(inDescs)... }; @@ -206,11 +215,19 @@ public: } void apply(detail::ProtoToParamT... inArgs, + detail::ProtoToParamT&... outArgs, + GCompileArgs &&args) + { + m_comp.apply(cv::gin(inArgs...), cv::gout(outArgs...), std::move(args)); + } + + void apply(detail::ProtoToParamT... inArgs, detail::ProtoToParamT&... outArgs) { - m_comp.apply(cv::gin(inArgs...), cv::gout(outArgs...)); + apply(inArgs..., outArgs..., GCompileArgs()); } + GCompiledT compile(detail::ProtoToMetaT... inDescs) { GMetaArgs inMetas = { GMetaArg(inDescs)... }; diff --git a/modules/gapi/test/gapi_typed_tests.cpp b/modules/gapi/test/gapi_typed_tests.cpp index c811cef..97de39f 100644 --- a/modules/gapi/test/gapi_typed_tests.cpp +++ b/modules/gapi/test/gapi_typed_tests.cpp @@ -7,6 +7,45 @@ #include "test_precomp.hpp" +#include "common/gapi_tests_common.hpp" + +namespace custom +{ +G_TYPED_KERNEL(GKernelForGArrayGMatOut, (cv::GMat)>, + "custom.test.kernelForGArrayGMatOut") +{ + static cv::GArrayDesc outMeta(const cv::GMatDesc&) + { + return cv::empty_array_desc(); + } +}; + +GAPI_OCV_KERNEL(GCPUKernelForGArrayGMatOut, custom::GKernelForGArrayGMatOut) +{ + static void run(const cv::Mat &src, std::vector &out) + { + out[0] = src.clone(); + } +}; + +G_TYPED_KERNEL(GSizeOfVectorGMat, (cv::GArray)>, + "custom.test.sizeOfVectorGMat") +{ + static cv::GOpaqueDesc outMeta(const cv::GArrayDesc&) + { + return cv::empty_gopaque_desc(); + } +}; + +GAPI_OCV_KERNEL(GCPUSizeOfVectorGMat, custom::GSizeOfVectorGMat) +{ + static void run(const std::vector &src, size_t &out) + { + out = src.size(); + } +}; +} + namespace opencv_test { @@ -94,7 +133,6 @@ TEST(GAPI_Typed, BinaryOp) EXPECT_EQ(0, cvtest::norm(out_mat_cv, out_mat_typed2, NORM_INF)); } - TEST(GAPI_Typed, MultipleOuts) { // Initialization ////////////////////////////////////////////////////////// @@ -147,4 +185,84 @@ TEST(GAPI_Typed, MultipleOuts) EXPECT_EQ(0, cvtest::norm(out_mat_cv2, out_mat_comp2, NORM_INF)); } +TEST(GAPI_Typed, GArrayGMatOut) +{ + // Initialization ////////////////////////////////////////////////////////// + const cv::Size sz(32, 32); + cv::Mat in_mat(sz, CV_8UC3); + std::vector out_vec_mat_untyped(1), + out_vec_mat_typed1 (1), + out_vec_mat_typed2 (1), + out_vec_mat_cv (1); + cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255)); + + auto customKernel = cv::gapi::kernels(); + auto absExactCompare = AbsExact().to_compare_f(); + + // Untyped G-API /////////////////////////////////////////////////////////// + cv::GComputation cptU([]() + { + cv::GMat in; + cv::GArray out = custom::GKernelForGArrayGMatOut::on(in); + return cv::GComputation(cv::GIn(in), cv::GOut(out)); + }); + cptU.apply(cv::gin(in_mat), cv::gout(out_vec_mat_untyped), cv::compile_args(customKernel)); + + // Typed G-API ///////////////////////////////////////////////////////////// + cv::GComputationT (cv::GMat)> cptT(custom::GKernelForGArrayGMatOut::on); + auto cplT = cptT.compile(cv::descr_of(in_mat), cv::compile_args(customKernel)); + + cptT.apply(in_mat, out_vec_mat_typed1, cv::compile_args(customKernel)); + cplT(in_mat, out_vec_mat_typed2); + + // Plain OpenCV //////////////////////////////////////////////////////////// + out_vec_mat_cv[0] = in_mat.clone(); + + // Comparison ////////////////////////////////////////////////////////////// + EXPECT_TRUE(absExactCompare(out_vec_mat_cv[0], out_vec_mat_untyped[0])); + EXPECT_TRUE(absExactCompare(out_vec_mat_cv[0], out_vec_mat_typed1 [0])); + EXPECT_TRUE(absExactCompare(out_vec_mat_cv[0], out_vec_mat_typed2 [0])); +} + +TEST(GAPI_Typed, GArrayGMatIn) +{ + // Initialization ////////////////////////////////////////////////////////// + const cv::Size sz(32, 32); + size_t vectorSize = 5; + + cv::Mat in_mat (sz, CV_8UC3); + size_t out_size_t_untyped, out_size_t_typed1, out_size_t_typed2, out_size_t_cv; + + cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255)); + std::vector in_vec(vectorSize); + for (size_t i = 0; i < vectorSize; i++) + in_vec[i] = in_mat.clone(); + + auto customKernel = cv::gapi::kernels(); + + // Untyped G-API /////////////////////////////////////////////////////////// + cv::GComputation cptU([]() + { + cv::GArray in; + cv::GOpaque out = custom::GSizeOfVectorGMat::on(in); + return cv::GComputation(cv::GIn(in), cv::GOut(out)); + }); + cptU.apply(cv::gin(in_vec), cv::gout(out_size_t_untyped), cv::compile_args(customKernel)); + + // Typed G-API ///////////////////////////////////////////////////////////// + cv::GComputationT (cv::GArray)> cptT(custom::GSizeOfVectorGMat::on); + auto cplT = cptT.compile(cv::descr_of(in_vec), cv::compile_args(customKernel)); + + cptT.apply(in_vec, out_size_t_typed1, cv::compile_args(customKernel)); + cplT(in_vec, out_size_t_typed2); + + // Plain OpenCV //////////////////////////////////////////////////////////// + out_size_t_cv = in_vec.size(); + + // Comparison ////////////////////////////////////////////////////////////// + EXPECT_TRUE(out_size_t_cv == vectorSize); + EXPECT_TRUE(out_size_t_untyped == vectorSize); + EXPECT_TRUE(out_size_t_typed1 == vectorSize); + EXPECT_TRUE(out_size_t_typed2 == vectorSize); +} } // opencv_test -- 2.7.4