Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/test/common/gapi_compoundkernel_tests.cpp
16354 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2018 Intel Corporation
6
7
8
// FIXME: move out from Common
9
10
#include "test_precomp.hpp"
11
#include "opencv2/gapi/cpu/core.hpp"
12
13
#include <ade/util/algorithm.hpp>
14
15
namespace opencv_test
16
{
17
namespace
18
{
19
G_TYPED_KERNEL(GCompoundDoubleAddC, <GMat(GMat, GScalar)>, "org.opencv.test.compound_double_addC")
20
{
21
static GMatDesc outMeta(GMatDesc in, GScalarDesc) { return in; }
22
};
23
24
GAPI_COMPOUND_KERNEL(GCompoundDoubleAddCImpl, GCompoundDoubleAddC)
25
{
26
static GMat expand(cv::GMat in, cv::GScalar s)
27
{
28
return cv::gapi::addC(cv::gapi::addC(in, s), s);
29
}
30
};
31
32
G_TYPED_KERNEL(GCompoundAddC, <GMat(GMat, GScalar)>, "org.opencv.test.compound_addC")
33
{
34
static GMatDesc outMeta(GMatDesc in, GScalarDesc) { return in; }
35
};
36
37
GAPI_COMPOUND_KERNEL(GCompoundAddCImpl, GCompoundAddC)
38
{
39
static GMat expand(cv::GMat in, cv::GScalar s)
40
{
41
return cv::gapi::addC(in, s);
42
}
43
};
44
45
using GMat3 = std::tuple<GMat,GMat,GMat>;
46
using GMat2 = std::tuple<GMat,GMat>;
47
48
G_TYPED_KERNEL_M(GCompoundMergeWithSplit, <GMat3(GMat, GMat, GMat)>, "org.opencv.test.compound_merge_split")
49
{
50
static std::tuple<GMatDesc,GMatDesc,GMatDesc> outMeta(GMatDesc a, GMatDesc b, GMatDesc c)
51
{
52
return std::make_tuple(a, b, c);
53
}
54
};
55
56
GAPI_COMPOUND_KERNEL(GCompoundMergeWithSplitImpl, GCompoundMergeWithSplit)
57
{
58
static GMat3 expand(cv::GMat a, cv::GMat b, cv::GMat c)
59
{
60
return cv::gapi::split3(cv::gapi::merge3(a, b, c));
61
}
62
};
63
64
G_TYPED_KERNEL(GCompoundAddWithAddC, <GMat(GMat, GMat, GScalar)>, "org.opencv.test.compound_add_with_addc")
65
{
66
static GMatDesc outMeta(GMatDesc in, GMatDesc, GScalarDesc)
67
{
68
return in;
69
}
70
};
71
72
GAPI_COMPOUND_KERNEL(GCompoundAddWithAddCImpl, GCompoundAddWithAddC)
73
{
74
static GMat expand(cv::GMat in1, cv::GMat in2, cv::GScalar s)
75
{
76
return cv::gapi::addC(cv::gapi::add(in1, in2), s);
77
}
78
};
79
80
G_TYPED_KERNEL_M(GCompoundSplitWithAdd, <GMat2(GMat)>, "org.opencv.test.compound_split_with_add")
81
{
82
static std::tuple<GMatDesc, GMatDesc> outMeta(GMatDesc in)
83
{
84
const auto out_depth = in.depth;
85
const auto out_desc = in.withType(out_depth, 1);
86
return std::make_tuple(out_desc, out_desc);
87
}
88
};
89
90
GAPI_COMPOUND_KERNEL(GCompoundSplitWithAddImpl, GCompoundSplitWithAdd)
91
{
92
static GMat2 expand(cv::GMat in)
93
{
94
cv::GMat a, b, c;
95
std::tie(a, b, c) = cv::gapi::split3(in);
96
return std::make_tuple(cv::gapi::add(a, b), c);
97
}
98
};
99
100
G_TYPED_KERNEL_M(GCompoundParallelAddC, <GMat2(GMat, GScalar)>, "org.opencv.test.compound_parallel_addc")
101
{
102
static std::tuple<GMatDesc, GMatDesc> outMeta(GMatDesc in, GScalarDesc)
103
{
104
return std::make_tuple(in, in);
105
}
106
};
107
108
GAPI_COMPOUND_KERNEL(GCompoundParallelAddCImpl, GCompoundParallelAddC)
109
{
110
static GMat2 expand(cv::GMat in, cv::GScalar s)
111
{
112
return std::make_tuple(cv::gapi::addC(in, s), cv::gapi::addC(in, s));
113
}
114
};
115
116
GAPI_COMPOUND_KERNEL(GCompoundAddImpl, cv::gapi::core::GAdd)
117
{
118
static GMat expand(cv::GMat in1, cv::GMat in2, int)
119
{
120
return cv::gapi::sub(cv::gapi::sub(in1, in2), in2);
121
}
122
};
123
124
G_TYPED_KERNEL(GCompoundAddWithAddCWithDoubleAddC, <GMat(GMat, GMat, GScalar)>, "org.opencv.test.compound_add_with_addC_with_double_addC")
125
{
126
static GMatDesc outMeta(GMatDesc in, GMatDesc, GScalarDesc)
127
{
128
return in;
129
}
130
};
131
132
GAPI_COMPOUND_KERNEL(GCompoundAddWithAddCWithDoubleAddCImpl, GCompoundAddWithAddCWithDoubleAddC)
133
{
134
static GMat expand(cv::GMat in1, cv::GMat in2, cv::GScalar s)
135
{
136
return GCompoundDoubleAddC::on(GCompoundAddWithAddC::on(in1, in2, s), s);
137
}
138
};
139
140
using GDoubleArray = cv::GArray<double>;
141
G_TYPED_KERNEL(GNegateArray, <GDoubleArray(GDoubleArray)>, "org.opencv.test.negate_array")
142
{
143
static GArrayDesc outMeta(const GArrayDesc&) { return empty_array_desc(); }
144
};
145
146
GAPI_OCV_KERNEL(GNegateArrayImpl, GNegateArray)
147
{
148
static void run(const std::vector<double>& in, std::vector<double>& out)
149
{
150
ade::util::transform(in, std::back_inserter(out), std::negate<double>());
151
}
152
};
153
154
G_TYPED_KERNEL(GMaxInArray, <GScalar(GDoubleArray)>, "org.opencv.test.max_in_array")
155
{
156
static GScalarDesc outMeta(const GArrayDesc&) { return empty_scalar_desc(); }
157
};
158
159
GAPI_OCV_KERNEL(GMaxInArrayImpl, GMaxInArray)
160
{
161
static void run(const std::vector<double>& in, cv::Scalar& out)
162
{
163
out = *std::max_element(in.begin(), in.end());
164
}
165
};
166
167
G_TYPED_KERNEL(GCompoundMaxInArray, <GScalar(GDoubleArray)>, "org.opencv.test.compound_max_in_array")
168
{
169
static GScalarDesc outMeta(const GArrayDesc&) { return empty_scalar_desc(); }
170
};
171
172
GAPI_COMPOUND_KERNEL(GCompoundMaxInArrayImpl, GCompoundMaxInArray)
173
{
174
static GScalar expand(GDoubleArray in)
175
{
176
return GMaxInArray::on(in);
177
}
178
};
179
180
G_TYPED_KERNEL(GCompoundNegateArray, <GDoubleArray(GDoubleArray)>, "org.opencv.test.compound_negate_array")
181
{
182
static GArrayDesc outMeta(const GArrayDesc&) { return empty_array_desc(); }
183
};
184
185
GAPI_COMPOUND_KERNEL(GCompoundNegateArrayImpl, GCompoundNegateArray)
186
{
187
static GDoubleArray expand(GDoubleArray in)
188
{
189
return GNegateArray::on(in);
190
}
191
};
192
193
G_TYPED_KERNEL(SetDiagKernel, <GMat(GMat, GDoubleArray)>, "org.opencv.test.empty_kernel")
194
{
195
static GMatDesc outMeta(GMatDesc in, GArrayDesc) { return in; }
196
};
197
198
void setDiag(cv::Mat& in, const std::vector<double>& diag)
199
{
200
GAPI_Assert(in.rows == static_cast<int>(diag.size()));
201
GAPI_Assert(in.cols == static_cast<int>(diag.size()));
202
for (int i = 0; i < in.rows; ++i)
203
{
204
in.at<uchar>(i, i) = static_cast<uchar>(diag[i]);
205
}
206
}
207
208
GAPI_OCV_KERNEL(SetDiagKernelImpl, SetDiagKernel)
209
{
210
static void run(const cv::Mat& in, const std::vector<double>& v, cv::Mat& out)
211
{
212
in.copyTo(out);
213
setDiag(out, v);
214
}
215
};
216
217
G_TYPED_KERNEL(GCompoundGMatGArrayGMat, <GMat(GMat, GDoubleArray, GMat)>, "org.opencv.test.compound_gmat_garray_gmat")
218
{
219
static GMatDesc outMeta(GMatDesc in, GArrayDesc, GMatDesc) { return in; }
220
};
221
222
GAPI_COMPOUND_KERNEL(GCompoundGMatGArrayGMatImpl, GCompoundGMatGArrayGMat)
223
{
224
static GMat expand(GMat a, GDoubleArray b, GMat c)
225
{
226
return SetDiagKernel::on(cv::gapi::add(a, c), b);
227
}
228
};
229
230
} // namespace
231
232
// FIXME avoid cv::combine that use custom and default kernels together
233
TEST(GCompoundKernel, ReplaceDefaultKernel)
234
{
235
cv::GMat in1, in2;
236
auto out = cv::gapi::add(in1, in2);
237
const auto custom_pkg = cv::gapi::kernels<GCompoundAddImpl>();
238
const auto full_pkg = cv::gapi::combine(cv::gapi::core::cpu::kernels(), custom_pkg, cv::unite_policy::REPLACE);
239
cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
240
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
241
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
242
out_mat(3, 3, CV_8UC1),
243
ref_mat(3, 3, CV_8UC1);
244
245
comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat), cv::compile_args(full_pkg));
246
ref_mat = in_mat1 - in_mat2 - in_mat2;
247
248
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
249
}
250
251
TEST(GCompoundKernel, DoubleAddC)
252
{
253
cv::GMat in1, in2;
254
cv::GScalar s;
255
auto add_res = cv::gapi::add(in1, in2);
256
auto super = GCompoundDoubleAddC::on(add_res, s);
257
auto out = cv::gapi::addC(super, s);
258
259
const auto custom_pkg = cv::gapi::kernels<GCompoundDoubleAddCImpl>();
260
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
261
cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
262
263
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
264
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
265
out_mat(3, 3, CV_8UC1),
266
ref_mat(3, 3, CV_8UC1);
267
268
cv::Scalar scalar = 2;
269
270
comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
271
ref_mat = in_mat1 + in_mat2 + scalar + scalar + scalar;
272
273
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
274
}
275
276
TEST(GCompoundKernel, AddC)
277
{
278
cv::GMat in1, in2;
279
cv::GScalar s;
280
auto add_res = cv::gapi::add(in1, in2);
281
auto super = GCompoundAddC::on(add_res, s);
282
auto out = cv::gapi::addC(super, s);
283
284
const auto custom_pkg = cv::gapi::kernels<GCompoundAddCImpl>();
285
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
286
cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
287
288
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
289
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
290
out_mat(3, 3, CV_8UC1),
291
ref_mat(3, 3, CV_8UC1);
292
293
cv::Scalar scalar = 2;
294
295
comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
296
ref_mat = in_mat1 + in_mat2 + scalar + scalar;
297
298
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
299
}
300
301
TEST(GCompoundKernel, MergeWithSplit)
302
{
303
cv::GMat in, a1, b1, c1,
304
a2, b2, c2;
305
306
std::tie(a1, b1, c1) = cv::gapi::split3(in);
307
std::tie(a2, b2, c2) = GCompoundMergeWithSplit::on(a1, b1, c1);
308
auto out = cv::gapi::merge3(a2, b2, c2);
309
310
const auto custom_pkg = cv::gapi::kernels<GCompoundMergeWithSplitImpl>();
311
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
312
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
313
314
cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC3), out_mat, ref_mat;
315
comp.apply(cv::gin(in_mat), cv::gout(out_mat), cv::compile_args(full_pkg));
316
ref_mat = in_mat;
317
318
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
319
}
320
321
TEST(GCompoundKernel, AddWithAddC)
322
{
323
cv::GMat in1, in2;
324
cv::GScalar s;
325
auto out = GCompoundAddWithAddC::on(in1, in2, s);
326
327
const auto custom_pkg = cv::gapi::kernels<GCompoundAddWithAddCImpl>();
328
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
329
cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
330
331
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
332
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
333
out_mat(3, 3, CV_8UC1),
334
ref_mat(3, 3, CV_8UC1);
335
336
cv::Scalar scalar = 2;
337
338
comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
339
ref_mat = in_mat1 + in_mat2 + scalar;
340
341
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
342
}
343
344
TEST(GCompoundKernel, SplitWithAdd)
345
{
346
cv::GMat in, out1, out2;
347
std::tie(out1, out2) = GCompoundSplitWithAdd::on(in);
348
349
const auto custom_pkg = cv::gapi::kernels<GCompoundSplitWithAddImpl>();
350
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
351
cv::GComputation comp(cv::GIn(in), cv::GOut(out1, out2));
352
353
cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC3),
354
out_mat1(3, 3, CV_8UC1),
355
out_mat2(3, 3, CV_8UC1),
356
ref_mat1(3, 3, CV_8UC1),
357
ref_mat2(3, 3, CV_8UC1);
358
359
comp.apply(cv::gin(in_mat), cv::gout(out_mat1, out_mat2), cv::compile_args(full_pkg));
360
361
std::vector<cv::Mat> channels(3);
362
cv::split(in_mat, channels);
363
364
ref_mat1 = channels[0] + channels[1];
365
ref_mat2 = channels[2];
366
367
EXPECT_EQ(0, cv::countNonZero(out_mat1 != ref_mat1));
368
EXPECT_EQ(0, cv::countNonZero(out_mat2 != ref_mat2));
369
}
370
371
TEST(GCompoundKernel, ParallelAddC)
372
{
373
cv::GMat in1, out1, out2;
374
cv::GScalar in2;
375
std::tie(out1, out2) = GCompoundParallelAddC::on(in1, in2);
376
377
const auto custom_pkg = cv::gapi::kernels<GCompoundParallelAddCImpl>();
378
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
379
cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out1, out2));
380
381
cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1),
382
out_mat1(3, 3, CV_8UC1),
383
out_mat2(3, 3, CV_8UC1),
384
ref_mat1(3, 3, CV_8UC1),
385
ref_mat2(3, 3, CV_8UC1);
386
387
cv::Scalar scalar = 2;
388
389
comp.apply(cv::gin(in_mat, scalar), cv::gout(out_mat1, out_mat2), cv::compile_args(full_pkg));
390
391
ref_mat1 = in_mat + scalar;
392
ref_mat2 = in_mat + scalar;
393
394
EXPECT_EQ(0, cv::countNonZero(out_mat1 != ref_mat1));
395
EXPECT_EQ(0, cv::countNonZero(out_mat2 != ref_mat2));
396
}
397
398
TEST(GCompoundKernel, GCompundKernelAndDefaultUseOneData)
399
{
400
cv::GMat in1, in2;
401
cv::GScalar s;
402
auto out = cv::gapi::add(GCompoundAddWithAddC::on(in1, in2, s), cv::gapi::addC(in2, s));
403
404
const auto custom_pkg = cv::gapi::kernels<GCompoundAddWithAddCImpl>();
405
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
406
cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
407
408
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
409
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
410
out_mat(3, 3, CV_8UC1),
411
ref_mat(3, 3, CV_8UC1);
412
413
cv::Scalar scalar = 2;
414
415
comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
416
ref_mat = in_mat1 + in_mat2 + scalar + in_mat2 + scalar;
417
418
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
419
}
420
421
TEST(GCompoundKernel, CompoundExpandedToCompound)
422
{
423
cv::GMat in1, in2;
424
cv::GScalar s;
425
auto out = GCompoundAddWithAddCWithDoubleAddC::on(in1, in2, s);
426
427
const auto custom_pkg = cv::gapi::kernels<GCompoundAddWithAddCWithDoubleAddCImpl,
428
GCompoundAddWithAddCImpl,
429
GCompoundDoubleAddCImpl>();
430
431
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
432
cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
433
434
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
435
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
436
out_mat(3, 3, CV_8UC1),
437
ref_mat(3, 3, CV_8UC1);
438
439
cv::Scalar scalar = 2;
440
441
comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
442
ref_mat = in_mat1 + in_mat2 + scalar + scalar + scalar;
443
444
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
445
}
446
447
TEST(GCompoundKernel, MaxInArray)
448
{
449
GDoubleArray in;
450
auto out = GCompoundMaxInArray::on(in);
451
const auto custom_pkg = cv::gapi::kernels<GCompoundMaxInArrayImpl, GMaxInArrayImpl>();
452
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
453
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
454
std::vector<double> v = { 1, 5, -2, 3, 10, 2};
455
cv::Scalar out_scl;
456
cv::Scalar ref_scl(*std::max_element(v.begin(), v.end()));
457
458
comp.apply(cv::gin(v), cv::gout(out_scl), cv::compile_args(full_pkg));
459
460
EXPECT_EQ(out_scl, ref_scl);
461
}
462
463
TEST(GCompoundKernel, NegateArray)
464
{
465
GDoubleArray in;
466
GDoubleArray out = GCompoundNegateArray::on(in);
467
const auto custom_pkg = cv::gapi::kernels<GCompoundNegateArrayImpl, GNegateArrayImpl>();
468
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
469
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
470
std::vector<double> in_v = {1, 5, -2, -10, 3};
471
std::vector<double> out_v;
472
std::vector<double> ref_v;
473
ade::util::transform(in_v, std::back_inserter(ref_v), std::negate<double>());
474
475
comp.apply(cv::gin(in_v), cv::gout(out_v), cv::compile_args(full_pkg));
476
477
EXPECT_EQ(out_v, ref_v);
478
}
479
480
TEST(GCompoundKernel, RightGArrayHandle)
481
{
482
cv::GMat in[2];
483
GDoubleArray a;
484
cv::GMat out = GCompoundGMatGArrayGMat::on(in[0], a, in[1]);
485
const auto custom_pkg = cv::gapi::kernels<GCompoundGMatGArrayGMatImpl, SetDiagKernelImpl>();
486
const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels(), cv::unite_policy::KEEP);
487
cv::GComputation comp(cv::GIn(in[0], a, in[1]), cv::GOut(out));
488
std::vector<double> in_v(3, 1.0);
489
cv::Mat in_mat1 = cv::Mat::eye(cv::Size(3, 3), CV_8UC1),
490
in_mat2 = cv::Mat::eye(cv::Size(3, 3), CV_8UC1),
491
out_mat;
492
cv::Mat ref_mat= in_mat1 + in_mat2;
493
setDiag(ref_mat, in_v);
494
495
comp.apply(cv::gin(in_mat1, in_v, in_mat2), cv::gout(out_mat), cv::compile_args(full_pkg));
496
497
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
498
499
}
500
} // opencv_test
501
502