mat_tests.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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/own/mat.hpp>
  8. #include <opencv2/gapi/own/convert.hpp>
  9. #include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
  10. namespace opencv_test
  11. {
  12. using Mat = cv::gapi::own::Mat;
  13. using Dims = std::vector<int>;
  14. namespace {
  15. inline std::size_t multiply_dims(Dims const& dims){
  16. return std::accumulate(dims.begin(), dims.end(), static_cast<size_t>(1), std::multiplies<std::size_t>());
  17. }
  18. }
  19. TEST(OwnMat, DefaultConstruction)
  20. {
  21. Mat m;
  22. ASSERT_EQ(nullptr, m.data);
  23. ASSERT_EQ(0, m.cols);
  24. ASSERT_EQ(0, m.rows);
  25. ASSERT_EQ(0, m.cols);
  26. ASSERT_EQ(0, m.type());
  27. ASSERT_EQ(0, m.depth());
  28. ASSERT_TRUE(m.dims.empty());
  29. ASSERT_TRUE(m.empty());
  30. }
  31. TEST(OwnMat, Create)
  32. {
  33. auto size = cv::gapi::own::Size{32,16};
  34. Mat m;
  35. m.create(size, CV_8UC1);
  36. ASSERT_NE(nullptr, m.data);
  37. ASSERT_EQ(size, (cv::gapi::own::Size{m.cols, m.rows}));
  38. ASSERT_EQ(static_cast<size_t>(size.height) * size.width, m.total());
  39. ASSERT_EQ(CV_8UC1, m.type());
  40. ASSERT_EQ(CV_8U, m.depth());
  41. ASSERT_EQ(1, m.channels());
  42. ASSERT_EQ(sizeof(uint8_t), m.elemSize());
  43. ASSERT_EQ(sizeof(uint8_t) * m.cols, m.step);
  44. ASSERT_TRUE(m.dims.empty());
  45. ASSERT_FALSE(m.empty());
  46. }
  47. TEST(OwnMat, CreateND)
  48. {
  49. Dims dims = {1,1,32,32};
  50. Mat m;
  51. m.create(dims, CV_32F);
  52. ASSERT_NE(nullptr , m.data);
  53. ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{m.cols, m.rows}));
  54. ASSERT_EQ(multiply_dims(dims), m.total());
  55. ASSERT_EQ(CV_32F, m.type());
  56. ASSERT_EQ(CV_32F, m.depth());
  57. ASSERT_EQ(-1, m.channels());
  58. ASSERT_EQ(sizeof(float), m.elemSize());
  59. ASSERT_EQ(0u, m.step);
  60. ASSERT_EQ(dims, m.dims);
  61. ASSERT_FALSE(m.empty());
  62. }
  63. TEST(OwnMat, CreateOverload)
  64. {
  65. auto size = cv::gapi::own::Size{32,16};
  66. Mat m;
  67. m.create(size.height,size.width, CV_8UC1);
  68. ASSERT_NE(nullptr, m.data);
  69. ASSERT_EQ(size, (cv::Size{m.cols, m.rows}));
  70. ASSERT_EQ(static_cast<size_t>(size.height) * size.width, m.total());
  71. ASSERT_EQ(CV_8UC1, m.type());
  72. ASSERT_EQ(CV_8U, m.depth());
  73. ASSERT_EQ(1, m.channels());
  74. ASSERT_EQ(sizeof(uint8_t), m.elemSize());
  75. ASSERT_EQ(sizeof(uint8_t) * m.cols, m.step);
  76. ASSERT_TRUE(m.dims.empty());
  77. ASSERT_FALSE(m.empty());
  78. }
  79. TEST(OwnMat, Create3chan)
  80. {
  81. auto size = cv::Size{32,16};
  82. Mat m;
  83. m.create(size, CV_8UC3);
  84. ASSERT_NE(nullptr, m.data);
  85. ASSERT_EQ(size, (cv::Size{m.cols, m.rows}));
  86. ASSERT_EQ(CV_8UC3, m.type());
  87. ASSERT_EQ(CV_8U, m.depth());
  88. ASSERT_EQ(3, m.channels());
  89. ASSERT_EQ(3 * sizeof(uint8_t), m.elemSize());
  90. ASSERT_EQ(3 * sizeof(uint8_t) * m.cols, m.step);
  91. ASSERT_TRUE(m.dims.empty());
  92. ASSERT_FALSE(m.empty());
  93. }
  94. struct NonEmptyMat {
  95. cv::gapi::own::Size size{32,16};
  96. Mat m;
  97. NonEmptyMat() {
  98. m.create(size, CV_8UC1);
  99. }
  100. };
  101. struct OwnMatSharedSemantics : NonEmptyMat, ::testing::Test {};
  102. namespace {
  103. auto state_of = [](Mat const& mat) {
  104. return std::make_tuple(
  105. mat.data,
  106. cv::Size{mat.cols, mat.rows},
  107. mat.type(),
  108. mat.depth(),
  109. mat.channels(),
  110. mat.dims,
  111. mat.empty()
  112. );
  113. };
  114. void ensure_mats_are_same(Mat const& copy, Mat const& m){
  115. EXPECT_NE(nullptr, copy.data);
  116. EXPECT_EQ(state_of(copy), state_of(m));
  117. }
  118. }
  119. TEST_F(OwnMatSharedSemantics, CopyConstruction)
  120. {
  121. Mat copy(m);
  122. ensure_mats_are_same(copy, m);
  123. }
  124. TEST_F(OwnMatSharedSemantics, CopyAssignment)
  125. {
  126. Mat copy;
  127. copy = m;
  128. ensure_mats_are_same(copy, m);
  129. }
  130. struct OwnMatMoveSemantics : NonEmptyMat, ::testing::Test {
  131. Mat& moved_from = m;
  132. decltype(state_of(moved_from)) initial_state = state_of(moved_from);
  133. void ensure_state_moved_to(Mat const& moved_to)
  134. {
  135. EXPECT_EQ(state_of(moved_to), initial_state);
  136. EXPECT_EQ(state_of(moved_from), state_of(Mat{}));
  137. }
  138. };
  139. TEST_F(OwnMatMoveSemantics, MoveConstruction)
  140. {
  141. Mat moved_to(std::move(moved_from));
  142. ensure_state_moved_to(moved_to);
  143. }
  144. TEST_F(OwnMatMoveSemantics, MoveAssignment)
  145. {
  146. Mat moved_to(std::move(moved_from));
  147. ensure_state_moved_to(moved_to);
  148. }
  149. struct OwnMatNonOwningView : NonEmptyMat, ::testing::Test {
  150. decltype(state_of(m)) initial_state = state_of(m);
  151. void TearDown() override {
  152. EXPECT_EQ(state_of(m), initial_state)<<"State of the source matrix changed?";
  153. //ASAN should complain here if memory is freed here (e.g. by bug in non owning logic of own::Mat)
  154. volatile uchar dummy = m.data[0];
  155. cv::util::suppress_unused_warning(dummy);
  156. }
  157. };
  158. TEST_F(OwnMatNonOwningView, Construction)
  159. {
  160. Mat non_owning_view(m.rows, m.cols, m.type(), static_cast<void*>(m.data));
  161. ensure_mats_are_same(non_owning_view, m);
  162. }
  163. TEST_F(OwnMatNonOwningView, CopyConstruction)
  164. {
  165. Mat non_owning_view{m.rows, m.cols, m.type(), static_cast<void*>(m.data)};
  166. Mat non_owning_view_copy = non_owning_view;
  167. ensure_mats_are_same(non_owning_view_copy, m);
  168. }
  169. TEST_F(OwnMatNonOwningView, Assignment)
  170. {
  171. Mat non_owning_view{m.rows, m.cols, m.type(), static_cast<void*>(m.data)};
  172. Mat non_owning_view_copy;
  173. non_owning_view_copy = non_owning_view;
  174. ensure_mats_are_same(non_owning_view_copy, m);
  175. }
  176. TEST(OwnMatConversion, WithStep)
  177. {
  178. constexpr int width = 8;
  179. constexpr int height = 8;
  180. constexpr int stepInPixels = 16;
  181. std::array<int, height * stepInPixels> data;
  182. for (size_t i = 0; i < data.size(); i++)
  183. {
  184. data[i] = static_cast<int>(i);
  185. }
  186. cv::Mat cvMat(cv::Size{width, height}, CV_32S, data.data(), stepInPixels * sizeof(int));
  187. auto ownMat = to_own(cvMat);
  188. auto cvMatFromOwn = cv::gapi::own::to_ocv(ownMat);
  189. EXPECT_EQ(0, cvtest::norm(cvMat, cvMatFromOwn, NORM_INF))
  190. << cvMat << std::endl
  191. << (cvMat != cvMatFromOwn);
  192. }
  193. TEST(OwnMatConversion, WithND)
  194. {
  195. const Dims dims = {1,3,8,8};
  196. std::vector<uint8_t> data(dims[0]*dims[1]*dims[2]*dims[3]);
  197. for (size_t i = 0u; i < data.size(); i++)
  198. {
  199. data[i] = static_cast<uint8_t>(i);
  200. }
  201. cv::Mat cvMat(dims, CV_8U, data.data());
  202. auto ownMat = to_own(cvMat);
  203. auto cvMatFromOwn = cv::gapi::own::to_ocv(ownMat);
  204. EXPECT_EQ(0, cv::norm(cvMat, cvMatFromOwn, NORM_INF))
  205. << cvMat << std::endl
  206. << (cvMat != cvMatFromOwn);
  207. }
  208. TEST(OwnMat, PtrWithStep)
  209. {
  210. constexpr int width = 8;
  211. constexpr int height = 8;
  212. constexpr int stepInPixels = 16;
  213. std::array<int, height * stepInPixels> data;
  214. for (size_t i = 0; i < data.size(); i++)
  215. {
  216. data[i] = static_cast<int>(i);
  217. }
  218. Mat mat(height, width, CV_32S, data.data(), stepInPixels * sizeof(int));
  219. EXPECT_EQ(& data[0], reinterpret_cast<int*>(mat.ptr(0)));
  220. EXPECT_EQ(& data[1], reinterpret_cast<int*>(mat.ptr(0, 1)));
  221. EXPECT_EQ(& data[stepInPixels], reinterpret_cast<int*>(mat.ptr(1)));
  222. EXPECT_EQ(& data[stepInPixels +1], reinterpret_cast<int*>(mat.ptr(1,1)));
  223. auto const& cmat = mat;
  224. EXPECT_EQ(& data[0], reinterpret_cast<const int*>(cmat.ptr(0)));
  225. EXPECT_EQ(& data[1], reinterpret_cast<const int*>(cmat.ptr(0, 1)));
  226. EXPECT_EQ(& data[stepInPixels], reinterpret_cast<const int*>(cmat.ptr(1)));
  227. EXPECT_EQ(& data[stepInPixels +1], reinterpret_cast<const int*>(cmat.ptr(1,1)));
  228. }
  229. TEST(OwnMat, CopyToWithStep)
  230. {
  231. constexpr int width = 8;
  232. constexpr int height = 8;
  233. constexpr int stepInPixels = 16;
  234. std::array<int, height * stepInPixels> data;
  235. for (size_t i = 0; i < data.size(); i++)
  236. {
  237. data[i] = static_cast<int>(i);
  238. }
  239. Mat mat(height, width, CV_32S, data.data(), stepInPixels * sizeof(int));
  240. Mat dst;
  241. mat.copyTo(dst);
  242. EXPECT_NE(mat.data, dst.data);
  243. EXPECT_EQ(0, cvtest::norm(to_ocv(mat), to_ocv(dst), NORM_INF))
  244. << to_ocv(mat) << std::endl
  245. << (to_ocv(mat) != to_ocv(dst));
  246. }
  247. TEST(OwnMat, AssignNDtoRegular)
  248. {
  249. const auto sz = cv::gapi::own::Size{32,32};
  250. const auto dims = Dims{1,3,224,224};
  251. Mat a;
  252. a.create(sz, CV_8U);
  253. const auto *old_ptr = a.data;
  254. ASSERT_NE(nullptr , a.data);
  255. ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
  256. ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
  257. ASSERT_EQ(CV_8U , a.type());
  258. ASSERT_EQ(CV_8U , a.depth());
  259. ASSERT_EQ(1 , a.channels());
  260. ASSERT_EQ(sizeof(uint8_t), a.elemSize());
  261. ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
  262. ASSERT_TRUE(a.dims.empty());
  263. Mat b;
  264. b.create(dims, CV_32F);
  265. a = b;
  266. ASSERT_NE(nullptr , a.data);
  267. ASSERT_NE(old_ptr , a.data);
  268. ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
  269. ASSERT_EQ(multiply_dims(dims), a.total());
  270. ASSERT_EQ(CV_32F , a.type());
  271. ASSERT_EQ(CV_32F , a.depth());
  272. ASSERT_EQ(-1 , a.channels());
  273. ASSERT_EQ(sizeof(float), a.elemSize());
  274. ASSERT_EQ(0u , a.step);
  275. ASSERT_EQ(dims , a.dims);
  276. }
  277. TEST(OwnMat, AssignRegularToND)
  278. {
  279. const auto sz = cv::gapi::own::Size{32,32};
  280. const auto dims = Dims{1,3,224,224};
  281. Mat a;
  282. a.create(dims, CV_32F);
  283. const auto *old_ptr = a.data;
  284. ASSERT_NE(nullptr , a.data);
  285. ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
  286. ASSERT_EQ(multiply_dims(dims), a.total());
  287. ASSERT_EQ(CV_32F , a.type());
  288. ASSERT_EQ(CV_32F , a.depth());
  289. ASSERT_EQ(-1 , a.channels());
  290. ASSERT_EQ(sizeof(float), a.elemSize());
  291. ASSERT_EQ(0u , a.step);
  292. ASSERT_EQ(dims , a.dims);
  293. Mat b;
  294. b.create(sz, CV_8U);
  295. a = b;
  296. ASSERT_NE(nullptr , a.data);
  297. ASSERT_NE(old_ptr , a.data);
  298. ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
  299. ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
  300. ASSERT_EQ(CV_8U , a.type());
  301. ASSERT_EQ(CV_8U , a.depth());
  302. ASSERT_EQ(1 , a.channels());
  303. ASSERT_EQ(sizeof(uint8_t), a.elemSize());
  304. ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
  305. ASSERT_TRUE(a.dims.empty());
  306. }
  307. TEST(OwnMat, CopyNDtoRegular)
  308. {
  309. const auto sz = cv::gapi::own::Size{32,32};
  310. const auto dims = Dims{1,3,224,224};
  311. Mat a;
  312. a.create(sz, CV_8U);
  313. const auto *old_ptr = a.data;
  314. ASSERT_NE(nullptr , a.data);
  315. ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
  316. ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
  317. ASSERT_EQ(CV_8U , a.type());
  318. ASSERT_EQ(CV_8U , a.depth());
  319. ASSERT_EQ(1 , a.channels());
  320. ASSERT_EQ(sizeof(uint8_t), a.elemSize());
  321. ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
  322. ASSERT_TRUE(a.dims.empty());
  323. Mat b;
  324. b.create(dims, CV_32F);
  325. b.copyTo(a);
  326. ASSERT_NE(nullptr , a.data);
  327. ASSERT_NE(old_ptr , a.data);
  328. ASSERT_NE(b.data , a.data);
  329. ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
  330. ASSERT_EQ(multiply_dims(dims), a.total());
  331. ASSERT_EQ(CV_32F , a.type());
  332. ASSERT_EQ(CV_32F , a.depth());
  333. ASSERT_EQ(-1 , a.channels());
  334. ASSERT_EQ(sizeof(float), a.elemSize());
  335. ASSERT_EQ(0u , a.step);
  336. ASSERT_EQ(dims , a.dims);
  337. }
  338. TEST(OwnMat, CopyRegularToND)
  339. {
  340. const auto sz = cv::gapi::own::Size{32,32};
  341. const auto dims = Dims{1,3,224,224};
  342. Mat a;
  343. a.create(dims, CV_32F);
  344. const auto *old_ptr = a.data;
  345. ASSERT_NE(nullptr , a.data);
  346. ASSERT_EQ((cv::gapi::own::Size{0,0}), (cv::gapi::own::Size{a.cols, a.rows}));
  347. ASSERT_EQ(multiply_dims(dims), a.total());
  348. ASSERT_EQ(CV_32F , a.type());
  349. ASSERT_EQ(CV_32F , a.depth());
  350. ASSERT_EQ(-1 , a.channels());
  351. ASSERT_EQ(sizeof(float), a.elemSize());
  352. ASSERT_EQ(0u , a.step);
  353. ASSERT_EQ(dims , a.dims);
  354. Mat b;
  355. b.create(sz, CV_8U);
  356. b.copyTo(a);
  357. ASSERT_NE(nullptr , a.data);
  358. ASSERT_NE(old_ptr , a.data);
  359. ASSERT_NE(b.data , a.data);
  360. ASSERT_EQ(sz , (cv::gapi::own::Size{a.cols, a.rows}));
  361. ASSERT_EQ(static_cast<size_t>(sz.width) * sz.height, a.total());
  362. ASSERT_EQ(CV_8U , a.type());
  363. ASSERT_EQ(CV_8U , a.depth());
  364. ASSERT_EQ(1 , a.channels());
  365. ASSERT_EQ(sizeof(uint8_t), a.elemSize());
  366. ASSERT_EQ(static_cast<size_t>(sz.width), a.step);
  367. ASSERT_TRUE(a.dims.empty());
  368. }
  369. TEST(OwnMat, ScalarAssign32SC1)
  370. {
  371. constexpr int width = 8;
  372. constexpr int height = 8;
  373. constexpr int stepInPixels = 16;
  374. std::array<int, height * stepInPixels> data;
  375. for (size_t i = 0; i < data.size(); i++)
  376. {
  377. data[i] = static_cast<int>(i);
  378. }
  379. Mat mat(height, width, CV_32S, data.data(), stepInPixels * sizeof(data[0]));
  380. mat = cv::gapi::own::Scalar{-1};
  381. std::array<int, height * stepInPixels> expected;
  382. for (size_t row = 0; row < height; row++)
  383. {
  384. for (size_t col = 0; col < stepInPixels; col++)
  385. {
  386. auto index = row*stepInPixels + col;
  387. expected[index] = col < width ? -1 : static_cast<int>(index);
  388. }
  389. }
  390. auto cmp_result_mat = (cv::Mat{height, stepInPixels, CV_32S, data.data()} != cv::Mat{height, stepInPixels, CV_32S, expected.data()});
  391. EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
  392. << cmp_result_mat;
  393. }
  394. TEST(OwnMat, ScalarAssign8UC1)
  395. {
  396. constexpr int width = 8;
  397. constexpr int height = 8;
  398. constexpr int stepInPixels = 16;
  399. std::array<uchar, height * stepInPixels> data;
  400. for (size_t i = 0; i < data.size(); i++)
  401. {
  402. data[i] = static_cast<uchar>(i);
  403. }
  404. Mat mat(height, width, CV_8U, data.data(), stepInPixels * sizeof(data[0]));
  405. mat = cv::gapi::own::Scalar{-1};
  406. std::array<uchar, height * stepInPixels> expected;
  407. for (size_t row = 0; row < height; row++)
  408. {
  409. for (size_t col = 0; col < stepInPixels; col++)
  410. {
  411. auto index = row*stepInPixels + col;
  412. expected[index] = col < width ? cv::saturate_cast<uchar>(-1) : static_cast<uchar>(index);
  413. }
  414. }
  415. auto cmp_result_mat = (cv::Mat{height, stepInPixels, CV_8U, data.data()} != cv::Mat{height, stepInPixels, CV_8U, expected.data()});
  416. EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
  417. << cmp_result_mat;
  418. }
  419. TEST(OwnMat, ScalarAssignND)
  420. {
  421. std::vector<int> dims = {1,1000};
  422. Mat m;
  423. m.create(dims, CV_32F);
  424. m = cv::gapi::own::Scalar{-1};
  425. const float *ptr = reinterpret_cast<float*>(m.data);
  426. for (auto i = 0u; i < m.total(); i++) {
  427. EXPECT_EQ(-1.f, ptr[i]);
  428. }
  429. }
  430. TEST(OwnMat, ScalarAssign8UC3)
  431. {
  432. constexpr auto cv_type = CV_8SC3;
  433. constexpr int channels = 3;
  434. constexpr int width = 8;
  435. constexpr int height = 8;
  436. constexpr int stepInPixels = 16;
  437. std::array<schar, height * stepInPixels * channels> data;
  438. for (size_t i = 0; i < data.size(); i+= channels)
  439. {
  440. data[i + 0] = static_cast<schar>(10 * i + 0);
  441. data[i + 1] = static_cast<schar>(10 * i + 1);
  442. data[i + 2] = static_cast<schar>(10 * i + 2);
  443. }
  444. Mat mat(height, width, cv_type, data.data(), channels * stepInPixels * sizeof(data[0]));
  445. mat = cv::gapi::own::Scalar{-10, -11, -12};
  446. std::array<schar, data.size()> expected;
  447. for (size_t row = 0; row < height; row++)
  448. {
  449. for (size_t col = 0; col < stepInPixels; col++)
  450. {
  451. int index = static_cast<int>(channels * (row*stepInPixels + col));
  452. expected[index + 0] = static_cast<schar>(col < width ? -10 : 10 * index + 0);
  453. expected[index + 1] = static_cast<schar>(col < width ? -11 : 10 * index + 1);
  454. expected[index + 2] = static_cast<schar>(col < width ? -12 : 10 * index + 2);
  455. }
  456. }
  457. auto cmp_result_mat = (cv::Mat{height, stepInPixels, cv_type, data.data()} != cv::Mat{height, stepInPixels, cv_type, expected.data()});
  458. EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
  459. << cmp_result_mat << std::endl
  460. << "data : " << std::endl
  461. << cv::Mat{height, stepInPixels, cv_type, data.data()} << std::endl
  462. << "expected : " << std::endl
  463. << cv::Mat{height, stepInPixels, cv_type, expected.data()} << std::endl;
  464. }
  465. TEST(OwnMat, ROIView)
  466. {
  467. constexpr int width = 8;
  468. constexpr int height = 8;
  469. constexpr int stepInPixels = 16;
  470. std::array<uchar, height * stepInPixels> data;
  471. for (size_t i = 0; i < data.size(); i++)
  472. {
  473. data[i] = static_cast<uchar>(i);
  474. }
  475. // std::cout<<cv::Mat{height, stepInPixels, CV_8U, data.data()}<<std::endl;
  476. std::array<uchar, 4 * 4> expected;
  477. for (size_t row = 0; row < 4; row++)
  478. {
  479. for (size_t col = 0; col < 4; col++)
  480. {
  481. expected[row*4 +col] = static_cast<uchar>(stepInPixels * (2 + row) + 2 + col);
  482. }
  483. }
  484. Mat mat(height, width, CV_8U, data.data(), stepInPixels * sizeof(data[0]));
  485. Mat roi_view (mat, cv::gapi::own::Rect{2,2,4,4});
  486. // std::cout<<cv::Mat{4, 4, CV_8U, expected.data()}<<std::endl;
  487. //
  488. auto expected_cv_mat = cv::Mat{4, 4, CV_8U, expected.data()};
  489. auto cmp_result_mat = (to_ocv(roi_view) != expected_cv_mat);
  490. EXPECT_EQ(0, cvtest::norm(cmp_result_mat, NORM_INF))
  491. << cmp_result_mat << std::endl
  492. << to_ocv(roi_view) << std::endl
  493. << expected_cv_mat << std::endl;
  494. }
  495. TEST(OwnMat, CreateWithNegativeDims)
  496. {
  497. Mat own_mat;
  498. ASSERT_ANY_THROW(own_mat.create(cv::Size{-1, -1}, CV_8U));
  499. }
  500. TEST(OwnMat, CreateWithNegativeWidth)
  501. {
  502. Mat own_mat;
  503. ASSERT_ANY_THROW(own_mat.create(cv::Size{-1, 1}, CV_8U));
  504. }
  505. TEST(OwnMat, CreateWithNegativeHeight)
  506. {
  507. Mat own_mat;
  508. ASSERT_ANY_THROW(own_mat.create(cv::Size{1, -1}, CV_8U));
  509. }
  510. TEST(OwnMat, ZeroHeightMat)
  511. {
  512. cv::GMat in, a, b, c, d;
  513. std::tie(a, b, c, d) = cv::gapi::split4(in);
  514. cv::GMat out = cv::gapi::merge3(a, b, c);
  515. cv::Mat in_mat(cv::Size(8, 0), CV_8UC4);
  516. cv::Mat out_mat(cv::Size(8, 8), CV_8UC3);
  517. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  518. ASSERT_ANY_THROW(comp.apply(cv::gin(in_mat), cv::gout(out_mat),
  519. cv::compile_args(cv::gapi::core::fluid::kernels())));
  520. }
  521. TEST(OwnMat, ZeroWidthMat)
  522. {
  523. cv::GMat in, a, b, c, d;
  524. std::tie(a, b, c, d) = cv::gapi::split4(in);
  525. cv::GMat out = cv::gapi::merge3(a, b, c);
  526. cv::Mat in_mat(cv::Size(0, 8), CV_8UC4);
  527. cv::Mat out_mat(cv::Size(8, 8), CV_8UC3);
  528. cv::GComputation comp(cv::GIn(in), cv::GOut(out));
  529. ASSERT_ANY_THROW(comp.apply(cv::gin(in_mat), cv::gout(out_mat),
  530. cv::compile_args(cv::gapi::core::fluid::kernels())));
  531. }
  532. } // namespace opencv_test