Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/test/gapi_fluid_test.cpp
16337 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
#include "test_precomp.hpp"
9
10
#include "opencv2/gapi/core.hpp"
11
12
#include "opencv2/gapi/fluid/gfluidbuffer.hpp"
13
#include "opencv2/gapi/fluid/gfluidkernel.hpp"
14
15
// FIXME: move these tests with priv() to internal suite
16
#include "backends/fluid/gfluidbuffer_priv.hpp"
17
18
#include "gapi_fluid_test_kernels.hpp"
19
#include "logger.hpp"
20
21
namespace opencv_test
22
{
23
24
using namespace cv::gapi_test_kernels;
25
26
namespace
27
{
28
void WriteFunction(uint8_t* row, int nr, int w) {
29
for (int i = 0; i < w; i++)
30
row[i] = static_cast<uint8_t>(nr+i);
31
};
32
void ReadFunction1x1(const uint8_t* row, int w) {
33
for (int i = 0; i < w; i++)
34
std::cout << std::setw(4) << static_cast<int>(row[i]) << " ";
35
std::cout << "\n";
36
};
37
void ReadFunction3x3(const uint8_t* rows[3], int w) {
38
for (int i = 0; i < 3; i++) {
39
for (int j = -1; j < w+1; j++) {
40
std::cout << std::setw(4) << static_cast<int>(rows[i][j]) << " ";
41
}
42
std::cout << "\n";
43
}
44
std::cout << "\n";
45
};
46
}
47
48
TEST(FluidBuffer, InputTest)
49
{
50
const cv::Size buffer_size = {8,8};
51
cv::Mat in_mat = cv::Mat::eye(buffer_size, CV_8U);
52
53
cv::gapi::fluid::Buffer buffer(to_own(in_mat), true);
54
cv::gapi::fluid::View view = buffer.mkView(1, 0, {}, false);
55
view.priv().reset(1);
56
int this_y = 0;
57
58
while (this_y < buffer_size.height)
59
{
60
const uint8_t* rrow = view.InLine<uint8_t>(0);
61
ReadFunction1x1(rrow, buffer_size.width);
62
view.priv().readDone(1,1);
63
64
cv::Mat from_buffer(1, buffer_size.width, CV_8U, const_cast<uint8_t*>(rrow));
65
EXPECT_EQ(0, cv::countNonZero(in_mat.row(this_y) != from_buffer));
66
67
this_y++;
68
}
69
}
70
71
TEST(FluidBuffer, CircularTest)
72
{
73
const cv::Size buffer_size = {8,16};
74
75
cv::gapi::fluid::Buffer buffer(cv::GMatDesc{CV_8U,1,buffer_size}, 3, 1, 0, 1,
76
util::make_optional(cv::gapi::fluid::Border{cv::BORDER_CONSTANT, cv::gapi::own::Scalar(255)}));
77
cv::gapi::fluid::View view = buffer.mkView(3, 1, {}, false);
78
view.priv().reset(3);
79
buffer.debug(std::cout);
80
81
const auto whole_line_is = [](const uint8_t *line, int len, int value)
82
{
83
return std::all_of(line, line+len, [&](const uint8_t v){return v == value;});
84
};
85
86
// Store all read/written data in separate Mats to compare with
87
cv::Mat written_data(buffer_size, CV_8U);
88
89
// Simulate write/read process
90
int num_reads = 0, num_writes = 0;
91
while (num_reads < buffer_size.height)
92
{
93
if (num_writes < buffer_size.height)
94
{
95
uint8_t* wrow = buffer.OutLine<uint8_t>();
96
WriteFunction(wrow, num_writes, buffer_size.width);
97
buffer.priv().writeDone();
98
99
cv::Mat(1, buffer_size.width, CV_8U, wrow)
100
.copyTo(written_data.row(num_writes));
101
num_writes++;
102
}
103
buffer.debug(std::cout);
104
105
if (view.ready())
106
{
107
view.priv().prepareToRead();
108
const uint8_t* rrow[3] = {
109
view.InLine<uint8_t>(-1),
110
view.InLine<uint8_t>( 0),
111
view.InLine<uint8_t>( 1),
112
};
113
ReadFunction3x3(rrow, buffer_size.width);
114
view.priv().readDone(1,3);
115
buffer.debug(std::cout);
116
117
// Check borders right here
118
EXPECT_EQ(255u, rrow[0][-1]);
119
EXPECT_EQ(255u, rrow[0][buffer_size.width]);
120
if (num_reads == 0)
121
{
122
EXPECT_TRUE(whole_line_is(rrow[0]-1, buffer_size.width+2, 255u));
123
}
124
if (num_reads == buffer_size.height-1)
125
{
126
EXPECT_TRUE(whole_line_is(rrow[2]-1, buffer_size.width+2, 255u));
127
}
128
129
// Check window (without borders)
130
if (num_reads > 0 && num_reads < buffer_size.height-1)
131
{
132
// +1 everywhere since num_writes was just incremented above
133
cv::Mat written_lastLine2 = written_data.row(num_writes - (2+1));
134
cv::Mat written_lastLine1 = written_data.row(num_writes - (1+1));
135
cv::Mat written_lastLine0 = written_data.row(num_writes - (0+1));
136
137
cv::Mat read_prevLine(1, buffer_size.width, CV_8U, const_cast<uint8_t*>(rrow[0]));
138
cv::Mat read_thisLine(1, buffer_size.width, CV_8U, const_cast<uint8_t*>(rrow[1]));
139
cv::Mat read_nextLine(1, buffer_size.width, CV_8U, const_cast<uint8_t*>(rrow[2]));
140
141
EXPECT_EQ(0, cv::countNonZero(written_lastLine2 != read_prevLine));
142
EXPECT_EQ(0, cv::countNonZero(written_lastLine1 != read_thisLine));
143
EXPECT_EQ(0, cv::countNonZero(written_lastLine0 != read_nextLine));
144
}
145
num_reads++;
146
}
147
}
148
}
149
150
TEST(FluidBuffer, OutputTest)
151
{
152
const cv::Size buffer_size = {8,16};
153
cv::Mat out_mat = cv::Mat(buffer_size, CV_8U);
154
155
cv::gapi::fluid::Buffer buffer(to_own(out_mat), false);
156
int num_writes = 0;
157
while (num_writes < buffer_size.height)
158
{
159
uint8_t* wrow = buffer.OutLine<uint8_t>();
160
WriteFunction(wrow, num_writes, buffer_size.width);
161
buffer.priv().writeDone();
162
num_writes++;
163
}
164
165
GAPI_LOG_INFO(NULL, "\n" << out_mat);
166
167
// Validity check
168
for (int r = 0; r < buffer_size.height; r++)
169
{
170
for (int c = 0; c < buffer_size.width; c++)
171
{
172
EXPECT_EQ(r+c, out_mat.at<uint8_t>(r, c));
173
}
174
}
175
}
176
177
TEST(Fluid, AddC_WithScalar)
178
{
179
cv::GMat in;
180
cv::GScalar s;
181
182
cv::GComputation c(cv::GIn(in, s), cv::GOut(TAddScalar::on(in, s)));
183
cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1), out_mat(3, 3, CV_8UC1), ref_mat;
184
cv::Scalar in_s(100);
185
186
auto cc = c.compile(cv::descr_of(in_mat), cv::descr_of(in_s), cv::compile_args(fluidTestPackage));
187
188
cc(cv::gin(in_mat, in_s), cv::gout(out_mat));
189
ref_mat = in_mat + in_s;
190
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
191
}
192
193
TEST(Fluid, Scalar_In_Middle_Graph)
194
{
195
cv::GMat in;
196
cv::GScalar s;
197
198
cv::GComputation c(cv::GIn(in, s), cv::GOut(TAddScalar::on(TAddCSimple::on(in, 5), s)));
199
cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1), out_mat(3, 3, CV_8UC1), ref_mat;
200
cv::Scalar in_s(100);
201
202
auto cc = c.compile(cv::descr_of(in_mat), cv::descr_of(in_s), cv::compile_args(fluidTestPackage));
203
204
cc(cv::gin(in_mat, in_s), cv::gout(out_mat));
205
ref_mat = (in_mat + 5) + in_s;
206
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
207
}
208
209
TEST(Fluid, Add_Scalar_To_Mat)
210
{
211
cv::GMat in;
212
cv::GScalar s;
213
214
cv::GComputation c(cv::GIn(s, in), cv::GOut(TAddScalarToMat::on(s, in)));
215
cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1), out_mat(3, 3, CV_8UC1), ref_mat;
216
cv::Scalar in_s(100);
217
218
auto cc = c.compile(cv::descr_of(in_s), cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
219
220
cc(cv::gin(in_s, in_mat), cv::gout(out_mat));
221
ref_mat = in_mat + in_s;
222
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
223
}
224
225
TEST(Fluid, Sum_2_Mats_And_Scalar)
226
{
227
cv::GMat a, b;
228
cv::GScalar s;
229
230
cv::GComputation c(cv::GIn(a, s, b), cv::GOut(TSum2MatsAndScalar::on(a, s, b)));
231
cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
232
in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
233
out_mat(3, 3, CV_8UC1),
234
ref_mat;
235
cv::Scalar in_s(100);
236
237
auto cc = c.compile(cv::descr_of(in_mat1), cv::descr_of(in_s), cv::descr_of(in_mat2), cv::compile_args(fluidTestPackage));
238
239
cc(cv::gin(in_mat1, in_s, in_mat2), cv::gout(out_mat));
240
ref_mat = in_mat1 + in_mat2 + in_s;
241
EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
242
}
243
244
TEST(Fluid, Split3)
245
{
246
cv::GMat bgr;
247
cv::GMat r,g,b;
248
std::tie(b,g,r) = cv::gapi::split3(bgr);
249
auto rr = TAddSimple::on(r, TId::on(b));
250
auto rrr = TAddSimple::on(TId::on(rr), g);
251
cv::GComputation c(bgr, TId::on(rrr));
252
253
cv::Size sz(5120, 5120);
254
cv::Mat eye_1 = cv::Mat::eye(sz, CV_8UC1);
255
std::vector<cv::Mat> eyes = {eye_1, eye_1, eye_1};
256
cv::Mat in_mat;
257
cv::merge(eyes, in_mat);
258
cv::Mat out_mat(sz, CV_8UC1);
259
260
// G-API
261
auto cc = c.compile(cv::descr_of(in_mat),
262
cv::compile_args(fluidTestPackage));
263
cc(in_mat, out_mat);
264
265
// OCV
266
std::vector<cv::Mat> chans;
267
cv::split(in_mat, chans);
268
269
// Compare
270
EXPECT_EQ(0, cv::countNonZero(out_mat != (chans[2]*3)));
271
}
272
273
TEST(Fluid, ScratchTest)
274
{
275
cv::GMat in;
276
cv::GMat out = TPlusRow0::on(TPlusRow0::on(in));
277
cv::GComputation c(in, out);
278
279
cv::Size sz(8, 8);
280
cv::Mat in_mat = cv::Mat::eye(sz, CV_8UC1);
281
cv::Mat out_mat(sz, CV_8UC1);
282
283
// OpenCV (reference)
284
cv::Mat ref;
285
{
286
cv::Mat first_row = cv::Mat::zeros(1, sz.width, CV_8U);
287
cv::Mat remaining = cv::repeat(in_mat.row(0), sz.height-1, 1);
288
cv::Mat operand;
289
cv::vconcat(first_row, 2*remaining, operand);
290
ref = in_mat + operand;
291
}
292
GAPI_LOG_INFO(NULL, "\n" << ref);
293
294
// G-API
295
auto cc = c.compile(cv::descr_of(in_mat),
296
cv::compile_args(fluidTestPackage));
297
cc(in_mat, out_mat);
298
GAPI_LOG_INFO(NULL, "\n" << out_mat);
299
EXPECT_EQ(0, cv::countNonZero(ref != out_mat));
300
301
cc(in_mat, out_mat);
302
GAPI_LOG_INFO(NULL, "\n" << out_mat);
303
EXPECT_EQ(0, cv::countNonZero(ref != out_mat));
304
}
305
306
TEST(Fluid, MultipleOutRowsTest)
307
{
308
cv::GMat in;
309
cv::GMat out = TAddCSimple::on(TAddCSimple::on(in, 1), 2);
310
cv::GComputation c(in, out);
311
312
cv::Size sz(4, 4);
313
cv::Mat in_mat = cv::Mat::eye(sz, CV_8UC1);
314
cv::Mat out_mat(sz, CV_8UC1);
315
316
auto cc = c.compile(cv::descr_of(in_mat),
317
cv::compile_args(fluidTestPackage));
318
cc(in_mat, out_mat);
319
320
std::cout << out_mat << std::endl;
321
322
cv::Mat ocv_ref = in_mat + 1 + 2;
323
EXPECT_EQ(0, cv::countNonZero(ocv_ref != out_mat));
324
}
325
326
327
TEST(Fluid, LPIWindow)
328
{
329
cv::GMat in;
330
cv::GMat r,g,b;
331
std::tie(r,g,b) = cv::gapi::split3(in);
332
cv::GMat rr = TId7x7::on(r);
333
cv::GMat tmp = TAddSimple::on(rr, g);
334
cv::GMat out = TAddSimple::on(tmp, b);
335
336
cv::GComputation c(in, out);
337
338
cv::Size sz(8, 8);
339
340
cv::Mat eye_1 = cv::Mat::eye(sz, CV_8UC1);
341
std::vector<cv::Mat> eyes = {eye_1, eye_1, eye_1};
342
cv::Mat in_mat;
343
cv::merge(eyes, in_mat);
344
345
cv::Mat out_mat(sz, CV_8U);
346
auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
347
cc(in_mat, out_mat);
348
349
//std::cout << out_mat << std::endl;
350
351
// OpenCV reference
352
cv::Mat ocv_ref = eyes[0]+eyes[1]+eyes[2];
353
354
EXPECT_EQ(0, cv::countNonZero(ocv_ref != out_mat));
355
}
356
357
TEST(Fluid, MultipleReaders_SameLatency)
358
{
359
// in -> AddC -> a -> AddC -> b -> Add -> out
360
// '--> AddC -> c -'
361
//
362
// b and c have the same skew
363
364
cv::GMat in;
365
cv::GMat a = TAddCSimple::on(in, 1); // FIXME - align naming (G, non-G)
366
cv::GMat b = TAddCSimple::on(a, 2);
367
cv::GMat c = TAddCSimple::on(a, 3);
368
cv::GMat out = TAddSimple::on(b, c);
369
cv::GComputation comp(in, out);
370
371
const auto sz = cv::Size(32, 32);
372
cv::Mat in_mat = cv::Mat::eye(sz, CV_8UC1);
373
cv::Mat out_mat_gapi(sz, CV_8UC1);
374
cv::Mat out_mat_ocv (sz, CV_8UC1);
375
376
// Run G-API
377
auto cc = comp.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
378
cc(in_mat, out_mat_gapi);
379
380
// Check with OpenCV
381
cv::Mat tmp = in_mat + 1;
382
out_mat_ocv = (tmp+2) + (tmp+3);
383
EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
384
}
385
386
TEST(Fluid, MultipleReaders_DifferentLatency)
387
{
388
// in1 -> AddC -> a -> AddC -------------> b -> Add -> out
389
// '--------------> Add --> c -'
390
// '--> Id7x7-> d -'
391
//
392
// b and c have different skew (due to latency introduced by Id7x7)
393
// a is ready by multiple views with different latency.
394
395
cv::GMat in;
396
cv::GMat a = TAddCSimple::on(in, 1); // FIXME - align naming (G, non-G)
397
cv::GMat b = TAddCSimple::on(a, 2);
398
cv::GMat d = TId7x7::on(a);
399
cv::GMat c = TAddSimple::on(a, d);
400
cv::GMat out = TAddSimple::on(b, c);
401
cv::GComputation comp(in, out);
402
403
const auto sz = cv::Size(32, 32);
404
cv::Mat in_mat = cv::Mat::eye(sz, CV_8UC1);
405
cv::Mat out_mat_gapi(sz, CV_8UC1);
406
407
// Run G-API
408
auto cc = comp.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
409
cc(in_mat, out_mat_gapi);
410
411
// Check with OpenCV
412
cv::Mat ocv_a = in_mat + 1;
413
cv::Mat ocv_b = ocv_a + 2;
414
cv::Mat ocv_d = ocv_a;
415
cv::Mat ocv_c = ocv_a + ocv_d;
416
cv::Mat out_mat_ocv = ocv_b + ocv_c;
417
EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
418
}
419
420
TEST(Fluid, MultipleOutputs)
421
{
422
// in -> AddC -> a -> AddC ------------------> out1
423
// `--> Id7x7 --> b --> AddC -> out2
424
425
cv::GMat in;
426
cv::GMat a = TAddCSimple::on(in, 1);
427
cv::GMat b = TId7x7::on(a);
428
cv::GMat out1 = TAddCSimple::on(a, 2);
429
cv::GMat out2 = TAddCSimple::on(b, 7);
430
cv::GComputation comp(cv::GIn(in), cv::GOut(out1, out2));
431
432
const auto sz = cv::Size(32, 32);
433
cv::Mat in_mat = cv::Mat::eye(sz, CV_8UC1);
434
cv::Mat out_mat_gapi1(sz, CV_8UC1), out_mat_gapi2(sz, CV_8UC1);
435
cv::Mat out_mat_ocv1(sz, CV_8UC1), out_mat_ocv2(sz, CV_8UC1);
436
437
// Run G-API
438
auto cc = comp.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
439
cc(cv::gin(in_mat), cv::gout(out_mat_gapi1, out_mat_gapi2));
440
441
// Check with OpenCV
442
out_mat_ocv1 = in_mat + 1 + 2;
443
out_mat_ocv2 = in_mat + 1 + 7;
444
EXPECT_EQ(0, cv::countNonZero(out_mat_gapi1 != out_mat_ocv1));
445
EXPECT_EQ(0, cv::countNonZero(out_mat_gapi2 != out_mat_ocv2));
446
}
447
448
TEST(Fluid, EmptyOutputMatTest)
449
{
450
cv::GMat in;
451
cv::GMat out = TAddCSimple::on(in, 2);
452
cv::GComputation c(in, out);
453
454
cv::Mat in_mat = cv::Mat::eye(cv::Size(32, 24), CV_8UC1);
455
cv::Mat out_mat;
456
457
auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
458
459
cc(in_mat, out_mat);
460
EXPECT_EQ(CV_8UC1, out_mat.type());
461
EXPECT_EQ(32, out_mat.cols);
462
EXPECT_EQ(24, out_mat.rows);
463
EXPECT_TRUE(out_mat.ptr() != nullptr);
464
}
465
466
struct LPISequenceTest : public TestWithParam<int>{};
467
TEST_P(LPISequenceTest, LPISequenceTest)
468
{
469
// in -> AddC -> a -> Blur (2lpi) -> out
470
471
int kernelSize = GetParam();
472
cv::GMat in;
473
cv::GMat a = TAddCSimple::on(in, 1);
474
auto blur = kernelSize == 3 ? &TBlur3x3_2lpi::on : &TBlur5x5_2lpi::on;
475
cv::GMat out = blur(a, cv::BORDER_CONSTANT, cv::Scalar(0));
476
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
477
478
const auto sz = cv::Size(8, 10);
479
cv::Mat in_mat = cv::Mat::eye(sz, CV_8UC1);
480
cv::Mat out_mat_gapi(sz, CV_8UC1);
481
cv::Mat out_mat_ocv(sz, CV_8UC1);
482
483
// Run G-API
484
auto cc = comp.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage));
485
cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
486
487
// Check with OpenCV
488
cv::blur(in_mat + 1, out_mat_ocv, {kernelSize,kernelSize}, {-1,-1}, cv::BORDER_CONSTANT);
489
EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
490
}
491
492
INSTANTIATE_TEST_CASE_P(Fluid, LPISequenceTest,
493
Values(3, 5));
494
495
struct InputImageBorderTest : public TestWithParam <std::tuple<int, int>> {};
496
TEST_P(InputImageBorderTest, InputImageBorderTest)
497
{
498
cv::Size sz_in = { 320, 240 };
499
500
int ks = 0;
501
int borderType = 0;
502
std::tie(ks, borderType) = GetParam();
503
cv::Mat in_mat1(sz_in, CV_8UC1);
504
cv::Scalar mean = cv::Scalar(127.0f);
505
cv::Scalar stddev = cv::Scalar(40.f);
506
507
cv::randn(in_mat1, mean, stddev);
508
509
cv::Size kernelSize = {ks, ks};
510
cv::Point anchor = {-1, -1};
511
cv::Scalar borderValue(0);
512
513
auto gblur = ks == 3 ? &TBlur3x3::on : &TBlur5x5::on;
514
515
GMat in;
516
auto out = gblur(in, borderType, borderValue);
517
518
Mat out_mat_gapi = Mat::zeros(sz_in, CV_8UC1);
519
520
GComputation c(GIn(in), GOut(out));
521
auto cc = c.compile(descr_of(in_mat1), cv::compile_args(fluidTestPackage));
522
cc(gin(in_mat1), gout(out_mat_gapi));
523
524
cv::Mat out_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
525
cv::blur(in_mat1, out_mat_ocv, kernelSize, anchor, borderType);
526
527
EXPECT_EQ(0, countNonZero(out_mat_ocv != out_mat_gapi));
528
}
529
530
INSTANTIATE_TEST_CASE_P(Fluid, InputImageBorderTest,
531
Combine(Values(3, 5),
532
Values(BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT_101)));
533
534
struct SequenceOfBlursTest : public TestWithParam <std::tuple<int>> {};
535
TEST_P(SequenceOfBlursTest, Test)
536
{
537
cv::Size sz_in = { 320, 240 };
538
539
int borderType = 0;;
540
std::tie(borderType) = GetParam();
541
cv::Mat in_mat(sz_in, CV_8UC1);
542
cv::Scalar mean = cv::Scalar(127.0f);
543
cv::Scalar stddev = cv::Scalar(40.f);
544
545
cv::randn(in_mat, mean, stddev);
546
547
cv::Point anchor = {-1, -1};
548
cv::Scalar borderValue(0);
549
550
GMat in;
551
auto mid = TBlur3x3::on(in, borderType, borderValue);
552
auto out = TBlur5x5::on(mid, borderType, borderValue);
553
554
Mat out_mat_gapi = Mat::zeros(sz_in, CV_8UC1);
555
556
GComputation c(GIn(in), GOut(out));
557
auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage));
558
cc(gin(in_mat), gout(out_mat_gapi));
559
560
cv::Mat mid_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
561
cv::Mat out_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
562
cv::blur(in_mat, mid_mat_ocv, {3,3}, anchor, borderType);
563
cv::blur(mid_mat_ocv, out_mat_ocv, {5,5}, anchor, borderType);
564
565
EXPECT_EQ(0, countNonZero(out_mat_ocv != out_mat_gapi));
566
}
567
568
INSTANTIATE_TEST_CASE_P(Fluid, SequenceOfBlursTest,
569
Values(BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT_101));
570
571
struct TwoBlursTest : public TestWithParam <std::tuple<int, int, int, int, int, int, bool>> {};
572
TEST_P(TwoBlursTest, Test)
573
{
574
cv::Size sz_in = { 320, 240 };
575
576
int kernelSize1 = 0, kernelSize2 = 0;
577
int borderType1 = -1, borderType2 = -1;
578
cv::Scalar borderValue1{}, borderValue2{};
579
bool readFromInput = false;
580
std::tie(kernelSize1, borderType1, borderValue1, kernelSize2, borderType2, borderValue2, readFromInput) = GetParam();
581
cv::Mat in_mat(sz_in, CV_8UC1);
582
cv::Scalar mean = cv::Scalar(127.0f);
583
cv::Scalar stddev = cv::Scalar(40.f);
584
585
cv::randn(in_mat, mean, stddev);
586
587
cv::Point anchor = {-1, -1};
588
589
auto blur1 = kernelSize1 == 3 ? &TBlur3x3::on : TBlur5x5::on;
590
auto blur2 = kernelSize2 == 3 ? &TBlur3x3::on : TBlur5x5::on;
591
592
GMat in, out1, out2;
593
if (readFromInput)
594
{
595
out1 = blur1(in, borderType1, borderValue1);
596
out2 = blur2(in, borderType2, borderValue2);
597
}
598
else
599
{
600
auto mid = TAddCSimple::on(in, 0);
601
out1 = blur1(mid, borderType1, borderValue1);
602
out2 = blur2(mid, borderType2, borderValue2);
603
}
604
605
Mat out_mat_gapi1 = Mat::zeros(sz_in, CV_8UC1);
606
Mat out_mat_gapi2 = Mat::zeros(sz_in, CV_8UC1);
607
608
GComputation c(GIn(in), GOut(out1, out2));
609
auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage));
610
cc(gin(in_mat), gout(out_mat_gapi1, out_mat_gapi2));
611
612
cv::Mat out_mat_ocv1 = Mat::zeros(sz_in, CV_8UC1);
613
cv::Mat out_mat_ocv2 = Mat::zeros(sz_in, CV_8UC1);
614
cv::blur(in_mat, out_mat_ocv1, {kernelSize1, kernelSize1}, anchor, borderType1);
615
cv::blur(in_mat, out_mat_ocv2, {kernelSize2, kernelSize2}, anchor, borderType2);
616
617
EXPECT_EQ(0, countNonZero(out_mat_ocv1 != out_mat_gapi1));
618
EXPECT_EQ(0, countNonZero(out_mat_ocv2 != out_mat_gapi2));
619
}
620
621
INSTANTIATE_TEST_CASE_P(Fluid, TwoBlursTest,
622
Combine(Values(3, 5),
623
Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
624
Values(0),
625
Values(3, 5),
626
Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
627
Values(0),
628
testing::Bool())); // Read from input directly or place a copy node at start
629
630
struct TwoReadersTest : public TestWithParam <std::tuple<int, int, int, bool>> {};
631
TEST_P(TwoReadersTest, Test)
632
{
633
cv::Size sz_in = { 320, 240 };
634
635
int kernelSize = 0;
636
int borderType = -1;
637
cv::Scalar borderValue;
638
bool readFromInput = false;
639
std::tie(kernelSize, borderType, borderValue, readFromInput) = GetParam();
640
cv::Mat in_mat(sz_in, CV_8UC1);
641
cv::Scalar mean = cv::Scalar(127.0f);
642
cv::Scalar stddev = cv::Scalar(40.f);
643
644
cv::randn(in_mat, mean, stddev);
645
646
cv::Point anchor = {-1, -1};
647
648
auto blur = kernelSize == 3 ? &TBlur3x3::on : TBlur5x5::on;
649
650
GMat in, out1, out2;
651
if (readFromInput)
652
{
653
out1 = TAddCSimple::on(in, 0);
654
out2 = blur(in, borderType, borderValue);
655
}
656
else
657
{
658
auto mid = TAddCSimple::on(in, 0);
659
out1 = TAddCSimple::on(mid, 0);
660
out2 = blur(mid, borderType, borderValue);
661
}
662
663
Mat out_mat_gapi1 = Mat::zeros(sz_in, CV_8UC1);
664
Mat out_mat_gapi2 = Mat::zeros(sz_in, CV_8UC1);
665
666
GComputation c(GIn(in), GOut(out1, out2));
667
auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage));
668
cc(gin(in_mat), gout(out_mat_gapi1, out_mat_gapi2));
669
670
cv::Mat out_mat_ocv1 = Mat::zeros(sz_in, CV_8UC1);
671
cv::Mat out_mat_ocv2 = Mat::zeros(sz_in, CV_8UC1);
672
out_mat_ocv1 = in_mat;
673
cv::blur(in_mat, out_mat_ocv2, {kernelSize, kernelSize}, anchor, borderType);
674
675
EXPECT_EQ(0, countNonZero(out_mat_ocv1 != out_mat_gapi1));
676
EXPECT_EQ(0, countNonZero(out_mat_ocv2 != out_mat_gapi2));
677
}
678
679
INSTANTIATE_TEST_CASE_P(Fluid, TwoReadersTest,
680
Combine(Values(3, 5),
681
Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
682
Values(0),
683
testing::Bool())); // Read from input directly or place a copy node at start
684
685
TEST(FluidTwoIslands, SanityTest)
686
{
687
cv::Size sz_in{8,8};
688
689
GMat in1, in2;
690
auto out1 = TAddScalar::on(in1, {0});
691
auto out2 = TAddScalar::on(in2, {0});
692
693
cv::Mat in_mat1(sz_in, CV_8UC1);
694
cv::Mat in_mat2(sz_in, CV_8UC1);
695
cv::Scalar mean = cv::Scalar(127.0f);
696
cv::Scalar stddev = cv::Scalar(40.f);
697
698
cv::randn(in_mat1, mean, stddev);
699
cv::randn(in_mat2, mean, stddev);
700
701
Mat out_mat1 = Mat::zeros(sz_in, CV_8UC1);
702
Mat out_mat2 = Mat::zeros(sz_in, CV_8UC1);
703
704
GComputation c(GIn(in1, in2), GOut(out1, out2));
705
EXPECT_NO_THROW(c.apply(gin(in_mat1, in_mat2), gout(out_mat1, out_mat2), cv::compile_args(fluidTestPackage)));
706
EXPECT_EQ(0, countNonZero(in_mat1 != out_mat1));
707
EXPECT_EQ(0, countNonZero(in_mat2 != out_mat2));
708
}
709
710
} // namespace opencv_test
711
712