我想在c ++代码中使用“engine.h”将Matrix发送到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));
有人知道进行转换的正确方法吗?任何帮助都会被贬低。感谢。
修改
答案 0 :(得分:10)
有一个由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)。样品用法:
#include "mexopencv.hpp" // include the library
#include "highgui.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
cv::Mat image;
image = imread("filename"); // read an image from file
plhs[0] = MxArray(image); // convert from cv::Mat to mxArray
}
就是这样。确保与库中的和 MxArray.cpp文件一起编译mex函数;您可以在MATLAB命令行中执行此操作:
mex yourmexfile.cpp MxArray.cpp
答案 1 :(得分:4)
This thread显示了如何将CvMat
转换为mxArray
。即使它不是您正在寻找的转换代码,但它非常接近。
这是一次简单的转换,您应该可以调整代码以使用cv::Mat
代替CvMat
。如果你不能,快速破解是将cv::Mat
数据转换为CvMat
,然后按原样使用下面的代码(取自我建议的链接):
mxArray* CvMat_to_new_mxArr (const CvMat* mat)
{
const int TYPE = cvGetElemType (mat);
// 2-d image
if (CV_64FC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_64FC1> (mat);
}
else if (CV_32FC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_32FC1> (mat);
}
else if (CV_32SC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_32SC1> (mat);
}
else if (CV_16SC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_16SC1> (mat);
}
else if (CV_16UC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_16UC1> (mat);
}
else if (CV_8UC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_8UC1> (mat);
}
else if (CV_8SC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_8SC1> (mat);
}
//Multi-dimensional arrays not supported, yet.
/*
// 3-d image
else if (CV_64FC3 == TYPE) {
return helper_rgbimage_to_mat<IPL_DEPTH_64F> (img);
}
else if (CV_32FC3 == TYPE) {
return helper_rgbimage_to_mat<IPL_DEPTH_32F> (img);
}
else if (CV_8UC3 == TYPE) {
return helper_rgbimage_to_mat<IPL_DEPTH_8U> (img);
}
*/
// unsupported conversion, return null mxArray
return mxCreateDoubleMatrix(0,0,mxREAL);
}
template<int TYPE>
mxArray* helper_2dcvmat_to_mat (const CvMat* mat)
{
void* pBeg;
int pitch;
cvGetRawData(mat, (uchar**)&pBeg,&pitch);
CvSize size = cvGetSize (mat);
const mxClassID cid = cvm_traits<TYPE>::CID;
mxArray* pArrOut =
mxCreateNumericMatrix(size.height,size.width,cid,mxREAL);
void* pBegOut = mxGetData(pArrOut);
typedef mc_traits<cid>::CT T;
pix_iterator_2d<T,eRowWise> it_src1(static_cast<T*>(pBeg),
size.width,size.height,pitch);
pix_iterator_2d<T,eRowWise> it_src2(static_cast<T*>(pBeg),
size.width,size.height,pitch);
it_src2.end ();
pix_iterator_2d<T,eColWise> it_dest(static_cast<T*>(pBegOut),
size.width,size.height);
std::copy (it_src1,it_src2,it_dest);
return pArrOut;
}
答案 2 :(得分:3)
经过一番努力后,我找到了一种更简单的方法来进行转换。我所做的是创建这样的函数:
void arithmetic::cvLoadMatrixToMatlab( Engine *ep, const Mat& m, string name)
{
int rows=m.rows;
int cols=m.cols;
//Mat data is float, and mxArray uses double, so we need to convert.
mxArray *T=mxCreateDoubleMatrix(cols, rows, mxREAL);
double *buffer=(double*)mxGetPr(T);
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
buffer[i*(cols)+j]= (double)m.at<float>(i, j);
}
}
//memcpy((char*)mxGetPr(T), (char*)m.data, rows*cols*sizeof(double));
engPutVariable(ep, name.c_str(), T);
name=name+"="+name+"'"; // Column major to row major (mat=mat')
engEvalString(ep, name.c_str());
mxDestroyArray(T);
}