将数据从 cv::Mat 转换为 mxArray
我想使用 C++ 代码中的“engine.h”将矩阵发送到 Matlab。事实是我在 cv::Mat 中有数据,并且我需要发送 mxArray。我尝试使用这个表达式,但它不起作用:
cv::Mat _priorP;
_priorP = Mat::eye(13, 13, CV_32FC1);
mxArray *mat;
mat = mxCreateDoubleMatrix(13, 13, mxREAL);
memcpy(mxGetPr(mat),_priorP.data, 13*13*sizeof(double));
有人知道进行转换的正确方法吗?任何帮助将不胜感激。谢谢。
编辑
I want to send a Matrix to Matlab using "engine.h" in c++ code. The fact is that I have the data inside a cv::Mat, and I need to sent a mxArray. I tried to use this expression but it doesn´t work:
cv::Mat _priorP;
_priorP = Mat::eye(13, 13, CV_32FC1);
mxArray *mat;
mat = mxCreateDoubleMatrix(13, 13, mxREAL);
memcpy(mxGetPr(mat),_priorP.data, 13*13*sizeof(double));
Anybody knows the correct way to do the conversion? Any help would be apreciated. Thanks.
EDIT
I found this way:
https://stackoverflow.com/a/8848711/744859
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个由 Kota Yamaguchi 开发的库,位于
http://github.com/kyamagu/mexopencv
该包包含一个 C++ 类(称为 MxArray),用于在 Matlab 的本机数据类型 (mxArray) 和 OpenCV 数据类型之间进行转换。该库直接支持 OpenCV 的 C++ API(Open CV 2.0 及更高版本),因此无需进行额外的转换(例如从 cvMat 到 cv::Mat 或从 IplImage 到 cv::Mat)。使用示例:
就是这样。确保将您的 mex 函数与库中的 MxArray.cpp 文件一起编译;您可以在 MATLAB 命令行中执行此操作:
There is a library developed by Kota Yamaguchi at
http://github.com/kyamagu/mexopencv
The package contains a C++ class (called MxArray) that converts between Matlab's native data type (mxArray) and OpenCV data types. The library directly supports C++ API for OpenCV (Open CV version 2.0 and higher), so there's no need to do an extra conversions (e.g. from cvMat to cv::Mat or from IplImage to cv::Mat). Sample usage:
That's it. Make sure to compile your mex function together with MxArray.cpp file from the library; you can do so in MATLAB command line:
此帖子展示了如何转换
CvMat
到mxArray
。尽管它并不完全是您正在寻找的转换代码,但它非常接近。这是一个简单的转换,您应该能够调整代码以使用
cv::Mat
而不是CvMat
。如果不能,一个快速技巧是将您的cv::Mat
数据转换为CvMat
,然后按原样使用下面的代码(取自我建议的链接) :This thread shows how to convert a
CvMat
tomxArray
. Even though it's not exactly the conversion code you are looking for, it's pretty close.This is a simple conversion and you should be able to adjust the code to work with
cv::Mat
instead ofCvMat
. If you can't, a quick hack is to convert yourcv::Mat
data toCvMat
and then use the code below as is (taken from the link I suggested):经过一番努力,我找到了一种更简单的方法来进行此转换。我所做的是创建一个像这样的函数:
I found a simpler method to do this convertion after some effort. What I do is creating a function like this: