From 1192cbe4ab484e496d10f79292120bc6a6b7d08b Mon Sep 17 00:00:00 2001 From: "Anastasiya(Asya) Pronina" Date: Tue, 25 Aug 2020 16:51:43 +0300 Subject: [PATCH] Merge pull request #17163 from AsyaPronina:gcompound_kernel_gmatp_coop * Fixed cooperation of Compound kernel and GMatP type * Added test for GCompound kernel + GMatP type cooperation --- .../gapi/include/opencv2/gapi/gcompoundkernel.hpp | 10 +++ .../gapi/src/backends/common/gcompoundkernel.cpp | 6 +- .../gapi/test/common/gapi_compoundkernel_tests.cpp | 92 ++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/modules/gapi/include/opencv2/gapi/gcompoundkernel.hpp b/modules/gapi/include/opencv2/gapi/gcompoundkernel.hpp index 2f17064..df0ce34 100644 --- a/modules/gapi/include/opencv2/gapi/gcompoundkernel.hpp +++ b/modules/gapi/include/opencv2/gapi/gcompoundkernel.hpp @@ -75,6 +75,16 @@ template struct get_compound_in> } }; +template<> struct get_compound_in +{ + static cv::GMatP get(GCompoundContext &ctx, int idx) + { + auto mat = cv::GMatP(); + ctx.m_args[idx] = GArg(mat); + return mat; + } +}; + template struct GCompoundCallHelper; diff --git a/modules/gapi/src/backends/common/gcompoundkernel.cpp b/modules/gapi/src/backends/common/gcompoundkernel.cpp index 05ed51d..8983a3a 100644 --- a/modules/gapi/src/backends/common/gcompoundkernel.cpp +++ b/modules/gapi/src/backends/common/gcompoundkernel.cpp @@ -32,7 +32,11 @@ cv::detail::GCompoundContext::GCompoundContext(const cv::GArgs& in_args) { case GShape::GMAT : m_args[i] = GArg(GMat()); break; case GShape::GSCALAR: m_args[i] = GArg(GScalar()); break; - case GShape::GARRAY :/* do nothing - as handled in a special way, see gcompoundkernel.hpp for details */; break; + case GShape::GARRAY : + case GShape::GOPAQUE: + // do nothing - as handled in a special way, see gcompoundkernel.hpp for details + // same applies to GMatP + break; default: GAPI_Assert(false); } } diff --git a/modules/gapi/test/common/gapi_compoundkernel_tests.cpp b/modules/gapi/test/common/gapi_compoundkernel_tests.cpp index f1364e9..523ecb0 100644 --- a/modules/gapi/test/common/gapi_compoundkernel_tests.cpp +++ b/modules/gapi/test/common/gapi_compoundkernel_tests.cpp @@ -227,6 +227,73 @@ namespace } }; + G_TYPED_KERNEL(GToInterleaved, , "org.opencv.test.to_interleaved") + { + static GMatDesc outMeta(GMatDesc in) + { + GAPI_Assert(in.planar == true); + GAPI_Assert(in.chan == 3); + return in.asInterleaved(); + } + }; + + G_TYPED_KERNEL(GToPlanar, , "org.opencv.test.to_planar") + { + static GMatDesc outMeta(GMatDesc in) + { + GAPI_Assert(in.planar == false); + GAPI_Assert(in.chan == 3); + return in.asPlanar(); + } + }; + + GAPI_OCV_KERNEL(GToInterleavedImpl, GToInterleaved) + { + static void run(const cv::Mat& in, cv::Mat& out) + { + constexpr int inPlanesCount = 3; + int inPlaneHeight = in.rows / inPlanesCount; + + std::vector inPlanes(inPlanesCount); + for (int i = 0; i < inPlanesCount; ++i) + { + int startRow = i * inPlaneHeight; + int endRow = startRow + inPlaneHeight; + inPlanes[i] = in.rowRange(startRow, endRow); + } + + cv::merge(inPlanes, out); + } + }; + + GAPI_OCV_KERNEL(GToPlanarImpl, GToPlanar) + { + static void run(const cv::Mat& in, cv::Mat& out) + { + std::vector inPlanes; + cv::split(in, inPlanes); + cv::vconcat(inPlanes, out); + } + }; + + G_TYPED_KERNEL(GCompoundToInterleavedToPlanar, , + "org.opencv.test.compound_to_interleaved_to_planar") + { + static GMatDesc outMeta(GMatDesc in) + { + GAPI_Assert(in.planar == true); + GAPI_Assert(in.chan == 3); + return in; + } + }; + + GAPI_COMPOUND_KERNEL(GCompoundToInterleavedToPlanarImpl, GCompoundToInterleavedToPlanar) + { + static GMatP expand(cv::GMatP in) + { + return GToPlanar::on(GToInterleaved::on(in)); + } + }; } // namespace // FIXME avoid cv::combine that use custom and default kernels together @@ -497,4 +564,29 @@ TEST(GCompoundKernel, RightGArrayHandle) EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF)); } + +TEST(GCompoundKernel, ToInterleavedToPlanar) +{ + cv::GMatP in; + cv::GMatP out = GCompoundToInterleavedToPlanar::on(in); + const auto pkg = cv::gapi::kernels(); + + cv::GComputation comp(cv::GIn(in), cv::GOut(out)); + + constexpr int numPlanes = 3; + cv::Mat in_mat(cv::Size(15, 15), CV_8UC1), + out_mat, + ref_mat; + + cv::randu(in_mat, 0, 255); + ref_mat = in_mat; + + comp.compile(cv::descr_of(in_mat).asPlanar(numPlanes), cv::compile_args(pkg)) + (cv::gin(in_mat), cv::gout(out_mat)); + + EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF)); + +} } // opencv_test -- 2.7.4