問題描述
是否有一種可逆的方法可以將 OpenCV cv::Mat
對象轉(zhuǎn)換為 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);
我已經(jīng)嘗試使用 cv2eigen
和 eigen2cv
,但是結(jié)果 cvMat
完全被破壞了,我不確定為什么.尺寸是正確的,但圖形完全被破壞了,所以可能是每像素字節(jié)數(shù)或數(shù)據(jù)大小問題?
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?
推薦答案
您應(yīng)該考慮使用 Eigen::Map 包裝 OpenCV 矩陣,以便直接由 Eigen SDK 使用.這允許您將 Eigen 中實(shí)現(xiàn)的幾乎所有功能應(yīng)用于 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
特別是,您只需實(shí)例化一個(gè) Eigen::Map 提供指向 cv::Mat 緩沖區(qū)的指針:
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() );
有關(guān) Eigen::Map 的更多信息,請查看Eigen 教程:地圖類
for more information on Eigen::Map take a look at Eigen Tutorial: Map Class
這篇關(guān)于OpenCV CV::Mat 和 Eigen::Matrix的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!