我编写了一个mex函数(在C中),它从matlab获取n * 4,然后在struct中转换它们,最后将k * 5输出到matlab。
typedef vector<float> vectorf;
typedef vector<int> vectori;
typedef Array<float> arrayf;
typedef Array<int> arrayi;
typedef struct { int c, r, w, h; float s; } Box;
typedef vector<Box> Boxes;
void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] )
{
if(mxGetClassID(pr[13])!=mxSINGLE_CLASS) mexErrMsgTxt("boxes must be a float*");
if((int) mxGetN(pr[13])!=4) mexErrMsgTxt("boxes must have 4 columns");
float * box= (float *)mxGetData(pr[13]); //input to mex function
int k= (int) mxGetM(pr[13]) ; Boxes boxes;
for(int i=0; i<k; i++){
boxes[i].c= box[i + 0*k]-1;
boxes[i].r= box[i + 1*k]-1;
boxes[i].w= box[i + 2*k];
boxes[i].h= box[i + 3*k];
boxes[i].s=0;
}
// optionally create memory for visualization
arrayf V; if( nl>1 ) {
const mwSize ds[3] = {h,w,3};
pl[1] = mxCreateNumericArray(3,ds,mxSINGLE_CLASS,mxREAL);
V._x = (float*) mxGetData(pl[1]); V._h=h; V._w=w;
}
int n = (int) boxes.size(); //create output to matlab
pl[0] = mxCreateNumericMatrix(n,5,mxSINGLE_CLASS,mxREAL);
float *bbs = (float*) mxGetData(pl[0]);
for(int i=0; i<n; i++) {
bbs[i + 0*n ] = (float) boxes[i].c+1;
bbs[ i + 1*n ] = (float) boxes[i].r+1;
bbs[ i + 2*n ] = (float) boxes[i].w;
bbs[ i + 3*n ] = (float) boxes[i].h;
bbs[ i + 4*n ] = boxes[i].s;
}
}
由mex函数正确编译但是当我运行代码时,matlab崩溃了,我收到了这个错误:
Configuration:
Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Default Encoding : windows-1252
Deployed : false
Graphics Driver : Unknown hardware
Graphics card 1 : Intel Corporation ( 0x8086 ) Intel(R) HD Graphics Family Version 20.19.15.4835 (2017-10-16)
Java Version : Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Architecture : win64
MATLAB Entitlement ID : 6257193
MATLAB Root : C:\Program Files (x86)
MATLAB Version : 9.4.0.813654 (R2018a)
OpenGL : hardware
Operating System : Microsoft Windows 10 Pro
Process ID : 5904
Processor ID : x86 Family 6 Model 69 Stepping 1, GenuineIntel
Session Key : e3a6c784-70a3-4032-9402-bbff2b8d5ce9
Window System : Version 10.0 (Build 16299)
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
我将14个输入参数称为
bbs=edgeBoxesMex(E,O,o.alpha,o.beta,o.minScore,o.maxBoxes,...
o.edgeMinMag,o.edgeMergeThr,o.clusterMinMag,...
o.maxAspectRatio,o.minBoxArea,o.gamma,o.kappa,boxes);
和第14个是矩阵n * 4单个矩阵。 任何建议可以帮助我知道错误在哪里?
答案 0 :(得分:0)
我可以将你的代码减少到这些行并仍然导致崩溃(我还没有编译任何这些,所以我在这里解释):
typedef struct { int c, r, w, h; float s; } Box;
typedef vector<Box> Boxes;
void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] )
{
int k=1;
Boxes boxes;
for(int i=0; i<k; i++) {
boxes[i].s=0;
}
}
这是因为您创建了std::vector<Box>
,但没有给它一个大小,然后分配给它。
请改为:
Boxes boxes(k);