From: Orest Chura Date: Thu, 1 Apr 2021 20:39:31 +0000 (+0300) Subject: Merge pull request #19828 from OrestChura:oc/fix_garray_garray_input X-Git-Tag: submit/tizen/20220120.021815~1^2~117 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=34a8a45a6a3339945c4aa38c441c3a8a25b73f2a;p=platform%2Fupstream%2Fopencv.git Merge pull request #19828 from OrestChura:oc/fix_garray_garray_input [G-API] Fix bug of GArray passing through a graph * Add test to check GArray passing through a graph (assertion failed) * G-API: Flatten GArray to std::vector when capturing VCtr - Also: Fix formatting in garray.hpp * Refactored test, added valuable check * Initialize size_t Co-authored-by: Dmitry Matveev --- diff --git a/modules/gapi/include/opencv2/gapi/garray.hpp b/modules/gapi/include/opencv2/gapi/garray.hpp index 36e61de2e1..32799bc07e 100644 --- a/modules/gapi/include/opencv2/gapi/garray.hpp +++ b/modules/gapi/include/opencv2/gapi/garray.hpp @@ -246,12 +246,18 @@ namespace detail public: VectorRef() = default; - template explicit VectorRef(const std::vector& vec) : - m_ref(new VectorRefT(vec)), m_kind(GOpaqueTraits::kind) {} - template explicit VectorRef(std::vector& vec) : - m_ref(new VectorRefT(vec)), m_kind(GOpaqueTraits::kind) {} - template explicit VectorRef(std::vector&& vec) : - m_ref(new VectorRefT(std::move(vec))), m_kind(GOpaqueTraits::kind) {} + template explicit VectorRef(const std::vector& vec) + : m_ref(new VectorRefT(vec)) + , m_kind(GOpaqueTraits::kind) + {} + template explicit VectorRef(std::vector& vec) + : m_ref(new VectorRefT(vec)) + , m_kind(GOpaqueTraits::kind) + {} + template explicit VectorRef(std::vector&& vec) + : m_ref(new VectorRefT(std::move(vec))) + , m_kind(GOpaqueTraits::kind) + {} cv::detail::OpaqueKind getKind() const { @@ -321,9 +327,10 @@ namespace detail # define FLATTEN_NS cv #endif template struct flatten_g; - template<> struct flatten_g { using type = FLATTEN_NS::Mat; }; - template<> struct flatten_g { using type = FLATTEN_NS::Scalar; }; - template struct flatten_g { using type = T; }; + template<> struct flatten_g { using type = FLATTEN_NS::Mat; }; + template<> struct flatten_g { using type = FLATTEN_NS::Scalar; }; + template struct flatten_g> { using type = std::vector; }; + template struct flatten_g { using type = T; }; #undef FLATTEN_NS // FIXME: the above mainly duplicates "ProtoToParam" thing from gtyped.hpp // but I decided not to include gtyped here - probably worth moving that stuff diff --git a/modules/gapi/test/gapi_array_tests.cpp b/modules/gapi/test/gapi_array_tests.cpp index 8bdc0854f0..1ae5261d99 100644 --- a/modules/gapi/test/gapi_array_tests.cpp +++ b/modules/gapi/test/gapi_array_tests.cpp @@ -32,6 +32,10 @@ G_TYPED_KERNEL(PointIncrement, , "test.point_inc { static GArrayDesc outMeta(const GMatDesc&, const GArrayDesc&) { return empty_array_desc(); } }; +G_TYPED_KERNEL(CountContours, (GArray)>, "test.array.array.in") +{ + static GOpaqueDesc outMeta(const GArrayDesc&) { return empty_gopaque_desc(); } +}; } // namespace ThisTest namespace @@ -70,6 +74,14 @@ GAPI_OCV_KERNEL(OCVPointIncrement, ThisTest::PointIncrement) } }; +GAPI_OCV_KERNEL(OCVCountContours, ThisTest::CountContours) +{ + static void run(const std::vector> &contours, size_t &out) + { + out = contours.size(); + } +}; + cv::Mat cross(int w, int h) { cv::Mat mat = cv::Mat::eye(h, w, CV_8UC1)*255; @@ -177,6 +189,24 @@ TEST(GArray, TestIntermediateOutput) EXPECT_EQ(10, out_count[0]); } +TEST(GArray, TestGArrayGArrayKernelInput) +{ + cv::GMat in; + auto contours = cv::gapi::findContours(in, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); + auto out = ThisTest::CountContours::on(contours); + cv::GComputation c(GIn(in), GOut(out)); + + // Create input - two filled rectangles + cv::Mat in_mat = cv::Mat::zeros(50, 50, CV_8UC1); + cv::rectangle(in_mat, cv::Point{5,5}, cv::Point{20,20}, 255, cv::FILLED); + cv::rectangle(in_mat, cv::Point{25,25}, cv::Point{40,40}, 255, cv::FILLED); + + size_t out_count = 0u; + c.apply(gin(in_mat), gout(out_count), cv::compile_args(cv::gapi::kernels())); + + EXPECT_EQ(2u, out_count) << "Two contours must be found"; +} + TEST(GArray, GArrayConstValInitialization) { std::vector initial_vec {Point(0,0), Point(1,1), Point(2,2)};