Merge pull request #21103 from OrestChura:oc/fix_1D_Mat_RMat_View_issue
authorOrest Chura <orest.chura@intel.com>
Fri, 26 Nov 2021 16:42:12 +0000 (19:42 +0300)
committerGitHub <noreply@github.com>
Fri, 26 Nov 2021 16:42:12 +0000 (16:42 +0000)
[G-API] Fix issue of getting 1D Mat out of RMat::View

* Fix issue of getting 1D Mat out of RMat::View
 - added test
 - fixed for standalone too (removed Assert(dims.empty()))

* Fixed asVeiw() function for standalone

* Put more detailed comment

modules/gapi/src/backends/common/gbackend.hpp
modules/gapi/test/rmat/rmat_view_tests.cpp

index 7532486..99b8f5d 100644 (file)
@@ -24,8 +24,17 @@ namespace gimpl {
 
     inline cv::Mat asMat(RMat::View& v) {
 #if !defined(GAPI_STANDALONE)
-        return v.dims().empty() ? cv::Mat(v.rows(), v.cols(), v.type(), v.ptr(), v.step())
-                                : cv::Mat(v.dims(), v.type(), v.ptr(), v.steps().data());
+        if (v.dims().empty()) {
+            return cv::Mat(v.rows(), v.cols(), v.type(), v.ptr(), v.step());
+        } else {
+            cv::Mat m(v.dims(), v.type(), v.ptr(), v.steps().data());
+            if (v.dims().size() == 1) {
+                // FIXME: cv::Mat() constructor will set m.dims to 2;
+                // To obtain 1D Mat, we have to set m.dims back to 1 manually
+                m.dims = 1;
+            }
+            return m;
+        }
 #else
         // FIXME: add a check that steps are default
         return v.dims().empty() ? cv::Mat(v.rows(), v.cols(), v.type(), v.ptr(), v.step())
@@ -41,7 +50,10 @@ namespace gimpl {
         }
         return RMat::View(cv::descr_of(m), m.data, steps, std::move(cb));
 #else
-        return RMat::View(cv::descr_of(m), m.data, m.step, std::move(cb));
+        return m.dims.empty()
+            ? RMat::View(cv::descr_of(m), m.data, m.step, std::move(cb))
+            // Own Mat doesn't support n-dimensional steps so default ones are used in this case
+            : RMat::View(cv::descr_of(m), m.data, RMat::View::stepsT{}, std::move(cb));
 #endif
     }
 
index 1402523..d829b6c 100644 (file)
@@ -268,4 +268,13 @@ TEST_F(RMatViewCallbackTest, MagazineInteraction) {
     mag.slot<View>().erase(rc);
     EXPECT_EQ(1, callbackCalls);
 }
+
+TEST(RMatView, Access1DMat) {
+    cv::Mat m({1}, CV_32FC1);
+    m.dims = 1;
+    auto rmat = cv::make_rmat<cv::gimpl::RMatOnMat>(m);
+    auto view = rmat.access(cv::RMat::Access::R);
+    auto out = cv::gimpl::asMat(view);
+    EXPECT_EQ(1, out.dims);
+}
 } // namespace opencv_test