OpenCV CV ::垫子和本征::矩阵 [英] OpenCV CV::Mat and 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的更多信息,请看 特征教程:地图类
for more information on Eigen::Map take a look at Eigen Tutorial: Map Class
这篇关于OpenCV CV ::垫子和本征::矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!