📄 sfcapproc.cpp
字号:
/***********************************************************************/
/* initialise camera */
/***********************************************************************/
//mexPrintf("New camera settings...\n");
mySetupCamera(mode);
/***********************************************************************/
/* initialise vision system (CMVision)... */
/***********************************************************************/
{
/* adjust 'width' (dims[1]) to be an integer multiple of the camera element width (3 for RGB, etc.) */
switch(mode) {
case CAMERA_YUV444_160x120:
case CAMERA_RGB8_640x480:
/* YUV, RGB -> each pixel is 3 bytes */
pixPerPacket = 1;
packetSize = 3;
break;
case CAMERA_YUV422_320x240:
case CAMERA_YUV422_640x480:
/* UYVY -> pixel pairs are represented by 4 bytes */
pixPerPacket = 2;
packetSize = 4;
break;
case CAMERA_YUV411_640x480:
/* YUV411 (UYYVYY) -> 4 pixel are represented by 6 bytes */
pixPerPacket = 4;
packetSize = 6;
break;
case CAMERA_Y8_640x480:
/* Y -> each byte is a pixel */
pixPerPacket = 1;
packetSize = 1;
break;
case CAMERA_Y16_640x480:
/* YY -> each pixel requires 2 bytes */
pixPerPacket = 1;
packetSize = 2;
break;
default:
// shouldn't be reached (assume RGB, just in case)
pixPerPacket = 1;
packetSize = 3;
break;
} /* switch */
// Determine beginning and end of the memory section to be processed
// (required by the 'convXXtoYY' methods above as these directly access the 'raw data' buffer
// of the camera -- width variable 'dims[1]' needs to be adjusted according to the number
// of pixels in the chosen processFrameWidth; the final output to MATLAB then further reduces
// this number to the requested number of pixels (fine selection)
//
// EXAMPLE: '3 pixels from pixel #4 on' -> need two packets (y1-y4) & (y5-y10)
// => firstPacketStart = 0, lastPacketStart = 6, dims[0] = 12
// | | |
// _________________v ____v__v_________
// --0--1--2--3--4--5---6--7--8--9-10-11--12-13--14-15--16--17
// -u1-y1-y2-v1-y3-y4--u2-y5-y6-v2-y7-y8--u3-y9-y10-v3-y11-y12
//
firstPacketStart = ((int)(origX/pixPerPacket) >= 0 ? (int)(origX/pixPerPacket) : 0 ) * packetSize;
lastPacketStart = ((int)((origX+dims[1]-1)/pixPerPacket) >= 0 ? (int)((origX+dims[1]-1)/pixPerPacket) : 0 ) * packetSize;
processFrameWidth = lastPacketStart - firstPacketStart + packetSize;
processFrameNumPix = processFrameWidth / packetSize * pixPerPacket;
packetOffsetX = origX - (int)(origX/pixPerPacket)*pixPerPacket;
// debugging stage...
#ifdef ERASE
if(mode != CAMERA_STOP) {
mexPrintf("firstPacketStart = %d\n", firstPacketStart);
mexPrintf("lastPacketStart = %d\n", lastPacketStart);
mexPrintf("processFrameWidth = %d\n", processFrameWidth);
mexPrintf("processFrameNumPix = %d\n", processFrameNumPix);
mexPrintf("origX = %d\n", origX);
mexPrintf("packetOffsetX = %d\n", packetOffsetX);
}
#endif
/* (re)initialize vision system with adjusted dimensions (CMVISION) */
/* NOTE: This now uses the adjusted number of pixels per row within the process frame
(might be slightly bigger than the requested width (dims[1]) */
mySetupVision(mode, processFrameNumPix /* width */, dims[0] /* height */);
}
// check if image data and regional data are to be displayed
if(retIMG) {
/* create numeric array FGretIMG (used to display the image) and make it persistent to ensure that
MATLAB remains its value between subsequent calls to mex32-functions (mdlStart, mdlOutput) */
FGretIMG = mxCreateNumericArray(3, (int const *)&dims[0], mxUINT8_CLASS, mxREAL);
mexMakeArrayPersistent(FGretIMG); /* IMPORTANT!! (FW-03-03) */
/* get pointer to data area */
MATbuf = (unsigned char *)mxGetData(FGretIMG);
/* open display... */
{
char myEvalStr[100];
unsigned int i;
int fi; // field index
// get IEEE representation of 'NaN'
myNaN = mxGetNaN();
// create 5-element vector call-up parameter and initialize it with [NaN NaN NaN NaN NaN]
Vect5U16Arg[0] = mxCreateNumericMatrix(1, 5, mxDOUBLE_CLASS, mxREAL);
mexMakeArrayPersistent(Vect5U16Arg[0]); /* IMPORTANT!! (FW-03-03) */
pVect5ArgData[0] = mxGetPr(Vect5U16Arg[0]); // beginning of data area
// fill array with [NaN NaN NaN NaN NaN] ... the values of this vector will be modified as we go along...
for(fi=0; fi<5; fi++) {
// set entry to outside the current frame -> 'NaN'
*(pVect5ArgData[0] + fi) = myNaN;
}
// create 5-element vector call-up parameter and initialize it with [NaN NaN NaN NaN NaN]
Vect5U16Arg[1] = mxCreateNumericMatrix(1, 5, mxDOUBLE_CLASS, mxREAL);
mexMakeArrayPersistent(Vect5U16Arg[1]); /* IMPORTANT!! (FW-03-03) */
pVect5ArgData[1] = mxGetPr(Vect5U16Arg[1]); // beginning of data area
// fill array with [NaN NaN NaN NaN NaN] ... this one will be kept at 'NaN'
for(fi=0; fi<5; fi++) {
// set entry to outside the current frame -> 'NaN'
*(pVect5ArgData[1] + fi) = myNaN;
}
// create 3-element vector call-up parameter and initialize it with [NaN NaN NaN NaN NaN]
Vect3U16Arg = mxCreateNumericMatrix(1, 3, mxDOUBLE_CLASS, mxREAL);
mexMakeArrayPersistent(Vect3U16Arg); /* IMPORTANT!! (FW-03-03) */
pVect3ArgData = mxGetPr(Vect3U16Arg); // beginning of data area
// fill array with [0 0 0]
for(fi=0; fi<5; fi++) {
// set colour to 'black'
*(pVect3ArgData + fi) = 0;
}
/* set command properties: 'XData', 'YData', 'CData', 'FaceColor', 'CloseRequestFcn', '', 'closereq', 'w-' */
SetPropStrings[0] = mxCreateString("XData");
SetPropStrings[1] = mxCreateString("YData");
SetPropStrings[2] = mxCreateString("CData");
SetPropStrings[3] = mxCreateString("FaceColor");
SetPropStrings[4] = mxCreateString("CloseRequestFcn");
SetPropStrings[5] = mxCreateString("");
SetPropStrings[6] = mxCreateString("closereq");
SetPropStrings[7] = mxCreateString("w-");
mexMakeArrayPersistent(SetPropStrings[0]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[1]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[2]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[3]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[4]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[5]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[6]); /* IMPORTANT!! (FW-03-03) */
mexMakeArrayPersistent(SetPropStrings[7]); /* IMPORTANT!! (FW-03-03) */
//mexPrintf("mdlSTART: before 'figure'.\n");
/* display image (which, at this stage, is an arbitrarily filled frame...) and make its handle
persistent to allow its use in mdlOutput */
mexCallMATLAB(1, &FGretIMGfigureHdl, 0, NULL, "figure");
mexMakeArrayPersistent(FGretIMGfigureHdl); /* IMPORTANT!! (FW-03-03) */
//mexPrintf("mdlSTART: before 'image'.\n");
mexCallMATLAB(1, &FGretIMGhandle, 1, &FGretIMG, "image");
mexMakeArrayPersistent(FGretIMGhandle); /* IMPORTANT!! (FW-03-03) */
// use fixed 'image' axes
mexEvalString("axis image");
// set image dimensions to (iWidth x iHeight)
sprintf(myEvalStr, "axis(0.5 + [0 %d 0 %d]);", frameWidth, frameHeight);
mexEvalString(myEvalStr);
// put axis on hold...
mexEvalString("hold on;");
//mexEvalString("set(gca, 'NextPlot', 'add');"); /* equivalent */
// set up 'patch' command call-up parameters ----------------------
// patch([NaN NaN NaN NaN NaN], [NaN NaN NaN NaN NaN], [0 0 0])
PlotArgs[0] = Vect5U16Arg[0];
PlotArgs[1] = Vect5U16Arg[0];
PlotArgs[2] = Vect3U16Arg;
// create centroids objects (patch objects, currently invisible)
for(i=0; i<maxREG; i++) {
//mexPrintf("mdlSTART: before 'patch'.\n");
/* call MATLAB to define 'patch' object and return its handle to variable FGretCEN[i] */
mexCallMATLAB(1, &FGretCEN[i], 3, PlotArgs, "patch");
/* ensure that MATLAB doesn't free data associated with FGretCEN (between subsequent calls to mdl-functions) */
/* !! needs to be done AFTER the call to mexCallMATLAB, because this command creates the corresponding handle object -- fw-01-08 !! */
mexMakeArrayPersistent(FGretCEN[i]); /* IMPORTANT!! (FW-03-03) */
} /* for (region) */
// set up 'plot command' call-up parameters ----------------------
// plot([NaN NaN NaN NaN NaN], [NaN NaN NaN NaN NaN], 'w-')
PlotArgs[0] = Vect5U16Arg[0];
PlotArgs[1] = Vect5U16Arg[0];
PlotArgs[2] = SetPropStrings[7]; // 'w-'
// create boundary box objects (plot objects, currently invisible)
for(i=0; i<maxREG; i++) {
/* call MATLAB to define 'plot' object and return its handle to variable FGretBOX[i] */
mexCallMATLAB(1, &FGretBOX[i], 3, PlotArgs, "plot");
/* ensure that MATLAB doesn't free data associated with FGretBOX (between subsequent calls to mdl-functions) */
/* !! needs to be done AFTER the call to mexCallMATLAB, because this command creates the corresponding handle object -- fw-01-08 !! */
mexMakeArrayPersistent(FGretBOX[i]); /* IMPORTANT!! (FW-03-03) */
} /* for (region) */
/* update display (... why not?! For a laugh...) */
sprintf(myEvalStr, "drawnow");
mexEvalString(myEvalStr);
/* set up call-up parameters for modifying the x-values of our patch/plot objects via command 'set' */
SetArgsXData[0] = FGretCEN[0]; // handle to first patch object (will be adjusted in 'mdlOutput')
SetArgsXData[1] = SetPropStrings[0]; // 'XData'
SetArgsXData[2] = Vect5U16Arg[0]; // [1 2 3 4 5]
/* set up call-up parameters for modifying the y-values of our patch/plot objects via command 'set' */
SetArgsYData[0] = FGretCEN[0]; // handle to first patch object (will be adjusted in 'mdlOutput')
SetArgsYData[1] = SetPropStrings[1]; // 'YData'
SetArgsYData[2] = Vect5U16Arg[0]; // [1 2 3 4 5]
/* set up call-up parameters for modifying the x-values of our patch/plot objects via command 'set' */
SetArgsXDataNaN[0] = FGretCEN[0]; // handle to first patch object (will be adjusted in 'mdlOutput')
SetArgsXDataNaN[1] = SetPropStrings[0]; // 'XData'
SetArgsXDataNaN[2] = Vect5U16Arg[1]; // [NaN NaN NaN NaN NaN]
/* set up call-up parameters for modifying the y-values of our patch/plot objects via command 'set' */
SetArgsYDataNaN[0] = FGretCEN[0]; // handle to first patch object (will be adjusted in 'mdlOutput')
SetArgsYDataNaN[1] = SetPropStrings[1]; // 'YData'
SetArgsYDataNaN[2] = Vect5U16Arg[1]; // [NaN NaN NaN NaN NaN]
/* set up call-up parameters for modifying the contents of FGretIMG (image) via command 'set' */
SetArgsCData[0] = FGretIMGhandle; // handle to image object
SetArgsCData[1] = SetPropStrings[2]; // 'CData'
SetArgsCData[2] = FGretIMG; // pointer to the array with the image data...
/* set up call-up parameters for modifying the face colour of our patch objects via command 'set' */
SetArgsFaceColor[0] = FGretCEN[0]; // handle to first patch object (will be adjusted in 'mdlOutput')
SetArgsFaceColor[1] = SetPropStrings[3]; // 'FaceColor'
SetArgsFaceColor[2] = Vect3U16Arg; // [1 2 3]
//mexPrintf("mdlStart: SetArgsFaceColor[0] = %0x\n", SetArgsFaceColor[0]);
//mexPrintf("mdlStart: SetArgsFaceColor[1] = %0x\n", SetArgsFaceColor[1]);
//mexPrintf("mdlStart: SetArgsFaceColor[2] = %0x\n", SetArgsFaceColor[2]);
/* set up call-up parameters for disabling the 'closereq' function of the figure window via command 'set' */
SetArgsCloseRequestFcnOff[0] = FGretIMGfigureHdl; // figure handle
SetArgsCloseRequestFcnOff[1] = SetPropStrings[4]; // 'CloseRequestFcn'
SetArgsCloseRequestFcnOff[2] = SetPropStrings[5]; // ''
/* set up call-up parameters for (re-)enabling the 'closereq' function of the figure window via command 'set' */
SetArgsCloseRequestFcnOn[0] = FGretIMGfigureHdl; // figure handle
SetArgsCloseRequestFcnOn[1] = SetPropStrings[4]; // 'CloseRequestFcn'
SetArgsCloseRequestFcnOn[2] = SetPropStrings[6]; // 'closereq'
// disable 'closereq' property of the current figure
mexCallMATLAB(0, NULL, 3, SetArgsCloseRequestFcnOff, "set");
}
} /* if(retIMG) */
// reset output variables with 1 (this becomes '-1' [invalid], unless a colour has been detected)
for(i=0; i<maxCOLS; i++) {
cen_av_x[i] = 1;
cen_av_y[i] = 1;
}
//mexPrintf("mdlSTART: out.\n");
}
/*
* mdlOutputs - compute the outputs
*
* In this function, you compute the outputs of your S-function
* block. The outputs are placed in the y variable.
*/
static void mdlOutputs(SimStruct *S, int_T tid) {
unsigned int numREG;
//mexPrintf("mdlOutput: in.\n");
/**********************/
/* acquire next frame */
/**********************/
/* acquire image -- make sure camera is actually running... */
if(initCamera == 1) {
//mexPrintf("Capturing image\n");
if(ACQmod == 1) {
// using ImageCapture
if(theCamera.CaptureImage() != CAM_SUCCESS)
mexErrMsgTxt("Error during image capture.\n");
} else {
/* start image capture */
if(theCamera.AcquireImage() != CAM_SUCCESS)
mexErrMsgTxt("Error during image acquisition.\n");
}
/* convert current frame to the YUYV format (required by CMVISION) */
convCurrentFrame2YUYV(mode, yuyvBuf);
}
/***********************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -