OpenCV CV::Mat and Eigen::Matrix(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);
我已经尝试使用 cv2eigen 和 eigen2cv,但是结果 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenCV CV::Mat 和 Eigen::Matrix
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
