gapi_sample_pipelines.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 Intel Corporation
  6. #include "test_precomp.hpp"
  7. #include <chrono>
  8. #include <stdexcept>
  9. #include <ade/util/iota_range.hpp>
  10. #include "logger.hpp"
  11. #include <opencv2/gapi/core.hpp>
  12. #include "executor/thread_pool.hpp"
  13. namespace opencv_test
  14. {
  15. namespace
  16. {
  17. G_TYPED_KERNEL(GInvalidResize, <GMat(GMat,Size,double,double,int)>, "org.opencv.test.invalid_resize")
  18. {
  19. static GMatDesc outMeta(GMatDesc in, Size, double, double, int) { return in; }
  20. };
  21. GAPI_OCV_KERNEL(GOCVInvalidResize, GInvalidResize)
  22. {
  23. static void run(const cv::Mat& in, cv::Size sz, double fx, double fy, int interp, cv::Mat &out)
  24. {
  25. cv::resize(in, out, sz, fx, fy, interp);
  26. }
  27. };
  28. G_TYPED_KERNEL(GReallocatingCopy, <GMat(GMat)>, "org.opencv.test.reallocating_copy")
  29. {
  30. static GMatDesc outMeta(GMatDesc in) { return in; }
  31. };
  32. GAPI_OCV_KERNEL(GOCVReallocatingCopy, GReallocatingCopy)
  33. {
  34. static void run(const cv::Mat& in, cv::Mat &out)
  35. {
  36. out = in.clone();
  37. }
  38. };
  39. G_TYPED_KERNEL(GCustom, <GMat(GMat)>, "org.opencv.test.custom")
  40. {
  41. static GMatDesc outMeta(GMatDesc in) { return in; }
  42. };
  43. G_TYPED_KERNEL(GZeros, <GMat(GMat, GMatDesc)>, "org.opencv.test.zeros")
  44. {
  45. static GMatDesc outMeta(GMatDesc /*in*/, GMatDesc user_desc)
  46. {
  47. return user_desc;
  48. }
  49. };
  50. GAPI_OCV_KERNEL(GOCVZeros, GZeros)
  51. {
  52. static void run(const cv::Mat& /*in*/,
  53. const cv::GMatDesc& /*desc*/,
  54. cv::Mat& out)
  55. {
  56. out.setTo(0);
  57. }
  58. };
  59. G_TYPED_KERNEL(GBusyWait, <GMat(GMat, uint32_t)>, "org.busy_wait") {
  60. static GMatDesc outMeta(GMatDesc in, uint32_t)
  61. {
  62. return in;
  63. }
  64. };
  65. GAPI_OCV_KERNEL(GOCVBusyWait, GBusyWait)
  66. {
  67. static void run(const cv::Mat& in,
  68. const uint32_t time_in_ms,
  69. cv::Mat& out)
  70. {
  71. using namespace std::chrono;
  72. auto s = high_resolution_clock::now();
  73. in.copyTo(out);
  74. auto e = high_resolution_clock::now();
  75. const auto elapsed_in_ms =
  76. static_cast<int32_t>(duration_cast<milliseconds>(e-s).count());
  77. int32_t diff = time_in_ms - elapsed_in_ms;
  78. const auto need_to_wait_in_ms = static_cast<uint32_t>(std::max(0, diff));
  79. s = high_resolution_clock::now();
  80. e = s;
  81. while (duration_cast<milliseconds>(e-s).count() < need_to_wait_in_ms) {
  82. e = high_resolution_clock::now();
  83. }
  84. }
  85. };
  86. // These definitions test the correct macro work if the kernel has multiple output values
  87. G_TYPED_KERNEL(GRetGArrayTupleOfGMat2Kernel, <GArray<std::tuple<GMat, GMat>>(GMat, Scalar)>, "org.opencv.test.retarrayoftupleofgmat2kernel") {};
  88. G_TYPED_KERNEL(GRetGArraTupleyOfGMat3Kernel, <GArray<std::tuple<GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat3kernel") {};
  89. G_TYPED_KERNEL(GRetGArraTupleyOfGMat4Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat4kernel") {};
  90. G_TYPED_KERNEL(GRetGArraTupleyOfGMat5Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat5kernel") {};
  91. G_TYPED_KERNEL(GRetGArraTupleyOfGMat6Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat6kernel") {};
  92. G_TYPED_KERNEL(GRetGArraTupleyOfGMat7Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat7kernel") {};
  93. G_TYPED_KERNEL(GRetGArraTupleyOfGMat8Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat8kernel") {};
  94. G_TYPED_KERNEL(GRetGArraTupleyOfGMat9Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat9kernel") {};
  95. G_TYPED_KERNEL(GRetGArraTupleyOfGMat10Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat10kernel") {};
  96. G_TYPED_KERNEL_M(GRetGMat2Kernel, <std::tuple<GMat, GMat>(GMat, GMat, GMat)>, "org.opencv.test.retgmat2kernel") {};
  97. G_TYPED_KERNEL_M(GRetGMat3Kernel, <std::tuple<GMat, GMat, GMat>(GMat, GScalar)>, "org.opencv.test.retgmat3kernel") {};
  98. G_TYPED_KERNEL_M(GRetGMat4Kernel, <std::tuple<GMat, GMat, GMat, GMat>(GMat, GArray<int>, GScalar)>, "org.opencv.test.retgmat4kernel") {};
  99. G_TYPED_KERNEL_M(GRetGMat5Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat5kernel") {};
  100. G_TYPED_KERNEL_M(GRetGMat6Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat6kernel") {};
  101. G_TYPED_KERNEL_M(GRetGMat7Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat7kernel") {};
  102. G_TYPED_KERNEL_M(GRetGMat8Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat8kernel") {};
  103. G_TYPED_KERNEL_M(GRetGMat9Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat9kernel") {};
  104. G_TYPED_KERNEL_M(GRetGMat10Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat10kernel") {};
  105. }
  106. TEST(GAPI_Pipeline, OverloadUnary_MatMat)
  107. {
  108. cv::GMat in;
  109. cv::GComputation comp(in, cv::gapi::bitwise_not(in));
  110. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  111. cv::Mat ref_mat = ~in_mat;
  112. cv::Mat out_mat;
  113. comp.apply(in_mat, out_mat);
  114. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  115. out_mat = cv::Mat();
  116. auto cc = comp.compile(cv::descr_of(in_mat));
  117. cc(in_mat, out_mat);
  118. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  119. }
  120. TEST(GAPI_Pipeline, OverloadUnary_MatScalar)
  121. {
  122. cv::GMat in;
  123. cv::GComputation comp(in, cv::gapi::sum(in));
  124. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  125. cv::Scalar ref_scl = cv::sum(in_mat);
  126. cv::Scalar out_scl;
  127. comp.apply(in_mat, out_scl);
  128. EXPECT_EQ(out_scl, ref_scl);
  129. out_scl = cv::Scalar();
  130. auto cc = comp.compile(cv::descr_of(in_mat));
  131. cc(in_mat, out_scl);
  132. EXPECT_EQ(out_scl, ref_scl);
  133. }
  134. TEST(GAPI_Pipeline, OverloadBinary_Mat)
  135. {
  136. cv::GMat a, b;
  137. cv::GComputation comp(a, b, cv::gapi::add(a, b));
  138. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  139. cv::Mat ref_mat = (in_mat+in_mat);
  140. cv::Mat out_mat;
  141. comp.apply(in_mat, in_mat, out_mat);
  142. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  143. out_mat = cv::Mat();
  144. auto cc = comp.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));
  145. cc(in_mat, in_mat, out_mat);
  146. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  147. }
  148. TEST(GAPI_Pipeline, OverloadBinary_Scalar)
  149. {
  150. cv::GMat a, b;
  151. cv::GComputation comp(a, b, cv::gapi::sum(a + b));
  152. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  153. cv::Scalar ref_scl = cv::sum(in_mat+in_mat);
  154. cv::Scalar out_scl;
  155. comp.apply(in_mat, in_mat, out_scl);
  156. EXPECT_EQ(out_scl, ref_scl);
  157. out_scl = cv::Scalar();
  158. auto cc = comp.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));
  159. cc(in_mat, in_mat, out_scl);
  160. EXPECT_EQ(out_scl, ref_scl);
  161. }
  162. TEST(GAPI_Pipeline, Sharpen)
  163. {
  164. const cv::Size sz_in (1280, 720);
  165. const cv::Size sz_out( 640, 480);
  166. cv::Mat in_mat (sz_in, CV_8UC3);
  167. in_mat = cv::Scalar(128, 33, 53);
  168. cv::Mat out_mat(sz_out, CV_8UC3);
  169. cv::Mat out_mat_y;
  170. cv::Mat out_mat_ocv(sz_out, CV_8UC3);
  171. float sharpen_coeffs[] = {
  172. 0.0f, -1.f, 0.0f,
  173. -1.0f, 5.f, -1.0f,
  174. 0.0f, -1.f, 0.0f
  175. };
  176. cv::Mat sharpen_kernel(3, 3, CV_32F, sharpen_coeffs);
  177. // G-API code //////////////////////////////////////////////////////////////
  178. cv::GMat in;
  179. auto vga = cv::gapi::resize(in, sz_out);
  180. auto yuv = cv::gapi::RGB2YUV(vga);
  181. auto yuv_p = cv::gapi::split3(yuv);
  182. auto y_sharp = cv::gapi::filter2D(std::get<0>(yuv_p), -1, sharpen_kernel);
  183. auto yuv_new = cv::gapi::merge3(y_sharp, std::get<1>(yuv_p), std::get<2>(yuv_p));
  184. auto out = cv::gapi::YUV2RGB(yuv_new);
  185. cv::GComputation c(cv::GIn(in), cv::GOut(y_sharp, out));
  186. c.apply(cv::gin(in_mat), cv::gout(out_mat_y, out_mat));
  187. // OpenCV code /////////////////////////////////////////////////////////////
  188. {
  189. cv::Mat smaller;
  190. cv::resize(in_mat, smaller, sz_out);
  191. cv::Mat yuv_mat;
  192. cv::cvtColor(smaller, yuv_mat, cv::COLOR_RGB2YUV);
  193. std::vector<cv::Mat> yuv_planar(3);
  194. cv::split(yuv_mat, yuv_planar);
  195. cv::filter2D(yuv_planar[0], yuv_planar[0], -1, sharpen_kernel);
  196. cv::merge(yuv_planar, yuv_mat);
  197. cv::cvtColor(yuv_mat, out_mat_ocv, cv::COLOR_YUV2RGB);
  198. }
  199. // Comparison //////////////////////////////////////////////////////////////
  200. {
  201. cv::Mat diff = out_mat_ocv != out_mat;
  202. std::vector<cv::Mat> diffBGR(3);
  203. cv::split(diff, diffBGR);
  204. EXPECT_EQ(0, cvtest::norm(diffBGR[0], NORM_INF));
  205. EXPECT_EQ(0, cvtest::norm(diffBGR[1], NORM_INF));
  206. EXPECT_EQ(0, cvtest::norm(diffBGR[2], NORM_INF));
  207. }
  208. // Metadata check /////////////////////////////////////////////////////////
  209. {
  210. auto cc = c.compile(cv::descr_of(in_mat));
  211. auto metas = cc.outMetas();
  212. ASSERT_EQ(2u, metas.size());
  213. auto out_y_meta = cv::util::get<cv::GMatDesc>(metas[0]);
  214. auto out_meta = cv::util::get<cv::GMatDesc>(metas[1]);
  215. // Y-output
  216. EXPECT_EQ(CV_8U, out_y_meta.depth);
  217. EXPECT_EQ(1, out_y_meta.chan);
  218. EXPECT_EQ(640, out_y_meta.size.width);
  219. EXPECT_EQ(480, out_y_meta.size.height);
  220. // Final output
  221. EXPECT_EQ(CV_8U, out_meta.depth);
  222. EXPECT_EQ(3, out_meta.chan);
  223. EXPECT_EQ(640, out_meta.size.width);
  224. EXPECT_EQ(480, out_meta.size.height);
  225. }
  226. }
  227. TEST(GAPI_Pipeline, CustomRGB2YUV)
  228. {
  229. const cv::Size sz(1280, 720);
  230. // BEWARE:
  231. //
  232. // std::vector<cv::Mat> out_mats_cv(3, cv::Mat(sz, CV_8U))
  233. //
  234. // creates a vector of 3 elements pointing to the same Mat!
  235. // FIXME: Make a G-API check for that
  236. const int INS = 3;
  237. std::vector<cv::Mat> in_mats(INS);
  238. for (auto i : ade::util::iota(INS))
  239. {
  240. in_mats[i].create(sz, CV_8U);
  241. cv::randu(in_mats[i], cv::Scalar::all(0), cv::Scalar::all(255));
  242. }
  243. const int OUTS = 3;
  244. std::vector<cv::Mat> out_mats_cv(OUTS);
  245. std::vector<cv::Mat> out_mats_gapi(OUTS);
  246. for (auto i : ade::util::iota(OUTS))
  247. {
  248. out_mats_cv [i].create(sz, CV_8U);
  249. out_mats_gapi[i].create(sz, CV_8U);
  250. }
  251. // G-API code //////////////////////////////////////////////////////////////
  252. {
  253. cv::GMat r, g, b;
  254. cv::GMat y = 0.299f*r + 0.587f*g + 0.114f*b;
  255. cv::GMat u = 0.492f*(b - y);
  256. cv::GMat v = 0.877f*(r - y);
  257. cv::GComputation customCvt({r, g, b}, {y, u, v});
  258. customCvt.apply(in_mats, out_mats_gapi);
  259. }
  260. // OpenCV code /////////////////////////////////////////////////////////////
  261. {
  262. cv::Mat r = in_mats[0], g = in_mats[1], b = in_mats[2];
  263. cv::Mat y = 0.299f*r + 0.587f*g + 0.114f*b;
  264. cv::Mat u = 0.492f*(b - y);
  265. cv::Mat v = 0.877f*(r - y);
  266. out_mats_cv[0] = y;
  267. out_mats_cv[1] = u;
  268. out_mats_cv[2] = v;
  269. }
  270. // Comparison //////////////////////////////////////////////////////////////
  271. {
  272. const auto diff = [](cv::Mat m1, cv::Mat m2, int t) {
  273. return cv::abs(m1-m2) > t;
  274. };
  275. // FIXME: Not bit-accurate even now!
  276. cv::Mat
  277. diff_y = diff(out_mats_cv[0], out_mats_gapi[0], 2),
  278. diff_u = diff(out_mats_cv[1], out_mats_gapi[1], 2),
  279. diff_v = diff(out_mats_cv[2], out_mats_gapi[2], 2);
  280. EXPECT_EQ(0, cvtest::norm(diff_y, NORM_INF));
  281. EXPECT_EQ(0, cvtest::norm(diff_u, NORM_INF));
  282. EXPECT_EQ(0, cvtest::norm(diff_v, NORM_INF));
  283. }
  284. }
  285. TEST(GAPI_Pipeline, PipelineWithInvalidKernel)
  286. {
  287. cv::GMat in, out;
  288. cv::Mat in_mat(500, 500, CV_8UC1), out_mat;
  289. out = GInvalidResize::on(in, cv::Size(300, 300), 0.0, 0.0, cv::INTER_LINEAR);
  290. const auto pkg = cv::gapi::kernels<GOCVInvalidResize>();
  291. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  292. EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error);
  293. }
  294. TEST(GAPI_Pipeline, InvalidOutputComputation)
  295. {
  296. cv::GMat in1, out1, out2, out3;
  297. std::tie(out1, out2, out2) = cv::gapi::split3(in1);
  298. cv::GComputation c({in1}, {out1, out2, out3});
  299. cv::Mat in_mat;
  300. cv::Mat out_mat1, out_mat2, out_mat3, out_mat4;
  301. std::vector<cv::Mat> u_outs = {out_mat1, out_mat2, out_mat3, out_mat4};
  302. std::vector<cv::Mat> u_ins = {in_mat};
  303. EXPECT_THROW(c.apply(u_ins, u_outs), std::logic_error);
  304. }
  305. TEST(GAPI_Pipeline, PipelineAllocatingKernel)
  306. {
  307. cv::GMat in, out;
  308. cv::Mat in_mat(500, 500, CV_8UC1), out_mat;
  309. out = GReallocatingCopy::on(in);
  310. const auto pkg = cv::gapi::kernels<GOCVReallocatingCopy>();
  311. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  312. EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error);
  313. }
  314. TEST(GAPI_Pipeline, CreateKernelImplFromLambda)
  315. {
  316. cv::Size size(300, 300);
  317. int type = CV_8UC3;
  318. cv::Mat in_mat(size, type);
  319. cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255));
  320. int value = 5;
  321. cv::GMat in;
  322. cv::GMat out = GCustom::on(in);
  323. cv::GComputation comp(in, out);
  324. // OpenCV //////////////////////////////////////////////////////////////////////////
  325. auto ref_mat = in_mat + value;
  326. // G-API //////////////////////////////////////////////////////////////////////////
  327. auto impl = cv::gapi::cpu::ocv_kernel<GCustom>([&value](const cv::Mat& src, cv::Mat& dst)
  328. {
  329. dst = src + value;
  330. });
  331. cv::Mat out_mat;
  332. auto pkg = cv::gapi::kernels(impl);
  333. comp.apply(in_mat, out_mat, cv::compile_args(pkg));
  334. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  335. }
  336. TEST(GAPI_Pipeline, ReplaceDefaultByLambda)
  337. {
  338. cv::Size size(300, 300);
  339. int type = CV_8UC3;
  340. cv::Mat in_mat1(size, type);
  341. cv::Mat in_mat2(size, type);
  342. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  343. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  344. cv::GMat in1, in2;
  345. cv::GMat out = cv::gapi::add(in1, in2);
  346. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
  347. // OpenCV //////////////////////////////////////////////////////////////////////////
  348. cv::Mat ref_mat = in_mat1 + in_mat2;
  349. // G-API //////////////////////////////////////////////////////////////////////////
  350. bool is_called = false;
  351. auto impl = cv::gapi::cpu::ocv_kernel<cv::gapi::core::GAdd>([&is_called]
  352. (const cv::Mat& src1, const cv::Mat& src2, int, cv::Mat& dst)
  353. {
  354. is_called = true;
  355. dst = src1 + src2;
  356. });
  357. cv::Mat out_mat;
  358. auto pkg = cv::gapi::kernels(impl);
  359. comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat), cv::compile_args(pkg));
  360. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  361. EXPECT_TRUE(is_called);
  362. }
  363. struct AddImpl
  364. {
  365. void operator()(const cv::Mat& in1, const cv::Mat& in2, int, cv::Mat& out)
  366. {
  367. out = in1 + in2;
  368. is_called = true;
  369. }
  370. bool is_called = false;
  371. };
  372. TEST(GAPI_Pipeline, ReplaceDefaultByFunctor)
  373. {
  374. cv::Size size(300, 300);
  375. int type = CV_8UC3;
  376. cv::Mat in_mat1(size, type);
  377. cv::Mat in_mat2(size, type);
  378. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  379. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  380. cv::GMat in1, in2;
  381. cv::GMat out = cv::gapi::add(in1, in2);
  382. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
  383. // OpenCV //////////////////////////////////////////////////////////////////////////
  384. cv::Mat ref_mat = in_mat1 + in_mat2;
  385. // G-API ///////////////////////////////////////////////////////////////////////////
  386. AddImpl f;
  387. EXPECT_FALSE(f.is_called);
  388. auto impl = cv::gapi::cpu::ocv_kernel<cv::gapi::core::GAdd>(f);
  389. cv::Mat out_mat;
  390. auto pkg = cv::gapi::kernels(impl);
  391. comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat), cv::compile_args(pkg));
  392. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  393. EXPECT_TRUE(f.is_called);
  394. }
  395. TEST(GAPI_Pipeline, GraphOutputIs1DMat)
  396. {
  397. int dim = 100;
  398. cv::Mat in_mat(1, 1, CV_8UC3);
  399. cv::Mat out_mat;
  400. cv::GMat in;
  401. auto cc = cv::GComputation(in, GZeros::on(in, cv::GMatDesc(CV_8U, {dim})))
  402. .compile(cv::descr_of(in_mat), cv::compile_args(cv::gapi::kernels<GOCVZeros>()));
  403. // NB: Computation is able to write 1D output cv::Mat to empty out_mat.
  404. ASSERT_NO_THROW(cc(cv::gin(in_mat), cv::gout(out_mat)));
  405. ASSERT_EQ(1, out_mat.size.dims());
  406. ASSERT_EQ(dim, out_mat.size[0]);
  407. // NB: Computation is able to write 1D output cv::Mat
  408. // to pre-allocated with the same meta out_mat.
  409. ASSERT_NO_THROW(cc(cv::gin(in_mat), cv::gout(out_mat)));
  410. ASSERT_EQ(1, out_mat.size.dims());
  411. ASSERT_EQ(dim, out_mat.size[0]);
  412. }
  413. TEST(GAPI_Pipeline, 1DMatBetweenIslands)
  414. {
  415. int dim = 100;
  416. cv::Mat in_mat(1, 1, CV_8UC3);
  417. cv::Mat out_mat;
  418. cv::Mat ref_mat({dim}, CV_8U);
  419. ref_mat.dims = 1;
  420. ref_mat.setTo(0);
  421. cv::GMat in;
  422. auto out = cv::gapi::copy(GZeros::on(cv::gapi::copy(in), cv::GMatDesc(CV_8U, {dim})));
  423. auto cc = cv::GComputation(in, out)
  424. .compile(cv::descr_of(in_mat), cv::compile_args(cv::gapi::kernels<GOCVZeros>()));
  425. cc(cv::gin(in_mat), cv::gout(out_mat));
  426. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  427. }
  428. TEST(GAPI_Pipeline, 1DMatWithinSingleIsland)
  429. {
  430. int dim = 100;
  431. cv::Size blur_sz(3, 3);
  432. cv::Mat in_mat(10, 10, CV_8UC3);
  433. cv::randu(in_mat, 0, 255);
  434. cv::Mat out_mat;
  435. cv::Mat ref_mat({dim}, CV_8U);
  436. ref_mat.dims = 1;
  437. ref_mat.setTo(0);
  438. cv::GMat in;
  439. auto out = cv::gapi::blur(
  440. GZeros::on(cv::gapi::blur(in, blur_sz), cv::GMatDesc(CV_8U, {dim})), blur_sz);
  441. auto cc = cv::GComputation(in, out)
  442. .compile(cv::descr_of(in_mat), cv::compile_args(cv::gapi::kernels<GOCVZeros>()));
  443. cc(cv::gin(in_mat), cv::gout(out_mat));
  444. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  445. }
  446. TEST(GAPI_Pipeline, BranchesExecutedInParallel)
  447. {
  448. cv::GMat in;
  449. // NB: cv::gapi::copy used to prevent fusing OCV backend operations
  450. // into the single island where they will be executed in turn
  451. auto out0 = GBusyWait::on(cv::gapi::copy(in), 1000u /*1sec*/);
  452. auto out1 = GBusyWait::on(cv::gapi::copy(in), 1000u /*1sec*/);
  453. auto out2 = GBusyWait::on(cv::gapi::copy(in), 1000u /*1sec*/);
  454. auto out3 = GBusyWait::on(cv::gapi::copy(in), 1000u /*1sec*/);
  455. cv::GComputation comp(cv::GIn(in), cv::GOut(out0,out1,out2,out3));
  456. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  457. cv::Mat out_mat0, out_mat1, out_mat2, out_mat3;
  458. using namespace std::chrono;
  459. auto s = high_resolution_clock::now();
  460. comp.apply(cv::gin(in_mat), cv::gout(out_mat0, out_mat1, out_mat2, out_mat3),
  461. cv::compile_args(cv::use_threaded_executor(4u),
  462. cv::gapi::kernels<GOCVBusyWait>()));
  463. auto e = high_resolution_clock::now();
  464. const auto elapsed_in_ms = duration_cast<milliseconds>(e-s).count();;
  465. EXPECT_GE(1200u, elapsed_in_ms);
  466. }
  467. } // namespace opencv_test