Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / test / gapi_typed_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 namespace opencv_test
11 {
12
13 namespace
14 {
15     cv::Mat diff(cv::Mat m1, cv::Mat m2, int t)
16     {
17         return cv::abs(m1-m2) > t;
18     }
19
20     int non_zero3(cv::Mat m3c)
21     {
22         std::vector<cv::Mat> mm(3);
23         cv::split(m3c, mm);
24         return (  cv::countNonZero(mm[0])
25                 + cv::countNonZero(mm[1])
26                 + cv::countNonZero(mm[2]));
27     }
28 }
29
30 TEST(GAPI_Typed, UnaryOp)
31 {
32     // Initialization //////////////////////////////////////////////////////////
33     const cv::Size sz(32, 32);
34     cv::Mat
35         in_mat         (sz, CV_8UC3),
36         out_mat_untyped(sz, CV_8UC3),
37         out_mat_typed1 (sz, CV_8UC3),
38         out_mat_typed2 (sz, CV_8UC3),
39         out_mat_cv     (sz, CV_8UC3);
40     cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255));
41
42     // Untyped G-API ///////////////////////////////////////////////////////////
43     cv::GComputation cvtU([]()
44     {
45         cv::GMat in;
46         cv::GMat out = cv::gapi::RGB2YUV(in);
47         return cv::GComputation(in, out);
48     });
49     cvtU.apply(in_mat, out_mat_untyped);
50
51     // Typed G-API /////////////////////////////////////////////////////////////
52     cv::GComputationT<cv::GMat (cv::GMat)> cvtT(cv::gapi::RGB2YUV);
53     auto cvtTComp = cvtT.compile(cv::descr_of(in_mat));
54
55     cvtT.apply(in_mat, out_mat_typed1);
56     cvtTComp(in_mat, out_mat_typed2);
57
58     // Plain OpenCV ////////////////////////////////////////////////////////////
59     cv::cvtColor(in_mat, out_mat_cv, cv::COLOR_RGB2YUV);
60
61     // Comparison //////////////////////////////////////////////////////////////
62     // FIXME: There must be OpenCV comparison test functions already available!
63     cv::Mat
64         diff_u  = diff(out_mat_cv, out_mat_untyped, 0),
65         diff_t  = diff(out_mat_cv, out_mat_typed1,  0),
66         diff_tc = diff(out_mat_cv, out_mat_typed2,  0);
67
68     EXPECT_EQ(0, non_zero3(diff_u));
69     EXPECT_EQ(0, non_zero3(diff_t));
70     EXPECT_EQ(0, non_zero3(diff_tc));
71 }
72
73 TEST(GAPI_Typed, BinaryOp)
74 {
75     // Initialization //////////////////////////////////////////////////////////
76     const cv::Size sz(32, 32);
77     cv::Mat
78         in_mat1        (sz, CV_8UC1),
79         in_mat2        (sz, CV_8UC1),
80         out_mat_untyped(sz, CV_8UC1),
81         out_mat_typed1 (sz, CV_8UC1),
82         out_mat_typed2 (sz, CV_8UC1),
83         out_mat_cv     (sz, CV_8UC1);
84     cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
85     cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
86
87     // Untyped G-API ///////////////////////////////////////////////////////////
88     cv::GComputation cvtU([]()
89     {
90         cv::GMat in1, in2;
91         cv::GMat out = cv::gapi::add(in1, in2);
92         return cv::GComputation({in1, in2}, {out});
93     });
94     std::vector<cv::Mat> u_ins  = {in_mat1, in_mat2};
95     std::vector<cv::Mat> u_outs = {out_mat_untyped};
96     cvtU.apply(u_ins, u_outs);
97
98     // Typed G-API /////////////////////////////////////////////////////////////
99     cv::GComputationT<cv::GMat (cv::GMat, cv::GMat)> cvtT([](cv::GMat m1, cv::GMat m2)
100     {
101         return m1+m2;
102     });
103     auto cvtTC =  cvtT.compile(cv::descr_of(in_mat1),
104                                cv::descr_of(in_mat2));
105
106     cvtT.apply(in_mat1, in_mat2, out_mat_typed1);
107     cvtTC(in_mat1, in_mat2, out_mat_typed2);
108
109     // Plain OpenCV ////////////////////////////////////////////////////////////
110     cv::add(in_mat1, in_mat2, out_mat_cv);
111
112     // Comparison //////////////////////////////////////////////////////////////
113     // FIXME: There must be OpenCV comparison test functions already available!
114     cv::Mat
115         diff_u  = diff(out_mat_cv, out_mat_untyped, 0),
116         diff_t  = diff(out_mat_cv, out_mat_typed1,  0),
117         diff_tc = diff(out_mat_cv, out_mat_typed2,  0);
118
119     EXPECT_EQ(0, cv::countNonZero(diff_u));
120     EXPECT_EQ(0, cv::countNonZero(diff_t));
121     EXPECT_EQ(0, cv::countNonZero(diff_tc));
122 }
123
124
125 TEST(GAPI_Typed, MultipleOuts)
126 {
127     // Initialization //////////////////////////////////////////////////////////
128     const cv::Size sz(32, 32);
129     cv::Mat
130         in_mat        (sz, CV_8UC1),
131         out_mat_unt1  (sz, CV_8UC1),
132         out_mat_unt2  (sz, CV_8UC1),
133         out_mat_typed1(sz, CV_8UC1),
134         out_mat_typed2(sz, CV_8UC1),
135         out_mat_comp1 (sz, CV_8UC1),
136         out_mat_comp2 (sz, CV_8UC1),
137         out_mat_cv1   (sz, CV_8UC1),
138         out_mat_cv2   (sz, CV_8UC1);
139     cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255));
140
141     // Untyped G-API ///////////////////////////////////////////////////////////
142     cv::GComputation cvtU([]()
143     {
144         cv::GMat in;
145         cv::GMat out1 = in * 2.f;
146         cv::GMat out2 = in * 4.f;
147         return cv::GComputation({in}, {out1, out2});
148     });
149     std::vector<cv::Mat> u_ins  = {in_mat};
150     std::vector<cv::Mat> u_outs = {out_mat_unt1, out_mat_unt2};
151     cvtU.apply(u_ins, u_outs);
152
153     // Typed G-API /////////////////////////////////////////////////////////////
154     cv::GComputationT<std::tuple<cv::GMat, cv::GMat> (cv::GMat)> cvtT([](cv::GMat in)
155     {
156         return std::make_tuple(in*2.f, in*4.f);
157     });
158     auto cvtTC =  cvtT.compile(cv::descr_of(in_mat));
159
160     cvtT.apply(in_mat, out_mat_typed1, out_mat_typed2);
161     cvtTC(in_mat, out_mat_comp1, out_mat_comp2);
162
163     // Plain OpenCV ////////////////////////////////////////////////////////////
164     out_mat_cv1 = in_mat * 2.f;
165     out_mat_cv2 = in_mat * 4.f;
166
167     // Comparison //////////////////////////////////////////////////////////////
168     // FIXME: There must be OpenCV comparison test functions already available!
169     cv::Mat
170         diff_u1 = diff(out_mat_cv1, out_mat_unt1,   0),
171         diff_u2 = diff(out_mat_cv2, out_mat_unt2,   0),
172         diff_t1 = diff(out_mat_cv1, out_mat_typed1, 0),
173         diff_t2 = diff(out_mat_cv2, out_mat_typed2, 0),
174         diff_c1 = diff(out_mat_cv1, out_mat_comp1,  0),
175         diff_c2 = diff(out_mat_cv2, out_mat_comp2,  0);
176
177     EXPECT_EQ(0, cv::countNonZero(diff_u1));
178     EXPECT_EQ(0, cv::countNonZero(diff_u2));
179     EXPECT_EQ(0, cv::countNonZero(diff_t1));
180     EXPECT_EQ(0, cv::countNonZero(diff_t2));
181     EXPECT_EQ(0, cv::countNonZero(diff_c1));
182     EXPECT_EQ(0, cv::countNonZero(diff_c2));
183 }
184
185 } // opencv_test