本文介绍了如何从opencv cv :: Mat或行优先的数组初始化本征矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现Eigen的Matrix是默认的以列为主的矩阵,类似于MATLAB,但是如何从cv :: Mat初始化Eigen :: MatrixXd?以下代码是我的测试。但是它们都无法成功编译。有人可以给我一些建议吗?或其他一些链接?
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<MatrixXd> MapMat;
MapMat A_eigen(A,m,n);
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> A_eigen;
Eigen::Map<Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> >(A,m,n) = A_eigen;
已更新:
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<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > A_mappedMat (A, m, n);
Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > B_mappedMat (B, n, p);
Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, 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;
然后,当我输出M_C时,其结果是错误的。似乎C_eigenMat没有将数据复制到M_C.data中。
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_<float> a = Mat_<float>::ones(2,2);
Eigen::Matrix<float,Dynamic,Dynamic> b;
cv2eigen(a,b);
已经被回答:
//allocate memory for a 4x4 float matrix
cv::Mat cvT(4,4,CV_32FC1);
//directly use the buffer allocated by OpenCV
Eigen::Map<Matrix4f> eigenT( cvT.data() );
以及另外一个
这篇关于如何从opencv cv :: Mat或行优先的数组初始化本征矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!