Reputation: 135
I have a mex function which tries to transfer the image data of the CCD to the matlab in real time. In order to achieve that, I have to call matlab function in the image data callback function to transfer the image data to the matlab.
This is my entry mex function that registers a callback function which is snapThreadCallback()
to get the image data.
void continousSnapOnChoosed(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ) {
x = mxCreateDoubleMatrix(1,1, mxREAL);
double *A = mxGetPr(x);
A[0] = 50;
setMaxResolution();//设置为最大分辨率
HVSTATUS status = STATUS_OK;
status = HVOpenSnap(hhv, snapThreadCallback, hWnd);
mexPrintf("1:%d\n",status);
m_pRawBuffer = new BYTE[WIDTH * HEIGHT];
m_pImageBuffer = new BYTE[WIDTH * HEIGHT * 3];
BYTE *ppBuf[1];
ppBuf[0] = m_pRawBuffer;
status = HVStartSnap(hhv, ppBuf,1);
mexPrintf("2:%d\n",status);
}
int CALLBACK snapThreadCallback(HV_SNAP_INFO *pInfo)
{
int i,j,z;
ConvertBayer2Rgb(m_pImageBuffer,m_pRawBuffer,WIDTH,HEIGHT,ConvertType,pLutR,pLutG,pLutB,true,layout);
mwSize ndim = 3;
mwSize dims[3] = {HEIGHT,WIDTH,3};
mxArray *OUTArray;
OUTArray = mxCreateNumericArray(ndim,dims,mxUINT8_CLASS, mxREAL);
unsigned char *A;
A = (unsigned char *)mxGetData(OUTArray);
for (i = 0;i < WIDTH;i++) {
for (j = 0;j < HEIGHT;j++) {
for (z = 0; z < 3; z++)
A[(2 - z) * WIDTH * HEIGHT + i * HEIGHT + HEIGHT - j] = m_pImageBuffer[j * WIDTH * 3 + i * 3 + z];
}
}
mexCallMATLAB(0,NULL,1, &OUTArray, "matlab_function");
return 1;
}
In my matlab side, I simply display the image by the given data.
function matlab_function(data)
disp(data);
end
However, when I start open the snapshot, The matlab crashed instantly. I later found out that if I comment the mexCallMATLAB
. The matlab won't crash, and I'm sure there is nothing wrong with how I handle the DLL of the CCD. Because I'm already able to shoot a single frame picture and display it in the matlab.
There is little reference that I can take as examples, since this involves mex file and callback function. I will really appreciate if someone could solve my problem.
At last, I solved the problem by sending message in my callback function and pass the image data in the corresponding message function. It seems like the problem is the memory structure in the callback function is different than the memory structure in the normal C function. However I don't know how difference they are.
Upvotes: 1
Views: 409
Reputation: 23898
You shouldn't pass NULL
for plhs
when calling mexCallMatlab
, because even if you're calling a Matlab function with zero explicit output variables, it might still return an implicit one. (This is the one that gets stored in ans
or output to the desktop if you're working interactively in the IDE.). Allocate a one-long mxArray and pass it in to plhs
.
Upvotes: 0
Reputation: 30589
Your indexing of A
appears to be wrong. Instead of
A[(2 - z) * WIDTH * HEIGHT + i * HEIGHT + HEIGHT - j] = ...
the indexing should be
A[(2 - z) * WIDTH * HEIGHT + i * HEIGHT + HEIGHT - 1 - j] = ...
Think about when z=2
, i=0
, and j=0
, the result would have been A[HEIGHT]
but you want A[HEIGHT-1]
. But the buffer overrun comes when z=0
, i=WIDTH-1
and j=HEIGHT-1
.
Upvotes: 1