Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / test / gapi_scalar_tests.cpp
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 #include "test_precomp.hpp"
9
10 #include <iostream>
11
12 namespace opencv_test
13 {
14
15 TEST(GAPI_Scalar, Argument)
16 {
17     cv::Size sz(2, 2);
18     cv::Mat in_mat(sz, CV_8U);
19     cv::randn(in_mat, cv::Scalar::all(127), cv::Scalar::all(40.f));
20
21     cv::GComputationT<cv::GMat (cv::GMat, cv::GScalar)> mulS([](cv::GMat in, cv::GScalar c)
22     {
23         return in*c;
24     });
25
26     cv::Mat out_mat(sz, CV_8U);
27     mulS.apply(in_mat, cv::Scalar(2), out_mat);
28
29     cv::Mat reference = in_mat*2;
30     EXPECT_EQ(0, cv::countNonZero(cv::abs(out_mat - reference)));
31 }
32
33 TEST(GAPI_Scalar, ReturnValue)
34 {
35     const cv::Size sz(2, 2);
36     cv::Mat in_mat(sz, CV_8U, cv::Scalar(1));
37
38     cv::GComputationT<cv::GScalar (cv::GMat)> sum_of_sum([](cv::GMat in)
39     {
40         return cv::gapi::sum(in + in);
41     });
42
43     cv::Scalar out;
44     sum_of_sum.apply(in_mat, out);
45
46     EXPECT_EQ(8, out[0]);
47 }
48
49 TEST(GAPI_Scalar, TmpScalar)
50 {
51     const cv::Size sz(2, 2);
52     cv::Mat in_mat(sz, CV_8U, cv::Scalar(1));
53
54     cv::GComputationT<cv::GMat (cv::GMat)> mul_by_sum([](cv::GMat in)
55     {
56         return in * cv::gapi::sum(in);
57     });
58
59     cv::Mat out_mat(sz, CV_8U);
60     mul_by_sum.apply(in_mat, out_mat);
61
62     cv::Mat reference = cv::Mat(sz, CV_8U, cv::Scalar(4));
63     EXPECT_EQ(0, cv::countNonZero(cv::abs(out_mat - reference)));
64 }
65
66 TEST(GAPI_ScalarWithValue, Simple_Arithmetic_Pipeline)
67 {
68     GMat in;
69     GMat out = (in + 1) * 2;
70     cv::GComputation comp(in, out);
71
72     cv::Mat in_mat  = cv::Mat::eye(3, 3, CV_8UC1);
73     cv::Mat ref_mat, out_mat;
74
75     ref_mat = (in_mat + 1) * 2;
76     comp.apply(in_mat, out_mat);
77
78     EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
79 }
80
81 TEST(GAPI_ScalarWithValue, GScalar_Initilization)
82 {
83     cv::Scalar sc(2);
84     cv::GMat in;
85     cv::GScalar s(sc);
86     cv::GComputation comp(in, cv::gapi::mulC(in, s));
87
88     cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);
89     cv::Mat ref_mat, out_mat;
90     cv::multiply(in_mat, sc, ref_mat, 1, CV_8UC1);
91     comp.apply(cv::gin(in_mat), cv::gout(out_mat));
92
93     EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
94 }
95
96 TEST(GAPI_ScalarWithValue, Constant_GScalar_In_Middle_Graph)
97 {
98     cv::Scalar  sc(5);
99     cv::GMat    in1;
100     cv::GScalar in2;
101     cv::GScalar s(sc);
102
103     auto add_out = cv::gapi::addC(in1, in2);
104     cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(cv::gapi::mulC(add_out, s)));
105
106     cv::Mat    in_mat = cv::Mat::eye(3, 3, CV_8UC1);
107     cv::Scalar in_scalar(3);
108
109     cv::Mat ref_mat, out_mat, add_mat;
110     cv::add(in_mat, in_scalar, add_mat);
111     cv::multiply(add_mat, sc, ref_mat, 1, CV_8UC1);
112     comp.apply(cv::gin(in_mat, in_scalar), cv::gout(out_mat));
113
114     EXPECT_EQ(0, cv::countNonZero(out_mat != ref_mat));
115 }
116
117 } // namespace opencv_test