gapi_sample_pipelines_s11n.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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) 2020 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #include <ade/util/iota_range.hpp>
  8. #include <opencv2/gapi/s11n.hpp>
  9. #include "api/render_priv.hpp"
  10. #include "../common/gapi_render_tests.hpp"
  11. namespace opencv_test
  12. {
  13. TEST(S11N, Pipeline_Crop_Rect)
  14. {
  15. cv::Rect rect_to{ 4,10,37,50 };
  16. cv::Size sz_in = cv::Size(1920, 1080);
  17. cv::Size sz_out = cv::Size(37, 50);
  18. cv::Mat in_mat = cv::Mat::eye(sz_in, CV_8UC1);
  19. cv::Mat out_mat_gapi(sz_out, CV_8UC1);
  20. cv::Mat out_mat_ocv(sz_out, CV_8UC1);
  21. // G-API code //////////////////////////////////////////////////////////////
  22. cv::GMat in;
  23. auto out = cv::gapi::crop(in, rect_to);
  24. auto p = cv::gapi::serialize(cv::GComputation(in, out));
  25. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  26. c.apply(in_mat, out_mat_gapi);
  27. // OpenCV code /////////////////////////////////////////////////////////////
  28. {
  29. out_mat_ocv = in_mat(rect_to);
  30. }
  31. // Comparison //////////////////////////////////////////////////////////////
  32. {
  33. EXPECT_EQ(0, cvtest::norm(out_mat_ocv, out_mat_gapi, NORM_INF));
  34. }
  35. }
  36. TEST(S11N, Pipeline_Canny_Bool)
  37. {
  38. const cv::Size sz_in(1280, 720);
  39. cv::GMat in;
  40. double thrLow = 120.0;
  41. double thrUp = 240.0;
  42. int apSize = 5;
  43. bool l2gr = true;
  44. cv::Mat in_mat = cv::Mat::eye(1280, 720, CV_8UC1);
  45. cv::Mat out_mat_gapi(sz_in, CV_8UC1);
  46. cv::Mat out_mat_ocv(sz_in, CV_8UC1);
  47. // G-API code //////////////////////////////////////////////////////////////
  48. auto out = cv::gapi::Canny(in, thrLow, thrUp, apSize, l2gr);
  49. auto p = cv::gapi::serialize(cv::GComputation(in, out));
  50. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  51. c.apply(in_mat, out_mat_gapi);
  52. // OpenCV code /////////////////////////////////////////////////////////////
  53. {
  54. cv::Canny(in_mat, out_mat_ocv, thrLow, thrUp, apSize, l2gr);
  55. }
  56. // Comparison //////////////////////////////////////////////////////////////
  57. EXPECT_EQ(0, cvtest::norm(out_mat_gapi, out_mat_ocv, NORM_INF));
  58. }
  59. TEST(S11N, Pipeline_Not)
  60. {
  61. cv::GMat in;
  62. auto p = cv::gapi::serialize(cv::GComputation(in, cv::gapi::bitwise_not(in)));
  63. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  64. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  65. cv::Mat ref_mat = ~in_mat;
  66. cv::Mat out_mat;
  67. c.apply(in_mat, out_mat);
  68. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  69. out_mat = cv::Mat();
  70. auto cc = c.compile(cv::descr_of(in_mat));
  71. cc(in_mat, out_mat);
  72. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  73. }
  74. TEST(S11N, Pipeline_Sum_Scalar)
  75. {
  76. cv::GMat in;
  77. auto p = cv::gapi::serialize(cv::GComputation(in, cv::gapi::sum(in)));
  78. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  79. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  80. cv::Scalar ref_scl = cv::sum(in_mat);
  81. cv::Scalar out_scl;
  82. c.apply(in_mat, out_scl);
  83. EXPECT_EQ(out_scl, ref_scl);
  84. out_scl = cv::Scalar();
  85. auto cc = c.compile(cv::descr_of(in_mat));
  86. cc(in_mat, out_scl);
  87. EXPECT_EQ(out_scl, ref_scl);
  88. }
  89. TEST(S11N, Pipeline_BinaryOp)
  90. {
  91. cv::GMat a, b;
  92. auto p = cv::gapi::serialize(cv::GComputation(a, b, cv::gapi::add(a, b)));
  93. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  94. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  95. cv::Mat ref_mat = (in_mat + in_mat);
  96. cv::Mat out_mat;
  97. c.apply(in_mat, in_mat, out_mat);
  98. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  99. out_mat = cv::Mat();
  100. auto cc = c.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));
  101. cc(in_mat, in_mat, out_mat);
  102. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  103. }
  104. TEST(S11N, Pipeline_Binary_Sum_Scalar)
  105. {
  106. cv::GMat a, b;
  107. auto p = cv::gapi::serialize(cv::GComputation(a, b, cv::gapi::sum(a + b)));
  108. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  109. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  110. cv::Scalar ref_scl = cv::sum(in_mat + in_mat);
  111. cv::Scalar out_scl;
  112. c.apply(in_mat, in_mat, out_scl);
  113. EXPECT_EQ(out_scl, ref_scl);
  114. out_scl = cv::Scalar();
  115. auto cc = c.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));
  116. cc(in_mat, in_mat, out_scl);
  117. EXPECT_EQ(out_scl, ref_scl);
  118. }
  119. TEST(S11N, Pipeline_Sharpen)
  120. {
  121. const cv::Size sz_in (1280, 720);
  122. const cv::Size sz_out( 640, 480);
  123. cv::Mat in_mat (sz_in, CV_8UC3);
  124. in_mat = cv::Scalar(128, 33, 53);
  125. cv::Mat out_mat(sz_out, CV_8UC3);
  126. cv::Mat out_mat_y;
  127. cv::Mat out_mat_ocv(sz_out, CV_8UC3);
  128. float sharpen_coeffs[] = {
  129. 0.0f, -1.f, 0.0f,
  130. -1.0f, 5.f, -1.0f,
  131. 0.0f, -1.f, 0.0f
  132. };
  133. cv::Mat sharpen_kernel(3, 3, CV_32F, sharpen_coeffs);
  134. // G-API code //////////////////////////////////////////////////////////////
  135. cv::GMat in;
  136. auto vga = cv::gapi::resize(in, sz_out);
  137. auto yuv = cv::gapi::RGB2YUV(vga);
  138. auto yuv_p = cv::gapi::split3(yuv);
  139. auto y_sharp = cv::gapi::filter2D(std::get<0>(yuv_p), -1, sharpen_kernel);
  140. auto yuv_new = cv::gapi::merge3(y_sharp, std::get<1>(yuv_p), std::get<2>(yuv_p));
  141. auto out = cv::gapi::YUV2RGB(yuv_new);
  142. auto p = cv::gapi::serialize(cv::GComputation(cv::GIn(in), cv::GOut(y_sharp, out)));
  143. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  144. c.apply(cv::gin(in_mat), cv::gout(out_mat_y, out_mat));
  145. // OpenCV code /////////////////////////////////////////////////////////////
  146. {
  147. cv::Mat smaller;
  148. cv::resize(in_mat, smaller, sz_out);
  149. cv::Mat yuv_mat;
  150. cv::cvtColor(smaller, yuv_mat, cv::COLOR_RGB2YUV);
  151. std::vector<cv::Mat> yuv_planar(3);
  152. cv::split(yuv_mat, yuv_planar);
  153. cv::filter2D(yuv_planar[0], yuv_planar[0], -1, sharpen_kernel);
  154. cv::merge(yuv_planar, yuv_mat);
  155. cv::cvtColor(yuv_mat, out_mat_ocv, cv::COLOR_YUV2RGB);
  156. }
  157. // Comparison //////////////////////////////////////////////////////////////
  158. {
  159. cv::Mat diff = out_mat_ocv != out_mat;
  160. std::vector<cv::Mat> diffBGR(3);
  161. cv::split(diff, diffBGR);
  162. EXPECT_EQ(0, cvtest::norm(diffBGR[0], NORM_INF));
  163. EXPECT_EQ(0, cvtest::norm(diffBGR[1], NORM_INF));
  164. EXPECT_EQ(0, cvtest::norm(diffBGR[2], NORM_INF));
  165. }
  166. // Metadata check /////////////////////////////////////////////////////////
  167. {
  168. auto cc = c.compile(cv::descr_of(in_mat));
  169. auto metas = cc.outMetas();
  170. ASSERT_EQ(2u, metas.size());
  171. auto out_y_meta = cv::util::get<cv::GMatDesc>(metas[0]);
  172. auto out_meta = cv::util::get<cv::GMatDesc>(metas[1]);
  173. // Y-output
  174. EXPECT_EQ(CV_8U, out_y_meta.depth);
  175. EXPECT_EQ(1, out_y_meta.chan);
  176. EXPECT_EQ(640, out_y_meta.size.width);
  177. EXPECT_EQ(480, out_y_meta.size.height);
  178. // Final output
  179. EXPECT_EQ(CV_8U, out_meta.depth);
  180. EXPECT_EQ(3, out_meta.chan);
  181. EXPECT_EQ(640, out_meta.size.width);
  182. EXPECT_EQ(480, out_meta.size.height);
  183. }
  184. }
  185. TEST(S11N, Pipeline_CustomRGB2YUV)
  186. {
  187. const cv::Size sz(1280, 720);
  188. const int INS = 3;
  189. std::vector<cv::Mat> in_mats(INS);
  190. for (auto i : ade::util::iota(INS))
  191. {
  192. in_mats[i].create(sz, CV_8U);
  193. cv::randu(in_mats[i], cv::Scalar::all(0), cv::Scalar::all(255));
  194. }
  195. const int OUTS = 3;
  196. std::vector<cv::Mat> out_mats_cv(OUTS);
  197. std::vector<cv::Mat> out_mats_gapi(OUTS);
  198. for (auto i : ade::util::iota(OUTS))
  199. {
  200. out_mats_cv[i].create(sz, CV_8U);
  201. out_mats_gapi[i].create(sz, CV_8U);
  202. }
  203. // G-API code //////////////////////////////////////////////////////////////
  204. {
  205. cv::GMat r, g, b;
  206. cv::GMat y = 0.299f*r + 0.587f*g + 0.114f*b;
  207. cv::GMat u = 0.492f*(b - y);
  208. cv::GMat v = 0.877f*(r - y);
  209. auto p = cv::gapi::serialize(cv::GComputation({r, g, b}, {y, u, v}));
  210. auto c = cv::gapi::deserialize<cv::GComputation>(p);
  211. c.apply(in_mats, out_mats_gapi);
  212. }
  213. // OpenCV code /////////////////////////////////////////////////////////////
  214. {
  215. cv::Mat r = in_mats[0], g = in_mats[1], b = in_mats[2];
  216. cv::Mat y = 0.299f*r + 0.587f*g + 0.114f*b;
  217. cv::Mat u = 0.492f*(b - y);
  218. cv::Mat v = 0.877f*(r - y);
  219. out_mats_cv[0] = y;
  220. out_mats_cv[1] = u;
  221. out_mats_cv[2] = v;
  222. }
  223. // Comparison //////////////////////////////////////////////////////////////
  224. {
  225. const auto diff = [](cv::Mat m1, cv::Mat m2, int t) {
  226. return cv::abs(m1 - m2) > t;
  227. };
  228. // FIXME: Not bit-accurate even now!
  229. cv::Mat
  230. diff_y = diff(out_mats_cv[0], out_mats_gapi[0], 2),
  231. diff_u = diff(out_mats_cv[1], out_mats_gapi[1], 2),
  232. diff_v = diff(out_mats_cv[2], out_mats_gapi[2], 2);
  233. EXPECT_EQ(0, cvtest::norm(diff_y, NORM_INF));
  234. EXPECT_EQ(0, cvtest::norm(diff_u, NORM_INF));
  235. EXPECT_EQ(0, cvtest::norm(diff_v, NORM_INF));
  236. }
  237. }
  238. namespace ThisTest
  239. {
  240. using GOpBool = GOpaque<bool>;
  241. using GOpInt = GOpaque<int>;
  242. using GOpDouble = GOpaque<double>;
  243. using GOpPoint = GOpaque<cv::Point>;
  244. using GOpSize = GOpaque<cv::Size>;
  245. using GOpRect = GOpaque<cv::Rect>;
  246. using GOpOut = std::tuple<GOpPoint, GOpSize, GOpRect>;
  247. G_TYPED_KERNEL_M(OpGenerate, <GOpOut(GOpBool, GOpInt, GOpDouble)>, "test.s11n.gopaque")
  248. {
  249. static std::tuple<GOpaqueDesc, GOpaqueDesc, GOpaqueDesc> outMeta(const GOpaqueDesc&, const GOpaqueDesc&, const GOpaqueDesc&) {
  250. return std::make_tuple(empty_gopaque_desc(), empty_gopaque_desc(), empty_gopaque_desc());
  251. }
  252. };
  253. GAPI_OCV_KERNEL(OCVOpGenerate, OpGenerate)
  254. {
  255. static void run(const bool& b, const int& i, const double& d,
  256. cv::Point& p, cv::Size& s, cv::Rect& r)
  257. {
  258. p = cv::Point(i, i*2);
  259. s = b ? cv::Size(42, 42) : cv::Size(7, 7);
  260. int ii = static_cast<int>(d);
  261. r = cv::Rect(ii, ii, ii, ii);
  262. }
  263. };
  264. using GArrInt = GArray<int>;
  265. using GArrDouble = GArray<double>;
  266. using GArrPoint = GArray<cv::Point>;
  267. using GArrSize = GArray<cv::Size>;
  268. using GArrRect = GArray<cv::Rect>;
  269. using GArrMat = GArray<cv::Mat>;
  270. using GArrScalar = GArray<cv::Scalar>;
  271. using GArrOut = std::tuple<GArrPoint, GArrSize, GArrRect, GArrMat>;
  272. G_TYPED_KERNEL_M(ArrGenerate, <GArrOut(GArrInt, GArrInt, GArrDouble, GArrScalar)>, "test.s11n.garray")
  273. {
  274. static std::tuple<GArrayDesc, GArrayDesc, GArrayDesc, GArrayDesc> outMeta(const GArrayDesc&, const GArrayDesc&,
  275. const GArrayDesc&, const GArrayDesc&) {
  276. return std::make_tuple(empty_array_desc(), empty_array_desc(), empty_array_desc(), empty_array_desc());
  277. }
  278. };
  279. GAPI_OCV_KERNEL(OCVArrGenerate, ArrGenerate)
  280. {
  281. static void run(const std::vector<int>& b, const std::vector<int>& i,
  282. const std::vector<double>& d, const std::vector<cv::Scalar>& sc,
  283. std::vector<cv::Point>& p, std::vector<cv::Size>& s,
  284. std::vector<cv::Rect>& r, std::vector<cv::Mat>& m)
  285. {
  286. p.clear(); p.resize(b.size());
  287. s.clear(); s.resize(b.size());
  288. r.clear(); r.resize(b.size());
  289. m.clear(); m.resize(b.size());
  290. for (std::size_t idx = 0; idx < b.size(); ++idx)
  291. {
  292. p[idx] = cv::Point(i[idx], i[idx]*2);
  293. s[idx] = b[idx] == 1 ? cv::Size(42, 42) : cv::Size(7, 7);
  294. int ii = static_cast<int>(d[idx]);
  295. r[idx] = cv::Rect(ii, ii, ii, ii);
  296. m[idx] = cv::Mat(3, 3, CV_8UC1, sc[idx]);
  297. }
  298. }
  299. };
  300. G_TYPED_KERNEL_M(OpArrK1, <std::tuple<GArrInt,GOpSize>(GOpInt, GArrSize)>, "test.s11n.oparrk1")
  301. {
  302. static std::tuple<GArrayDesc, GOpaqueDesc> outMeta(const GOpaqueDesc&, const GArrayDesc&) {
  303. return std::make_tuple(empty_array_desc(), empty_gopaque_desc());
  304. }
  305. };
  306. GAPI_OCV_KERNEL(OCVOpArrK1, OpArrK1)
  307. {
  308. static void run(const int& i, const std::vector<cv::Size>& vs,
  309. std::vector<int>& vi, cv::Size& s)
  310. {
  311. vi.clear(); vi.resize(vs.size());
  312. s = cv::Size(i, i);
  313. for (std::size_t idx = 0; idx < vs.size(); ++ idx)
  314. vi[idx] = vs[idx].area();
  315. }
  316. };
  317. G_TYPED_KERNEL_M(OpArrK2, <std::tuple<GOpDouble,GArrPoint>(GArrInt, GOpSize)>, "test.s11n.oparrk2")
  318. {
  319. static std::tuple<GOpaqueDesc, GArrayDesc> outMeta(const GArrayDesc&, const GOpaqueDesc&) {
  320. return std::make_tuple(empty_gopaque_desc(), empty_array_desc());
  321. }
  322. };
  323. GAPI_OCV_KERNEL(OCVOpArrK2, OpArrK2)
  324. {
  325. static void run(const std::vector<int>& vi, const cv::Size& s,
  326. double& d, std::vector<cv::Point>& vp)
  327. {
  328. vp.clear(); vp.resize(vi.size());
  329. d = s.area() * 1.5;
  330. for (std::size_t idx = 0; idx < vi.size(); ++ idx)
  331. vp[idx] = cv::Point(vi[idx], vi[idx]);
  332. }
  333. };
  334. using GK3Out = std::tuple<cv::GArray<uint64_t>, cv::GArray<int32_t>>;
  335. G_TYPED_KERNEL_M(OpArrK3, <GK3Out(cv::GArray<bool>, cv::GArray<int32_t>, cv::GOpaque<float>)>, "test.s11n.oparrk3")
  336. {
  337. static std::tuple<GArrayDesc, GArrayDesc> outMeta(const GArrayDesc&, const GArrayDesc&, const GOpaqueDesc&) {
  338. return std::make_tuple(empty_array_desc(), empty_array_desc());
  339. }
  340. };
  341. GAPI_OCV_KERNEL(OCVOpArrK3, OpArrK3)
  342. {
  343. static void run(const std::vector<bool>& vb, const std::vector<int32_t>& vi_in, const float& f,
  344. std::vector<uint64_t>& vui, std::vector<int32_t>& vi)
  345. {
  346. vui.clear(); vui.resize(vi_in.size());
  347. vi.clear(); vi.resize(vi_in.size());
  348. for (std::size_t idx = 0; idx < vi_in.size(); ++ idx)
  349. {
  350. vi[idx] = vb[idx] ? vi_in[idx] : -vi_in[idx];
  351. vui[idx] = vb[idx] ? static_cast<uint64_t>(vi_in[idx] * f) :
  352. static_cast<uint64_t>(vi_in[idx] / f);
  353. }
  354. }
  355. };
  356. using GK4Out = std::tuple<cv::GOpaque<int>, cv::GArray<std::string>>;
  357. G_TYPED_KERNEL_M(OpArrK4, <GK4Out(cv::GOpaque<bool>, cv::GOpaque<std::string>)>, "test.s11n.oparrk4")
  358. {
  359. static std::tuple<GOpaqueDesc, GArrayDesc> outMeta(const GOpaqueDesc&, const GOpaqueDesc&) {
  360. return std::make_tuple(empty_gopaque_desc(), empty_array_desc());
  361. }
  362. };
  363. GAPI_OCV_KERNEL(OCVOpArrK4, OpArrK4)
  364. {
  365. static void run(const bool& b, const std::string& s,
  366. int& i, std::vector<std::string>& vs)
  367. {
  368. vs.clear();
  369. vs.resize(2);
  370. i = b ? 42 : 24;
  371. auto s_copy = s + " world";
  372. vs = std::vector<std::string>{s_copy, s_copy};
  373. }
  374. };
  375. } // namespace ThisTest
  376. TEST(S11N, Pipeline_GOpaque)
  377. {
  378. using namespace ThisTest;
  379. GOpBool in1;
  380. GOpInt in2;
  381. GOpDouble in3;
  382. auto out = OpGenerate::on(in1, in2, in3);
  383. cv::GComputation c(cv::GIn(in1, in2, in3), cv::GOut(std::get<0>(out), std::get<1>(out), std::get<2>(out)));
  384. auto p = cv::gapi::serialize(c);
  385. auto dc = cv::gapi::deserialize<cv::GComputation>(p);
  386. bool b = true;
  387. int i = 33;
  388. double d = 128.7;
  389. cv::Point pp;
  390. cv::Size s;
  391. cv::Rect r;
  392. dc.apply(cv::gin(b, i, d), cv::gout(pp, s, r), cv::compile_args(cv::gapi::kernels<OCVOpGenerate>()));
  393. EXPECT_EQ(pp, cv::Point(i, i*2));
  394. EXPECT_EQ(s, cv::Size(42, 42));
  395. int ii = static_cast<int>(d);
  396. EXPECT_EQ(r, cv::Rect(ii, ii, ii, ii));
  397. }
  398. TEST(S11N, Pipeline_GArray)
  399. {
  400. using namespace ThisTest;
  401. GArrInt in1, in2;
  402. GArrDouble in3;
  403. GArrScalar in4;
  404. auto out = ArrGenerate::on(in1, in2, in3, in4);
  405. cv::GComputation c(cv::GIn(in1, in2, in3, in4),
  406. cv::GOut(std::get<0>(out), std::get<1>(out),
  407. std::get<2>(out), std::get<3>(out)));
  408. auto p = cv::gapi::serialize(c);
  409. auto dc = cv::gapi::deserialize<cv::GComputation>(p);
  410. std::vector<int> b {1, 0, -1};
  411. std::vector<int> i {3, 0 , 59};
  412. std::vector<double> d {0.7, 120.5, 44.14};
  413. std::vector<cv::Scalar> sc {cv::Scalar::all(10), cv::Scalar::all(15), cv::Scalar::all(99)};
  414. std::vector<cv::Point> pp;
  415. std::vector<cv::Size> s;
  416. std::vector<cv::Rect> r;
  417. std::vector<cv::Mat> m;
  418. dc.apply(cv::gin(b, i, d, sc), cv::gout(pp, s, r, m), cv::compile_args(cv::gapi::kernels<OCVArrGenerate>()));
  419. for (std::size_t idx = 0; idx < b.size(); ++idx)
  420. {
  421. EXPECT_EQ(pp[idx], cv::Point(i[idx], i[idx]*2));
  422. EXPECT_EQ(s[idx], b[idx] == 1 ? cv::Size(42, 42) : cv::Size(7, 7));
  423. int ii = static_cast<int>(d[idx]);
  424. EXPECT_EQ(r[idx], cv::Rect(ii, ii, ii, ii));
  425. }
  426. }
  427. TEST(S11N, Pipeline_GArray_GOpaque_Multinode)
  428. {
  429. using namespace ThisTest;
  430. GOpInt in1;
  431. GArrSize in2;
  432. auto tmp = OpArrK1::on(in1, in2);
  433. auto out = OpArrK2::on(std::get<0>(tmp), std::get<1>(tmp));
  434. cv::GComputation c(cv::GIn(in1, in2),
  435. cv::GOut(std::get<0>(out), std::get<1>(out)));
  436. auto p = cv::gapi::serialize(c);
  437. auto dc = cv::gapi::deserialize<cv::GComputation>(p);
  438. int i = 42;
  439. std::vector<cv::Size> s{cv::Size(11, 22), cv::Size(13, 18)};
  440. double d;
  441. std::vector<cv::Point> pp;
  442. dc.apply(cv::gin(i, s), cv::gout(d, pp), cv::compile_args(cv::gapi::kernels<OCVOpArrK1, OCVOpArrK2>()));
  443. auto st = cv::Size(i ,i);
  444. EXPECT_EQ(d, st.area() * 1.5);
  445. for (std::size_t idx = 0; idx < s.size(); ++idx)
  446. {
  447. EXPECT_EQ(pp[idx], cv::Point(s[idx].area(), s[idx].area()));
  448. }
  449. }
  450. TEST(S11N, Pipeline_GArray_GOpaque_2)
  451. {
  452. using namespace ThisTest;
  453. cv::GArray<bool> in1;
  454. cv::GArray<int32_t> in2;
  455. cv::GOpaque<float> in3;
  456. auto out = OpArrK3::on(in1, in2, in3);
  457. cv::GComputation c(cv::GIn(in1, in2, in3),
  458. cv::GOut(std::get<0>(out), std::get<1>(out)));
  459. auto p = cv::gapi::serialize(c);
  460. auto dc = cv::gapi::deserialize<cv::GComputation>(p);
  461. std::vector<bool> b {true, false, false};
  462. std::vector<int32_t> i {234324, -234252, 999};
  463. float f = 0.85f;
  464. std::vector<int32_t> out_i;
  465. std::vector<uint64_t> out_ui;
  466. dc.apply(cv::gin(b, i, f), cv::gout(out_ui, out_i), cv::compile_args(cv::gapi::kernels<OCVOpArrK3>()));
  467. for (std::size_t idx = 0; idx < b.size(); ++idx)
  468. {
  469. EXPECT_EQ(out_i[idx], b[idx] ? i[idx] : -i[idx]);
  470. EXPECT_EQ(out_ui[idx], b[idx] ? static_cast<uint64_t>(i[idx] * f) :
  471. static_cast<uint64_t>(i[idx] / f));
  472. }
  473. }
  474. TEST(S11N, Pipeline_GArray_GOpaque_3)
  475. {
  476. using namespace ThisTest;
  477. cv::GOpaque<bool> in1;
  478. cv::GOpaque<std::string> in2;
  479. auto out = OpArrK4::on(in1, in2);
  480. cv::GComputation c(cv::GIn(in1, in2),
  481. cv::GOut(std::get<0>(out), std::get<1>(out)));
  482. auto p = cv::gapi::serialize(c);
  483. auto dc = cv::gapi::deserialize<cv::GComputation>(p);
  484. bool b = false;
  485. std::string s("hello");
  486. int i = 0;
  487. std::vector<std::string> vs{};
  488. dc.apply(cv::gin(b, s), cv::gout(i, vs), cv::compile_args(cv::gapi::kernels<OCVOpArrK4>()));
  489. EXPECT_EQ(24, i);
  490. std::vector<std::string> vs_ref{"hello world", "hello world"};
  491. EXPECT_EQ(vs_ref, vs);
  492. }
  493. TEST(S11N, Pipeline_Render_NV12)
  494. {
  495. cv::Size sz (100, 200);
  496. int rects_num = 10;
  497. int text_num = 10;
  498. int image_num = 10;
  499. int thick = 2;
  500. int lt = LINE_8;
  501. cv::Scalar color(111, 222, 77);
  502. // G-API code //////////////////////////////////////////////////////////////
  503. cv::gapi::wip::draw::Prims prims;
  504. // Rects
  505. int shift = 0;
  506. for (int i = 0; i < rects_num; ++i) {
  507. cv::Rect rect(200 + i, 200 + i, 200, 200);
  508. prims.emplace_back(cv::gapi::wip::draw::Rect(rect, color, thick, lt, shift));
  509. }
  510. // Mosaic
  511. int cellsz = 50;
  512. int decim = 0;
  513. for (int i = 0; i < rects_num; ++i) {
  514. cv::Rect mos(200 + i, 200 + i, 200, 200);
  515. prims.emplace_back(cv::gapi::wip::draw::Mosaic(mos, cellsz, decim));
  516. }
  517. // Text
  518. std::string text = "Some text";
  519. int ff = FONT_HERSHEY_SIMPLEX;
  520. double fs = 2.0;
  521. bool blo = false;
  522. for (int i = 0; i < text_num; ++i) {
  523. cv::Point org(200 + i, 200 + i);
  524. prims.emplace_back(cv::gapi::wip::draw::Text(text, org, ff, fs, color, thick, lt, blo));
  525. }
  526. // Image
  527. double transparency = 1.0;
  528. cv::Rect rect_img(0 ,0 , 50, 50);
  529. cv::Mat img(rect_img.size(), CV_8UC3, color);
  530. cv::Mat alpha(rect_img.size(), CV_32FC1, transparency);
  531. auto tl = rect_img.tl();
  532. for (int i = 0; i < image_num; ++i) {
  533. cv::Point org_img = {tl.x + i, tl.y + rect_img.size().height + i};
  534. prims.emplace_back(cv::gapi::wip::draw::Image({org_img, img, alpha}));
  535. }
  536. // Circle
  537. cv::Point center(300, 400);
  538. int rad = 25;
  539. prims.emplace_back(cv::gapi::wip::draw::Circle({center, rad, color, thick, lt, shift}));
  540. // Line
  541. cv::Point point_next(300, 425);
  542. prims.emplace_back(cv::gapi::wip::draw::Line({center, point_next, color, thick, lt, shift}));
  543. // Poly
  544. std::vector<cv::Point> points = {{300, 400}, {290, 450}, {348, 410}, {300, 400}};
  545. prims.emplace_back(cv::gapi::wip::draw::Poly({points, color, thick, lt, shift}));
  546. cv::GMat y_in, uv_in, y_out, uv_out;
  547. cv::GArray<cv::gapi::wip::draw::Prim> arr;
  548. std::tie(y_out, uv_out) = cv::gapi::wip::draw::renderNV12(y_in, uv_in, arr);
  549. cv::GComputation comp(cv::GIn(y_in, uv_in, arr), cv::GOut(y_out, uv_out));
  550. auto serialized = cv::gapi::serialize(comp);
  551. auto dc = cv::gapi::deserialize<cv::GComputation>(serialized);
  552. cv::Mat y(1920, 1080, CV_8UC1);
  553. cv::Mat uv(960, 540, CV_8UC2);
  554. cv::randu(y, cv::Scalar(0), cv::Scalar(255));
  555. cv::randu(uv, cv::Scalar::all(0), cv::Scalar::all(255));
  556. cv::Mat y_ref_mat = y.clone(), uv_ref_mat = uv.clone();
  557. dc.apply(cv::gin(y, uv, prims), cv::gout(y, uv));
  558. // OpenCV code //////////////////////////////////////////////////////////////
  559. cv::Mat yuv;
  560. cv::gapi::wip::draw::cvtNV12ToYUV(y_ref_mat, uv_ref_mat, yuv);
  561. for (int i = 0; i < rects_num; ++i) {
  562. cv::Rect rect(200 + i, 200 + i, 200, 200);
  563. cv::rectangle(yuv, rect, cvtBGRToYUVC(color), thick, lt, shift);
  564. }
  565. for (int i = 0; i < rects_num; ++i) {
  566. cv::Rect mos(200 + i, 200 + i, 200, 200);
  567. drawMosaicRef(yuv, mos, cellsz);
  568. }
  569. for (int i = 0; i < text_num; ++i) {
  570. cv::Point org(200 + i, 200 + i);
  571. cv::putText(yuv, text, org, ff, fs, cvtBGRToYUVC(color), thick, lt, blo);
  572. }
  573. for (int i = 0; i < image_num; ++i) {
  574. cv::Point org_img = {tl.x + i, tl.y + rect_img.size().height + i};
  575. cv::Mat yuv_img;
  576. cv::cvtColor(img, yuv_img, cv::COLOR_BGR2YUV);
  577. blendImageRef(yuv, org_img, yuv_img, alpha);
  578. }
  579. cv::circle(yuv, center, rad, cvtBGRToYUVC(color), thick, lt, shift);
  580. cv::line(yuv, center, point_next, cvtBGRToYUVC(color), thick, lt, shift);
  581. std::vector<std::vector<cv::Point>> pp{points};
  582. cv::fillPoly(yuv, pp, cvtBGRToYUVC(color), lt, shift);
  583. // YUV -> NV12
  584. cv::gapi::wip::draw::cvtYUVToNV12(yuv, y_ref_mat, uv_ref_mat);
  585. EXPECT_EQ(cv::norm( y, y_ref_mat), 0);
  586. EXPECT_EQ(cv::norm(uv, uv_ref_mat), 0);
  587. }
  588. TEST(S11N, Pipeline_Render_RGB)
  589. {
  590. cv::Size sz (100, 200);
  591. int rects_num = 10;
  592. int text_num = 10;
  593. int image_num = 10;
  594. int thick = 2;
  595. int lt = LINE_8;
  596. cv::Scalar color(111, 222, 77);
  597. // G-API code //////////////////////////////////////////////////////////////
  598. cv::gapi::wip::draw::Prims prims;
  599. // Rects
  600. int shift = 0;
  601. for (int i = 0; i < rects_num; ++i) {
  602. cv::Rect rect(200 + i, 200 + i, 200, 200);
  603. prims.emplace_back(cv::gapi::wip::draw::Rect(rect, color, thick, lt, shift));
  604. }
  605. // Mosaic
  606. int cellsz = 50;
  607. int decim = 0;
  608. for (int i = 0; i < rects_num; ++i) {
  609. cv::Rect mos(200 + i, 200 + i, 200, 200);
  610. prims.emplace_back(cv::gapi::wip::draw::Mosaic(mos, cellsz, decim));
  611. }
  612. // Text
  613. std::string text = "Some text";
  614. int ff = FONT_HERSHEY_SIMPLEX;
  615. double fs = 2.0;
  616. bool blo = false;
  617. for (int i = 0; i < text_num; ++i) {
  618. cv::Point org(200 + i, 200 + i);
  619. prims.emplace_back(cv::gapi::wip::draw::Text(text, org, ff, fs, color, thick, lt, blo));
  620. }
  621. // Image
  622. double transparency = 1.0;
  623. cv::Rect rect_img(0 ,0 , 50, 50);
  624. cv::Mat img(rect_img.size(), CV_8UC3, color);
  625. cv::Mat alpha(rect_img.size(), CV_32FC1, transparency);
  626. auto tl = rect_img.tl();
  627. for (int i = 0; i < image_num; ++i) {
  628. cv::Point org_img = {tl.x + i, tl.y + rect_img.size().height + i};
  629. prims.emplace_back(cv::gapi::wip::draw::Image({org_img, img, alpha}));
  630. }
  631. // Circle
  632. cv::Point center(300, 400);
  633. int rad = 25;
  634. prims.emplace_back(cv::gapi::wip::draw::Circle({center, rad, color, thick, lt, shift}));
  635. // Line
  636. cv::Point point_next(300, 425);
  637. prims.emplace_back(cv::gapi::wip::draw::Line({center, point_next, color, thick, lt, shift}));
  638. // Poly
  639. std::vector<cv::Point> points = {{300, 400}, {290, 450}, {348, 410}, {300, 400}};
  640. prims.emplace_back(cv::gapi::wip::draw::Poly({points, color, thick, lt, shift}));
  641. cv::GMat in, out;
  642. cv::GArray<cv::gapi::wip::draw::Prim> arr;
  643. out = cv::gapi::wip::draw::render3ch(in, arr);
  644. cv::GComputation comp(cv::GIn(in, arr), cv::GOut(out));
  645. auto serialized = cv::gapi::serialize(comp);
  646. auto dc = cv::gapi::deserialize<cv::GComputation>(serialized);
  647. cv::Mat input(1920, 1080, CV_8UC3);
  648. cv::randu(input, cv::Scalar::all(0), cv::Scalar::all(255));
  649. cv::Mat ref_mat = input.clone();
  650. dc.apply(cv::gin(input, prims), cv::gout(input));
  651. // OpenCV code //////////////////////////////////////////////////////////////
  652. for (int i = 0; i < rects_num; ++i) {
  653. cv::Rect rect(200 + i, 200 + i, 200, 200);
  654. cv::rectangle(ref_mat, rect, color, thick, lt, shift);
  655. }
  656. for (int i = 0; i < rects_num; ++i) {
  657. cv::Rect mos(200 + i, 200 + i, 200, 200);
  658. drawMosaicRef(ref_mat, mos, cellsz);
  659. }
  660. for (int i = 0; i < text_num; ++i) {
  661. cv::Point org(200 + i, 200 + i);
  662. cv::putText(ref_mat, text, org, ff, fs, color, thick, lt, blo);
  663. }
  664. for (int i = 0; i < image_num; ++i) {
  665. cv::Point org_img = {tl.x + i, tl.y + rect_img.size().height + i};
  666. blendImageRef(ref_mat, org_img, img, alpha);
  667. }
  668. cv::circle(ref_mat, center, rad, color, thick, lt, shift);
  669. cv::line(ref_mat, center, point_next, color, thick, lt, shift);
  670. std::vector<std::vector<cv::Point>> pp{points};
  671. cv::fillPoly(ref_mat, pp, color, lt, shift);
  672. EXPECT_EQ(cv::norm(input, ref_mat), 0);
  673. }
  674. TEST(S11N, Pipeline_Const_GScalar)
  675. {
  676. static constexpr auto in_scalar = 10;
  677. cv::GMat a;
  678. cv::GScalar s;
  679. cv::GComputation computation(GIn(a), GOut(cv::gapi::addC(a, in_scalar)));
  680. auto p = cv::gapi::serialize(computation);
  681. auto deserialized_computation = cv::gapi::deserialize<cv::GComputation>(p);
  682. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  683. cv::Mat ref_mat;
  684. cv::add(in_mat, in_scalar, ref_mat);
  685. cv::Mat out_mat;
  686. computation.apply(cv::gin(in_mat/*, in_scalar*/), cv::gout(out_mat));
  687. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  688. out_mat = cv::Mat();
  689. deserialized_computation.apply(cv::gin(in_mat/*, in_scalar*/), cv::gout(out_mat));
  690. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  691. out_mat = cv::Mat();
  692. auto cc = deserialized_computation.compile(cv::descr_of(in_mat));
  693. cc(cv::gin(in_mat/*, in_scalar*/), cv::gout(out_mat));
  694. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  695. }
  696. } // namespace opencv_test