Skip to content

Commit 1192cbe

Browse files
authored
Merge pull request opencv#17163 from AsyaPronina:gcompound_kernel_gmatp_coop
* Fixed cooperation of Compound kernel and GMatP type * Added test for GCompound kernel + GMatP type cooperation
1 parent 458bd16 commit 1192cbe

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

modules/gapi/include/opencv2/gapi/gcompoundkernel.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ template<typename U> struct get_compound_in<cv::GOpaque<U>>
7575
}
7676
};
7777

78+
template<> struct get_compound_in<cv::GMatP>
79+
{
80+
static cv::GMatP get(GCompoundContext &ctx, int idx)
81+
{
82+
auto mat = cv::GMatP();
83+
ctx.m_args[idx] = GArg(mat);
84+
return mat;
85+
}
86+
};
87+
7888
template<typename, typename, typename>
7989
struct GCompoundCallHelper;
8090

modules/gapi/src/backends/common/gcompoundkernel.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ cv::detail::GCompoundContext::GCompoundContext(const cv::GArgs& in_args)
3232
{
3333
case GShape::GMAT : m_args[i] = GArg(GMat()); break;
3434
case GShape::GSCALAR: m_args[i] = GArg(GScalar()); break;
35-
case GShape::GARRAY :/* do nothing - as handled in a special way, see gcompoundkernel.hpp for details */; break;
35+
case GShape::GARRAY :
36+
case GShape::GOPAQUE:
37+
// do nothing - as handled in a special way, see gcompoundkernel.hpp for details
38+
// same applies to GMatP
39+
break;
3640
default: GAPI_Assert(false);
3741
}
3842
}

modules/gapi/test/common/gapi_compoundkernel_tests.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,73 @@ namespace
227227
}
228228
};
229229

230+
G_TYPED_KERNEL(GToInterleaved, <GMat(GMatP)>, "org.opencv.test.to_interleaved")
231+
{
232+
static GMatDesc outMeta(GMatDesc in)
233+
{
234+
GAPI_Assert(in.planar == true);
235+
GAPI_Assert(in.chan == 3);
236+
return in.asInterleaved();
237+
}
238+
};
239+
240+
G_TYPED_KERNEL(GToPlanar, <GMatP(GMat)>, "org.opencv.test.to_planar")
241+
{
242+
static GMatDesc outMeta(GMatDesc in)
243+
{
244+
GAPI_Assert(in.planar == false);
245+
GAPI_Assert(in.chan == 3);
246+
return in.asPlanar();
247+
}
248+
};
249+
250+
GAPI_OCV_KERNEL(GToInterleavedImpl, GToInterleaved)
251+
{
252+
static void run(const cv::Mat& in, cv::Mat& out)
253+
{
254+
constexpr int inPlanesCount = 3;
255+
int inPlaneHeight = in.rows / inPlanesCount;
256+
257+
std::vector<cv::Mat> inPlanes(inPlanesCount);
258+
for (int i = 0; i < inPlanesCount; ++i)
259+
{
260+
int startRow = i * inPlaneHeight;
261+
int endRow = startRow + inPlaneHeight;
262+
inPlanes[i] = in.rowRange(startRow, endRow);
263+
}
264+
265+
cv::merge(inPlanes, out);
266+
}
267+
};
268+
269+
GAPI_OCV_KERNEL(GToPlanarImpl, GToPlanar)
270+
{
271+
static void run(const cv::Mat& in, cv::Mat& out)
272+
{
273+
std::vector<cv::Mat> inPlanes;
274+
cv::split(in, inPlanes);
275+
cv::vconcat(inPlanes, out);
276+
}
277+
};
278+
279+
G_TYPED_KERNEL(GCompoundToInterleavedToPlanar, <GMatP(GMatP)>,
280+
"org.opencv.test.compound_to_interleaved_to_planar")
281+
{
282+
static GMatDesc outMeta(GMatDesc in)
283+
{
284+
GAPI_Assert(in.planar == true);
285+
GAPI_Assert(in.chan == 3);
286+
return in;
287+
}
288+
};
289+
290+
GAPI_COMPOUND_KERNEL(GCompoundToInterleavedToPlanarImpl, GCompoundToInterleavedToPlanar)
291+
{
292+
static GMatP expand(cv::GMatP in)
293+
{
294+
return GToPlanar::on(GToInterleaved::on(in));
295+
}
296+
};
230297
} // namespace
231298

232299
// FIXME avoid cv::combine that use custom and default kernels together
@@ -496,5 +563,30 @@ TEST(GCompoundKernel, RightGArrayHandle)
496563

497564
EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
498565

566+
}
567+
568+
TEST(GCompoundKernel, ToInterleavedToPlanar)
569+
{
570+
cv::GMatP in;
571+
cv::GMatP out = GCompoundToInterleavedToPlanar::on(in);
572+
const auto pkg = cv::gapi::kernels<GCompoundToInterleavedToPlanarImpl,
573+
GToInterleavedImpl,
574+
GToPlanarImpl>();
575+
576+
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
577+
578+
constexpr int numPlanes = 3;
579+
cv::Mat in_mat(cv::Size(15, 15), CV_8UC1),
580+
out_mat,
581+
ref_mat;
582+
583+
cv::randu(in_mat, 0, 255);
584+
ref_mat = in_mat;
585+
586+
comp.compile(cv::descr_of(in_mat).asPlanar(numPlanes), cv::compile_args(pkg))
587+
(cv::gin(in_mat), cv::gout(out_mat));
588+
589+
EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
590+
499591
}
500592
} // opencv_test

0 commit comments

Comments
 (0)