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 #include "test_precomp.hpp"
15 cv::Mat diff(cv::Mat m1, cv::Mat m2, int t)
17 return cv::abs(m1-m2) > t;
20 int non_zero3(cv::Mat m3c)
22 std::vector<cv::Mat> mm(3);
24 return ( cv::countNonZero(mm[0])
25 + cv::countNonZero(mm[1])
26 + cv::countNonZero(mm[2]));
30 TEST(GAPI_Typed, UnaryOp)
32 // Initialization //////////////////////////////////////////////////////////
33 const cv::Size sz(32, 32);
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));
42 // Untyped G-API ///////////////////////////////////////////////////////////
43 cv::GComputation cvtU([]()
46 cv::GMat out = cv::gapi::RGB2YUV(in);
47 return cv::GComputation(in, out);
49 cvtU.apply(in_mat, out_mat_untyped);
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));
55 cvtT.apply(in_mat, out_mat_typed1);
56 cvtTComp(in_mat, out_mat_typed2);
58 // Plain OpenCV ////////////////////////////////////////////////////////////
59 cv::cvtColor(in_mat, out_mat_cv, cv::COLOR_RGB2YUV);
61 // Comparison //////////////////////////////////////////////////////////////
62 // FIXME: There must be OpenCV comparison test functions already available!
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);
68 EXPECT_EQ(0, non_zero3(diff_u));
69 EXPECT_EQ(0, non_zero3(diff_t));
70 EXPECT_EQ(0, non_zero3(diff_tc));
73 TEST(GAPI_Typed, BinaryOp)
75 // Initialization //////////////////////////////////////////////////////////
76 const cv::Size sz(32, 32);
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));
87 // Untyped G-API ///////////////////////////////////////////////////////////
88 cv::GComputation cvtU([]()
91 cv::GMat out = cv::gapi::add(in1, in2);
92 return cv::GComputation({in1, in2}, {out});
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);
98 // Typed G-API /////////////////////////////////////////////////////////////
99 cv::GComputationT<cv::GMat (cv::GMat, cv::GMat)> cvtT([](cv::GMat m1, cv::GMat m2)
103 auto cvtTC = cvtT.compile(cv::descr_of(in_mat1),
104 cv::descr_of(in_mat2));
106 cvtT.apply(in_mat1, in_mat2, out_mat_typed1);
107 cvtTC(in_mat1, in_mat2, out_mat_typed2);
109 // Plain OpenCV ////////////////////////////////////////////////////////////
110 cv::add(in_mat1, in_mat2, out_mat_cv);
112 // Comparison //////////////////////////////////////////////////////////////
113 // FIXME: There must be OpenCV comparison test functions already available!
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);
119 EXPECT_EQ(0, cv::countNonZero(diff_u));
120 EXPECT_EQ(0, cv::countNonZero(diff_t));
121 EXPECT_EQ(0, cv::countNonZero(diff_tc));
125 TEST(GAPI_Typed, MultipleOuts)
127 // Initialization //////////////////////////////////////////////////////////
128 const cv::Size sz(32, 32);
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));
141 // Untyped G-API ///////////////////////////////////////////////////////////
142 cv::GComputation cvtU([]()
145 cv::GMat out1 = in * 2.f;
146 cv::GMat out2 = in * 4.f;
147 return cv::GComputation({in}, {out1, out2});
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);
153 // Typed G-API /////////////////////////////////////////////////////////////
154 cv::GComputationT<std::tuple<cv::GMat, cv::GMat> (cv::GMat)> cvtT([](cv::GMat in)
156 return std::make_tuple(in*2.f, in*4.f);
158 auto cvtTC = cvtT.compile(cv::descr_of(in_mat));
160 cvtT.apply(in_mat, out_mat_typed1, out_mat_typed2);
161 cvtTC(in_mat, out_mat_comp1, out_mat_comp2);
163 // Plain OpenCV ////////////////////////////////////////////////////////////
164 out_mat_cv1 = in_mat * 2.f;
165 out_mat_cv2 = in_mat * 4.f;
167 // Comparison //////////////////////////////////////////////////////////////
168 // FIXME: There must be OpenCV comparison test functions already available!
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);
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));