gapi_sample_pipelines.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 <stdexcept>
  8. #include <ade/util/iota_range.hpp>
  9. #include "logger.hpp"
  10. #include <opencv2/gapi/core.hpp>
  11. namespace opencv_test
  12. {
  13. namespace
  14. {
  15. G_TYPED_KERNEL(GInvalidResize, <GMat(GMat,Size,double,double,int)>, "org.opencv.test.invalid_resize")
  16. {
  17. static GMatDesc outMeta(GMatDesc in, Size, double, double, int) { return in; }
  18. };
  19. GAPI_OCV_KERNEL(GOCVInvalidResize, GInvalidResize)
  20. {
  21. static void run(const cv::Mat& in, cv::Size sz, double fx, double fy, int interp, cv::Mat &out)
  22. {
  23. cv::resize(in, out, sz, fx, fy, interp);
  24. }
  25. };
  26. G_TYPED_KERNEL(GReallocatingCopy, <GMat(GMat)>, "org.opencv.test.reallocating_copy")
  27. {
  28. static GMatDesc outMeta(GMatDesc in) { return in; }
  29. };
  30. GAPI_OCV_KERNEL(GOCVReallocatingCopy, GReallocatingCopy)
  31. {
  32. static void run(const cv::Mat& in, cv::Mat &out)
  33. {
  34. out = in.clone();
  35. }
  36. };
  37. G_TYPED_KERNEL(GCustom, <GMat(GMat)>, "org.opencv.test.custom")
  38. {
  39. static GMatDesc outMeta(GMatDesc in) { return in; }
  40. };
  41. // These definitons test the correct macro work if the kernel has multiple output values
  42. G_TYPED_KERNEL(GRetGArrayTupleOfGMat2Kernel, <GArray<std::tuple<GMat, GMat>>(GMat, Scalar)>, "org.opencv.test.retarrayoftupleofgmat2kernel") {};
  43. G_TYPED_KERNEL(GRetGArraTupleyOfGMat3Kernel, <GArray<std::tuple<GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat3kernel") {};
  44. G_TYPED_KERNEL(GRetGArraTupleyOfGMat4Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat4kernel") {};
  45. G_TYPED_KERNEL(GRetGArraTupleyOfGMat5Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat5kernel") {};
  46. G_TYPED_KERNEL(GRetGArraTupleyOfGMat6Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat6kernel") {};
  47. G_TYPED_KERNEL(GRetGArraTupleyOfGMat7Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat7kernel") {};
  48. G_TYPED_KERNEL(GRetGArraTupleyOfGMat8Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat8kernel") {};
  49. G_TYPED_KERNEL(GRetGArraTupleyOfGMat9Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat9kernel") {};
  50. G_TYPED_KERNEL(GRetGArraTupleyOfGMat10Kernel, <GArray<std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>>(GMat)>, "org.opencv.test.retarrayoftupleofgmat10kernel") {};
  51. G_TYPED_KERNEL_M(GRetGMat2Kernel, <std::tuple<GMat, GMat>(GMat, GMat, GMat)>, "org.opencv.test.retgmat2kernel") {};
  52. G_TYPED_KERNEL_M(GRetGMat3Kernel, <std::tuple<GMat, GMat, GMat>(GMat, GScalar)>, "org.opencv.test.retgmat3kernel") {};
  53. G_TYPED_KERNEL_M(GRetGMat4Kernel, <std::tuple<GMat, GMat, GMat, GMat>(GMat, GArray<int>, GScalar)>, "org.opencv.test.retgmat4kernel") {};
  54. G_TYPED_KERNEL_M(GRetGMat5Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat5kernel") {};
  55. G_TYPED_KERNEL_M(GRetGMat6Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat6kernel") {};
  56. G_TYPED_KERNEL_M(GRetGMat7Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat7kernel") {};
  57. G_TYPED_KERNEL_M(GRetGMat8Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat8kernel") {};
  58. G_TYPED_KERNEL_M(GRetGMat9Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat9kernel") {};
  59. G_TYPED_KERNEL_M(GRetGMat10Kernel, <std::tuple<GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat, GMat>(GMat)>, "org.opencv.test.retgmat10kernel") {};
  60. }
  61. TEST(GAPI_Pipeline, OverloadUnary_MatMat)
  62. {
  63. cv::GMat in;
  64. cv::GComputation comp(in, cv::gapi::bitwise_not(in));
  65. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  66. cv::Mat ref_mat = ~in_mat;
  67. cv::Mat out_mat;
  68. comp.apply(in_mat, out_mat);
  69. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  70. out_mat = cv::Mat();
  71. auto cc = comp.compile(cv::descr_of(in_mat));
  72. cc(in_mat, out_mat);
  73. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  74. }
  75. TEST(GAPI_Pipeline, OverloadUnary_MatScalar)
  76. {
  77. cv::GMat in;
  78. cv::GComputation comp(in, cv::gapi::sum(in));
  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. comp.apply(in_mat, out_scl);
  83. EXPECT_EQ(out_scl, ref_scl);
  84. out_scl = cv::Scalar();
  85. auto cc = comp.compile(cv::descr_of(in_mat));
  86. cc(in_mat, out_scl);
  87. EXPECT_EQ(out_scl, ref_scl);
  88. }
  89. TEST(GAPI_Pipeline, OverloadBinary_Mat)
  90. {
  91. cv::GMat a, b;
  92. cv::GComputation comp(a, b, cv::gapi::add(a, b));
  93. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  94. cv::Mat ref_mat = (in_mat+in_mat);
  95. cv::Mat out_mat;
  96. comp.apply(in_mat, in_mat, out_mat);
  97. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  98. out_mat = cv::Mat();
  99. auto cc = comp.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));
  100. cc(in_mat, in_mat, out_mat);
  101. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  102. }
  103. TEST(GAPI_Pipeline, OverloadBinary_Scalar)
  104. {
  105. cv::GMat a, b;
  106. cv::GComputation comp(a, b, cv::gapi::sum(a + b));
  107. cv::Mat in_mat = cv::Mat::eye(32, 32, CV_8UC1);
  108. cv::Scalar ref_scl = cv::sum(in_mat+in_mat);
  109. cv::Scalar out_scl;
  110. comp.apply(in_mat, in_mat, out_scl);
  111. EXPECT_EQ(out_scl, ref_scl);
  112. out_scl = cv::Scalar();
  113. auto cc = comp.compile(cv::descr_of(in_mat), cv::descr_of(in_mat));
  114. cc(in_mat, in_mat, out_scl);
  115. EXPECT_EQ(out_scl, ref_scl);
  116. }
  117. TEST(GAPI_Pipeline, Sharpen)
  118. {
  119. const cv::Size sz_in (1280, 720);
  120. const cv::Size sz_out( 640, 480);
  121. cv::Mat in_mat (sz_in, CV_8UC3);
  122. in_mat = cv::Scalar(128, 33, 53);
  123. cv::Mat out_mat(sz_out, CV_8UC3);
  124. cv::Mat out_mat_y;
  125. cv::Mat out_mat_ocv(sz_out, CV_8UC3);
  126. float sharpen_coeffs[] = {
  127. 0.0f, -1.f, 0.0f,
  128. -1.0f, 5.f, -1.0f,
  129. 0.0f, -1.f, 0.0f
  130. };
  131. cv::Mat sharpen_kernel(3, 3, CV_32F, sharpen_coeffs);
  132. // G-API code //////////////////////////////////////////////////////////////
  133. cv::GMat in;
  134. auto vga = cv::gapi::resize(in, sz_out);
  135. auto yuv = cv::gapi::RGB2YUV(vga);
  136. auto yuv_p = cv::gapi::split3(yuv);
  137. auto y_sharp = cv::gapi::filter2D(std::get<0>(yuv_p), -1, sharpen_kernel);
  138. auto yuv_new = cv::gapi::merge3(y_sharp, std::get<1>(yuv_p), std::get<2>(yuv_p));
  139. auto out = cv::gapi::YUV2RGB(yuv_new);
  140. cv::GComputation c(cv::GIn(in), cv::GOut(y_sharp, out));
  141. c.apply(cv::gin(in_mat), cv::gout(out_mat_y, out_mat));
  142. // OpenCV code /////////////////////////////////////////////////////////////
  143. {
  144. cv::Mat smaller;
  145. cv::resize(in_mat, smaller, sz_out);
  146. cv::Mat yuv_mat;
  147. cv::cvtColor(smaller, yuv_mat, cv::COLOR_RGB2YUV);
  148. std::vector<cv::Mat> yuv_planar(3);
  149. cv::split(yuv_mat, yuv_planar);
  150. cv::filter2D(yuv_planar[0], yuv_planar[0], -1, sharpen_kernel);
  151. cv::merge(yuv_planar, yuv_mat);
  152. cv::cvtColor(yuv_mat, out_mat_ocv, cv::COLOR_YUV2RGB);
  153. }
  154. // Comparison //////////////////////////////////////////////////////////////
  155. {
  156. cv::Mat diff = out_mat_ocv != out_mat;
  157. std::vector<cv::Mat> diffBGR(3);
  158. cv::split(diff, diffBGR);
  159. EXPECT_EQ(0, cvtest::norm(diffBGR[0], NORM_INF));
  160. EXPECT_EQ(0, cvtest::norm(diffBGR[1], NORM_INF));
  161. EXPECT_EQ(0, cvtest::norm(diffBGR[2], NORM_INF));
  162. }
  163. // Metadata check /////////////////////////////////////////////////////////
  164. {
  165. auto cc = c.compile(cv::descr_of(in_mat));
  166. auto metas = cc.outMetas();
  167. ASSERT_EQ(2u, metas.size());
  168. auto out_y_meta = cv::util::get<cv::GMatDesc>(metas[0]);
  169. auto out_meta = cv::util::get<cv::GMatDesc>(metas[1]);
  170. // Y-output
  171. EXPECT_EQ(CV_8U, out_y_meta.depth);
  172. EXPECT_EQ(1, out_y_meta.chan);
  173. EXPECT_EQ(640, out_y_meta.size.width);
  174. EXPECT_EQ(480, out_y_meta.size.height);
  175. // Final output
  176. EXPECT_EQ(CV_8U, out_meta.depth);
  177. EXPECT_EQ(3, out_meta.chan);
  178. EXPECT_EQ(640, out_meta.size.width);
  179. EXPECT_EQ(480, out_meta.size.height);
  180. }
  181. }
  182. TEST(GAPI_Pipeline, CustomRGB2YUV)
  183. {
  184. const cv::Size sz(1280, 720);
  185. // BEWARE:
  186. //
  187. // std::vector<cv::Mat> out_mats_cv(3, cv::Mat(sz, CV_8U))
  188. //
  189. // creates a vector of 3 elements pointing to the same Mat!
  190. // FIXME: Make a G-API check for that
  191. const int INS = 3;
  192. std::vector<cv::Mat> in_mats(INS);
  193. for (auto i : ade::util::iota(INS))
  194. {
  195. in_mats[i].create(sz, CV_8U);
  196. cv::randu(in_mats[i], cv::Scalar::all(0), cv::Scalar::all(255));
  197. }
  198. const int OUTS = 3;
  199. std::vector<cv::Mat> out_mats_cv(OUTS);
  200. std::vector<cv::Mat> out_mats_gapi(OUTS);
  201. for (auto i : ade::util::iota(OUTS))
  202. {
  203. out_mats_cv [i].create(sz, CV_8U);
  204. out_mats_gapi[i].create(sz, CV_8U);
  205. }
  206. // G-API code //////////////////////////////////////////////////////////////
  207. {
  208. cv::GMat r, g, b;
  209. cv::GMat y = 0.299f*r + 0.587f*g + 0.114f*b;
  210. cv::GMat u = 0.492f*(b - y);
  211. cv::GMat v = 0.877f*(r - y);
  212. cv::GComputation customCvt({r, g, b}, {y, u, v});
  213. customCvt.apply(in_mats, out_mats_gapi);
  214. }
  215. // OpenCV code /////////////////////////////////////////////////////////////
  216. {
  217. cv::Mat r = in_mats[0], g = in_mats[1], b = in_mats[2];
  218. cv::Mat y = 0.299f*r + 0.587f*g + 0.114f*b;
  219. cv::Mat u = 0.492f*(b - y);
  220. cv::Mat v = 0.877f*(r - y);
  221. out_mats_cv[0] = y;
  222. out_mats_cv[1] = u;
  223. out_mats_cv[2] = v;
  224. }
  225. // Comparison //////////////////////////////////////////////////////////////
  226. {
  227. const auto diff = [](cv::Mat m1, cv::Mat m2, int t) {
  228. return cv::abs(m1-m2) > t;
  229. };
  230. // FIXME: Not bit-accurate even now!
  231. cv::Mat
  232. diff_y = diff(out_mats_cv[0], out_mats_gapi[0], 2),
  233. diff_u = diff(out_mats_cv[1], out_mats_gapi[1], 2),
  234. diff_v = diff(out_mats_cv[2], out_mats_gapi[2], 2);
  235. EXPECT_EQ(0, cvtest::norm(diff_y, NORM_INF));
  236. EXPECT_EQ(0, cvtest::norm(diff_u, NORM_INF));
  237. EXPECT_EQ(0, cvtest::norm(diff_v, NORM_INF));
  238. }
  239. }
  240. TEST(GAPI_Pipeline, PipelineWithInvalidKernel)
  241. {
  242. cv::GMat in, out;
  243. cv::Mat in_mat(500, 500, CV_8UC1), out_mat;
  244. out = GInvalidResize::on(in, cv::Size(300, 300), 0.0, 0.0, cv::INTER_LINEAR);
  245. const auto pkg = cv::gapi::kernels<GOCVInvalidResize>();
  246. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  247. EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error);
  248. }
  249. TEST(GAPI_Pipeline, InvalidOutputComputation)
  250. {
  251. cv::GMat in1, out1, out2, out3;
  252. std::tie(out1, out2, out2) = cv::gapi::split3(in1);
  253. cv::GComputation c({in1}, {out1, out2, out3});
  254. cv::Mat in_mat;
  255. cv::Mat out_mat1, out_mat2, out_mat3, out_mat4;
  256. std::vector<cv::Mat> u_outs = {out_mat1, out_mat2, out_mat3, out_mat4};
  257. std::vector<cv::Mat> u_ins = {in_mat};
  258. EXPECT_THROW(c.apply(u_ins, u_outs), std::logic_error);
  259. }
  260. TEST(GAPI_Pipeline, PipelineAllocatingKernel)
  261. {
  262. cv::GMat in, out;
  263. cv::Mat in_mat(500, 500, CV_8UC1), out_mat;
  264. out = GReallocatingCopy::on(in);
  265. const auto pkg = cv::gapi::kernels<GOCVReallocatingCopy>();
  266. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  267. EXPECT_THROW(comp.apply(in_mat, out_mat, cv::compile_args(pkg)), std::logic_error);
  268. }
  269. TEST(GAPI_Pipeline, CreateKernelImplFromLambda)
  270. {
  271. cv::Size size(300, 300);
  272. int type = CV_8UC3;
  273. cv::Mat in_mat(size, type);
  274. cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(255));
  275. int value = 5;
  276. cv::GMat in;
  277. cv::GMat out = GCustom::on(in);
  278. cv::GComputation comp(in, out);
  279. // OpenCV //////////////////////////////////////////////////////////////////////////
  280. auto ref_mat = in_mat + value;
  281. // G-API //////////////////////////////////////////////////////////////////////////
  282. auto impl = cv::gapi::cpu::ocv_kernel<GCustom>([&value](const cv::Mat& src, cv::Mat& dst)
  283. {
  284. dst = src + value;
  285. });
  286. cv::Mat out_mat;
  287. auto pkg = cv::gapi::kernels(impl);
  288. comp.apply(in_mat, out_mat, cv::compile_args(pkg));
  289. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  290. }
  291. TEST(GAPI_Pipeline, ReplaceDefaultByLambda)
  292. {
  293. cv::Size size(300, 300);
  294. int type = CV_8UC3;
  295. cv::Mat in_mat1(size, type);
  296. cv::Mat in_mat2(size, type);
  297. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  298. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  299. cv::GMat in1, in2;
  300. cv::GMat out = cv::gapi::add(in1, in2);
  301. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
  302. // OpenCV //////////////////////////////////////////////////////////////////////////
  303. cv::Mat ref_mat = in_mat1 + in_mat2;
  304. // G-API //////////////////////////////////////////////////////////////////////////
  305. bool is_called = false;
  306. auto impl = cv::gapi::cpu::ocv_kernel<cv::gapi::core::GAdd>([&is_called]
  307. (const cv::Mat& src1, const cv::Mat& src2, int, cv::Mat& dst)
  308. {
  309. is_called = true;
  310. dst = src1 + src2;
  311. });
  312. cv::Mat out_mat;
  313. auto pkg = cv::gapi::kernels(impl);
  314. comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat), cv::compile_args(pkg));
  315. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  316. EXPECT_TRUE(is_called);
  317. }
  318. struct AddImpl
  319. {
  320. void operator()(const cv::Mat& in1, const cv::Mat& in2, int, cv::Mat& out)
  321. {
  322. out = in1 + in2;
  323. is_called = true;
  324. }
  325. bool is_called = false;
  326. };
  327. TEST(GAPI_Pipeline, ReplaceDefaultByFunctor)
  328. {
  329. cv::Size size(300, 300);
  330. int type = CV_8UC3;
  331. cv::Mat in_mat1(size, type);
  332. cv::Mat in_mat2(size, type);
  333. cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
  334. cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
  335. cv::GMat in1, in2;
  336. cv::GMat out = cv::gapi::add(in1, in2);
  337. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
  338. // OpenCV //////////////////////////////////////////////////////////////////////////
  339. cv::Mat ref_mat = in_mat1 + in_mat2;
  340. // G-API ///////////////////////////////////////////////////////////////////////////
  341. AddImpl f;
  342. EXPECT_FALSE(f.is_called);
  343. auto impl = cv::gapi::cpu::ocv_kernel<cv::gapi::core::GAdd>(f);
  344. cv::Mat out_mat;
  345. auto pkg = cv::gapi::kernels(impl);
  346. comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat), cv::compile_args(pkg));
  347. EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
  348. EXPECT_TRUE(f.is_called);
  349. }
  350. } // namespace opencv_test