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.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #ifndef OPENCV_GAPI_CORE_TESTS_INL_HPP
9 #define OPENCV_GAPI_CORE_TESTS_INL_HPP
11 #include "opencv2/gapi/core.hpp"
12 #include "gapi_core_tests.hpp"
17 TEST_P(MathOpTest, MatricesAccuracyTest )
20 int type = 0, dtype = 0;
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);
28 // G-API code & corresponding OpenCV code ////////////////////////////////
29 cv::GMat in1, in2, out;
37 out = cv::gapi::addC(in1, sc1, dtype);
38 cv::add(in_mat1, sc, out_mat_ocv, cv::noArray(), dtype);
45 out = cv::gapi::subRC(sc1, in1, dtype);
46 cv::subtract(sc, in_mat1, out_mat_ocv, cv::noArray(), dtype);
50 out = cv::gapi::subC(in1, sc1, dtype);
51 cv::subtract(in_mat1, sc, out_mat_ocv, cv::noArray(), dtype);
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);
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);
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);
81 FAIL() << "no such math operation type for scalar and matrix!";
84 cv::GComputation c(GIn(in1, sc1), GOut(out));
85 c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
93 out = cv::gapi::add(in1, in2, dtype);
94 cv::add(in_mat1, in_mat2, out_mat_ocv, cv::noArray(), dtype);
99 out = cv::gapi::sub(in1, in2, dtype);
100 cv::subtract(in_mat1, in_mat2, out_mat_ocv, cv::noArray(), dtype);
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);
112 out = cv::gapi::mul(in1, in2, scale, dtype);
113 cv::multiply(in_mat1, in_mat2, out_mat_ocv, scale, dtype);
118 FAIL() << "no such math operation type for matrix and matrix!";
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));
124 // Comparison //////////////////////////////////////////////////////////////
126 // TODO: make threshold vs bit-exact criteria be driven by testing parameter
128 if (CV_MAT_DEPTH(out_mat_ocv.type()) != CV_32F &&
129 CV_MAT_DEPTH(out_mat_ocv.type()) != CV_64F)
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);
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)));
143 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
145 EXPECT_EQ(out_mat_gapi.size(), sz);
149 TEST_P(MulDoubleTest, AccuracyTest)
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);
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);
162 // G-API code ////////////////////////////////////////////////////////////
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));
168 // OpenCV code ///////////////////////////////////////////////////////////
169 cv::multiply(in_mat1, d, out_mat_ocv, 1, dtype);
171 // Comparison ////////////////////////////////////////////////////////////
173 if (CV_MAT_DEPTH(out_mat_ocv.type()) != CV_32F &&
174 CV_MAT_DEPTH(out_mat_ocv.type()) != CV_64F)
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);
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)));
188 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
190 EXPECT_EQ(out_mat_gapi.size(), sz_in);
193 TEST_P(DivTest, DISABLED_DivByZeroTest) // https://github.com/opencv/opencv/pull/12826
195 int type = 0, dtype = 0;
197 bool initOut = false;
198 cv::GCompileArgs compile_args;
199 std::tie(type, sz_in, dtype, initOut, compile_args) = GetParam();
201 initMatrixRandU(type, sz_in, dtype, initOut);
202 in_mat2 = cv::Mat(sz_in, type);
203 in_mat2.setTo(cv::Scalar::all(0));
205 // G-API code //////////////////////////////////////////////////////////////
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));
211 // OpenCV code /////////////////////////////////////////////////////////////
213 cv::divide(in_mat1, in_mat2, out_mat_ocv, 1.0, dtype);
216 // Comparison //////////////////////////////////////////////////////////////
218 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
219 EXPECT_EQ(out_mat_gapi.size(), sz_in);
223 TEST_P(DivCTest, DISABLED_DivByZeroTest) // https://github.com/opencv/opencv/pull/12826
225 int type = 0, dtype = 0;
227 bool initOut = false;
228 cv::GCompileArgs compile_args;
229 std::tie(type, sz_in, dtype, initOut, compile_args) = GetParam();
231 initMatrixRandU(type, sz_in, dtype, initOut);
232 sc = cv::Scalar::all(0);
234 // G-API code //////////////////////////////////////////////////////////////
237 auto out = cv::gapi::divC(in1, sc1, dtype);
238 cv::GComputation c(GIn(in1, sc1), GOut(out));
240 c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
242 // OpenCV code /////////////////////////////////////////////////////////////
244 cv::divide(in_mat1, sc, out_mat_ocv, dtype);
247 // Comparison //////////////////////////////////////////////////////////////
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));
255 TEST_P(MeanTest, AccuracyTest)
258 bool initOut = false;
260 cv::GCompileArgs compile_args;
261 std::tie(type, sz_in, initOut, compile_args) = GetParam();
262 initMatrixRandU(type, sz_in, initOut);
264 cv::Scalar out_norm_ocv;
266 // G-API code //////////////////////////////////////////////////////////////
268 auto out = cv::gapi::mean(in);
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 /////////////////////////////////////////////////////////////
274 out_norm_ocv = cv::mean(in_mat1);
276 // Comparison //////////////////////////////////////////////////////////////
278 EXPECT_EQ(out_norm[0], out_norm_ocv[0]);
282 TEST_P(MaskTest, AccuracyTest)
285 bool initOut = false;
287 cv::GCompileArgs compile_args;
288 std::tie(type, sz_in, initOut, compile_args) = GetParam();
289 initMatrixRandU(type, sz_in, type, initOut);
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;
295 // G-API code //////////////////////////////////////////////////////////////
297 auto out = cv::gapi::mask(in, m);
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 /////////////////////////////////////////////////////////////
303 out_mat_ocv = cv::Mat::zeros(in_mat1.size(), in_mat1.type());
304 in_mat1.copyTo(out_mat_ocv, in_mat2);
306 // Comparison //////////////////////////////////////////////////////////////
308 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
312 TEST_P(Polar2CartTest, AccuracyTest)
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));
320 cv::Mat out_mat_ocv2;
321 if(std::get<1>(param) == true)
323 out_mat2 = cv::Mat(sz_in, CV_32FC1);
324 out_mat_ocv2 = cv::Mat(sz_in, CV_32FC1);
327 // G-API code //////////////////////////////////////////////////////////////
328 cv::GMat in1, in2, out1, out2;
329 std::tie(out1, out2) = cv::gapi::polarToCart(in1, in2);
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 /////////////////////////////////////////////////////////////
335 cv::polarToCart(in_mat1, in_mat2, out_mat_ocv, out_mat_ocv2);
337 // Comparison //////////////////////////////////////////////////////////////
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.
342 // Relative threshold like 1e-6 is very restrictive, nearly best we can
343 // expect of single-precision elementary functions implementation.
345 // However, good idea is making such threshold configurable: parameter
346 // of this test - which a specific test istantiation could setup.
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.
351 // TODO: Make threshold a configurable parameter of this test (ADE-221)
353 cv::Mat &outx = out_mat_gapi,
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);
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);
368 TEST_P(Cart2PolarTest, AccuracyTest)
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));
375 cv::Mat out_mat2(sz_in, CV_32FC1);
376 cv::Mat out_mat_ocv2(sz_in, CV_32FC1);
378 // G-API code //////////////////////////////////////////////////////////////
379 cv::GMat in1, in2, out1, out2;
380 std::tie(out1, out2) = cv::gapi::cartToPolar(in1, in2);
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 /////////////////////////////////////////////////////////////
386 cv::cartToPolar(in_mat1, in_mat2, out_mat_ocv, out_mat_ocv2);
388 // Comparison //////////////////////////////////////////////////////////////
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.
393 // Relative threshold like 1e-6 is very restrictive, nearly best we can
394 // expect of single-precision elementary functions implementation.
396 // However, good idea is making such threshold configurable: parameter
397 // of this test - which a specific test istantiation could setup.
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.
402 // TODO: Make threshold a configurable parameter of this test (ADE-221)
404 cv::Mat &outm = out_mat_gapi,
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);
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);
421 TEST_P(CmpTest, AccuracyTest)
423 CmpTypes opType = CMP_EQ;
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);
431 // G-API code & corresponding OpenCV code ////////////////////////////////
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!";
447 cv::compare(in_mat1, sc, out_mat_ocv, opType);
449 cv::GComputation c(GIn(in1, in2), GOut(out));
450 c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
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!";
466 cv::compare(in_mat1, in_mat2, out_mat_ocv, opType);
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));
472 // Comparison //////////////////////////////////////////////////////////////
474 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
475 EXPECT_EQ(out_mat_gapi.size(), sz);
479 TEST_P(BitwiseTest, AccuracyTest)
481 bitwiseOp opType = AND;
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);
489 // G-API code & corresponding OpenCV code ////////////////////////////////
490 cv::GMat in1, in2, out;
495 out = cv::gapi::bitwise_and(in1, in2);
496 cv::bitwise_and(in_mat1, in_mat2, out_mat_ocv);
501 out = cv::gapi::bitwise_or(in1, in2);
502 cv::bitwise_or(in_mat1, in_mat2, out_mat_ocv);
507 out = cv::gapi::bitwise_xor(in1, in2);
508 cv::bitwise_xor(in_mat1, in_mat2, out_mat_ocv);
513 FAIL() << "no such bitwise operation type!";
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));
519 // Comparison //////////////////////////////////////////////////////////////
521 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
522 EXPECT_EQ(out_mat_gapi.size(), sz);
526 TEST_P(NotTest, AccuracyTest)
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));
533 // G-API code //////////////////////////////////////////////////////////////
535 auto out = cv::gapi::bitwise_not(in);
536 cv::GComputation c(in, out);
538 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
540 // OpenCV code /////////////////////////////////////////////////////////////
542 cv::bitwise_not(in_mat1, out_mat_ocv);
544 // Comparison //////////////////////////////////////////////////////////////
546 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
547 EXPECT_EQ(out_mat_gapi.size(), sz_in);
551 TEST_P(SelectTest, AccuracyTest)
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));
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));
566 c.apply(gin(in_mat1, in_mat2, in_mask), gout(out_mat_gapi), std::move(compile_args));
568 // OpenCV code /////////////////////////////////////////////////////////////
570 in_mat2.copyTo(out_mat_ocv);
571 in_mat1.copyTo(out_mat_ocv, in_mask);
573 // Comparison //////////////////////////////////////////////////////////////
575 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
576 EXPECT_EQ(out_mat_gapi.size(), sz_in);
580 TEST_P(MinTest, AccuracyTest)
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));
587 // G-API code //////////////////////////////////////////////////////////////
589 auto out = cv::gapi::min(in1, in2);
590 cv::GComputation c(GIn(in1, in2), GOut(out));
592 c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
594 // OpenCV code /////////////////////////////////////////////////////////////
596 cv::min(in_mat1, in_mat2, out_mat_ocv);
598 // Comparison //////////////////////////////////////////////////////////////
600 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
601 EXPECT_EQ(out_mat_gapi.size(), sz_in);
605 TEST_P(MaxTest, AccuracyTest)
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));
612 // G-API code //////////////////////////////////////////////////////////////
614 auto out = cv::gapi::max(in1, in2);
615 cv::GComputation c(GIn(in1, in2), GOut(out));
617 c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
619 // OpenCV code /////////////////////////////////////////////////////////////
621 cv::max(in_mat1, in_mat2, out_mat_ocv);
623 // Comparison //////////////////////////////////////////////////////////////
625 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
626 EXPECT_EQ(out_mat_gapi.size(), sz_in);
630 TEST_P(AbsDiffTest, AccuracyTest)
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));
637 // G-API code //////////////////////////////////////////////////////////////
639 auto out = cv::gapi::absDiff(in1, in2);
640 cv::GComputation c(GIn(in1, in2), GOut(out));
642 c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
644 // OpenCV code /////////////////////////////////////////////////////////////
646 cv::absdiff(in_mat1, in_mat2, out_mat_ocv);
648 // Comparison //////////////////////////////////////////////////////////////
650 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
651 EXPECT_EQ(out_mat_gapi.size(), sz_in);
655 TEST_P(AbsDiffCTest, AccuracyTest)
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));
662 // G-API code //////////////////////////////////////////////////////////////
665 auto out = cv::gapi::absDiffC(in1, sc1);
666 cv::GComputation c(cv::GIn(in1, sc1), cv::GOut(out));
668 c.apply(gin(in_mat1, sc), gout(out_mat_gapi), std::move(compile_args));
670 // OpenCV code /////////////////////////////////////////////////////////////
672 cv::absdiff(in_mat1, sc, out_mat_ocv);
674 // Comparison //////////////////////////////////////////////////////////////
676 EXPECT_EQ(0, cv::countNonZero(out_mat_gapi != out_mat_ocv));
677 EXPECT_EQ(out_mat_gapi.size(), sz_in);
681 TEST_P(SumTest, AccuracyTest)
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));
692 cv::Scalar out_sum_ocv;
694 // G-API code //////////////////////////////////////////////////////////////
696 auto out = cv::gapi::sum(in);
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 /////////////////////////////////////////////////////////////
702 out_sum_ocv = cv::sum(in_mat1);
704 // Comparison //////////////////////////////////////////////////////////////
706 EXPECT_TRUE(cmpF(out_sum, out_sum_ocv));
710 TEST_P(AddWeightedTest, AccuracyTest)
712 int type = 0, dtype = 0;
714 bool initOut = false;
715 cv::GCompileArgs compile_args;
717 std::tie(type, sz_in, dtype, initOut, cmpF, compile_args) = GetParam();
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);
725 // G-API code //////////////////////////////////////////////////////////////
727 auto out = cv::gapi::addWeighted(in1, alpha, in2, beta, gamma, dtype);
728 cv::GComputation c(GIn(in1, in2), GOut(out));
730 c.apply(gin(in_mat1, in_mat2), gout(out_mat_gapi), std::move(compile_args));
732 // OpenCV code /////////////////////////////////////////////////////////////
734 cv::addWeighted(in_mat1, alpha, in_mat2, beta, gamma, out_mat_ocv, dtype);
736 // Comparison //////////////////////////////////////////////////////////////
737 EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
738 EXPECT_EQ(out_mat_gapi.size(), sz_in);
742 TEST_P(NormTest, AccuracyTest)
744 compare_scalar_f cmpF;
745 NormTypes opType = NORM_INF;
748 cv::GCompileArgs compile_args;
749 std::tie(opType, type, sz, cmpF, compile_args) = GetParam();
750 initMatrixRandU(type, sz, type, false);
753 cv::Scalar out_norm_ocv;
755 // G-API code & corresponding OpenCV code ////////////////////////////////
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!";
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));
769 // Comparison //////////////////////////////////////////////////////////////
771 EXPECT_TRUE(cmpF(out_norm, out_norm_ocv));
775 TEST_P(IntegralTest, AccuracyTest)
777 int type = std::get<0>(GetParam());
778 cv::Size sz_in = std::get<1>(GetParam());
779 auto compile_args = std::get<2>(GetParam());
781 int type_out = (type == CV_8U) ? CV_32SC1 : CV_64FC1;
782 cv::Mat in_mat1(sz_in, type);
784 cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
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);
790 cv::Mat out_mat2(sz_out, CV_64FC1);
791 cv::Mat out_mat_ocv2(sz_out, CV_64FC1);
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));
798 c.apply(cv::gin(in_mat1), cv::gout(out_mat1, out_mat2), std::move(compile_args));
800 // OpenCV code /////////////////////////////////////////////////////////////
802 cv::integral(in_mat1, out_mat_ocv1, out_mat_ocv2);
804 // Comparison //////////////////////////////////////////////////////////////
806 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv1 != out_mat1));
807 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv2 != out_mat2));
811 TEST_P(ThresholdTest, AccuracyTestBinary)
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);
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;
824 // G-API code //////////////////////////////////////////////////////////////
826 cv::GScalar th1, mv1;
827 out = cv::gapi::threshold(in1, th1, mv1, tt);
828 cv::GComputation c(GIn(in1, th1, mv1), GOut(out));
830 c.apply(gin(in_mat1, thr, maxval), gout(out_mat_gapi), std::move(compile_args));
832 // OpenCV code /////////////////////////////////////////////////////////////
834 cv::threshold(in_mat1, out_mat_ocv, thr.val[0], maxval.val[0], tt);
836 // Comparison //////////////////////////////////////////////////////////////
838 ASSERT_EQ(out_mat_gapi.size(), sz_in);
839 EXPECT_EQ(0, cv::norm(out_mat_ocv, out_mat_gapi, NORM_L1));
843 TEST_P(ThresholdOTTest, AccuracyTestOtsu)
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);
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;
856 // G-API code //////////////////////////////////////////////////////////////
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));
862 c.apply(gin(in_mat1, maxval), gout(out_mat_gapi, out_gapi_scalar), std::move(compile_args));
864 // OpenCV code /////////////////////////////////////////////////////////////
866 ocv_res = cv::threshold(in_mat1, out_mat_ocv, maxval.val[0], maxval.val[0], tt);
868 // Comparison //////////////////////////////////////////////////////////////
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]);
876 TEST_P(InRangeTest, AccuracyTest)
878 auto param = GetParam();
879 int type = std::get<0>(param);
880 cv::Size sz_in = std::get<1>(param);
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));
887 // G-API code //////////////////////////////////////////////////////////////
889 cv::GScalar th1, mv1;
890 auto out = cv::gapi::inRange(in1, th1, mv1);
891 cv::GComputation c(GIn(in1, th1, mv1), GOut(out));
893 c.apply(gin(in_mat1, thrLow, thrUp), gout(out_mat_gapi), std::move(compile_args));
895 // OpenCV code /////////////////////////////////////////////////////////////
897 cv::inRange(in_mat1, thrLow, thrUp, out_mat_ocv);
899 // Comparison //////////////////////////////////////////////////////////////
901 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
902 EXPECT_EQ(out_mat_gapi.size(), sz_in);
906 TEST_P(Split3Test, AccuracyTest)
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);
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));
921 c.apply(cv::gin(in_mat1), cv::gout(out_mat_gapi, out_mat2, out_mat3), std::move(compile_args));
922 // OpenCV code /////////////////////////////////////////////////////////////
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);
927 // Comparison //////////////////////////////////////////////////////////////
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));
935 TEST_P(Split4Test, AccuracyTest)
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);
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));
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 /////////////////////////////////////////////////////////////
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);
958 // Comparison //////////////////////////////////////////////////////////////
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));
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)
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);
973 cv::randn(in_mat1, mean, stddev);
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))
978 cv::Mat out_mat(out_mat_sz, type);
979 cv::Mat out_mat_ocv(out_mat_sz, type);
981 // G-API code //////////////////////////////////////////////////////////////
983 auto out = cv::gapi::resize(in, sz_out, fx, fy, interp);
985 cv::GComputation c(in, out);
986 c.apply(in_mat1, out_mat, std::move(compile_args));
987 // OpenCV code /////////////////////////////////////////////////////////////
989 cv::resize(in_mat1, out_mat_ocv, sz_out, fx, fy, interp);
991 // Comparison //////////////////////////////////////////////////////////////
993 EXPECT_TRUE(cmpF(out_mat, out_mat_ocv));
997 TEST_P(ResizeTest, AccuracyTest)
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));
1007 TEST_P(ResizeTestFxFy, AccuracyTest)
1010 int type = 0, interp = 0;
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));
1018 TEST_P(Merge3Test, AccuracyTest)
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);
1027 cv::randn(in_mat3, mean, stddev);
1029 // G-API code //////////////////////////////////////////////////////////////
1030 cv::GMat in1, in2, in3;
1031 auto out = cv::gapi::merge3(in1, in2, in3);
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 /////////////////////////////////////////////////////////////
1037 std::vector<cv::Mat> in_mats_ocv = {in_mat1, in_mat2, in_mat3};
1038 cv::merge(in_mats_ocv, out_mat_ocv);
1040 // Comparison //////////////////////////////////////////////////////////////
1042 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1046 TEST_P(Merge4Test, AccuracyTest)
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);
1056 cv::randn(in_mat3, mean, stddev);
1057 cv::randn(in_mat4, mean, stddev);
1059 // G-API code //////////////////////////////////////////////////////////////
1060 cv::GMat in1, in2, in3, in4;
1061 auto out = cv::gapi::merge4(in1, in2, in3, in4);
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 /////////////////////////////////////////////////////////////
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);
1070 // Comparison //////////////////////////////////////////////////////////////
1072 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1076 TEST_P(RemapTest, AccuracyTest)
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();
1088 // G-API code //////////////////////////////////////////////////////////////
1090 auto out = cv::gapi::remap(in1, in_map1, in_map2, cv::INTER_NEAREST, cv::BORDER_REPLICATE, bv);
1091 cv::GComputation c(in1, out);
1093 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1095 // OpenCV code /////////////////////////////////////////////////////////////
1097 cv::remap(in_mat1, out_mat_ocv, in_map1, in_map2, cv::INTER_NEAREST, cv::BORDER_REPLICATE, bv);
1099 // Comparison //////////////////////////////////////////////////////////////
1101 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1102 EXPECT_EQ(out_mat_gapi.size(), sz_in);
1106 TEST_P(FlipTest, AccuracyTest)
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());
1115 // G-API code //////////////////////////////////////////////////////////////
1117 auto out = cv::gapi::flip(in, flipCode);
1119 cv::GComputation c(in, out);
1120 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1121 // OpenCV code /////////////////////////////////////////////////////////////
1123 cv::flip(in_mat1, out_mat_ocv, flipCode);
1125 // Comparison //////////////////////////////////////////////////////////////
1127 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1128 EXPECT_EQ(out_mat_gapi.size(), sz_in);
1132 TEST_P(CropTest, AccuracyTest)
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);
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 )
1144 out_mat_gapi = cv::Mat(sz_out, type);
1145 out_mat_ocv = cv::Mat(sz_out, type);
1148 // G-API code //////////////////////////////////////////////////////////////
1150 auto out = cv::gapi::crop(in, rect_to);
1152 cv::GComputation c(in, out);
1153 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1154 // OpenCV code /////////////////////////////////////////////////////////////
1156 cv::Mat(in_mat1, rect_to).copyTo(out_mat_ocv);
1158 // Comparison //////////////////////////////////////////////////////////////
1160 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1161 EXPECT_EQ(out_mat_gapi.size(), sz_out);
1165 TEST_P(ConcatHorTest, AccuracyTest)
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);
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);
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);
1181 cv::randn(in_mat1, mean, stddev);
1182 cv::randn(in_mat2, mean, stddev);
1184 cv::Mat out_mat(sz_out, type);
1185 cv::Mat out_mat_ocv(sz_out, type);
1187 // G-API code //////////////////////////////////////////////////////////////
1189 auto out = cv::gapi::concatHor(in1, in2);
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 /////////////////////////////////////////////////////////////
1195 cv::hconcat(in_mat1, in_mat2, out_mat_ocv );
1197 // Comparison //////////////////////////////////////////////////////////////
1199 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1203 TEST_P(ConcatVertTest, AccuracyTest)
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);
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);
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);
1219 cv::randn(in_mat1, mean, stddev);
1220 cv::randn(in_mat2, mean, stddev);
1222 cv::Mat out_mat(sz_out, type);
1223 cv::Mat out_mat_ocv(sz_out, type);
1225 // G-API code //////////////////////////////////////////////////////////////
1227 auto out = cv::gapi::concatVert(in1, in2);
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 /////////////////////////////////////////////////////////////
1233 cv::vconcat(in_mat1, in_mat2, out_mat_ocv );
1235 // Comparison //////////////////////////////////////////////////////////////
1237 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1241 TEST_P(ConcatVertVecTest, AccuracyTest)
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);
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);
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);
1260 cv::randn(in_mat1, mean, stddev);
1261 cv::randn(in_mat2, mean, stddev);
1262 cv::randn(in_mat3, mean, stddev);
1264 cv::Mat out_mat(sz_out, type);
1265 cv::Mat out_mat_ocv(sz_out, type);
1267 // G-API code //////////////////////////////////////////////////////////////
1268 std::vector <cv::GMat> mats(3);
1269 auto out = cv::gapi::concatVert(mats);
1271 std::vector <cv::Mat> cvmats = {in_mat1, in_mat2, in_mat3};
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));
1276 // OpenCV code /////////////////////////////////////////////////////////////
1278 cv::vconcat(cvmats, out_mat_ocv );
1280 // Comparison //////////////////////////////////////////////////////////////
1282 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1286 TEST_P(ConcatHorVecTest, AccuracyTest)
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);
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);
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);
1305 cv::randn(in_mat1, mean, stddev);
1306 cv::randn(in_mat2, mean, stddev);
1307 cv::randn(in_mat3, mean, stddev);
1309 cv::Mat out_mat(sz_out, type);
1310 cv::Mat out_mat_ocv(sz_out, type);
1312 // G-API code //////////////////////////////////////////////////////////////
1313 std::vector <cv::GMat> mats(3);
1314 auto out = cv::gapi::concatHor(mats);
1316 std::vector <cv::Mat> cvmats = {in_mat1, in_mat2, in_mat3};
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));
1321 // OpenCV code /////////////////////////////////////////////////////////////
1323 cv::hconcat(cvmats, out_mat_ocv );
1325 // Comparison //////////////////////////////////////////////////////////////
1327 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat));
1331 TEST_P(LUTTest, AccuracyTest)
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());
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));
1345 // G-API code //////////////////////////////////////////////////////////////
1347 auto out = cv::gapi::LUT(in, in_lut);
1349 cv::GComputation c(in, out);
1350 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1351 // OpenCV code /////////////////////////////////////////////////////////////
1353 cv::LUT(in_mat1, in_lut, out_mat_ocv);
1355 // Comparison //////////////////////////////////////////////////////////////
1357 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1358 EXPECT_EQ(out_mat_gapi.size(), sz_in);
1362 TEST_P(ConvertToTest, AccuracyTest)
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());
1372 // G-API code //////////////////////////////////////////////////////////////
1374 auto out = cv::gapi::convertTo(in, depth_to);
1376 cv::GComputation c(in, out);
1377 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1378 // OpenCV code /////////////////////////////////////////////////////////////
1380 in_mat1.convertTo(out_mat_ocv, depth_to);
1382 // Comparison //////////////////////////////////////////////////////////////
1384 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1385 EXPECT_EQ(out_mat_gapi.size(), sz_in);
1389 TEST_P(PhaseTest, AccuracyTest)
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);
1398 // G-API code //////////////////////////////////////////////////////////////
1399 cv::GMat in_x, in_y;
1400 auto out = cv::gapi::phase(in_x, in_y, angle_in_degrees);
1402 cv::GComputation c(in_x, in_y, out);
1403 c.apply(in_mat1, in_mat2, out_mat_gapi, std::move(compile_args));
1405 // OpenCV code /////////////////////////////////////////////////////////////
1406 cv::phase(in_mat1, in_mat2, out_mat_ocv, angle_in_degrees);
1408 // Comparison //////////////////////////////////////////////////////////////
1409 // FIXME: use a comparison functor instead (after enabling OpenCL)
1411 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1415 TEST_P(SqrtTest, AccuracyTest)
1419 cv::GCompileArgs compile_args;
1420 std::tie(img_type, img_size, compile_args) = GetParam();
1421 initMatrixRandU(img_type, img_size, img_type);
1423 // G-API code //////////////////////////////////////////////////////////////
1425 auto out = cv::gapi::sqrt(in);
1427 cv::GComputation c(in, out);
1428 c.apply(in_mat1, out_mat_gapi, std::move(compile_args));
1430 // OpenCV code /////////////////////////////////////////////////////////////
1431 cv::sqrt(in_mat1, out_mat_ocv);
1433 // Comparison //////////////////////////////////////////////////////////////
1434 // FIXME: use a comparison functor instead (after enabling OpenCL)
1436 EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi));
1443 #endif //OPENCV_GAPI_CORE_TESTS_INL_HPP