gapi_desc_tests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <opencv2/gapi/cpu/gcpukernel.hpp>
  8. namespace opencv_test
  9. {
  10. namespace
  11. {
  12. G_TYPED_KERNEL(KTest, <cv::GScalar(cv::GScalar)>, "org.opencv.test.scalar_kernel") {
  13. static cv::GScalarDesc outMeta(cv::GScalarDesc in) { return in; }
  14. };
  15. GAPI_OCV_KERNEL(GOCVScalarTest, KTest)
  16. {
  17. static void run(const cv::Scalar &in, cv::Scalar &out) { out = in+cv::Scalar(1); }
  18. };
  19. }
  20. TEST(GAPI_MetaDesc, MatDescOneCh)
  21. {
  22. cv::Mat mat(240, 320, CV_8U);
  23. const auto desc = cv::descr_of(mat);
  24. EXPECT_EQ(CV_8U, desc.depth);
  25. EXPECT_EQ(1, desc.chan);
  26. EXPECT_EQ(320, desc.size.width);
  27. EXPECT_EQ(240, desc.size.height);
  28. EXPECT_FALSE(desc.isND());
  29. }
  30. TEST(GAPI_MetaDesc, MatDescThreeCh)
  31. {
  32. cv::Mat mat(480, 640, CV_8UC3);
  33. const auto desc = cv::descr_of(mat);
  34. EXPECT_EQ(CV_8U, desc.depth);
  35. EXPECT_EQ(3, desc.chan);
  36. EXPECT_EQ(640, desc.size.width);
  37. EXPECT_EQ(480, desc.size.height);
  38. EXPECT_FALSE(desc.isND());
  39. }
  40. TEST(GAPI_MetaDesc, MatDescND)
  41. {
  42. std::vector<int> dims = {1,3,299,299};
  43. cv::Mat m(dims, CV_32F);
  44. const auto desc = cv::descr_of(m);
  45. EXPECT_EQ(CV_32F, desc.depth);
  46. EXPECT_EQ(-1, desc.chan);
  47. EXPECT_EQ(1, desc.dims[0]);
  48. EXPECT_EQ(3, desc.dims[1]);
  49. EXPECT_EQ(299, desc.dims[2]);
  50. EXPECT_EQ(299, desc.dims[3]);
  51. EXPECT_TRUE(desc.isND());
  52. }
  53. TEST(GAPI_MetaDesc, VecMatDesc)
  54. {
  55. std::vector<cv::Mat> vec1 = {
  56. cv::Mat(240, 320, CV_8U)};
  57. const auto desc1 = cv::descrs_of(vec1);
  58. EXPECT_EQ((GMatDesc{CV_8U, 1, {320, 240}}), get<GMatDesc>(desc1[0]));
  59. std::vector<cv::UMat> vec2 = {
  60. cv::UMat(480, 640, CV_8UC3)};
  61. const auto desc2 = cv::descrs_of(vec2);
  62. EXPECT_EQ((GMatDesc{CV_8U, 3, {640, 480}}), get<GMatDesc>(desc2[0]));
  63. }
  64. TEST(GAPI_MetaDesc, CanDescribe)
  65. {
  66. constexpr int w = 15;
  67. constexpr int h = 7;
  68. cv::Mat m0(h, w, CV_8UC3);
  69. cv::GMatDesc md0{CV_8U,3,{w,h},false};
  70. cv::Mat m1(h*3, w, CV_8UC1);
  71. cv::GMatDesc md10{CV_8U,3,{w,h},true};
  72. cv::GMatDesc md11{CV_8U,1,{w,h*3},false};
  73. EXPECT_TRUE (md0 .canDescribe(m0));
  74. EXPECT_FALSE(md0 .canDescribe(m1));
  75. EXPECT_TRUE (md10.canDescribe(m1));
  76. EXPECT_TRUE (md11.canDescribe(m1));
  77. }
  78. TEST(GAPI_MetaDesc, OwnMatDescOneCh)
  79. {
  80. cv::gapi::own::Mat mat(240, 320, CV_8U, nullptr);
  81. const auto desc = cv::gapi::own::descr_of(mat);
  82. EXPECT_EQ(CV_8U, desc.depth);
  83. EXPECT_EQ(1, desc.chan);
  84. EXPECT_EQ(320, desc.size.width);
  85. EXPECT_EQ(240, desc.size.height);
  86. EXPECT_FALSE(desc.isND());
  87. }
  88. TEST(GAPI_MetaDesc, OwnMatDescThreeCh)
  89. {
  90. cv::gapi::own::Mat mat(480, 640, CV_8UC3, nullptr);
  91. const auto desc = cv::gapi::own::descr_of(mat);
  92. EXPECT_EQ(CV_8U, desc.depth);
  93. EXPECT_EQ(3, desc.chan);
  94. EXPECT_EQ(640, desc.size.width);
  95. EXPECT_EQ(480, desc.size.height);
  96. EXPECT_FALSE(desc.isND());
  97. }
  98. TEST(GAPI_MetaDesc, OwnMatDescND)
  99. {
  100. std::vector<int> dims = {1,3,224,224};
  101. cv::gapi::own::Mat m(dims, CV_32F, nullptr);
  102. const auto desc = cv::gapi::own::descr_of(m);
  103. EXPECT_EQ(CV_32F, desc.depth);
  104. EXPECT_EQ(-1, desc.chan);
  105. EXPECT_EQ(1, desc.dims[0]);
  106. EXPECT_EQ(3, desc.dims[1]);
  107. EXPECT_EQ(224, desc.dims[2]);
  108. EXPECT_EQ(224, desc.dims[3]);
  109. EXPECT_TRUE(desc.isND());
  110. }
  111. TEST(GAPI_MetaDesc, VecOwnMatDesc)
  112. {
  113. std::vector<cv::gapi::own::Mat> vec = {
  114. cv::gapi::own::Mat(240, 320, CV_8U, nullptr),
  115. cv::gapi::own::Mat(480, 640, CV_8UC3, nullptr)};
  116. const auto desc = cv::gapi::own::descrs_of(vec);
  117. EXPECT_EQ((GMatDesc{CV_8U, 1, {320, 240}}), get<GMatDesc>(desc[0]));
  118. EXPECT_EQ((GMatDesc{CV_8U, 3, {640, 480}}), get<GMatDesc>(desc[1]));
  119. }
  120. TEST(GAPI_MetaDesc, AdlVecOwnMatDesc)
  121. {
  122. std::vector<cv::gapi::own::Mat> vec = {
  123. cv::gapi::own::Mat(240, 320, CV_8U, nullptr),
  124. cv::gapi::own::Mat(480, 640, CV_8UC3, nullptr)};
  125. const auto desc = descrs_of(vec);
  126. EXPECT_EQ((GMatDesc{CV_8U, 1, {320, 240}}), get<GMatDesc>(desc[0]));
  127. EXPECT_EQ((GMatDesc{CV_8U, 3, {640, 480}}), get<GMatDesc>(desc[1]));
  128. }
  129. TEST(GAPI_MetaDesc, Compare_Equal_MatDesc)
  130. {
  131. const auto desc1 = cv::GMatDesc{CV_8U, 1, {64, 64}};
  132. const auto desc2 = cv::GMatDesc{CV_8U, 1, {64, 64}};
  133. EXPECT_TRUE(desc1 == desc2);
  134. }
  135. TEST(GAPI_MetaDesc, Compare_Not_Equal_MatDesc)
  136. {
  137. const auto desc1 = cv::GMatDesc{CV_8U, 1, {64, 64}};
  138. const auto desc2 = cv::GMatDesc{CV_32F, 1, {64, 64}};
  139. EXPECT_TRUE(desc1 != desc2);
  140. }
  141. TEST(GAPI_MetaDesc, Compare_Equal_MatDesc_ND)
  142. {
  143. const auto desc1 = cv::GMatDesc{CV_8U, {1,3,224,224}};
  144. const auto desc2 = cv::GMatDesc{CV_8U, {1,3,224,224}};
  145. EXPECT_TRUE(desc1 == desc2);
  146. }
  147. TEST(GAPI_MetaDesc, Compare_Not_Equal_MatDesc_ND_1)
  148. {
  149. const auto desc1 = cv::GMatDesc{CV_8U, {1,1000}};
  150. const auto desc2 = cv::GMatDesc{CV_32F, {1,1000}};
  151. EXPECT_TRUE(desc1 != desc2);
  152. }
  153. TEST(GAPI_MetaDesc, Compare_Not_Equal_MatDesc_ND_2)
  154. {
  155. const auto desc1 = cv::GMatDesc{CV_8U, {1,1000}};
  156. const auto desc2 = cv::GMatDesc{CV_8U, {1,1400}};
  157. EXPECT_TRUE(desc1 != desc2);
  158. }
  159. TEST(GAPI_MetaDesc, Compare_Not_Equal_MatDesc_ND_3)
  160. {
  161. const auto desc1 = cv::GMatDesc{CV_8U, {1,1000}};
  162. const auto desc2 = cv::GMatDesc{CV_8U, 1, {32,32}};
  163. EXPECT_TRUE(desc1 != desc2);
  164. }
  165. TEST(GAPI_MetaDesc, Compile_MatchMetaNumber_1)
  166. {
  167. cv::GMat in;
  168. cv::GComputation cc(in, in+in);
  169. const auto desc1 = cv::GMatDesc{CV_8U,1,{64,64}};
  170. const auto desc2 = cv::GMatDesc{CV_32F,1,{128,128}};
  171. EXPECT_NO_THROW(cc.compile(desc1));
  172. EXPECT_NO_THROW(cc.compile(desc2));
  173. // FIXME: custom exception type?
  174. // It is worth checking if compilation fails with different number
  175. // of meta parameters
  176. EXPECT_THROW(cc.compile(desc1, desc1), std::logic_error);
  177. EXPECT_THROW(cc.compile(desc1, desc2, desc2), std::logic_error);
  178. }
  179. TEST(GAPI_MetaDesc, Compile_MatchMetaNumber_2)
  180. {
  181. cv::GMat a, b;
  182. cv::GComputation cc(cv::GIn(a, b), cv::GOut(a+b));
  183. const auto desc1 = cv::GMatDesc{CV_8U,1,{64,64}};
  184. EXPECT_NO_THROW(cc.compile(desc1, desc1));
  185. const auto desc2 = cv::GMatDesc{CV_32F,1,{128,128}};
  186. EXPECT_NO_THROW(cc.compile(desc2, desc2));
  187. // FIXME: custom exception type?
  188. EXPECT_THROW(cc.compile(desc1), std::logic_error);
  189. EXPECT_THROW(cc.compile(desc2), std::logic_error);
  190. EXPECT_THROW(cc.compile(desc2, desc2, desc2), std::logic_error);
  191. }
  192. TEST(GAPI_MetaDesc, Compile_MatchMetaType_Mat)
  193. {
  194. cv::GMat in;
  195. cv::GComputation cc(in, in+in);
  196. EXPECT_NO_THROW(cc.compile(cv::GMatDesc{CV_8U,1,{64,64}}));
  197. // FIXME: custom exception type?
  198. EXPECT_THROW(cc.compile(cv::empty_scalar_desc()), std::logic_error);
  199. }
  200. TEST(GAPI_MetaDesc, Compile_MatchMetaType_Scalar)
  201. {
  202. cv::GScalar in;
  203. cv::GComputation cc(cv::GIn(in), cv::GOut(KTest::on(in)));
  204. const auto desc1 = cv::descr_of(cv::Scalar(128));
  205. const auto desc2 = cv::GMatDesc{CV_8U,1,{64,64}};
  206. const auto pkg = cv::gapi::kernels<GOCVScalarTest>();
  207. EXPECT_NO_THROW(cc.compile(desc1, cv::compile_args(pkg)));
  208. // FIXME: custom exception type?
  209. EXPECT_THROW(cc.compile(desc2, cv::compile_args(pkg)), std::logic_error);
  210. }
  211. TEST(GAPI_MetaDesc, Compile_MatchMetaType_Mixed)
  212. {
  213. cv::GMat a;
  214. cv::GScalar v;
  215. cv::GComputation cc(cv::GIn(a, v), cv::GOut(cv::gapi::addC(a, v)));
  216. const auto desc1 = cv::GMatDesc{CV_8U,1,{64,64}};
  217. const auto desc2 = cv::descr_of(cv::Scalar(4));
  218. EXPECT_NO_THROW(cc.compile(desc1, desc2));
  219. // FIXME: custom exception type(s)?
  220. EXPECT_THROW(cc.compile(desc1), std::logic_error);
  221. EXPECT_THROW(cc.compile(desc2), std::logic_error);
  222. EXPECT_THROW(cc.compile(desc2, desc1), std::logic_error);
  223. EXPECT_THROW(cc.compile(desc1, desc1, desc1), std::logic_error);
  224. EXPECT_THROW(cc.compile(desc1, desc2, desc1), std::logic_error);
  225. }
  226. TEST(GAPI_MetaDesc, Typed_Compile_MatchMetaNumber_1)
  227. {
  228. cv::GComputationT<cv::GMat(cv::GMat)> cc([](cv::GMat in)
  229. {
  230. return in+in;
  231. });
  232. const auto desc1 = cv::GMatDesc{CV_8U,1,{64,64}};
  233. const auto desc2 = cv::GMatDesc{CV_32F,1,{128,128}};
  234. EXPECT_NO_THROW(cc.compile(desc1));
  235. EXPECT_NO_THROW(cc.compile(desc2));
  236. }
  237. TEST(GAPI_MetaDesc, Typed_Compile_MatchMetaNumber_2)
  238. {
  239. cv::GComputationT<cv::GMat(cv::GMat,cv::GMat)> cc([](cv::GMat a, cv::GMat b)
  240. {
  241. return a + b;
  242. });
  243. const auto desc1 = cv::GMatDesc{CV_8U,1,{64,64}};
  244. EXPECT_NO_THROW(cc.compile(desc1, desc1));
  245. const auto desc2 = cv::GMatDesc{CV_32F,1,{128,128}};
  246. EXPECT_NO_THROW(cc.compile(desc2, desc2));
  247. }
  248. TEST(GAPI_MetaDesc, Typed_Compile_MatchMetaType_Mat)
  249. {
  250. cv::GComputationT<cv::GMat(cv::GMat)> cc([](cv::GMat in)
  251. {
  252. return in+in;
  253. });
  254. EXPECT_NO_THROW(cc.compile(cv::GMatDesc{CV_8U,1,{64,64}}));
  255. }
  256. TEST(GAPI_MetaDesc, Typed_Compile_MatchMetaType_Scalar)
  257. {
  258. cv::GComputationT<cv::GScalar(cv::GScalar)> cc([](cv::GScalar in)
  259. {
  260. return KTest::on(in);
  261. });
  262. const auto desc1 = cv::descr_of(cv::Scalar(128));
  263. const auto pkg = cv::gapi::kernels<GOCVScalarTest>();
  264. // EXPECT_NO_THROW(cc.compile(desc1, cv::compile_args(pkg)));
  265. cc.compile(desc1, cv::compile_args(pkg));
  266. }
  267. TEST(GAPI_MetaDesc, Typed_Compile_MatchMetaType_Mixed)
  268. {
  269. cv::GComputationT<cv::GMat(cv::GMat,cv::GScalar)> cc([](cv::GMat a, cv::GScalar v)
  270. {
  271. return cv::gapi::addC(a, v);
  272. });
  273. const auto desc1 = cv::GMatDesc{CV_8U,1,{64,64}};
  274. const auto desc2 = cv::descr_of(cv::Scalar(4));
  275. EXPECT_NO_THROW(cc.compile(desc1, desc2));
  276. }
  277. TEST(GAPI_MetaDesc, Compare_Planar)
  278. {
  279. const auto desc0 = cv::GMatDesc{CV_8U,3,{32,32},false};
  280. const auto desc1 = cv::GMatDesc{CV_8U,3,{32,32},false};
  281. const auto desc2 = cv::GMatDesc{CV_8U,3,{32,32},true};
  282. const auto desc3 = cv::GMatDesc{CV_8U,3,{64,64},true};
  283. EXPECT_TRUE(desc0 == desc1);
  284. EXPECT_TRUE(desc1 != desc2);
  285. EXPECT_TRUE(desc1 != desc3);
  286. EXPECT_TRUE(desc2 != desc3);
  287. }
  288. TEST(GAPI_MetaDesc, Sanity_asPlanar)
  289. {
  290. constexpr int w = 32;
  291. constexpr int h = 16;
  292. const auto desc1 = cv::GMatDesc{CV_8U,3,{w,h},false};
  293. const auto desc2 = cv::GMatDesc{CV_8U,3,{w,h},true};
  294. EXPECT_NO_THROW(desc1.asPlanar());
  295. EXPECT_NO_THROW(desc2.asInterleaved());
  296. EXPECT_ANY_THROW(desc1.asInterleaved());
  297. EXPECT_ANY_THROW(desc2.asPlanar());
  298. }
  299. TEST(GAPI_MetaDesc, Compare_asPlanar)
  300. {
  301. constexpr int w = 32;
  302. constexpr int h = 64;
  303. const auto desc0 = cv::GMatDesc{CV_8U,3,{w,h},false};
  304. const auto desc1 = cv::GMatDesc{CV_8U,3,{w,h},true};
  305. EXPECT_TRUE(desc0.asPlanar() == desc1);
  306. EXPECT_TRUE(desc1.asInterleaved() == desc0);
  307. }
  308. TEST(GAPI_MetaDesc, Compare_asPlanarTransform)
  309. {
  310. constexpr int w = 64;
  311. constexpr int h = 32;
  312. const auto desc0 = cv::GMatDesc{CV_8U,3,{w,h},true};
  313. const auto desc1 = cv::GMatDesc{CV_8U,1,{w,h*3},false};
  314. EXPECT_ANY_THROW(desc0.asPlanar(3));
  315. EXPECT_NO_THROW(desc1.asPlanar(3));
  316. EXPECT_TRUE(desc1.asPlanar(3) == desc0);
  317. }
  318. } // namespace opencv_test