Skip to content

Commit 830d8d6

Browse files
author
Maxim Pashchenkov
authored
Merge pull request opencv#18196 from mpashchenkov:mp/garray-initialization
[G-API]: Add GArray initialization support * Added GArray initialization (CONST_VALUE, GScalar analog) and test for this * Whitespaces * And one more space * Trailing whitespace * Test name changed. Build with magic commands. * GArray works with rvalue initialization * Code cleanup * Ternary operator in the initialization list.
1 parent a07f064 commit 830d8d6

File tree

7 files changed

+78
-5
lines changed

7 files changed

+78
-5
lines changed

modules/gapi/include/opencv2/gapi/garray.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ namespace detail
8080

8181
protected:
8282
GArrayU(); // Default constructor
83+
GArrayU(const detail::VectorRef& vref); // Constant value constructor
8384
template<class> friend class cv::GArray; // (available to GArray<T> only)
8485

8586
void setConstructFcn(ConstructVec &&cv); // Store T-aware constructor
@@ -328,17 +329,21 @@ namespace detail
328329
template<typename T> class GArray
329330
{
330331
public:
332+
// Host type (or Flat type) - the type this GArray is actually
333+
// specified to.
334+
using HT = typename detail::flatten_g<typename std::decay<T>::type>::type;
335+
336+
explicit GArray(const std::vector<HT>& v) // Constant value constructor
337+
: m_ref(detail::GArrayU(detail::VectorRef(v))) { putDetails(); }
338+
explicit GArray(std::vector<HT>&& v) // Move-constructor
339+
: m_ref(detail::GArrayU(detail::VectorRef(std::move(v)))) { putDetails(); }
331340
GArray() { putDetails(); } // Empty constructor
332341
explicit GArray(detail::GArrayU &&ref) // GArrayU-based constructor
333342
: m_ref(ref) { putDetails(); } // (used by GCall, not for users)
334343

335344
detail::GArrayU strip() const { return m_ref; }
336345

337346
private:
338-
// Host type (or Flat type) - the type this GArray is actually
339-
// specified to.
340-
using HT = typename detail::flatten_g<typename std::decay<T>::type>::type;
341-
342347
static void VCTor(detail::VectorRef& vref) {
343348
vref.reset<HT>();
344349
vref.storeKind<HT>();

modules/gapi/src/api/garray.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ cv::detail::GArrayU::GArrayU(const GNode &n, std::size_t out)
2020
{
2121
}
2222

23+
cv::detail::GArrayU::GArrayU(const detail::VectorRef& vref)
24+
: m_priv(new GOrigin(GShape::GARRAY, cv::gimpl::ConstVal(vref)))
25+
{
26+
}
27+
2328
cv::GOrigin& cv::detail::GArrayU::priv()
2429
{
2530
return *m_priv;

modules/gapi/src/api/gorigin.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ cv::GOrigin::GOrigin(GShape s,
2121
}
2222

2323
cv::GOrigin::GOrigin(GShape s, cv::gimpl::ConstVal v)
24-
: shape(s), node(cv::GNode::Const()), value(v), port(INVALID_PORT), kind(cv::detail::OpaqueKind::CV_UNKNOWN)
24+
: shape(s), node(cv::GNode::Const()), value(v), port(INVALID_PORT),
25+
kind(util::holds_alternative<detail::VectorRef>(v)
26+
? util::get<detail::VectorRef>(v).getKind()
27+
: cv::detail::OpaqueKind::CV_UNKNOWN)
2528
{
2629
}
2730

modules/gapi/src/api/gproto.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ cv::GRunArg cv::value_of(const cv::GOrigin &origin)
7979
switch (origin.shape)
8080
{
8181
case GShape::GSCALAR: return GRunArg(util::get<cv::Scalar>(origin.value));
82+
case GShape::GARRAY: return GRunArg(util::get<cv::detail::VectorRef>(origin.value));
8283
default: util::throw_error(std::logic_error("Unsupported shape for constant"));
8384
}
8485
}

modules/gapi/src/compiler/gobjref.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace gimpl
2929
using ConstVal = util::variant
3030
< util::monostate
3131
, cv::Scalar
32+
, cv::detail::VectorRef
3233
>;
3334

3435
struct RcDesc

modules/gapi/src/executor/gexecutor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ void cv::gimpl::GExecutor::initResource(const ade::NodeHandle &orig_nh)
114114
break;
115115

116116
case GShape::GARRAY:
117+
if (d.storage == Data::Storage::CONST_VAL)
118+
{
119+
auto rc = RcDesc{d.rc, d.shape, d.ctor};
120+
magazine::bindInArg(m_res, rc, m_gm.metadata(orig_nh).get<ConstValue>().arg);
121+
}
122+
break;
117123
case GShape::GOPAQUE:
118124
// Constructed on Reset, do nothing here
119125
break;

modules/gapi/test/gapi_array_tests.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ G_TYPED_KERNEL(CountCorners, <GScalar(GPointArray)>, "test.array.in")
2828
{
2929
static GScalarDesc outMeta(const GArrayDesc &) { return empty_scalar_desc(); }
3030
};
31+
G_TYPED_KERNEL(PointIncrement, <GPointArray(GMat, GPointArray)>, "test.point_increment")
32+
{
33+
static GArrayDesc outMeta(const GMatDesc&, const GArrayDesc&) { return empty_array_desc(); }
34+
};
3135
} // namespace ThisTest
3236

3337
namespace
@@ -57,6 +61,15 @@ GAPI_OCV_KERNEL(OCVCountCorners, ThisTest::CountCorners)
5761
}
5862
};
5963

64+
GAPI_OCV_KERNEL(OCVPointIncrement, ThisTest::PointIncrement)
65+
{
66+
static void run(const cv::Mat&, const std::vector<cv::Point>& in, std::vector<cv::Point>& out)
67+
{
68+
for (const auto& el : in)
69+
out.emplace_back(el + Point(1,1));
70+
}
71+
};
72+
6073
cv::Mat cross(int w, int h)
6174
{
6275
cv::Mat mat = cv::Mat::eye(h, w, CV_8UC1)*255;
@@ -164,6 +177,45 @@ TEST(GArray, TestIntermediateOutput)
164177
EXPECT_EQ(10, out_count[0]);
165178
}
166179

180+
TEST(GArray, GArrayConstValInitialization)
181+
{
182+
std::vector<cv::Point> initial_vec {Point(0,0), Point(1,1), Point(2,2)};
183+
std::vector<cv::Point> ref_vec {Point(1,1), Point(2,2), Point(3,3)};
184+
std::vector<cv::Point> out_vec;
185+
cv::Mat in_mat;
186+
187+
cv::GComputationT<ThisTest::GPointArray(cv::GMat)> c([&](cv::GMat in)
188+
{
189+
// Initialization
190+
ThisTest::GPointArray test_garray(initial_vec);
191+
return ThisTest::PointIncrement::on(in, test_garray);
192+
});
193+
auto cc = c.compile(cv::descr_of(in_mat),
194+
cv::compile_args(cv::gapi::kernels<OCVPointIncrement>()));
195+
cc(in_mat, out_vec);
196+
197+
EXPECT_EQ(ref_vec, out_vec);
198+
}
199+
200+
TEST(GArray, GArrayRValInitialization)
201+
{
202+
std::vector<cv::Point> ref_vec {Point(1,1), Point(2,2), Point(3,3)};
203+
std::vector<cv::Point> out_vec;
204+
cv::Mat in_mat;
205+
206+
cv::GComputationT<ThisTest::GPointArray(cv::GMat)> c([&](cv::GMat in)
207+
{
208+
// Rvalue initialization
209+
ThisTest::GPointArray test_garray({Point(0,0), Point(1,1), Point(2,2)});
210+
return ThisTest::PointIncrement::on(in, test_garray);
211+
});
212+
auto cc = c.compile(cv::descr_of(in_mat),
213+
cv::compile_args(cv::gapi::kernels<OCVPointIncrement>()));
214+
cc(in_mat, out_vec);
215+
216+
EXPECT_EQ(ref_vec, out_vec);
217+
}
218+
167219
TEST(GArray_VectorRef, TestMov)
168220
{
169221
// Warning: this test is testing some not-very-public APIs

0 commit comments

Comments
 (0)