opencv矩阵转eigen_如何从opencv cv :: Mat或行优先的数组初始化本征矩阵?

本文探讨了如何将OpenCV的cv::Mat转换为Eigen的MatrixXd。由于Eigen矩阵默认为列主序,而cv::Mat可能是行主序,因此直接转换可能会导致编译错误。解决方案包括使用Eigen的Map类来映射cv::Mat的数据,并通过Map类的构造函数正确地指定行数和列数。在完成转换后,进行了矩阵乘法操作,但发现结果不正确,可能是因为数据没有正确复制回cv::Mat。
摘要由CSDN通过智能技术生成

I found that Eigen's Matrix is default column-major, which is like MATLAB, but how do I initialize an Eigen::MatrixXd from an cv::Mat? The following code is my test. But none of them could be compiled successfully. Could someone give me some advice, please? or some other links? Thanks.

cv::Mat A_M=cv::Mat(rows, cols, CV_64FC1);

double *A=(double *)A_M.data();

typedef Map MapMat;

MapMat A_eigen(A,m,n);

Eigen::Matrix A_eigen;

Eigen::Map >(A,m,n) = A_eigen;

Updated:

double *A=(double *)A_M.data();//m*n

double *B=(double *)B_M.data();//n*p

double *C=(double *)C_M.data();//m*p

//regular Eigen Matrix

Eigen::MatrixXd A_eigenMat;

Eigen::MatrixXd B_eigenMat;

Eigen::MatrixXd C_eigenMat;

Eigen::Map<:matrix eigen::dynamic eigen::rowmajor> > A_mappedMat (A, m, n);

Eigen::Map<:matrix eigen::dynamic eigen::rowmajor> > B_mappedMat (B, n, p);

Eigen::Map<:matrix eigen::dynamic eigen::rowmajor> > C_mappedMat (C, m, p);

// Eigen handles the conversion from row major to column major

A_eigenMat = A_mappedMat;

B_eigenMat = B_mappedMat;

C_eigenMat = C_mappedMat;

// multiplication

C_eigenMat=A_eigenMat*B_eigenMat;

Then, when I output the M_C, its result is wrong. It seems the C_eigenMat didn't copy the data into M_C.data.

解决方案cv::Mat_ a = Mat_::ones(2,2);

Eigen::Matrix b;

cv2eigen(a,b);

It is already answered on SO:

//allocate memory for a 4x4 float matrix

cv::Mat cvT(4,4,CV_32FC1);

//directly use the buffer allocated by OpenCV

Eigen::Map eigenT( cvT.data() );

and in one more SO post

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值