📄 capclassify.cpp
字号:
delete img;
img = NULL;
//mexPrintf("img buffer destroyed.\n");
}
}
} /* mySetupVision ============================================================ */
/* mexFunction ============================================================= */
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) {
mxArray *pArray; /* output data */
unsigned char *MATbuf; /* pointer to MATLAB memory */
myCameraModes mode;
/************************************************************************/
/* Check arguments and read input (number of pixels) */
/************************************************************************/
if(!(nlhs<=1 && (nrhs==1 || nrhs==2 || nrhs==3 || nrhs==5 || nrhs==7))) {
mexErrMsgTxt("Usage: data = capClassify(mode [, 'colour_filename'][, verbose][, sizeX, sizeY][, originX, originY])\n " \
"mode: -1 STOP\n" \
" 0 YUV444 (160x120, 30 fps)\n" \
" 1 YUV422 (320x240, 30 fps)\n" \
" 2 YUV411 (640x480, 30 fps)\n" \
" 3 YUV422 (640x480, 15 fps)\n" \
" 4 RGB8 (640x480, 15 fps)\n" \
" 5 Y8 (640x480, 30 fps)\n" \
" 6 Y16 (640x480, 15 fps)\n" \
" verbose: 0 silent\n" \
" 1 verbose\n" \
" default colour file: testcolors.txt\n" );
}
else {
// defaults
strcpy(colourFilename, defColourFilename);
verboseFlag = 0;
requestedWidth = 0;
requestedHeight = 0;
requestedOrigX = 0;
requestedOrigY = 0;
/* get 'mode' parameter (always required) */
mode = myCameraModes((int)(mxGetScalar(prhs[0])));
//mexPrintf("mode = %d\n", mode);
//mexPrintf("initCamera = %d\n", initCamera);
/* optional: colour file name */
if(nrhs >= 2) {
mxGetString(prhs[1], colourFilename, colourFilenameLength);
//mexPrintf("colour file: %s\n", colourFilename);
}
/* optional: verbosity flag */
if(nrhs >= 3) {
verboseFlag = (unsigned int)(mxGetScalar(prhs[2]));
}
/* optional: size */
if(nrhs >= 5) {
requestedWidth = (unsigned int)(mxGetScalar(prhs[3]));
requestedHeight = (unsigned int)(mxGetScalar(prhs[4]));
}
/* optional: origin */
if(nrhs == 7) {
requestedOrigX = (unsigned int)(mxGetScalar(prhs[5]));
requestedOrigY = (unsigned int)(mxGetScalar(prhs[6]));
}
}
/* resize... if necessary */
if( currentMode != mode || \
currWidth != requestedWidth || \
currHeight != requestedHeight || \
currOrigX != requestedOrigX || \
currOrigY != requestedOrigY ) {
/* stop acquisition - if currently ongoing */
if(initCamera) {
/* stop camera */
mySetupCamera(CAMERA_STOP);
/* stop vision */
mySetupVision(CAMERA_STOP, 0, 0);
}
/* (re)initialize camera */
mySetupCamera(mode);
/* set requested ORIGIN (X) of the image to be returned to MATLAB */
if(requestedOrigX >= 0 && requestedOrigX < frameWidth) {
/* change of origin (X) */
origX = requestedOrigX;
} else {
/* default origin (X) */
origX = 0;
//mexPrintf("Requested origin-x (%d) outside valid limits (0 ... %d). Using default origin (0).\n", requestedOrigX, frameWidth-1);
}
/* remember that the origin has been set */
currOrigX = requestedOrigX;
/* set requested WIDTH of the image to be returned to MATLAB */
if(requestedWidth > 0 && requestedWidth <= frameWidth-origX) {
/* requested width is valid -> reduce width of output array */
dims[1] = requestedWidth;
} else {
/* requested width exceeds framewidth -> allocate as much as available */
dims[1] = frameWidth - origX;
}
/* remember that the width has been set */
currWidth = requestedWidth;
/* set requested ORIGIN (Y) of the image to be returned to MATLAB */
if(requestedOrigY >= 0 && requestedOrigY < frameHeight) {
/* change of origin (Y) */
origY = requestedOrigY;
} else {
/* default origin (X) */
origY = 0;
//mexPrintf("Requested origin-y (%d) outside valid limits (0 ... %d). Using default origin (0).\n", requestedOrigY, frameHeight-1);
}
/* remember that the origin has been set */
currOrigY = requestedOrigY;
/* set requested HEIGHT of the image to be returned to MATLAB */
if(requestedHeight > 0 && requestedHeight <= frameHeight-origY) {
/* requested height is valid -> reduce height of output array */
dims[0] = requestedHeight;
} else {
/* requested height exceeds frameheight -> allocate as much as available */
dims[0] = frameHeight - origY;
}
/* remember that the height has been set */
currHeight = requestedHeight;
#ifdef ERASE
// debug only
mexPrintf("requestedWidth = %d\n", requestedWidth);
mexPrintf("currWidth = %d\n", currWidth);
mexPrintf("dims[1] = %d\n", dims[1]);
mexPrintf("requestedHeight = %d\n", requestedHeight);
mexPrintf("currHeight = %d\n", currHeight);
mexPrintf("dims[0] = %d\n", dims[0]);
mexPrintf("requestedOrigX = %d\n", requestedOrigX);
mexPrintf("currOrigX = %d\n", currOrigY);
mexPrintf("origX = %d\n", origX);
mexPrintf("requestedOrigY = %d\n", requestedOrigY);
mexPrintf("currOrigY = %d\n", currOrigY);
mexPrintf("origY = %d\n", origY);
#endif
/* 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("requestedWidth = %d\n", requestedWidth);
mexPrintf("requestedOrigX = %d\n", requestedOrigX);
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 */);
}
/* create a MATLAB array with 'm_width' columns, 'm_height' rows, 'real numbers' */
/* NOTE: dims[] are still the requested dimensions! -> output size is always as expected */
pArray = mxCreateNumericArray(3, (int const *)&dims[0], mxUINT8_CLASS, mxREAL);
//mexPrintf("pArray created\n");
/* acquire image -- make sure camera is actually running... */
if(initCamera == 1) {
//mexPrintf("Capturing image\n");
if(theCamera.CaptureImage() != CAM_SUCCESS)
mexErrMsgTxt("Error during image capture.\n");
/* convert camera data to RGB and copy it to 'bufRGB' (input format automatically accounted for) */
//if(theCamera.getRGB(bufRGB, nElements) != CAM_SUCCESS)
// mexErrMsgTxt("Error during conversion to RGB.\n");
/* ------------- CMVision ------------- */
/* convert current frame to the YUYV format (required by CMVISION) */
convCurrentFrame2YUYV(mode, yuyvBuf);
// process image data
vision.testClassify(img, (yuv422 *)yuyvBuf);
// convert selected frame back to RGB
//convYUYV2RGB(img, (yuv422 *)yuyvBuf);
/* ------------- CMVision ------------- */
/* transfer data from DMA buffer to the MATLAB buffer... */
MATbuf = (unsigned char *)mxGetPr(pArray);
/* copy RGB to the MATLAB buffer... & turn image upside down */
//
// define the following macro to reverse the orientation of the image (upside down)
//#define UPSIDEDOWN
//
{
#ifdef UPSIDEDOWN
unsigned long lastRGBelement = dims[0]*processFrameNumPix;
#endif
for(unsigned int row=0; row<dims[0]; row++) /* height */ {
//mexPrintf("row = %d -- ", row);
//for(unsigned int col=0; col<dims[1]; col++) /* width */ {
for(unsigned int col=0; col<dims[1]; col++) /* width */ {
//mexPrintf("%d ", col);
#ifdef UPSIDEDOWN
unsigned int RGBindex = lastRGBelement - (row+1)*processFrameNumPix + col + packetOffsetX;
#else
unsigned int RGBindex = row*processFrameNumPix + col + packetOffsetX;
#endif /* UPSIDEDOWN */
MATbuf[row+col*dims[0]+0*dims[1]*dims[0]] = img[RGBindex].red;
MATbuf[row+col*dims[0]+1*dims[1]*dims[0]] = img[RGBindex].green;
MATbuf[row+col*dims[0]+2*dims[1]*dims[0]] = img[RGBindex].blue;
}
}
} /* copy RGB */
} /* initCamera == 1 */
/* set return pointer */
plhs[0] = pArray;
} /* mexFunction ============================================================ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -