【发布时间】:2014-12-16 21:55:26
【问题描述】:
当我编写使用 OpenCV 函数的 MEX 文件时,很容易将数据 从 MATLAB 传递到 MEX 环境,而无需复制数据。有没有办法以相同的方式将数据返回 MATLAB? (也就是说,无需复制数据,也不会导致 MATLAB 崩溃……)
一个简单的例子:
#include "mex.h"
#include "/opencv2/core.hpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[])
{
Rows=mxGetM(prhs[0]);
Cols=mxGetN(prhs[0]);
Mat InMat(Cols,Rows,CV_64FC1,mxGetPr(prhs[0]));//Matlab passes data column-wise
// no need to copy data - SUPER!
InMat=InMat.t();//transpose so the matrix is identical to MATLAB's one
//Make some openCV operations on InMat to get OutMat...
//Way of preventing the following code??
plhs[0]=mxCreateDoubleMatrix(OutMat.rows,OutMat.cols,mxREAL);
double *pOut=mxGetPr(plhs[0]);
for (int i(0);i<OutMat.rows;i++)
for (int j(0);j<OutMat.cols;j++)
pOut[i+j*OutMat.rows]=OutMat.at<double>(i,j);
}
【问题讨论】: