Skip to content

Add eigen tensor conversions#17320

Merged
alalek merged 14 commits intoopencv:3.4from
jgbradley1:add-eigen-tensor-conversions
May 23, 2020
Merged

Add eigen tensor conversions#17320
alalek merged 14 commits intoopencv:3.4from
jgbradley1:add-eigen-tensor-conversions

Conversation

@jgbradley1
Copy link
Copy Markdown

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
Copy Markdown
Author

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

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

2 participants