📄 imgprocsilent.cpp
字号:
/************************************************************************/
/* Check for proper number of input and output arguments */
/************************************************************************/
if (!(nlhs==1 && (nrhs==1 || nrhs==2 || nrhs==3))) {
mexErrMsgTxt("Usage: Region_StructureArray = imgProcSilent(RGBimage [, 'colour_filename'][, colour_list])\n " \
" default colour file: testcolors.txt\n" );
}
else {
// defaults
strcpy(colourFilename, defColourFilename);
maxCOL = defCOL; // default: just one colour
COLIDs[0] = 0; // default colour '0'
/* get 'RGBimage' parameter (always required) */
MATbuf = (unsigned char*)mxGetData(prhs[0]);
/* optional RHS: colour file name */
if(nrhs >= 2) {
mxGetString(prhs[1], colourFilename, colourFilenameLength);
//mexPrintf("colour file: %s\n", colourFilename);
}
/* optional RHS: list of colours to be scanned for */
if(nrhs >= 3) {
/* get list of colours */
if(!mxIsEmpty(prhs[2])) {
if(!mxIsNumeric(prhs[2]) && !mxIsComplex(prhs[2]))
mexErrMsgTxt("The specified colour list has to be a vector with up to 16 colour IDs\n");
maxCOL = (unsigned int)mxGetNumberOfElements(prhs[2]);
if(maxCOL > CMV_MAX_COLORS) {
mexWarnMsgTxt("Too many colours specified. Reducing to maximum permitted (32)\n");
maxCOL = CMV_MAX_COLORS;
}
// read colour vector
pfout = mxGetPr(prhs[2]);
for(fi=0; fi<maxCOL; fi++) {
COLIDs[fi] = (unsigned int)(*(pfout+fi))-1;
if((COLIDs[fi] < 0) || (COLIDs[fi] >= CMV_MAX_COLORS))
mexErrMsgTxt("Colour(s) outside the valid range (1 ... 32).\n");
}
}
}
}
/* determine image size */
inDims = mxGetDimensions(prhs[0]);
frameHeight = (unsigned long)inDims[0]; // rows
frameWidth = (unsigned long)inDims[1]; // columns
/* ensure minimum width and height (to avoid crashes) */
if(frameWidth < 2 || frameHeight < 1) {
// input image size is too small
mexErrMsgTxt("Size of provided RGB image is too small (minimum: 2 x 1 pixels).\n");
}
/* vision algorithms are yuyv based -- won't work if 'frameWidth' is an odd number
(need 2 RGB pixels to form one yuyv pixel) -> adjust width, if required */
if((frameWidth%2) == 1) {
//mexPrintf("Reducing width by one pixel to make frameWidth become an even number\n");
frameWidth--;
}
//mexPrintf("frameWidth = %d\n", frameWidth);
//mexPrintf("frameHeight = %d\n", frameHeight);
/* allocate necessary memory... */
mySetupVision(frameWidth, frameHeight);
// convert RGB image to YUYV equivalent
convRGB_2_YUYV(yuyvBuf, MATbuf);
//mexPrintf("RGB image converted to yuyv.\n");
// process image data
//mexPrintf("before process.\n");
vision.processFrame(yuyvBuf);
//mexPrintf("after process.\n");
// reset 'detected regions' list
numCOL = 0;
for(i=0; i<maxCOL; i++) COLdetected[i] = -1;
// scan regions with specified colour IDs
for(i=0; i<maxCOL; i++) {
reg = vision.getRegions(COLIDs[i]);
// allocate memory to store data
retREG[i] = (struct myREG *)mxCalloc(1, sizeof(struct myREG));
// initialize structure
retREG[i]->colourID = COLIDs[i];
retREG[i]->colour = vision.getColorVisual(COLIDs[i]);
mexPrintf("-------------------------------\n");
mexPrintf("R|G|B: %3d|%3d|%3d\n", retREG[i]->colour.red,
retREG[i]->colour.green,
retREG[i]->colour.blue);
numREG = 0;
while(reg && reg->area>minAREA && numREG<maxREG) {
retREG[i]->cen_x[numREG] = (unsigned int)reg->cen_x;
retREG[i]->cen_y[numREG] = (unsigned int)reg->cen_y;
retREG[i]->x1[numREG] = (unsigned int)reg->x1;
retREG[i]->y1[numREG] = (unsigned int)reg->y1;
retREG[i]->x2[numREG] = (unsigned int)reg->x2;
retREG[i]->y2[numREG] = (unsigned int)reg->y2;
mexPrintf("Area of region %d: %d\n", numREG, reg->area);
mexPrintf("X1|Y1|X2|Y2: %3d|%3d|%3d|%3d\n", retREG[i]->x1[numREG], retREG[i]->y1[numREG],
retREG[i]->x2[numREG], retREG[i]->y2[numREG]);
retREG[i]->nREG = ++numREG;
reg = reg->next;
}
// check if any regions were detected for this colour
if(numREG) {
// valid colour... (found at least one valid region)
COLdetected[numCOL++] = i; // corresponding colour ID (loop variable)
}
}
mexPrintf("-------------------------------\n");
/* destroy vision object & free memory */
mySetupVision(0, 0);
/*************************************************************************/
/* return regional information in form of a structure array */
/*************************************************************************/
if((pArray = mxCreateStructMatrix(maxCOL, 1, 4, fnames)) == NULL)
mexErrMsgTxt("Failure assigning stucture memory\n");
// update 'ColourID' field...
for (i=0; i<numCOL; i++) // ... in all elements of the structure
{
// get index of the detected colour
j = COLdetected[i];
// create mxArray to store the colour information
fout = mxCreateDoubleMatrix(1,1,mxREAL); // colourID
pfout = mxGetPr(fout); // beginning of data area
//printf("Updating colourID (%d)\n", j);
// copy data
*(pfout ) = (double)(retREG[j]->colourID);
// update structure entry (field index: 0)
mxSetFieldByNumber(pArray,i,0,fout);
}
//printf("Updated colourID\n");
// update 'Colour' field...
for (i=0; i<numCOL; i++) // ... in all elements of the structure
{
// get index of the detected colour
j = COLdetected[i];
// create mxArray to store the colour information
fout = mxCreateDoubleMatrix(1,3,mxREAL); // R, G, B
pfout = mxGetPr(fout); // beginning of data area
// copy data
*(pfout ) = (double)(retREG[j]->colour.red)/255.0;
*(pfout+1) = (double)(retREG[j]->colour.green)/255.0;
*(pfout+2) = (double)(retREG[j]->colour.blue)/255.0;
// update structure entry (field index: 0)
mxSetFieldByNumber(pArray,i,1,fout);
}
//printf("Updated colour\n");
// update 'nRegions' field...
for (i=0; i<numCOL; i++) // ... in all elements of the structure
{
// get index of the detected colour
j = COLdetected[i];
// create mxArray to store the number of valid regions found
fout = mxCreateDoubleMatrix(1,1,mxREAL);
pfout = mxGetPr(fout); // beginning of data area
// copy data
*(pfout ) = (double)(retREG[j]->nREG);
// update structure entry (field index: 1)
mxSetFieldByNumber(pArray,i,2,fout);
}
//printf("Updated nRegions\n");
// update 'Regions' field...
for (i=0; i<numCOL; i++) // ... in all elements of the structure
{
// get index of the detected colour
j = COLdetected[i];
// create mxArray to store the colour information
numREG = (unsigned int)(retREG[j]->nREG);
if(numREG)
{
//printf(">> In numREG %d\n", i);
//printf(">> numREG = %d\n", numREG);
*((int*)foutdims) = 6; // cen_x, cen_y, x, y, w, h
foutdims[1] = (int)numREG; // valid regions
fout = mxCreateNumericArray(2, foutdims, mxDOUBLE_CLASS, mxREAL);
pfout = mxGetPr(fout); // beginning of data area
// copy data
for(fi=0; fi<numREG; fi++)
{
*(pfout+fi*6 ) = (double)(retREG[j]->cen_x[fi]);
*(pfout+fi*6+1) = (double)(retREG[j]->cen_y[fi]);
*(pfout+fi*6+2) = (double)(retREG[j]->x1[fi]);
*(pfout+fi*6+3) = (double)(retREG[j]->y1[fi]);
*(pfout+fi*6+4) = (double)(retREG[j]->x2[fi]);
*(pfout+fi*6+5) = (double)(retREG[j]->y2[fi]);
// update structure entry (field index: 2)
mxSetFieldByNumber(pArray,i,3,fout);
}
}
}
//printf("Updated Regions\n");
// free dynamically allocated memory
for(i=0; i<maxCOL; i++) mxFree(retREG[i]);
// return pointer
plhs[0] = pArray;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -