在cv :: Mat到Eigen :: Matrix和之间的转换矩阵

时间:2016-10-06 01:58:53

标签: c++ opencv eigen

说我有:

Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> someMatrixEigen = someMatrix();

如果我要创建一个cv :: Mat标头(类型为CV_8U),我应该可以这样做,对吗?

cv::Mat someMatrixOCV(someMatrixEigen.rows(), someMatrixEigen.cols(), CV_8U, someMatrixEigen.data());

然后,如果我有cv::Mat anotherMatrixOCV,要回到Eigen,这也应该有用吗?

Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > anotherMatrixEigen(anotherMatrixOCV.ptr<int>(), anotherMatrixOCV.rows, anotherMatrixOCV.cols);

这里的整数类型是否兼容?当我使用float和CV_32F时,我似乎能够使这个工作,但整数版似乎不起作用(segfault)。

1 个答案:

答案 0 :(得分:0)

CV_8U似乎是无符号类型(特别是8位,例如unsigned char),而您的矩阵是带符号的int类型(多少位取决于您的编译器) ,但超过8)。您可以尝试使用CV_32S进行转化,或Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>