Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / test / common / gapi_core_tests_inl.hpp
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-2019 Intel Corporation
6
7
8 #ifndef OPENCV_GAPI_CORE_TESTS_INL_HPP
9 #define OPENCV_GAPI_CORE_TESTS_INL_HPP
10
11 #include "opencv2/gapi/core.hpp"
12 #include "gapi_core_tests.hpp"
13
14 namespace opencv_test
15 {
16
17 TEST_P(MathOpTest, MatricesAccuracyTest )
18 {
19     mathOp opType = ADD;
20     int type = 0, dtype = 0;
21     cv::Size sz;
22     double scale = 1; // mul, div
23     bool testWithScalar = false, initOutMatr = false, doReverseOp = false;
24     cv::GCompileArgs compile_args;
25     std::tie(opType, testWithScalar, type, scale, sz, dtype, initOutMatr, doReverseOp, compile_args) = GetParam();
26     initMatsRandU(type, sz, dtype, initOutMatr);
27
28     // G-API code & corresponding OpenCV code ////////////////////////////////
29     cv::GMat in1, in2, out;
30     if( testWithScalar )
31     {
32         cv::GScalar sc1;
33         switch(opType)
34         {
35         case (ADD):
36         {
37             out = cv::gapi::addC(in1, sc1, dtype);
38             cv::add(in_mat1, sc, out_mat_ocv, cv::noArray(), dtype);
39             break;
40         }
41         case (SUB):
42         {
43             if( doReverseOp )
44             {
45                 out = cv::gapi::subRC(sc1, in1, dtype);
46                 cv::subtract(sc, in_mat1, out_mat_ocv, cv::noArray(), dtype);
47             }
48             else
49             {
50                 out = cv::gapi::subC(in1, sc1, dtype);
51                 cv::subtract(in_mat1, sc, out_mat_ocv, cv::noArray(), dtype);
52             }
53             break;
54         }
55         case (DIV):
56         {
57             if( doReverseOp )
58             {
59                 in_mat1.setTo(1, in_mat1 == 0);  // avoid zeros in divide input data
60                 out = cv::gapi::divRC(sc1, in1, scale, dtype);
61                 cv::divide(sc, in_mat1, out_mat_ocv, scale, dtype);
62                 break;
63             }
64             else
65             {
66                 sc += Scalar(1, 1, 1, 1);  // avoid zeros in divide input data
67                 out = cv::gapi::divC(in1, sc1, scale, dtype);
68                 cv::divide(in_mat1, sc, out_mat_ocv, scale, dtype);
69                 break;
70             }
71         }
72         case (MUL):
73         {
74             // FIXME: add `scale` parameter to mulC
75             out = cv::gapi::mulC(in1, sc1, /* scale, */ dtype);
76             cv::multiply(in_mat1, sc, out_mat_ocv, 1., dtype);
77             break;
78         }
79         default:
80         {
81             FAIL() << "no such math operation type for scalar and matrix!";
82         }
83         }
84         cv::GComputation c(GIn(in1, sc1), GOut(out));
85         c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
86     }
87     else
88     {
89         switch(opType)
90         {
91         case (ADD):
92         {
93             out = cv::gapi::add(in1, in2, dtype);
94             cv::add(in_mat1, in_mat2, out_mat_ocv, cv::noArray(), dtype);
95             break;
96         }
97         case (SUB):
98         {
99             out = cv::gapi::sub(in1, in2, dtype);
100             cv::subtract(in_mat1, in_mat2, out_mat_ocv, cv::noArray(), dtype);
101             break;
102         }
103         case (DIV):
104         {
105             in_mat2.setTo(1, in_mat2 == 0);  // avoid zeros in divide input data
106             out = cv::gapi::div(in1, in2, scale, dtype);
107             cv::divide(in_mat1, in_mat2, out_mat_ocv, scale, dtype);
108             break;
109         }
110         case (MUL):
111         {
112             out = cv::gapi::mul(in1, in2, scale, dtype);
113             cv::multiply(in_mat1, in_mat2, out_mat_ocv, scale, dtype);
114             break;
115         }
116         default:
117         {
118             FAIL() << "no such math operation type for matrix and matrix!";
119         }}
120         cv::GComputation c(GIn(in1, in2), GOut(out));
121         c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
122     }
123
124     // Comparison //////////////////////////////////////////////////////////////
125     {
126     // TODO: make threshold vs bit-exact criteria be driven by testing parameter
127     #if 1
128         if (CV_MAT_DEPTH(out_mat_ocv.type()) != CV_32F &&
129             CV_MAT_DEPTH(out_mat_ocv.type()) != CV_64F)
130         {
131             // integral: allow 1% of differences, and no diffs by >1 unit
132             EXPECT_LE(countNonZeroPixels(cv::abs(out_mat_gapi - out_mat_ocv) > 0),
133                                                            0.01*out_mat_ocv.total());
134             EXPECT_LE(countNonZeroPixels(cv::abs(out_mat_gapi - out_mat_ocv) > 1), 0);
135         }
136         else
137         {
138             // floating-point: expect 6 decimal digits - best we expect of F32
139             EXPECT_EQ(0, cv::countNonZero(cv::abs(out_mat_gapi - out_mat_ocv) >
140                                                     1e-6*cv::abs(out_mat_ocv)));
141         }
142     #else
143         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
144     #endif
145         EXPECT_EQ(out_mat_gapi.size(), sz);
146     }
147 }
148
149 TEST_P(MulDoubleTest, AccuracyTest)
150 {
151     auto param = GetParam();
152     int type = std::get<0>(param);
153     int dtype = std::get<2>(param);
154     cv::Size sz_in = std::get<1>(param);
155     bool initOut = std::get<3>(param);
156
157     auto& rng = cv::theRNG();
158     double d = rng.uniform(0.0, 10.0);
159     auto compile_args = std::get<4>(param);
160     initMatrixRandU(type, sz_in, dtype, initOut);
161
162     // G-API code ////////////////////////////////////////////////////////////
163     cv::GMat in1, out;
164     out = cv::gapi::mulC(in1, d, dtype);
165     cv::GComputation c(in1, out);
166     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
167
168     // OpenCV code ///////////////////////////////////////////////////////////
169     cv::multiply(in_mat1, d, out_mat_ocv, 1, dtype);
170
171     // Comparison ////////////////////////////////////////////////////////////
172 #if 1
173     if (CV_MAT_DEPTH(out_mat_ocv.type()) != CV_32F &&
174         CV_MAT_DEPTH(out_mat_ocv.type()) != CV_64F)
175     {
176         // integral: allow 1% of differences, and no diffs by >1 unit
177         EXPECT_LE(countNonZeroPixels(cv::abs(out_mat_gapi - out_mat_ocv) > 0),
178                                                     0.01*out_mat_ocv.total());
179         EXPECT_LE(countNonZeroPixels(cv::abs(out_mat_gapi - out_mat_ocv) > 1), 0);
180     }
181     else
182     {
183         // floating-point: expect 6 decimal digits - best we expect of F32
184         EXPECT_EQ(0, cv::countNonZero(cv::abs(out_mat_gapi - out_mat_ocv) >
185             1e-6*cv::abs(out_mat_ocv)));
186     }
187 #else
188     EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
189 #endif
190     EXPECT_EQ(out_mat_gapi.size(), sz_in);
191 }
192
193 TEST_P(DivTest, DISABLED_DivByZeroTest)  // https://github.com/opencv/opencv/pull/12826
194 {
195     int type = 0, dtype = 0;
196     cv::Size sz_in;
197     bool initOut = false;
198     cv::GCompileArgs compile_args;
199     std::tie(type, sz_in, dtype, initOut, compile_args) = GetParam();
200
201     initMatrixRandU(type, sz_in, dtype, initOut);
202     in_mat2 = cv::Mat(sz_in, type);
203     in_mat2.setTo(cv::Scalar::all(0));
204
205     // G-API code //////////////////////////////////////////////////////////////
206     cv::GMat in1, in2;
207     auto out = cv::gapi::div(in1, in2, 1.0, dtype);
208     cv::GComputation c(GIn(in1, in2), GOut(out));
209     c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
210
211     // OpenCV code /////////////////////////////////////////////////////////////
212     {
213         cv::divide(in_mat1, in_mat2, out_mat_ocv, 1.0, dtype);
214     }
215
216     // Comparison //////////////////////////////////////////////////////////////
217     {
218         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
219         EXPECT_EQ(out_mat_gapi.size(), sz_in);
220     }
221 }
222
223 TEST_P(DivCTest, DISABLED_DivByZeroTest)  // https://github.com/opencv/opencv/pull/12826
224 {
225     int type = 0, dtype = 0;
226     cv::Size sz_in;
227     bool initOut = false;
228     cv::GCompileArgs compile_args;
229     std::tie(type, sz_in, dtype, initOut, compile_args) = GetParam();
230
231     initMatrixRandU(type, sz_in, dtype, initOut);
232     sc = cv::Scalar::all(0);
233
234     // G-API code //////////////////////////////////////////////////////////////
235     cv::GMat in1;
236     cv::GScalar sc1;
237     auto out = cv::gapi::divC(in1, sc1, dtype);
238     cv::GComputation c(GIn(in1, sc1), GOut(out));
239
240     c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
241
242     // OpenCV code /////////////////////////////////////////////////////////////
243     {
244         cv::divide(in_mat1, sc, out_mat_ocv, dtype);
245     }
246
247     // Comparison //////////////////////////////////////////////////////////////
248     {
249         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
250         cv::Mat zeros = cv::Mat::zeros(sz_in, type);
251         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != zeros));
252     }
253 }
254
255 TEST_P(MeanTest, AccuracyTest)
256 {
257     int type = 0;
258     bool initOut = false;
259     cv::Size sz_in;
260     cv::GCompileArgs compile_args;
261     std::tie(type, sz_in, initOut, compile_args) = GetParam();
262     initMatrixRandU(type, sz_in, initOut);
263     cv::Scalar out_norm;
264     cv::Scalar out_norm_ocv;
265
266     // G-API code //////////////////////////////////////////////////////////////
267     cv::GMat in;
268     auto out = cv::gapi::mean(in);
269
270     cv::GComputation c(cv::GIn(in), cv::GOut(out));
271     c.apply(cv::gin(in_mat1), cv::gout(out_norm), std::move(compile_args));
272     // OpenCV code /////////////////////////////////////////////////////////////
273     {
274         out_norm_ocv = cv::mean(in_mat1);
275     }
276     // Comparison //////////////////////////////////////////////////////////////
277     {
278         EXPECT_EQ(out_norm[0], out_norm_ocv[0]);
279     }
280 }
281
282 TEST_P(MaskTest, AccuracyTest)
283 {
284     int type = 0;
285     bool initOut = false;
286     cv::Size sz_in;
287     cv::GCompileArgs compile_args;
288     std::tie(type, sz_in, initOut, compile_args) = GetParam();
289     initMatrixRandU(type, sz_in, type, initOut);
290
291     in_mat2 = cv::Mat(sz_in, CV_8UC1);
292     cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
293     in_mat2 = in_mat2 > 128;
294
295     // G-API code //////////////////////////////////////////////////////////////
296     cv::GMat in, m;
297     auto out = cv::gapi::mask(in, m);
298
299     cv::GComputation c(cv::GIn(in, m), cv::GOut(out));
300     c.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat_gapi), std::move(compile_args));
301     // OpenCV code /////////////////////////////////////////////////////////////
302     {
303         out_mat_ocv = cv::Mat::zeros(in_mat1.size(), in_mat1.type());
304         in_mat1.copyTo(out_mat_ocv, in_mat2);
305     }
306     // Comparison //////////////////////////////////////////////////////////////
307     {
308         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
309     }
310 }
311
312 TEST_P(Polar2CartTest, AccuracyTest)
313 {
314     auto param = GetParam();
315     cv::Size sz_in = std::get<0>(param);
316     auto compile_args = std::get<2>(param);
317     initMatsRandU(CV_32FC1, sz_in, CV_32FC1, std::get<1>(param));
318
319     cv::Mat out_mat2;
320     cv::Mat out_mat_ocv2;
321     if(std::get<1>(param) == true)
322     {
323         out_mat2 = cv::Mat(sz_in, CV_32FC1);
324         out_mat_ocv2 = cv::Mat(sz_in, CV_32FC1);
325     }
326
327     // G-API code //////////////////////////////////////////////////////////////
328     cv::GMat in1, in2, out1, out2;
329     std::tie(out1, out2) = cv::gapi::polarToCart(in1, in2);
330
331     cv::GComputation c(GIn(in1, in2), GOut(out1, out2));
332     c.apply(gin(in_mat1,in_mat2), gout(out_mat_gapi, out_mat2), std::move(compile_args));
333     // OpenCV code /////////////////////////////////////////////////////////////
334     {
335         cv::polarToCart(in_mat1, in_mat2, out_mat_ocv, out_mat_ocv2);
336     }
337     // Comparison //////////////////////////////////////////////////////////////
338     {
339         // Note that we cannot rely on bit-exact sin/cos functions used for this
340         // transform, so we need a threshold for verifying results vs reference.
341         //
342         // Relative threshold like 1e-6 is very restrictive, nearly best we can
343         // expect of single-precision elementary functions implementation.
344         //
345         // However, good idea is making such threshold configurable: parameter
346         // of this test - which a specific test istantiation could setup.
347         //
348         // Note that test instantiation for the OpenCV back-end could even let
349         // the threshold equal to zero, as CV back-end calls the same kernel.
350         //
351         // TODO: Make threshold a configurable parameter of this test (ADE-221)
352
353         cv::Mat &outx = out_mat_gapi,
354                 &outy = out_mat2;
355         cv::Mat &refx = out_mat_ocv,
356                 &refy = out_mat_ocv2;
357         cv::Mat difx = cv::abs(refx - outx),
358                 dify = cv::abs(refy - outy);
359         cv::Mat absx = cv::abs(refx),
360                 absy = cv::abs(refy);
361
362         EXPECT_EQ(0, cv::countNonZero(difx > 1e-6*absx));
363         EXPECT_EQ(0, cv::countNonZero(dify > 1e-6*absy));
364         EXPECT_EQ(out_mat_gapi.size(), sz_in);
365     }
366 }
367
368 TEST_P(Cart2PolarTest, AccuracyTest)
369 {
370     auto param = GetParam();
371     cv::Size sz_in = std::get<0>(param);
372     auto compile_args = std::get<2>(param);
373     initMatsRandU(CV_32FC1, sz_in, CV_32FC1, std::get<1>(param));
374
375     cv::Mat out_mat2(sz_in, CV_32FC1);
376     cv::Mat out_mat_ocv2(sz_in, CV_32FC1);
377
378     // G-API code //////////////////////////////////////////////////////////////
379     cv::GMat in1, in2, out1, out2;
380     std::tie(out1, out2) = cv::gapi::cartToPolar(in1, in2);
381
382     cv::GComputation c(GIn(in1, in2), GOut(out1, out2));
383     c.apply(gin(in_mat1,in_mat2), gout(out_mat_gapi, out_mat2));
384     // OpenCV code /////////////////////////////////////////////////////////////
385     {
386         cv::cartToPolar(in_mat1, in_mat2, out_mat_ocv, out_mat_ocv2);
387     }
388     // Comparison //////////////////////////////////////////////////////////////
389     {
390         // Note that we cannot rely on bit-exact sin/cos functions used for this
391         // transform, so we need a threshold for verifying results vs reference.
392         //
393         // Relative threshold like 1e-6 is very restrictive, nearly best we can
394         // expect of single-precision elementary functions implementation.
395         //
396         // However, good idea is making such threshold configurable: parameter
397         // of this test - which a specific test istantiation could setup.
398         //
399         // Note that test instantiation for the OpenCV back-end could even let
400         // the threshold equal to zero, as CV back-end calls the same kernel.
401         //
402         // TODO: Make threshold a configurable parameter of this test (ADE-221)
403
404         cv::Mat &outm = out_mat_gapi,
405                 &outa = out_mat2;
406         cv::Mat &refm = out_mat_ocv,
407                 &refa = out_mat_ocv2;
408         cv::Mat difm = cv::abs(refm - outm),
409                 difa = cv::abs(refa - outa);
410         cv::Mat absm = cv::abs(refm),
411                 absa = cv::abs(refa);
412
413         // FIXME: Angle result looks inaccurate at OpenCV
414         //        (expected relative accuracy like 1e-6)
415         EXPECT_EQ(0, cv::countNonZero(difm > 1e-6*absm));
416         EXPECT_EQ(0, cv::countNonZero(difa > 1e-3*absa));
417         EXPECT_EQ(out_mat_gapi.size(), sz_in);
418     }
419 }
420
421 TEST_P(CmpTest, AccuracyTest)
422 {
423     CmpTypes opType = CMP_EQ;
424     int type = 0;
425     cv::Size sz;
426     bool testWithScalar = false, initOutMatr = false;
427     cv::GCompileArgs compile_args;
428     std::tie(opType, testWithScalar, type, sz, initOutMatr, compile_args) = GetParam();
429     initMatsRandU(type, sz, CV_8U, initOutMatr);
430
431     // G-API code & corresponding OpenCV code ////////////////////////////////
432     cv::GMat in1, out;
433     if( testWithScalar )
434     {
435         cv::GScalar in2;
436         switch(opType)
437         {
438         case CMP_EQ: out = cv::gapi::cmpEQ(in1, in2); break;
439         case CMP_GT: out = cv::gapi::cmpGT(in1, in2); break;
440         case CMP_GE: out = cv::gapi::cmpGE(in1, in2); break;
441         case CMP_LT: out = cv::gapi::cmpLT(in1, in2); break;
442         case CMP_LE: out = cv::gapi::cmpLE(in1, in2); break;
443         case CMP_NE: out = cv::gapi::cmpNE(in1, in2); break;
444         default: FAIL() << "no such compare operation type for matrix and scalar!";
445         }
446
447         cv::compare(in_mat1, sc, out_mat_ocv, opType);
448
449         cv::GComputation c(GIn(in1, in2), GOut(out));
450         c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
451     }
452     else
453     {
454         cv::GMat in2;
455         switch(opType)
456         {
457         case CMP_EQ: out = cv::gapi::cmpEQ(in1, in2); break;
458         case CMP_GT: out = cv::gapi::cmpGT(in1, in2); break;
459         case CMP_GE: out = cv::gapi::cmpGE(in1, in2); break;
460         case CMP_LT: out = cv::gapi::cmpLT(in1, in2); break;
461         case CMP_LE: out = cv::gapi::cmpLE(in1, in2); break;
462         case CMP_NE: out = cv::gapi::cmpNE(in1, in2); break;
463         default: FAIL() << "no such compare operation type for two matrices!";
464         }
465
466         cv::compare(in_mat1, in_mat2, out_mat_ocv, opType);
467
468         cv::GComputation c(GIn(in1, in2), GOut(out));
469         c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
470     }
471
472     // Comparison //////////////////////////////////////////////////////////////
473     {
474         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
475         EXPECT_EQ(out_mat_gapi.size(), sz);
476     }
477 }
478
479 TEST_P(BitwiseTest, AccuracyTest)
480 {
481     bitwiseOp opType = AND;
482     int type = 0;
483     cv::Size sz;
484     bool initOutMatr = false;
485     cv::GCompileArgs compile_args;
486     std::tie(opType, type, sz, initOutMatr, compile_args) = GetParam();
487     initMatsRandU(type, sz, type, initOutMatr);
488
489     // G-API code & corresponding OpenCV code ////////////////////////////////
490     cv::GMat in1, in2, out;
491     switch(opType)
492     {
493         case AND:
494         {
495             out = cv::gapi::bitwise_and(in1, in2);
496             cv::bitwise_and(in_mat1, in_mat2, out_mat_ocv);
497             break;
498         }
499         case OR:
500         {
501             out = cv::gapi::bitwise_or(in1, in2);
502             cv::bitwise_or(in_mat1, in_mat2, out_mat_ocv);
503             break;
504         }
505         case XOR:
506         {
507             out = cv::gapi::bitwise_xor(in1, in2);
508             cv::bitwise_xor(in_mat1, in_mat2, out_mat_ocv);
509             break;
510         }
511         default:
512         {
513             FAIL() << "no such bitwise operation type!";
514         }
515     }
516     cv::GComputation c(GIn(in1, in2), GOut(out));
517     c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
518
519     // Comparison //////////////////////////////////////////////////////////////
520     {
521         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
522         EXPECT_EQ(out_mat_gapi.size(), sz);
523     }
524 }
525
526 TEST_P(NotTest, AccuracyTest)
527 {
528     auto param = GetParam();
529     cv::Size sz_in = std::get<1>(param);
530     auto compile_args = std::get<3>(param);
531     initMatrixRandU(std::get<0>(param), sz_in, std::get<0>(param), std::get<2>(param));
532
533     // G-API code //////////////////////////////////////////////////////////////
534     cv::GMat in;
535     auto out = cv::gapi::bitwise_not(in);
536     cv::GComputation c(in, out);
537
538     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
539
540     // OpenCV code /////////////////////////////////////////////////////////////
541     {
542         cv::bitwise_not(in_mat1, out_mat_ocv);
543     }
544     // Comparison //////////////////////////////////////////////////////////////
545     {
546         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
547         EXPECT_EQ(out_mat_gapi.size(), sz_in);
548     }
549 }
550
551 TEST_P(SelectTest, AccuracyTest)
552 {
553     auto param = GetParam();
554     int type = std::get<0>(param);
555     cv::Size sz_in = std::get<1>(param);
556     auto compile_args = std::get<3>(param);
557     initMatsRandU(type, sz_in, type, std::get<2>(param));
558     cv::Mat in_mask(sz_in, CV_8UC1);
559     cv::randu(in_mask, cv::Scalar::all(0), cv::Scalar::all(255));
560
561     // G-API code //////////////////////////////////////////////////////////////
562     cv::GMat in1, in2, in3;
563     auto out = cv::gapi::select(in1, in2, in3);
564     cv::GComputation c(GIn(in1, in2, in3), GOut(out));
565
566     c.apply(gin(in_mat1, in_mat2, in_mask), gout(out_mat_gapi), std::move(compile_args));
567
568     // OpenCV code /////////////////////////////////////////////////////////////
569     {
570         in_mat2.copyTo(out_mat_ocv);
571         in_mat1.copyTo(out_mat_ocv, in_mask);
572     }
573     // Comparison //////////////////////////////////////////////////////////////
574     {
575         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
576         EXPECT_EQ(out_mat_gapi.size(), sz_in);
577     }
578 }
579
580 TEST_P(MinTest, AccuracyTest)
581 {
582     auto param = GetParam();
583     cv::Size sz_in = std::get<1>(param);
584     auto compile_args = std::get<3>(param);
585     initMatsRandU(std::get<0>(param), sz_in, std::get<0>(param), std::get<2>(param));
586
587     // G-API code //////////////////////////////////////////////////////////////
588     cv::GMat in1, in2;
589     auto out = cv::gapi::min(in1, in2);
590     cv::GComputation c(GIn(in1, in2), GOut(out));
591
592     c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
593
594     // OpenCV code /////////////////////////////////////////////////////////////
595     {
596         cv::min(in_mat1, in_mat2, out_mat_ocv);
597     }
598     // Comparison //////////////////////////////////////////////////////////////
599     {
600         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
601         EXPECT_EQ(out_mat_gapi.size(), sz_in);
602     }
603 }
604
605 TEST_P(MaxTest, AccuracyTest)
606 {
607     auto param = GetParam();
608     cv::Size sz_in = std::get<1>(param);
609     auto compile_args = std::get<3>(param);
610     initMatsRandU(std::get<0>(param), sz_in, std::get<0>(param), std::get<2>(param));
611
612     // G-API code //////////////////////////////////////////////////////////////
613     cv::GMat in1, in2;
614     auto out = cv::gapi::max(in1, in2);
615     cv::GComputation c(GIn(in1, in2), GOut(out));
616
617     c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
618
619     // OpenCV code /////////////////////////////////////////////////////////////
620     {
621         cv::max(in_mat1, in_mat2, out_mat_ocv);
622     }
623     // Comparison //////////////////////////////////////////////////////////////
624     {
625         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
626         EXPECT_EQ(out_mat_gapi.size(), sz_in);
627     }
628 }
629
630 TEST_P(AbsDiffTest, AccuracyTest)
631 {
632     auto param = GetParam();
633     cv::Size sz_in = std::get<1>(param);
634     auto compile_args = std::get<3>(param);
635     initMatsRandU(std::get<0>(param), sz_in, std::get<0>(param), std::get<2>(param));
636
637     // G-API code //////////////////////////////////////////////////////////////
638     cv::GMat in1, in2;
639     auto out = cv::gapi::absDiff(in1, in2);
640     cv::GComputation c(GIn(in1, in2), GOut(out));
641
642     c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
643
644     // OpenCV code /////////////////////////////////////////////////////////////
645     {
646         cv::absdiff(in_mat1, in_mat2, out_mat_ocv);
647     }
648     // Comparison //////////////////////////////////////////////////////////////
649     {
650         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
651         EXPECT_EQ(out_mat_gapi.size(), sz_in);
652     }
653 }
654
655 TEST_P(AbsDiffCTest, AccuracyTest)
656 {
657     auto param = GetParam();
658     cv::Size sz_in = std::get<1>(param);
659     auto compile_args = std::get<3>(param);
660     initMatsRandU(std::get<0>(param), sz_in, std::get<0>(param), std::get<2>(param));
661
662     // G-API code //////////////////////////////////////////////////////////////
663     cv::GMat in1;
664     cv::GScalar sc1;
665     auto out = cv::gapi::absDiffC(in1, sc1);
666     cv::GComputation c(cv::GIn(in1, sc1), cv::GOut(out));
667
668     c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
669
670     // OpenCV code /////////////////////////////////////////////////////////////
671     {
672         cv::absdiff(in_mat1, sc, out_mat_ocv);
673     }
674     // Comparison //////////////////////////////////////////////////////////////
675     {
676         EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
677         EXPECT_EQ(out_mat_gapi.size(), sz_in);
678     }
679 }
680
681 TEST_P(SumTest, AccuracyTest)
682 {
683     auto param = GetParam();
684     compare_scalar_f cmpF = get<3>(GetParam());
685     MatType type = std::get<0>(param);
686     cv::Size sz_in = std::get<1>(param);
687     auto compile_args = std::get<4>(param);
688     initMatrixRandU(type, sz_in, type, std::get<2>(param));
689
690
691     cv::Scalar out_sum;
692     cv::Scalar out_sum_ocv;
693
694     // G-API code //////////////////////////////////////////////////////////////
695     cv::GMat in;
696     auto out = cv::gapi::sum(in);
697
698     cv::GComputation c(cv::GIn(in), cv::GOut(out));
699     c.apply(cv::gin(in_mat1), cv::gout(out_sum), std::move(compile_args));
700     // OpenCV code /////////////////////////////////////////////////////////////
701     {
702         out_sum_ocv = cv::sum(in_mat1);
703     }
704     // Comparison //////////////////////////////////////////////////////////////
705     {
706         EXPECT_TRUE(cmpF(out_sum, out_sum_ocv));
707     }
708 }
709
710 TEST_P(AddWeightedTest, AccuracyTest)
711 {
712     int type = 0, dtype = 0;
713     cv::Size sz_in;
714     bool initOut = false;
715     cv::GCompileArgs compile_args;
716     compare_f cmpF;
717     std::tie(type, sz_in, dtype, initOut, cmpF, compile_args) = GetParam();
718
719     auto& rng = cv::theRNG();
720     double alpha = rng.uniform(0.0, 1.0);
721     double beta = rng.uniform(0.0, 1.0);
722     double gamma = rng.uniform(0.0, 1.0);
723     initMatsRandU(type, sz_in, dtype, initOut);
724
725     // G-API code //////////////////////////////////////////////////////////////
726     cv::GMat in1, in2;
727     auto out = cv::gapi::addWeighted(in1, alpha, in2, beta, gamma, dtype);
728     cv::GComputation c(GIn(in1, in2), GOut(out));
729
730     c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
731
732     // OpenCV code /////////////////////////////////////////////////////////////
733     {
734         cv::addWeighted(in_mat1, alpha, in_mat2, beta, gamma, out_mat_ocv, dtype);
735     }
736     // Comparison //////////////////////////////////////////////////////////////
737     EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
738     EXPECT_EQ(out_mat_gapi.size(), sz_in);
739
740 }
741
742 TEST_P(NormTest, AccuracyTest)
743 {
744     compare_scalar_f cmpF;
745     NormTypes opType = NORM_INF;
746     int type = 0;
747     cv::Size sz;
748     cv::GCompileArgs compile_args;
749     std::tie(opType, type, sz, cmpF, compile_args) = GetParam();
750     initMatrixRandU(type, sz, type, false);
751
752     cv::Scalar out_norm;
753     cv::Scalar out_norm_ocv;
754
755     // G-API code & corresponding OpenCV code ////////////////////////////////
756     cv::GMat in1;
757     cv::GScalar out;
758     switch(opType)
759     {
760         case NORM_L1: out = cv::gapi::normL1(in1); break;
761         case NORM_L2: out = cv::gapi::normL2(in1); break;
762         case NORM_INF: out = cv::gapi::normInf(in1); break;
763         default: FAIL() << "no such norm operation type!";
764     }
765     out_norm_ocv = cv::norm(in_mat1, opType);
766     cv::GComputation c(GIn(in1), GOut(out));
767     c.apply(gin(in_mat1), gout(out_norm), std::move(compile_args));
768
769     // Comparison //////////////////////////////////////////////////////////////
770     {
771         EXPECT_TRUE(cmpF(out_norm, out_norm_ocv));
772     }
773 }
774
775 TEST_P(IntegralTest, AccuracyTest)
776 {
777     int type = std::get<0>(GetParam());
778     cv::Size sz_in = std::get<1>(GetParam());
779     auto compile_args = std::get<2>(GetParam());
780
781     int type_out = (type == CV_8U) ? CV_32SC1 : CV_64FC1;
782     cv::Mat in_mat1(sz_in, type);
783
784     cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
785
786     cv::Size sz_out = cv::Size(sz_in.width + 1, sz_in.height + 1);
787     cv::Mat out_mat1(sz_out, type_out);
788     cv::Mat out_mat_ocv1(sz_out, type_out);
789
790     cv::Mat out_mat2(sz_out, CV_64FC1);
791     cv::Mat out_mat_ocv2(sz_out, CV_64FC1);
792
793     // G-API code //////////////////////////////////////////////////////////////
794     cv::GMat in1, out1, out2;
795     std::tie(out1, out2)  = cv::gapi::integral(in1, type_out, CV_64FC1);
796     cv::GComputation c(cv::GIn(in1), cv::GOut(out1, out2));
797
798     c.apply(cv::gin(in_mat1), cv::gout(out_mat1, out_mat2), std::move(compile_args));
799
800     // OpenCV code /////////////////////////////////////////////////////////////
801     {
802         cv::integral(in_mat1, out_mat_ocv1, out_mat_ocv2);
803     }
804     // Comparison //////////////////////////////////////////////////////////////
805     {
806         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv1 != out_mat1));
807         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv2 != out_mat2));
808     }
809 }
810
811 TEST_P(ThresholdTest, AccuracyTestBinary)
812 {
813     auto param = GetParam();
814     int type = std::get<0>(param);
815     cv::Size sz_in = std::get<1>(param);
816     int tt = std::get<2>(param);
817
818     auto compile_args = std::get<4>(param);
819     cv::Scalar thr = initScalarRandU(50);
820     cv::Scalar maxval = initScalarRandU(50) + cv::Scalar(50, 50, 50, 50);
821     initMatrixRandU(type, sz_in, type, std::get<3>(param));
822     cv::Scalar out_scalar;
823
824     // G-API code //////////////////////////////////////////////////////////////
825     cv::GMat in1, out;
826     cv::GScalar th1, mv1;
827     out = cv::gapi::threshold(in1, th1, mv1, tt);
828     cv::GComputation c(GIn(in1, th1, mv1), GOut(out));
829
830     c.apply(gin(in_mat1, thr, maxval), gout(out_mat_gapi), std::move(compile_args));
831
832     // OpenCV code /////////////////////////////////////////////////////////////
833     {
834         cv::threshold(in_mat1, out_mat_ocv, thr.val[0], maxval.val[0], tt);
835     }
836     // Comparison //////////////////////////////////////////////////////////////
837     {
838         ASSERT_EQ(out_mat_gapi.size(), sz_in);
839         EXPECT_EQ(0, cv::norm(out_mat_ocv, out_mat_gapi, NORM_L1));
840     }
841 }
842
843 TEST_P(ThresholdOTTest, AccuracyTestOtsu)
844 {
845     auto param = GetParam();
846     int type = std::get<0>(param);
847     cv::Size sz_in = std::get<1>(param);
848     int tt = std::get<2>(param);
849
850     auto compile_args = std::get<4>(param);
851     cv::Scalar maxval = initScalarRandU(50) + cv::Scalar(50, 50, 50, 50);
852     initMatrixRandU(type, sz_in, type, std::get<3>(param));
853     cv::Scalar out_gapi_scalar;
854     double ocv_res;
855
856     // G-API code //////////////////////////////////////////////////////////////
857     cv::GMat in1, out;
858     cv::GScalar mv1, scout;
859     std::tie<cv::GMat, cv::GScalar>(out, scout) = cv::gapi::threshold(in1, mv1, tt);
860     cv::GComputation c(cv::GIn(in1, mv1), cv::GOut(out, scout));
861
862     c.apply(gin(in_mat1, maxval), gout(out_mat_gapi, out_gapi_scalar), std::move(compile_args));
863
864     // OpenCV code /////////////////////////////////////////////////////////////
865     {
866         ocv_res = cv::threshold(in_mat1, out_mat_ocv, maxval.val[0], maxval.val[0], tt);
867     }
868     // Comparison //////////////////////////////////////////////////////////////
869     {
870         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
871         EXPECT_EQ(out_mat_gapi.size(), sz_in);
872         EXPECT_EQ(ocv_res, out_gapi_scalar.val[0]);
873     }
874 }
875
876 TEST_P(InRangeTest, AccuracyTest)
877 {
878     auto param = GetParam();
879     int type = std::get<0>(param);
880     cv::Size sz_in = std::get<1>(param);
881
882     auto compile_args = std::get<3>(param);
883     cv::Scalar thrLow = initScalarRandU(100);
884     cv::Scalar thrUp = initScalarRandU(100) + cv::Scalar(100, 100, 100, 100);
885     initMatrixRandU(type, sz_in, type, std::get<2>(param));
886
887     // G-API code //////////////////////////////////////////////////////////////
888     cv::GMat in1;
889     cv::GScalar th1, mv1;
890     auto out = cv::gapi::inRange(in1, th1, mv1);
891     cv::GComputation c(GIn(in1, th1, mv1), GOut(out));
892
893     c.apply(gin(in_mat1, thrLow, thrUp), gout(out_mat_gapi), std::move(compile_args));
894
895     // OpenCV code /////////////////////////////////////////////////////////////
896     {
897         cv::inRange(in_mat1, thrLow, thrUp, out_mat_ocv);
898     }
899     // Comparison //////////////////////////////////////////////////////////////
900     {
901         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
902         EXPECT_EQ(out_mat_gapi.size(), sz_in);
903     }
904 }
905
906 TEST_P(Split3Test, AccuracyTest)
907 {
908     cv::Size sz_in = std::get<0>(GetParam());
909     auto compile_args = std::get<1>(GetParam());
910     initMatrixRandU(CV_8UC3, sz_in, CV_8UC1);
911
912     cv::Mat out_mat2 = cv::Mat(sz_in, CV_8UC1);
913     cv::Mat out_mat3 = cv::Mat(sz_in, CV_8UC1);
914     cv::Mat out_mat_ocv2 = cv::Mat(sz_in, CV_8UC1);
915     cv::Mat out_mat_ocv3 = cv::Mat(sz_in, CV_8UC1);
916     // G-API code //////////////////////////////////////////////////////////////
917     cv::GMat in1, out1, out2, out3;
918     std::tie(out1, out2, out3)  = cv::gapi::split3(in1);
919     cv::GComputation c(cv::GIn(in1), cv::GOut(out1, out2, out3));
920
921     c.apply(cv::gin(in_mat1), cv::gout(out_mat_gapi, out_mat2, out_mat3), std::move(compile_args));
922     // OpenCV code /////////////////////////////////////////////////////////////
923     {
924         std::vector<cv::Mat> out_mats_ocv = {out_mat_ocv, out_mat_ocv2, out_mat_ocv3};
925         cv::split(in_mat1, out_mats_ocv);
926     }
927     // Comparison //////////////////////////////////////////////////////////////
928     {
929         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv  != out_mat_gapi));
930         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv2 != out_mat2));
931         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv3 != out_mat3));
932     }
933 }
934
935 TEST_P(Split4Test, AccuracyTest)
936 {
937     cv::Size sz_in = std::get<0>(GetParam());
938     auto compile_args = std::get<1>(GetParam());
939     initMatrixRandU(CV_8UC4, sz_in, CV_8UC1);
940     cv::Mat out_mat2 = cv::Mat(sz_in, CV_8UC1);
941     cv::Mat out_mat3 = cv::Mat(sz_in, CV_8UC1);
942     cv::Mat out_mat4 = cv::Mat(sz_in, CV_8UC1);
943     cv::Mat out_mat_ocv2 = cv::Mat(sz_in, CV_8UC1);
944     cv::Mat out_mat_ocv3 = cv::Mat(sz_in, CV_8UC1);
945     cv::Mat out_mat_ocv4 = cv::Mat(sz_in, CV_8UC1);
946
947     // G-API code //////////////////////////////////////////////////////////////
948     cv::GMat in1, out1, out2, out3, out4;
949     std::tie(out1, out2, out3, out4)  = cv::gapi::split4(in1);
950     cv::GComputation c(cv::GIn(in1), cv::GOut(out1, out2, out3, out4));
951
952     c.apply(cv::gin(in_mat1), cv::gout(out_mat_gapi, out_mat2, out_mat3, out_mat4), std::move(compile_args));
953     // OpenCV code /////////////////////////////////////////////////////////////
954     {
955         std::vector<cv::Mat> out_mats_ocv = {out_mat_ocv, out_mat_ocv2, out_mat_ocv3, out_mat_ocv4};
956         cv::split(in_mat1, out_mats_ocv);
957     }
958     // Comparison //////////////////////////////////////////////////////////////
959     {
960         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv  != out_mat_gapi));
961         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv2 != out_mat2));
962         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv3 != out_mat3));
963         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv4 != out_mat4));
964     }
965 }
966
967 static void ResizeAccuracyTest(compare_f cmpF, int type, int interp, cv::Size sz_in, cv::Size sz_out, double fx, double fy, cv::GCompileArgs&& compile_args)
968 {
969     cv::Mat in_mat1 (sz_in, type );
970     cv::Scalar mean = cv::Scalar::all(127);
971     cv::Scalar stddev = cv::Scalar::all(40.f);
972
973     cv::randn(in_mat1, mean, stddev);
974
975     auto out_mat_sz = sz_out.area() == 0 ? cv::Size(saturate_cast<int>(sz_in.width *fx),
976                                                     saturate_cast<int>(sz_in.height*fy))
977                                          : sz_out;
978     cv::Mat out_mat(out_mat_sz, type);
979     cv::Mat out_mat_ocv(out_mat_sz, type);
980
981     // G-API code //////////////////////////////////////////////////////////////
982     cv::GMat in;
983     auto out = cv::gapi::resize(in, sz_out, fx, fy, interp);
984
985     cv::GComputation c(in, out);
986     c.apply(in_mat1, out_mat, std::move(compile_args));
987     // OpenCV code /////////////////////////////////////////////////////////////
988     {
989         cv::resize(in_mat1, out_mat_ocv, sz_out, fx, fy, interp);
990     }
991     // Comparison //////////////////////////////////////////////////////////////
992     {
993         EXPECT_TRUE(cmpF(out_mat, out_mat_ocv));
994     }
995 }
996
997 TEST_P(ResizeTest, AccuracyTest)
998 {
999     compare_f cmpF;
1000     int type = 0, interp = 0;
1001     cv::Size sz_in, sz_out;
1002     cv::GCompileArgs compile_args;
1003     std::tie(cmpF, type, interp, sz_in, sz_out, compile_args) = GetParam();
1004     ResizeAccuracyTest(cmpF, type, interp, sz_in, sz_out, 0.0, 0.0, std::move(compile_args));
1005 }
1006
1007 TEST_P(ResizeTestFxFy, AccuracyTest)
1008 {
1009     compare_f cmpF;
1010     int type = 0, interp = 0;
1011     cv::Size sz_in;
1012     double fx = 0.0, fy = 0.0;
1013     cv::GCompileArgs compile_args;
1014     std::tie(cmpF, type, interp, sz_in, fx, fy, compile_args) = GetParam();
1015     ResizeAccuracyTest(cmpF, type, interp, sz_in, cv::Size{0, 0}, fx, fy, std::move(compile_args));
1016 }
1017
1018 TEST_P(Merge3Test, AccuracyTest)
1019 {
1020     cv::Size sz_in = std::get<0>(GetParam());
1021     initMatsRandU(CV_8UC1, sz_in, CV_8UC3);
1022     auto compile_args = std::get<1>(GetParam());
1023     cv::Mat in_mat3(sz_in,  CV_8UC1);
1024     cv::Scalar mean = cv::Scalar::all(127);
1025     cv::Scalar stddev = cv::Scalar::all(40.f);
1026
1027     cv::randn(in_mat3, mean, stddev);
1028
1029     // G-API code //////////////////////////////////////////////////////////////
1030     cv::GMat in1, in2, in3;
1031     auto out = cv::gapi::merge3(in1, in2, in3);
1032
1033     cv::GComputation c(cv::GIn(in1, in2, in3), cv::GOut(out));
1034     c.apply(cv::gin(in_mat1, in_mat2, in_mat3), cv::gout(out_mat_gapi), std::move(compile_args));
1035     // OpenCV code /////////////////////////////////////////////////////////////
1036     {
1037         std::vector<cv::Mat> in_mats_ocv = {in_mat1, in_mat2, in_mat3};
1038         cv::merge(in_mats_ocv, out_mat_ocv);
1039     }
1040     // Comparison //////////////////////////////////////////////////////////////
1041     {
1042         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1043     }
1044 }
1045
1046 TEST_P(Merge4Test, AccuracyTest)
1047 {
1048     cv::Size sz_in = std::get<0>(GetParam());
1049     initMatsRandU(CV_8UC1, sz_in, CV_8UC4);
1050     auto compile_args = std::get<1>(GetParam());
1051     cv::Mat in_mat3(sz_in,  CV_8UC1);
1052     cv::Mat in_mat4(sz_in,  CV_8UC1);
1053     cv::Scalar mean = cv::Scalar::all(127);
1054     cv::Scalar stddev = cv::Scalar::all(40.f);
1055
1056     cv::randn(in_mat3, mean, stddev);
1057     cv::randn(in_mat4, mean, stddev);
1058
1059     // G-API code //////////////////////////////////////////////////////////////
1060     cv::GMat in1, in2, in3, in4;
1061     auto out = cv::gapi::merge4(in1, in2, in3, in4);
1062
1063     cv::GComputation c(cv::GIn(in1, in2, in3, in4), cv::GOut(out));
1064     c.apply(cv::gin(in_mat1, in_mat2, in_mat3, in_mat4), cv::gout(out_mat_gapi), std::move(compile_args));
1065     // OpenCV code /////////////////////////////////////////////////////////////
1066     {
1067         std::vector<cv::Mat> in_mats_ocv = {in_mat1, in_mat2, in_mat3, in_mat4};
1068         cv::merge(in_mats_ocv, out_mat_ocv);
1069     }
1070     // Comparison //////////////////////////////////////////////////////////////
1071     {
1072         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1073     }
1074 }
1075
1076 TEST_P(RemapTest, AccuracyTest)
1077 {
1078     auto param = GetParam();
1079     int type = std::get<0>(param);
1080     cv::Size sz_in = std::get<1>(param);
1081     auto compile_args = std::get<3>(param);
1082     initMatrixRandU(type, sz_in, type, std::get<2>(param));
1083     cv::Mat in_map1(sz_in,  CV_16SC2);
1084     cv::Mat in_map2 = cv::Mat();
1085     cv::randu(in_map1, cv::Scalar::all(0), cv::Scalar::all(255));
1086     cv::Scalar bv = cv::Scalar();
1087
1088     // G-API code //////////////////////////////////////////////////////////////
1089     cv::GMat in1;
1090     auto out = cv::gapi::remap(in1, in_map1, in_map2, cv::INTER_NEAREST,  cv::BORDER_REPLICATE, bv);
1091     cv::GComputation c(in1, out);
1092
1093     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1094
1095     // OpenCV code /////////////////////////////////////////////////////////////
1096     {
1097         cv::remap(in_mat1, out_mat_ocv, in_map1, in_map2, cv::INTER_NEAREST, cv::BORDER_REPLICATE, bv);
1098     }
1099     // Comparison //////////////////////////////////////////////////////////////
1100     {
1101         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1102         EXPECT_EQ(out_mat_gapi.size(), sz_in);
1103     }
1104 }
1105
1106 TEST_P(FlipTest, AccuracyTest)
1107 {
1108     auto param = GetParam();
1109     int type = std::get<0>(param);
1110     int flipCode =  std::get<1>(param);
1111     cv::Size sz_in = std::get<2>(param);
1112     initMatrixRandU(type, sz_in, type, false);
1113     auto compile_args = std::get<4>(GetParam());
1114
1115     // G-API code //////////////////////////////////////////////////////////////
1116     cv::GMat in;
1117     auto out = cv::gapi::flip(in, flipCode);
1118
1119     cv::GComputation c(in, out);
1120     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1121     // OpenCV code /////////////////////////////////////////////////////////////
1122     {
1123         cv::flip(in_mat1, out_mat_ocv, flipCode);
1124     }
1125     // Comparison //////////////////////////////////////////////////////////////
1126     {
1127         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1128         EXPECT_EQ(out_mat_gapi.size(), sz_in);
1129     }
1130 }
1131
1132 TEST_P(CropTest, AccuracyTest)
1133 {
1134     auto param = GetParam();
1135     int type = std::get<0>(param);
1136     cv::Rect rect_to = std::get<1>(param);
1137     cv::Size sz_in = std::get<2>(param);
1138     auto compile_args = std::get<4>(param);
1139
1140     initMatrixRandU(type, sz_in, type, false);
1141     cv::Size sz_out = cv::Size(rect_to.width, rect_to.height);
1142     if( std::get<3>(param) == true )
1143     {
1144         out_mat_gapi = cv::Mat(sz_out, type);
1145         out_mat_ocv = cv::Mat(sz_out, type);
1146     }
1147
1148     // G-API code //////////////////////////////////////////////////////////////
1149     cv::GMat in;
1150     auto out = cv::gapi::crop(in, rect_to);
1151
1152     cv::GComputation c(in, out);
1153     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1154     // OpenCV code /////////////////////////////////////////////////////////////
1155     {
1156         cv::Mat(in_mat1, rect_to).copyTo(out_mat_ocv);
1157     }
1158     // Comparison //////////////////////////////////////////////////////////////
1159     {
1160         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1161         EXPECT_EQ(out_mat_gapi.size(), sz_out);
1162     }
1163 }
1164
1165 TEST_P(ConcatHorTest, AccuracyTest)
1166 {
1167     auto param = GetParam();
1168     int type = std::get<0>(param);
1169     cv::Size sz_out = std::get<1>(param);
1170     auto compile_args = std::get<2>(param);
1171
1172     int wpart = sz_out.width / 4;
1173     cv::Size sz_in1 = cv::Size(wpart, sz_out.height);
1174     cv::Size sz_in2 = cv::Size(sz_out.width - wpart, sz_out.height);
1175
1176     cv::Mat in_mat1 (sz_in1, type );
1177     cv::Mat in_mat2 (sz_in2, type);
1178     cv::Scalar mean = cv::Scalar::all(127);
1179     cv::Scalar stddev = cv::Scalar::all(40.f);
1180
1181     cv::randn(in_mat1, mean, stddev);
1182     cv::randn(in_mat2, mean, stddev);
1183
1184     cv::Mat out_mat(sz_out, type);
1185     cv::Mat out_mat_ocv(sz_out, type);
1186
1187     // G-API code //////////////////////////////////////////////////////////////
1188     cv::GMat in1, in2;
1189     auto out = cv::gapi::concatHor(in1, in2);
1190
1191     cv::GComputation c(GIn(in1, in2), GOut(out));
1192     c.apply(gin(in_mat1, in_mat2), gout(out_mat), std::move(compile_args));
1193     // OpenCV code /////////////////////////////////////////////////////////////
1194     {
1195         cv::hconcat(in_mat1, in_mat2, out_mat_ocv );
1196     }
1197     // Comparison //////////////////////////////////////////////////////////////
1198     {
1199         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1200     }
1201 }
1202
1203 TEST_P(ConcatVertTest, AccuracyTest)
1204 {
1205     auto param = GetParam();
1206     int type = std::get<0>(param);
1207     cv::Size sz_out = std::get<1>(param);
1208     auto compile_args = std::get<2>(param);
1209
1210     int hpart = sz_out.height * 2/3;
1211     cv::Size sz_in1 = cv::Size(sz_out.width, hpart);
1212     cv::Size sz_in2 = cv::Size(sz_out.width, sz_out.height - hpart);
1213
1214     cv::Mat in_mat1 (sz_in1, type);
1215     cv::Mat in_mat2 (sz_in2, type);
1216     cv::Scalar mean = cv::Scalar::all(127);
1217     cv::Scalar stddev = cv::Scalar::all(40.f);
1218
1219     cv::randn(in_mat1, mean, stddev);
1220     cv::randn(in_mat2, mean, stddev);
1221
1222     cv::Mat out_mat(sz_out, type);
1223     cv::Mat out_mat_ocv(sz_out, type);
1224
1225     // G-API code //////////////////////////////////////////////////////////////
1226     cv::GMat in1, in2;
1227     auto out = cv::gapi::concatVert(in1, in2);
1228
1229     cv::GComputation c(GIn(in1, in2), GOut(out));
1230     c.apply(gin(in_mat1, in_mat2), gout(out_mat), std::move(compile_args));
1231     // OpenCV code /////////////////////////////////////////////////////////////
1232     {
1233         cv::vconcat(in_mat1, in_mat2, out_mat_ocv );
1234     }
1235     // Comparison //////////////////////////////////////////////////////////////
1236     {
1237         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1238     }
1239 }
1240
1241 TEST_P(ConcatVertVecTest, AccuracyTest)
1242 {
1243     auto param = GetParam();
1244     int type = std::get<0>(param);
1245     cv::Size sz_out = std::get<1>(param);
1246     auto compile_args = std::get<2>(param);
1247
1248     int hpart1 = sz_out.height * 2/5;
1249     int hpart2 = sz_out.height / 5;
1250     cv::Size sz_in1 = cv::Size(sz_out.width, hpart1);
1251     cv::Size sz_in2 = cv::Size(sz_out.width, hpart2);
1252     cv::Size sz_in3 = cv::Size(sz_out.width, sz_out.height - hpart1 - hpart2);
1253
1254     cv::Mat in_mat1 (sz_in1, type);
1255     cv::Mat in_mat2 (sz_in2, type);
1256     cv::Mat in_mat3 (sz_in3, type);
1257     cv::Scalar mean = cv::Scalar::all(127);
1258     cv::Scalar stddev = cv::Scalar::all(40.f);
1259
1260     cv::randn(in_mat1, mean, stddev);
1261     cv::randn(in_mat2, mean, stddev);
1262     cv::randn(in_mat3, mean, stddev);
1263
1264     cv::Mat out_mat(sz_out, type);
1265     cv::Mat out_mat_ocv(sz_out, type);
1266
1267     // G-API code //////////////////////////////////////////////////////////////
1268     std::vector <cv::GMat> mats(3);
1269     auto out = cv::gapi::concatVert(mats);
1270
1271     std::vector <cv::Mat> cvmats = {in_mat1, in_mat2, in_mat3};
1272
1273     cv::GComputation c({mats[0], mats[1], mats[2]}, {out});
1274     c.apply(gin(in_mat1, in_mat2, in_mat3), gout(out_mat), std::move(compile_args));
1275
1276     // OpenCV code /////////////////////////////////////////////////////////////
1277     {
1278         cv::vconcat(cvmats, out_mat_ocv );
1279     }
1280     // Comparison //////////////////////////////////////////////////////////////
1281     {
1282         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1283     }
1284 }
1285
1286 TEST_P(ConcatHorVecTest, AccuracyTest)
1287 {
1288     auto param = GetParam();
1289     int type = std::get<0>(param);
1290     cv::Size sz_out = std::get<1>(param);
1291     auto compile_args = std::get<2>(param);
1292
1293     int wpart1 = sz_out.width / 3;
1294     int wpart2 = sz_out.width / 4;
1295     cv::Size sz_in1 = cv::Size(wpart1, sz_out.height);
1296     cv::Size sz_in2 = cv::Size(wpart2, sz_out.height);
1297     cv::Size sz_in3 = cv::Size(sz_out.width - wpart1 - wpart2, sz_out.height);
1298
1299     cv::Mat in_mat1 (sz_in1, type);
1300     cv::Mat in_mat2 (sz_in2, type);
1301     cv::Mat in_mat3 (sz_in3, type);
1302     cv::Scalar mean = cv::Scalar::all(127);
1303     cv::Scalar stddev = cv::Scalar::all(40.f);
1304
1305     cv::randn(in_mat1, mean, stddev);
1306     cv::randn(in_mat2, mean, stddev);
1307     cv::randn(in_mat3, mean, stddev);
1308
1309     cv::Mat out_mat(sz_out, type);
1310     cv::Mat out_mat_ocv(sz_out, type);
1311
1312     // G-API code //////////////////////////////////////////////////////////////
1313     std::vector <cv::GMat> mats(3);
1314     auto out = cv::gapi::concatHor(mats);
1315
1316     std::vector <cv::Mat> cvmats = {in_mat1, in_mat2, in_mat3};
1317
1318     cv::GComputation c({mats[0], mats[1], mats[2]}, {out});
1319     c.apply(gin(in_mat1, in_mat2, in_mat3), gout(out_mat), std::move(compile_args));
1320
1321     // OpenCV code /////////////////////////////////////////////////////////////
1322     {
1323         cv::hconcat(cvmats, out_mat_ocv );
1324     }
1325     // Comparison //////////////////////////////////////////////////////////////
1326     {
1327         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1328     }
1329 }
1330
1331 TEST_P(LUTTest, AccuracyTest)
1332 {
1333     auto param = GetParam();
1334     int type_mat = std::get<0>(param);
1335     int type_lut = std::get<1>(param);
1336     int type_out = CV_MAKETYPE(CV_MAT_DEPTH(type_lut), CV_MAT_CN(type_mat));
1337     cv::Size sz_in = std::get<2>(param);
1338     auto compile_args = std::get<4>(GetParam());
1339
1340     initMatrixRandU(type_mat, sz_in, type_out);
1341     cv::Size sz_lut = cv::Size(1, 256);
1342     cv::Mat in_lut(sz_lut, type_lut);
1343     cv::randu(in_lut, cv::Scalar::all(0), cv::Scalar::all(255));
1344
1345     // G-API code //////////////////////////////////////////////////////////////
1346     cv::GMat in;
1347     auto out = cv::gapi::LUT(in, in_lut);
1348
1349     cv::GComputation c(in, out);
1350     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1351     // OpenCV code /////////////////////////////////////////////////////////////
1352     {
1353         cv::LUT(in_mat1, in_lut, out_mat_ocv);
1354     }
1355     // Comparison //////////////////////////////////////////////////////////////
1356     {
1357         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1358         EXPECT_EQ(out_mat_gapi.size(), sz_in);
1359     }
1360 }
1361
1362 TEST_P(ConvertToTest, AccuracyTest)
1363 {
1364     auto param = GetParam();
1365     int type_mat = std::get<0>(param);
1366     int depth_to = std::get<1>(param);
1367     cv::Size sz_in = std::get<2>(param);
1368     int type_out = CV_MAKETYPE(depth_to, CV_MAT_CN(type_mat));
1369     initMatrixRandU(type_mat, sz_in, type_out);
1370     auto compile_args = std::get<3>(GetParam());
1371
1372     // G-API code //////////////////////////////////////////////////////////////
1373     cv::GMat in;
1374     auto out = cv::gapi::convertTo(in, depth_to);
1375
1376     cv::GComputation c(in, out);
1377     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1378     // OpenCV code /////////////////////////////////////////////////////////////
1379     {
1380         in_mat1.convertTo(out_mat_ocv, depth_to);
1381     }
1382     // Comparison //////////////////////////////////////////////////////////////
1383     {
1384         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1385         EXPECT_EQ(out_mat_gapi.size(), sz_in);
1386     }
1387 }
1388
1389 TEST_P(PhaseTest, AccuracyTest)
1390 {
1391     int img_type = -1;
1392     cv::Size img_size;
1393     bool angle_in_degrees = false;
1394     cv::GCompileArgs compile_args;
1395     std::tie(img_type, img_size, angle_in_degrees, compile_args) = GetParam();
1396     initMatsRandU(img_type, img_size, img_type);
1397
1398     // G-API code //////////////////////////////////////////////////////////////
1399     cv::GMat in_x, in_y;
1400     auto out = cv::gapi::phase(in_x, in_y, angle_in_degrees);
1401
1402     cv::GComputation c(in_x, in_y, out);
1403     c.apply(in_mat1, in_mat2, out_mat_gapi, std::move(compile_args));
1404
1405     // OpenCV code /////////////////////////////////////////////////////////////
1406     cv::phase(in_mat1, in_mat2, out_mat_ocv, angle_in_degrees);
1407
1408     // Comparison //////////////////////////////////////////////////////////////
1409     // FIXME: use a comparison functor instead (after enabling OpenCL)
1410     {
1411         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1412     }
1413 }
1414
1415 TEST_P(SqrtTest, AccuracyTest)
1416 {
1417     int img_type = -1;
1418     cv::Size img_size;
1419     cv::GCompileArgs compile_args;
1420     std::tie(img_type, img_size, compile_args) = GetParam();
1421     initMatrixRandU(img_type, img_size, img_type);
1422
1423     // G-API code //////////////////////////////////////////////////////////////
1424     cv::GMat in;
1425     auto out = cv::gapi::sqrt(in);
1426
1427     cv::GComputation c(in, out);
1428     c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1429
1430     // OpenCV code /////////////////////////////////////////////////////////////
1431     cv::sqrt(in_mat1, out_mat_ocv);
1432
1433     // Comparison //////////////////////////////////////////////////////////////
1434     // FIXME: use a comparison functor instead (after enabling OpenCL)
1435     {
1436         EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1437     }
1438 }
1439
1440
1441 } // opencv_test
1442
1443 #endif //OPENCV_GAPI_CORE_TESTS_INL_HPP