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

Add eigen tensor conversions #17320

Merged
merged 14 commits into from May 23, 2020
Merged

Add eigen tensor conversions #17320

merged 14 commits into from May 23, 2020

Conversation

jgbradley1
Copy link

This PR addresses issue #17237. The primary contributions are two conversion functions (cv2eigen and eigen2cv) for the Eigen::Tensor module along with unit tests.

Pull Request Readiness Checklist

  • I agree to contribute to the project under OpenCV (BSD) License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@jgbradley1
Copy link
Author

I added a third convenience function (cv2eigen_tensormap) which provides a Tensor interface to cv Mat data without copying any data.

modules/core/include/opencv2/core/eigen.hpp Outdated Show resolved Hide resolved
template <typename _Tp> static inline
Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>> cv2eigen_tensormap(const cv::InputArray &src)
{
Mat mat = src.getMat();
Copy link
Member

Choose a reason for hiding this comment

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

Type check is required here through CV_CheckTypeEQ(mat.type(), traits::Type<_Tp>::value, "")

Copy link
Author

Choose a reason for hiding this comment

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

The suggestion almost worked. I had to modify to account for multiple channels.

CV_CheckTypeEQ(mat.type(), CV_MAKETYPE(traits::Type<_Tp>::value, mat.channels()), "");

Copy link
Member

Choose a reason for hiding this comment

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

@jgbradley1 You are right!

Perhaps this one should be clear:
CV_CheckDepthEQ(mat.depth(), traits::Depth<_Tp>::value, "");

Copy link
Author

Choose a reason for hiding this comment

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

Yes, checking the depth would have been clear (and less verbose code). It doesn’t matter to me which approach we use. Since you already approved the merge, I’ll let you decide if it’s worth opening up another PR to update the code.

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Well done! Thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants