Matrix和Mat的格式转

发布时间 2023-04-06 16:47:13作者: DoubleLi

说明

在 opencv 中矩阵都是使用 cv::Mat 表示,但是在 slam 中使用到了 Eigen::Matrix4d 数据类型,对于它们的类型转换,可以使用 opencv 的库,代码如下:

#include <opencv2/core/eigen.hpp>
#include <Eigen/Core>

int main()
{
    cv::Mat R = cv::ones(3, 3);
    Eigen::Matrix<double, 3, 3> R_matrix;
    cv::cv2eigen(R, R_matrix); // cv::Mat 转换成 Eigen::Matrix
    cv::eigen2cv(R_matrix, R); // Eigen::Matrix 转换成 cv::Mat
}

转换过程中,它会自动的从 Eigen::Matrix 的 double 数据类型转换成 cv::Mat 的 float 类型。

原型:

void eigen2cv(const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, Mat& dst)
void cv2eigen(const Mat& src, Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst)