OpenCV CV::Mat 和 Eigen::Matrix [英] OpenCV CV::Mat and Eigen::Matrix

查看:58
本文介绍了OpenCV CV::Mat 和 Eigen::Matrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种可逆的方法可以将 OpenCV cv::Mat 对象转换为 Eigen::Matrix 对象?

Is there a reversible way to convert an OpenCV cv::Mat object to an Eigen::Matrix?

例如,某种方式:

cv::Mat cvMat;
Eigen::Matrix eigMat;
camera->retrieve(cvMat);

// magic to convert cvMat to eigMat
// work on eigMat
// convert eigMat back to cvMat

imshow("Image", cvMat);

我已经尝试使用 cv2eigeneigen2cv,但是结果 cvMat 完全被破坏了,我不确定为什么.尺寸是正确的,但图形完全被破坏了,所以可能是每像素字节数或数据大小问题?

I've tried using cv2eigen and eigen2cv, but the resulting cvMat is completely mangled and I'm not exactly sure why. The dimensions are correct, but the graphics are totally trashed, so possibly a bytes-per-pixel or datasize issue?

推荐答案

您应该考虑使用 Eigen::Map 包装 OpenCV 矩阵,以便直接由 Eigen SDK 使用.这允许您将 Eigen 中实现的几乎所有功能应用于 OpenCV 分配的矩阵

You should consider using Eigen::Map to wrap OpenCV matrices in order to be used directly by the Eigen SDK. This allows you to apply almost all functionalities implemented in Eigen on matrix allocated by OpenCV

特别是,您只需实例化一个 Eigen::Map 提供指向 cv::Mat 缓冲区的指针:

In particular you simply instantiate an Eigen::Map providing the pointer to the cv::Mat buffer:

//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() ); 

有关 Eigen::Map 的更多信息,请查看Eigen 教程:地图类

for more information on Eigen::Map take a look at Eigen Tutorial: Map Class

这篇关于OpenCV CV::Mat 和 Eigen::Matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆