gapi_compoundkernel_tests.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. // FIXME: move out from Common
  7. #include "../test_precomp.hpp"
  8. #include <opencv2/gapi/cpu/core.hpp>
  9. #include <ade/util/algorithm.hpp>
  10. namespace opencv_test
  11. {
  12. namespace
  13. {
  14. G_TYPED_KERNEL(GCompoundDoubleAddC, <GMat(GMat, GScalar)>, "org.opencv.test.compound_double_addC")
  15. {
  16. static GMatDesc outMeta(GMatDesc in, GScalarDesc) { return in; }
  17. };
  18. GAPI_COMPOUND_KERNEL(GCompoundDoubleAddCImpl, GCompoundDoubleAddC)
  19. {
  20. static GMat expand(cv::GMat in, cv::GScalar s)
  21. {
  22. return cv::gapi::addC(cv::gapi::addC(in, s), s);
  23. }
  24. };
  25. G_TYPED_KERNEL(GCompoundAddC, <GMat(GMat, GScalar)>, "org.opencv.test.compound_addC")
  26. {
  27. static GMatDesc outMeta(GMatDesc in, GScalarDesc) { return in; }
  28. };
  29. GAPI_COMPOUND_KERNEL(GCompoundAddCImpl, GCompoundAddC)
  30. {
  31. static GMat expand(cv::GMat in, cv::GScalar s)
  32. {
  33. return cv::gapi::addC(in, s);
  34. }
  35. };
  36. using GMat3 = std::tuple<GMat,GMat,GMat>;
  37. using GMat2 = std::tuple<GMat,GMat>;
  38. G_TYPED_KERNEL_M(GCompoundMergeWithSplit, <GMat3(GMat, GMat, GMat)>, "org.opencv.test.compound_merge_split")
  39. {
  40. static std::tuple<GMatDesc,GMatDesc,GMatDesc> outMeta(GMatDesc a, GMatDesc b, GMatDesc c)
  41. {
  42. return std::make_tuple(a, b, c);
  43. }
  44. };
  45. GAPI_COMPOUND_KERNEL(GCompoundMergeWithSplitImpl, GCompoundMergeWithSplit)
  46. {
  47. static GMat3 expand(cv::GMat a, cv::GMat b, cv::GMat c)
  48. {
  49. return cv::gapi::split3(cv::gapi::merge3(a, b, c));
  50. }
  51. };
  52. G_TYPED_KERNEL(GCompoundAddWithAddC, <GMat(GMat, GMat, GScalar)>, "org.opencv.test.compound_add_with_addc")
  53. {
  54. static GMatDesc outMeta(GMatDesc in, GMatDesc, GScalarDesc)
  55. {
  56. return in;
  57. }
  58. };
  59. GAPI_COMPOUND_KERNEL(GCompoundAddWithAddCImpl, GCompoundAddWithAddC)
  60. {
  61. static GMat expand(cv::GMat in1, cv::GMat in2, cv::GScalar s)
  62. {
  63. return cv::gapi::addC(cv::gapi::add(in1, in2), s);
  64. }
  65. };
  66. G_TYPED_KERNEL_M(GCompoundSplitWithAdd, <GMat2(GMat)>, "org.opencv.test.compound_split_with_add")
  67. {
  68. static std::tuple<GMatDesc, GMatDesc> outMeta(GMatDesc in)
  69. {
  70. const auto out_depth = in.depth;
  71. const auto out_desc = in.withType(out_depth, 1);
  72. return std::make_tuple(out_desc, out_desc);
  73. }
  74. };
  75. GAPI_COMPOUND_KERNEL(GCompoundSplitWithAddImpl, GCompoundSplitWithAdd)
  76. {
  77. static GMat2 expand(cv::GMat in)
  78. {
  79. cv::GMat a, b, c;
  80. std::tie(a, b, c) = cv::gapi::split3(in);
  81. return std::make_tuple(cv::gapi::add(a, b), c);
  82. }
  83. };
  84. G_TYPED_KERNEL_M(GCompoundParallelAddC, <GMat2(GMat, GScalar)>, "org.opencv.test.compound_parallel_addc")
  85. {
  86. static std::tuple<GMatDesc, GMatDesc> outMeta(GMatDesc in, GScalarDesc)
  87. {
  88. return std::make_tuple(in, in);
  89. }
  90. };
  91. GAPI_COMPOUND_KERNEL(GCompoundParallelAddCImpl, GCompoundParallelAddC)
  92. {
  93. static GMat2 expand(cv::GMat in, cv::GScalar s)
  94. {
  95. return std::make_tuple(cv::gapi::addC(in, s), cv::gapi::addC(in, s));
  96. }
  97. };
  98. GAPI_COMPOUND_KERNEL(GCompoundAddImpl, cv::gapi::core::GAdd)
  99. {
  100. static GMat expand(cv::GMat in1, cv::GMat in2, int)
  101. {
  102. return cv::gapi::sub(cv::gapi::sub(in1, in2), in2);
  103. }
  104. };
  105. G_TYPED_KERNEL(GCompoundAddWithAddCWithDoubleAddC, <GMat(GMat, GMat, GScalar)>, "org.opencv.test.compound_add_with_addC_with_double_addC")
  106. {
  107. static GMatDesc outMeta(GMatDesc in, GMatDesc, GScalarDesc)
  108. {
  109. return in;
  110. }
  111. };
  112. GAPI_COMPOUND_KERNEL(GCompoundAddWithAddCWithDoubleAddCImpl, GCompoundAddWithAddCWithDoubleAddC)
  113. {
  114. static GMat expand(cv::GMat in1, cv::GMat in2, cv::GScalar s)
  115. {
  116. return GCompoundDoubleAddC::on(GCompoundAddWithAddC::on(in1, in2, s), s);
  117. }
  118. };
  119. using GDoubleArray = cv::GArray<double>;
  120. G_TYPED_KERNEL(GNegateArray, <GDoubleArray(GDoubleArray)>, "org.opencv.test.negate_array")
  121. {
  122. static GArrayDesc outMeta(const GArrayDesc&) { return empty_array_desc(); }
  123. };
  124. GAPI_OCV_KERNEL(GNegateArrayImpl, GNegateArray)
  125. {
  126. static void run(const std::vector<double>& in, std::vector<double>& out)
  127. {
  128. ade::util::transform(in, std::back_inserter(out), std::negate<double>());
  129. }
  130. };
  131. G_TYPED_KERNEL(GMaxInArray, <GScalar(GDoubleArray)>, "org.opencv.test.max_in_array")
  132. {
  133. static GScalarDesc outMeta(const GArrayDesc&) { return empty_scalar_desc(); }
  134. };
  135. GAPI_OCV_KERNEL(GMaxInArrayImpl, GMaxInArray)
  136. {
  137. static void run(const std::vector<double>& in, cv::Scalar& out)
  138. {
  139. out = *std::max_element(in.begin(), in.end());
  140. }
  141. };
  142. G_TYPED_KERNEL(GCompoundMaxInArray, <GScalar(GDoubleArray)>, "org.opencv.test.compound_max_in_array")
  143. {
  144. static GScalarDesc outMeta(const GArrayDesc&) { return empty_scalar_desc(); }
  145. };
  146. GAPI_COMPOUND_KERNEL(GCompoundMaxInArrayImpl, GCompoundMaxInArray)
  147. {
  148. static GScalar expand(GDoubleArray in)
  149. {
  150. return GMaxInArray::on(in);
  151. }
  152. };
  153. G_TYPED_KERNEL(GCompoundNegateArray, <GDoubleArray(GDoubleArray)>, "org.opencv.test.compound_negate_array")
  154. {
  155. static GArrayDesc outMeta(const GArrayDesc&) { return empty_array_desc(); }
  156. };
  157. GAPI_COMPOUND_KERNEL(GCompoundNegateArrayImpl, GCompoundNegateArray)
  158. {
  159. static GDoubleArray expand(GDoubleArray in)
  160. {
  161. return GNegateArray::on(in);
  162. }
  163. };
  164. G_TYPED_KERNEL(SetDiagKernel, <GMat(GMat, GDoubleArray)>, "org.opencv.test.empty_kernel")
  165. {
  166. static GMatDesc outMeta(GMatDesc in, GArrayDesc) { return in; }
  167. };
  168. void setDiag(cv::Mat& in, const std::vector<double>& diag)
  169. {
  170. GAPI_Assert(in.rows == static_cast<int>(diag.size()));
  171. GAPI_Assert(in.cols == static_cast<int>(diag.size()));
  172. for (int i = 0; i < in.rows; ++i)
  173. {
  174. in.at<uchar>(i, i) = static_cast<uchar>(diag[i]);
  175. }
  176. }
  177. GAPI_OCV_KERNEL(SetDiagKernelImpl, SetDiagKernel)
  178. {
  179. static void run(const cv::Mat& in, const std::vector<double>& v, cv::Mat& out)
  180. {
  181. in.copyTo(out);
  182. setDiag(out, v);
  183. }
  184. };
  185. G_TYPED_KERNEL(GCompoundGMatGArrayGMat, <GMat(GMat, GDoubleArray, GMat)>, "org.opencv.test.compound_gmat_garray_gmat")
  186. {
  187. static GMatDesc outMeta(GMatDesc in, GArrayDesc, GMatDesc) { return in; }
  188. };
  189. GAPI_COMPOUND_KERNEL(GCompoundGMatGArrayGMatImpl, GCompoundGMatGArrayGMat)
  190. {
  191. static GMat expand(GMat a, GDoubleArray b, GMat c)
  192. {
  193. return SetDiagKernel::on(cv::gapi::add(a, c), b);
  194. }
  195. };
  196. G_TYPED_KERNEL(GToInterleaved, <GMat(GMatP)>, "org.opencv.test.to_interleaved")
  197. {
  198. static GMatDesc outMeta(GMatDesc in)
  199. {
  200. GAPI_Assert(in.planar == true);
  201. GAPI_Assert(in.chan == 3);
  202. return in.asInterleaved();
  203. }
  204. };
  205. G_TYPED_KERNEL(GToPlanar, <GMatP(GMat)>, "org.opencv.test.to_planar")
  206. {
  207. static GMatDesc outMeta(GMatDesc in)
  208. {
  209. GAPI_Assert(in.planar == false);
  210. GAPI_Assert(in.chan == 3);
  211. return in.asPlanar();
  212. }
  213. };
  214. GAPI_OCV_KERNEL(GToInterleavedImpl, GToInterleaved)
  215. {
  216. static void run(const cv::Mat& in, cv::Mat& out)
  217. {
  218. constexpr int inPlanesCount = 3;
  219. int inPlaneHeight = in.rows / inPlanesCount;
  220. std::vector<cv::Mat> inPlanes(inPlanesCount);
  221. for (int i = 0; i < inPlanesCount; ++i)
  222. {
  223. int startRow = i * inPlaneHeight;
  224. int endRow = startRow + inPlaneHeight;
  225. inPlanes[i] = in.rowRange(startRow, endRow);
  226. }
  227. cv::merge(inPlanes, out);
  228. }
  229. };
  230. GAPI_OCV_KERNEL(GToPlanarImpl, GToPlanar)
  231. {
  232. static void run(const cv::Mat& in, cv::Mat& out)
  233. {
  234. std::vector<cv::Mat> inPlanes;
  235. cv::split(in, inPlanes);
  236. cv::vconcat(inPlanes, out);
  237. }
  238. };
  239. G_TYPED_KERNEL(GCompoundToInterleavedToPlanar, <GMatP(GMatP)>,
  240. "org.opencv.test.compound_to_interleaved_to_planar")
  241. {
  242. static GMatDesc outMeta(GMatDesc in)
  243. {
  244. GAPI_Assert(in.planar == true);
  245. GAPI_Assert(in.chan == 3);
  246. return in;
  247. }
  248. };
  249. GAPI_COMPOUND_KERNEL(GCompoundToInterleavedToPlanarImpl, GCompoundToInterleavedToPlanar)
  250. {
  251. static GMatP expand(cv::GMatP in)
  252. {
  253. return GToPlanar::on(GToInterleaved::on(in));
  254. }
  255. };
  256. } // namespace
  257. // FIXME avoid cv::combine that use custom and default kernels together
  258. TEST(GCompoundKernel, ReplaceDefaultKernel)
  259. {
  260. cv::GMat in1, in2;
  261. auto out = cv::gapi::add(in1, in2);
  262. const auto custom_pkg = cv::gapi::kernels<GCompoundAddImpl>();
  263. const auto full_pkg = cv::gapi::combine(cv::gapi::core::cpu::kernels(), custom_pkg);
  264. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
  265. cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
  266. in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
  267. out_mat(3, 3, CV_8UC1),
  268. ref_mat(3, 3, CV_8UC1);
  269. comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat), cv::compile_args(full_pkg));
  270. ref_mat = in_mat1 - in_mat2 - in_mat2;
  271. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  272. }
  273. TEST(GCompoundKernel, DoubleAddC)
  274. {
  275. cv::GMat in1, in2;
  276. cv::GScalar s;
  277. auto add_res = cv::gapi::add(in1, in2);
  278. auto super = GCompoundDoubleAddC::on(add_res, s);
  279. auto out = cv::gapi::addC(super, s);
  280. const auto custom_pkg = cv::gapi::kernels<GCompoundDoubleAddCImpl>();
  281. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  282. cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
  283. cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
  284. in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
  285. out_mat(3, 3, CV_8UC1),
  286. ref_mat(3, 3, CV_8UC1);
  287. cv::Scalar scalar = 2;
  288. comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
  289. ref_mat = in_mat1 + in_mat2 + scalar + scalar + scalar;
  290. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  291. }
  292. TEST(GCompoundKernel, AddC)
  293. {
  294. cv::GMat in1, in2;
  295. cv::GScalar s;
  296. auto add_res = cv::gapi::add(in1, in2);
  297. auto super = GCompoundAddC::on(add_res, s);
  298. auto out = cv::gapi::addC(super, s);
  299. const auto custom_pkg = cv::gapi::kernels<GCompoundAddCImpl>();
  300. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  301. cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
  302. cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
  303. in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
  304. out_mat(3, 3, CV_8UC1),
  305. ref_mat(3, 3, CV_8UC1);
  306. cv::Scalar scalar = 2;
  307. comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
  308. ref_mat = in_mat1 + in_mat2 + scalar + scalar;
  309. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  310. }
  311. TEST(GCompoundKernel, MergeWithSplit)
  312. {
  313. cv::GMat in, a1, b1, c1,
  314. a2, b2, c2;
  315. std::tie(a1, b1, c1) = cv::gapi::split3(in);
  316. std::tie(a2, b2, c2) = GCompoundMergeWithSplit::on(a1, b1, c1);
  317. auto out = cv::gapi::merge3(a2, b2, c2);
  318. const auto custom_pkg = cv::gapi::kernels<GCompoundMergeWithSplitImpl>();
  319. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  320. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  321. cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC3), out_mat, ref_mat;
  322. comp.apply(cv::gin(in_mat), cv::gout(out_mat), cv::compile_args(full_pkg));
  323. ref_mat = in_mat;
  324. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  325. }
  326. TEST(GCompoundKernel, AddWithAddC)
  327. {
  328. cv::GMat in1, in2;
  329. cv::GScalar s;
  330. auto out = GCompoundAddWithAddC::on(in1, in2, s);
  331. const auto custom_pkg = cv::gapi::kernels<GCompoundAddWithAddCImpl>();
  332. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  333. cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
  334. cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
  335. in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
  336. out_mat(3, 3, CV_8UC1),
  337. ref_mat(3, 3, CV_8UC1);
  338. cv::Scalar scalar = 2;
  339. comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
  340. ref_mat = in_mat1 + in_mat2 + scalar;
  341. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  342. }
  343. TEST(GCompoundKernel, SplitWithAdd)
  344. {
  345. cv::GMat in, out1, out2;
  346. std::tie(out1, out2) = GCompoundSplitWithAdd::on(in);
  347. const auto custom_pkg = cv::gapi::kernels<GCompoundSplitWithAddImpl>();
  348. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  349. cv::GComputation comp(cv::GIn(in), cv::GOut(out1, out2));
  350. cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC3),
  351. out_mat1(3, 3, CV_8UC1),
  352. out_mat2(3, 3, CV_8UC1),
  353. ref_mat1(3, 3, CV_8UC1),
  354. ref_mat2(3, 3, CV_8UC1);
  355. comp.apply(cv::gin(in_mat), cv::gout(out_mat1, out_mat2), cv::compile_args(full_pkg));
  356. std::vector<cv::Mat> channels(3);
  357. cv::split(in_mat, channels);
  358. ref_mat1 = channels[0] + channels[1];
  359. ref_mat2 = channels[2];
  360. EXPECT_EQ(0, cvtest::norm(out_mat1, ref_mat1, NORM_INF));
  361. EXPECT_EQ(0, cvtest::norm(out_mat2, ref_mat2, NORM_INF));
  362. }
  363. TEST(GCompoundKernel, ParallelAddC)
  364. {
  365. cv::GMat in1, out1, out2;
  366. cv::GScalar in2;
  367. std::tie(out1, out2) = GCompoundParallelAddC::on(in1, in2);
  368. const auto custom_pkg = cv::gapi::kernels<GCompoundParallelAddCImpl>();
  369. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  370. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out1, out2));
  371. cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1),
  372. out_mat1(3, 3, CV_8UC1),
  373. out_mat2(3, 3, CV_8UC1),
  374. ref_mat1(3, 3, CV_8UC1),
  375. ref_mat2(3, 3, CV_8UC1);
  376. cv::Scalar scalar = 2;
  377. comp.apply(cv::gin(in_mat, scalar), cv::gout(out_mat1, out_mat2), cv::compile_args(full_pkg));
  378. ref_mat1 = in_mat + scalar;
  379. ref_mat2 = in_mat + scalar;
  380. EXPECT_EQ(0, cvtest::norm(out_mat1, ref_mat1, NORM_INF));
  381. EXPECT_EQ(0, cvtest::norm(out_mat2, ref_mat2, NORM_INF));
  382. }
  383. TEST(GCompoundKernel, GCompundKernelAndDefaultUseOneData)
  384. {
  385. cv::GMat in1, in2;
  386. cv::GScalar s;
  387. auto out = cv::gapi::add(GCompoundAddWithAddC::on(in1, in2, s), cv::gapi::addC(in2, s));
  388. const auto custom_pkg = cv::gapi::kernels<GCompoundAddWithAddCImpl>();
  389. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  390. cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
  391. cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
  392. in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
  393. out_mat(3, 3, CV_8UC1),
  394. ref_mat(3, 3, CV_8UC1);
  395. cv::Scalar scalar = 2;
  396. comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
  397. ref_mat = in_mat1 + in_mat2 + scalar + in_mat2 + scalar;
  398. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  399. }
  400. TEST(GCompoundKernel, CompoundExpandedToCompound)
  401. {
  402. cv::GMat in1, in2;
  403. cv::GScalar s;
  404. auto out = GCompoundAddWithAddCWithDoubleAddC::on(in1, in2, s);
  405. const auto custom_pkg = cv::gapi::kernels<GCompoundAddWithAddCWithDoubleAddCImpl,
  406. GCompoundAddWithAddCImpl,
  407. GCompoundDoubleAddCImpl>();
  408. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  409. cv::GComputation comp(cv::GIn(in1, in2, s), cv::GOut(out));
  410. cv::Mat in_mat1 = cv::Mat::eye(3, 3, CV_8UC1),
  411. in_mat2 = cv::Mat::eye(3, 3, CV_8UC1),
  412. out_mat(3, 3, CV_8UC1),
  413. ref_mat(3, 3, CV_8UC1);
  414. cv::Scalar scalar = 2;
  415. comp.apply(cv::gin(in_mat1, in_mat2, scalar), cv::gout(out_mat), cv::compile_args(full_pkg));
  416. ref_mat = in_mat1 + in_mat2 + scalar + scalar + scalar;
  417. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  418. }
  419. TEST(GCompoundKernel, MaxInArray)
  420. {
  421. GDoubleArray in;
  422. auto out = GCompoundMaxInArray::on(in);
  423. const auto custom_pkg = cv::gapi::kernels<GCompoundMaxInArrayImpl, GMaxInArrayImpl>();
  424. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  425. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  426. std::vector<double> v = { 1, 5, -2, 3, 10, 2};
  427. cv::Scalar out_scl;
  428. cv::Scalar ref_scl(*std::max_element(v.begin(), v.end()));
  429. comp.apply(cv::gin(v), cv::gout(out_scl), cv::compile_args(full_pkg));
  430. EXPECT_EQ(out_scl, ref_scl);
  431. }
  432. TEST(GCompoundKernel, NegateArray)
  433. {
  434. GDoubleArray in;
  435. GDoubleArray out = GCompoundNegateArray::on(in);
  436. const auto custom_pkg = cv::gapi::kernels<GCompoundNegateArrayImpl, GNegateArrayImpl>();
  437. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  438. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  439. std::vector<double> in_v = {1, 5, -2, -10, 3};
  440. std::vector<double> out_v;
  441. std::vector<double> ref_v;
  442. ade::util::transform(in_v, std::back_inserter(ref_v), std::negate<double>());
  443. comp.apply(cv::gin(in_v), cv::gout(out_v), cv::compile_args(full_pkg));
  444. EXPECT_EQ(out_v, ref_v);
  445. }
  446. TEST(GCompoundKernel, RightGArrayHandle)
  447. {
  448. cv::GMat in[2];
  449. GDoubleArray a;
  450. cv::GMat out = GCompoundGMatGArrayGMat::on(in[0], a, in[1]);
  451. const auto custom_pkg = cv::gapi::kernels<GCompoundGMatGArrayGMatImpl, SetDiagKernelImpl>();
  452. const auto full_pkg = cv::gapi::combine(custom_pkg, cv::gapi::core::cpu::kernels());
  453. cv::GComputation comp(cv::GIn(in[0], a, in[1]), cv::GOut(out));
  454. std::vector<double> in_v(3, 1.0);
  455. cv::Mat in_mat1 = cv::Mat::eye(cv::Size(3, 3), CV_8UC1),
  456. in_mat2 = cv::Mat::eye(cv::Size(3, 3), CV_8UC1),
  457. out_mat;
  458. cv::Mat ref_mat= in_mat1 + in_mat2;
  459. setDiag(ref_mat, in_v);
  460. comp.apply(cv::gin(in_mat1, in_v, in_mat2), cv::gout(out_mat), cv::compile_args(full_pkg));
  461. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  462. }
  463. TEST(GCompoundKernel, ToInterleavedToPlanar)
  464. {
  465. cv::GMatP in;
  466. cv::GMatP out = GCompoundToInterleavedToPlanar::on(in);
  467. const auto pkg = cv::gapi::kernels<GCompoundToInterleavedToPlanarImpl,
  468. GToInterleavedImpl,
  469. GToPlanarImpl>();
  470. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  471. constexpr int numPlanes = 3;
  472. cv::Mat in_mat(cv::Size(15, 15), CV_8UC1),
  473. out_mat,
  474. ref_mat;
  475. cv::randu(in_mat, 0, 255);
  476. ref_mat = in_mat;
  477. comp.compile(cv::descr_of(in_mat).asPlanar(numPlanes), cv::compile_args(pkg))
  478. (cv::gin(in_mat), cv::gout(out_mat));
  479. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  480. }
  481. } // opencv_test