rmat_view_tests.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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) 2020 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #include <opencv2/gapi/rmat.hpp>
  8. #include <opencv2/gapi/util/compiler_hints.hpp>
  9. #include "../src/backends/common/gbackend.hpp"
  10. namespace opencv_test
  11. {
  12. using cv::GMatDesc;
  13. using View = cv::RMat::View;
  14. using cv::Mat;
  15. using cv::gimpl::asMat;
  16. using cv::gimpl::asView;
  17. using namespace ::testing;
  18. static void expect_eq_desc(const GMatDesc& desc, const View& view) {
  19. EXPECT_EQ(desc.size, view.size());
  20. EXPECT_EQ(desc.dims, view.dims());
  21. EXPECT_EQ(desc.size.width, view.cols());
  22. EXPECT_EQ(desc.size.height, view.rows());
  23. EXPECT_EQ(desc.depth, view.depth());
  24. EXPECT_EQ(desc.chan, view.chan());
  25. EXPECT_EQ(desc.depth, view.depth());
  26. EXPECT_EQ(desc.chan, view.chan());
  27. }
  28. TEST(RMatView, TestDefaultConstruction) {
  29. View view;
  30. GMatDesc desc{};
  31. expect_eq_desc(desc, view);
  32. EXPECT_EQ(nullptr, view.ptr());
  33. EXPECT_EQ(0u, view.step());
  34. }
  35. struct RMatViewTest : public TestWithParam<int /*dataType*/>{};
  36. TEST_P(RMatViewTest, ConstructionFromMat) {
  37. auto type = GetParam();
  38. Mat mat(8,8,type);
  39. const auto desc = cv::descr_of(mat);
  40. View view = asView(mat);
  41. expect_eq_desc(desc, view);
  42. EXPECT_EQ(mat.ptr(), view.ptr());
  43. EXPECT_EQ(mat.step, view.step());
  44. }
  45. TEST(RMatView, TestConstructionFromMatND) {
  46. std::vector<int> dims(4, 8);
  47. Mat mat(dims, CV_8UC1);
  48. const auto desc = cv::descr_of(mat);
  49. View view(cv::descr_of(mat), mat.ptr());
  50. expect_eq_desc(desc, view);
  51. EXPECT_EQ(mat.ptr(), view.ptr());
  52. }
  53. TEST_P(RMatViewTest, DefaultStep) {
  54. auto type = GetParam();
  55. GMatDesc desc;
  56. desc.chan = CV_MAT_CN(type);
  57. desc.depth = CV_MAT_DEPTH(type);
  58. desc.size = {8,8};
  59. std::vector<unsigned char> data(desc.size.width*desc.size.height*CV_ELEM_SIZE(type));
  60. View view(desc, data.data());
  61. EXPECT_EQ(static_cast<size_t>(desc.size.width)*CV_ELEM_SIZE(type), view.step());
  62. }
  63. struct RMatViewNDTest : public TestWithParam<
  64. std::tuple<int /*depth*/, int /*ndims*/>>{};
  65. TEST_P(RMatViewNDTest, DefaultStep) {
  66. int depth = 0, ndims = 0;
  67. std::tie(depth, ndims) = GetParam();
  68. std::vector<int> dims(ndims, 12);
  69. GMatDesc desc;
  70. desc.dims = dims;
  71. desc.depth = depth;
  72. GAPI_Assert(desc.chan == -1);
  73. auto elemSize = CV_ELEM_SIZE(depth);
  74. auto total = std::accumulate(dims.begin(), dims.end(), elemSize, std::multiplies<int>());
  75. std::vector<unsigned char> data(total);
  76. View view(desc, data.data());
  77. auto step = static_cast<size_t>(total/dims[0]);
  78. EXPECT_EQ(step, view.step(0));
  79. for (int i = 1; i < ndims; i++) {
  80. step /= dims[i];
  81. EXPECT_EQ(step, view.step(i));
  82. }
  83. }
  84. TEST_P(RMatViewNDTest, StepFromMat) {
  85. int depth = 0, ndims = 0;
  86. std::tie(depth, ndims) = GetParam();
  87. std::vector<int> dims(ndims, 12);
  88. cv::Mat mat(dims, depth);
  89. auto view = asView(mat);
  90. EXPECT_EQ(mat.ptr(), view.ptr());
  91. for (int i = 0; i < ndims; i++) {
  92. EXPECT_EQ(mat.step[i], view.step(i));
  93. }
  94. }
  95. TEST_P(RMatViewNDTest, StepFromView) {
  96. int depth = 0, ndims = 0;
  97. std::tie(depth, ndims) = GetParam();
  98. std::vector<int> dims(ndims, 12);
  99. std::vector<int> aligned(ndims, 16);
  100. GMatDesc desc;
  101. desc.dims = dims;
  102. desc.depth = depth;
  103. GAPI_Assert(desc.chan == -1);
  104. auto elemSize = CV_ELEM_SIZE(depth);
  105. auto total = std::accumulate(aligned.begin(), aligned.end(), elemSize, std::multiplies<int>());
  106. std::vector<unsigned char> data(total);
  107. View::stepsT steps(ndims);
  108. auto step = static_cast<size_t>(total/aligned[0]);
  109. steps[0] = step;
  110. for (int i = 1; i < ndims; i++) {
  111. step /= aligned[i];
  112. steps[i] = step;
  113. }
  114. View view(desc, data.data(), steps);
  115. auto mat = asMat(view);
  116. EXPECT_EQ(mat.ptr(), view.ptr());
  117. for (int i = 0; i < ndims; i++) {
  118. EXPECT_EQ(mat.step[i], view.step(i));
  119. }
  120. }
  121. INSTANTIATE_TEST_CASE_P(Test, RMatViewNDTest,
  122. Combine(Values(CV_8U, CV_32F), // depth
  123. Values(1,2,3,4,7))); // ndims
  124. struct RMatViewNDTestNegative : public TestWithParam<
  125. std::tuple<int /*depth*/, int /*chan*/, int /*ndims*/>>{};
  126. TEST_P(RMatViewNDTestNegative, DefaultStep) {
  127. int depth = 0, chan = 0, ndims = 0;
  128. std::tie(depth, chan, ndims) = GetParam();
  129. std::vector<int> dims(ndims, 12);
  130. GMatDesc desc;
  131. desc.dims = dims;
  132. desc.depth = depth;
  133. desc.chan = chan;
  134. auto elemSize = CV_ELEM_SIZE(depth);
  135. auto total = std::accumulate(dims.begin(), dims.end(), elemSize, std::multiplies<int>());
  136. std::vector<unsigned char> data(total);
  137. EXPECT_ANY_THROW(View view(desc, data.data()));
  138. }
  139. INSTANTIATE_TEST_CASE_P(Test, RMatViewNDTestNegative,
  140. Combine(Values(CV_8U, CV_32F), // depth
  141. Values(1,2,3,4), // chan
  142. Values(2,4,7))); // ndims
  143. TEST_P(RMatViewTest, NonDefaultStepInput) {
  144. auto type = GetParam();
  145. Mat bigMat(16,16,type);
  146. cv::randn(bigMat, cv::Scalar::all(127), cv::Scalar::all(40));
  147. Mat mat = bigMat(cv::Rect{4,4,8,8});
  148. View view = asView(mat);
  149. const auto viewMat = asMat(view);
  150. Mat ref, out;
  151. cv::Size ksize{1,1};
  152. cv::blur(viewMat, out, ksize);
  153. cv::blur( mat, ref, ksize);
  154. EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
  155. }
  156. TEST_P(RMatViewTest, NonDefaultStepOutput) {
  157. auto type = GetParam();
  158. Mat mat(8,8,type);
  159. cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
  160. Mat bigMat = Mat::zeros(16,16,type);
  161. Mat out = bigMat(cv::Rect{4,4,8,8});
  162. View view = asView(out);
  163. auto viewMat = asMat(view);
  164. Mat ref;
  165. cv::Size ksize{1,1};
  166. cv::blur(mat, viewMat, ksize);
  167. cv::blur(mat, ref, ksize);
  168. EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
  169. }
  170. TEST_P(RMatViewTest, NonDefaultStep2DInput) {
  171. auto type = GetParam();
  172. Mat bigMat(16,16,type);
  173. cv::randn(bigMat, cv::Scalar::all(127), cv::Scalar::all(40));
  174. Mat mat = bigMat(cv::Rect{4,4,8,8});
  175. View view(cv::descr_of(mat), mat.data, mat.step);
  176. const auto viewMat = asMat(view);
  177. Mat ref, out;
  178. cv::Size ksize{1,1};
  179. cv::blur(viewMat, out, ksize);
  180. cv::blur( mat, ref, ksize);
  181. EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
  182. }
  183. TEST_P(RMatViewTest, NonDefaultStep2DOutput) {
  184. auto type = GetParam();
  185. Mat mat(8,8,type);
  186. cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
  187. Mat bigMat = Mat::zeros(16,16,type);
  188. Mat out = bigMat(cv::Rect{4,4,8,8});
  189. View view(cv::descr_of(out), out.data, out.step);
  190. auto viewMat = asMat(view);
  191. Mat ref;
  192. cv::Size ksize{1,1};
  193. cv::blur(mat, viewMat, ksize);
  194. cv::blur(mat, ref, ksize);
  195. EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
  196. }
  197. INSTANTIATE_TEST_CASE_P(Test, RMatViewTest,
  198. Values(CV_8UC1, CV_8UC3, CV_32FC1));
  199. struct RMatViewCallbackTest : public ::testing::Test {
  200. RMatViewCallbackTest()
  201. : mat(8,8,CV_8UC1) {
  202. cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
  203. }
  204. View getView() { return asView(mat, [this](){ callbackCalls++; }); }
  205. int callbackCalls = 0;
  206. Mat mat;
  207. };
  208. TEST_F(RMatViewCallbackTest, MoveCtor) {
  209. {
  210. View copy(getView());
  211. cv::util::suppress_unused_warning(copy);
  212. EXPECT_EQ(0, callbackCalls);
  213. }
  214. EXPECT_EQ(1, callbackCalls);
  215. }
  216. TEST_F(RMatViewCallbackTest, MoveCopy) {
  217. {
  218. View copy;
  219. copy = getView();
  220. cv::util::suppress_unused_warning(copy);
  221. EXPECT_EQ(0, callbackCalls);
  222. }
  223. EXPECT_EQ(1, callbackCalls);
  224. }
  225. static int firstElement(const View& view) { return *view.ptr(); }
  226. static void setFirstElement(View& view, uchar value) { *view.ptr() = value; }
  227. TEST_F(RMatViewCallbackTest, MagazineInteraction) {
  228. cv::gimpl::magazine::Class<View> mag;
  229. constexpr int rc = 1;
  230. constexpr uchar value = 11;
  231. mag.slot<View>()[rc] = getView();
  232. {
  233. auto& mag_view = mag.slot<View>()[rc];
  234. setFirstElement(mag_view, value);
  235. auto mag_el = firstElement(mag_view);
  236. EXPECT_EQ(value, mag_el);
  237. }
  238. {
  239. const auto& mag_view = mag.slot<View>()[rc];
  240. auto mag_el = firstElement(mag_view);
  241. EXPECT_EQ(value, mag_el);
  242. }
  243. EXPECT_EQ(0, callbackCalls);
  244. mag.slot<View>().erase(rc);
  245. EXPECT_EQ(1, callbackCalls);
  246. }
  247. TEST(RMatView, Access1DMat) {
  248. cv::Mat m({1}, CV_32FC1);
  249. m.dims = 1;
  250. auto rmat = cv::make_rmat<cv::gimpl::RMatOnMat>(m);
  251. auto view = rmat.access(cv::RMat::Access::R);
  252. auto out = cv::gimpl::asMat(view);
  253. EXPECT_EQ(1, out.dims);
  254. }
  255. } // namespace opencv_test