Merge pull request #25751 from simonkampe:fix-eigen-rowmajor

Add missing cv2eigen overload #25751

Fixes #16606

Add overloads to cv2eigen to handle eigen matrices of type
Eigen::Matrix<Tp_, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the 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
pull/25800/head
Simon Kämpe 9 months ago committed by GitHub
parent 57984e689b
commit 7ef42d7706
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 22
      modules/core/include/opencv2/core/eigen.hpp
  2. 21
      modules/core/test/test_mat.cpp

@ -287,6 +287,17 @@ void cv2eigen( const Mat& src,
}
}
template<typename _Tp> static inline
void cv2eigen( const Mat& src,
Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>& dst )
{
CV_CheckEQ(src.dims, 2, "");
dst.resize(src.rows, src.cols);
const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
src.convertTo(_dst, _dst.type());
}
// Matx case
template<typename _Tp, int _rows, int _cols> static inline
void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
@ -307,6 +318,17 @@ void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
}
}
template<typename _Tp, int _rows, int _cols> static inline
void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>& dst )
{
CV_CheckEQ(src.dims, 2, "");
dst.resize(_rows, _cols);
const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
Mat(src).copyTo(_dst);
}
template<typename _Tp> static inline
void cv2eigen( const Mat& src,
Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )

@ -2259,6 +2259,27 @@ TEST(Core_Eigen, eigen2cv_check_Mat_type)
EXPECT_ANY_THROW(eigen2cv(eigen_A, d_mat));
//EXPECT_EQ(CV_64FC1, d_mat.type());
}
TEST(Core_Eigen, cv2eigen_check_RowMajor)
{
Mat A(3, 2, CV_32FC1, Scalar::all(0));
A.at<float>(0,0) = 1.0;
A.at<float>(0,1) = 2.0;
A.at<float>(1,0) = 3.0;
A.at<float>(1,1) = 4.0;
A.at<float>(2,0) = 5.0;
A.at<float>(2,1) = 6.0;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> eigen_A;
EXPECT_NO_THROW(cv2eigen(A, eigen_A));
ASSERT_EQ(1.0, eigen_A(0, 0));
ASSERT_EQ(2.0, eigen_A(0, 1));
ASSERT_EQ(3.0, eigen_A(1, 0));
ASSERT_EQ(4.0, eigen_A(1, 1));
ASSERT_EQ(5.0, eigen_A(2, 0));
ASSERT_EQ(6.0, eigen_A(2, 1));
}
#endif // HAVE_EIGEN
#ifdef OPENCV_EIGEN_TENSOR_SUPPORT

Loading…
Cancel
Save