Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[G-API] Fix issue of getting 1D Mat out of RMat::View #21103

Merged
merged 3 commits into from Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions modules/gapi/src/backends/common/gbackend.hpp
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps #else part should be fixed too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there are changes for standalone to support 1D, but not here they are required (as local testing shows)

Added standalone changes in asView()

// FIXME: add a check that steps are default
return v.dims().empty() ? cv::Mat(v.rows(), v.cols(), v.type(), v.ptr(), v.step())
Expand All @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions modules/gapi/test/rmat/rmat_view_tests.cpp
Expand Up @@ -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