📄 sfcapproc.cpp
字号:
/* display image (opt) */
/***********************/
/* do we need to display image and regional data? */
if(retIMG) {
// now a tuneable parameter... fw-01-08
IMGmod = DISPMOD; /* 0: regular RGB display, 1: classified display */
// determine type of output required (regular, classified)
if(IMGmod == 0) {
// regular RGB output
// convert raw yuv422 image to RGB
convYUYV_2_RGB(img, (yuv422 *)yuyvBuf);
//mexPrintf("image converted to RGB\n");
/* copy RGB to the MATLAB buffer... */
for(unsigned int row=0; row<frameHeight; row++) {
for(unsigned int col=0; col<frameWidth; col++) {
unsigned int RGBindex = row*frameWidth+col;
MATbuf[row+col*frameHeight+0*frameWidth*frameHeight] = img[RGBindex].red;
MATbuf[row+col*frameHeight+1*frameWidth*frameHeight] = img[RGBindex].green;
MATbuf[row+col*frameHeight+2*frameWidth*frameHeight] = img[RGBindex].blue;
}
}
//mexPrintf("After 'copy'...\n");
// command: set(FGretIMG_handle, 'CData', FGretIMG);
mexCallMATLAB(0, NULL, 3, SetArgsCData, "set");
}
else {
// classified output
// classify image data
vision.testClassify(img, yuyvBuf);
//mexPrintf("Image classified\n");
/* copy classified image to the MATLAB buffer... */
for(unsigned int row=0; row<frameHeight; row++) {
for(unsigned int col=0; col<frameWidth; col++) {
unsigned int RGBindex = row*frameWidth+col;
MATbuf[row+col*frameHeight+0*frameWidth*frameHeight] = img[RGBindex].red;
MATbuf[row+col*frameHeight+1*frameWidth*frameHeight] = img[RGBindex].green;
MATbuf[row+col*frameHeight+2*frameWidth*frameHeight] = img[RGBindex].blue;
}
}
//mexPrintf("Classified image copied to output\n");
// command: set(FGretIMG_handle, 'CData', FGretIMG);
mexCallMATLAB(0, NULL, 3, SetArgsCData, "set");
} /* IMGmod */
} /* retIMG */
/**********************/
/* process image data */
/**********************/
//mexPrintf("frame about to be processed.\n");
vision.processFrame(yuyvBuf);
//mexPrintf("frame processed.\n");
/******************/
/* image analysis */
/******************/
for(unsigned int j=0; j<numCOLS; j++) {
// scan for detected regions with specified colour ID (now fixed ID '0', FW-03-03)
reg = vision.getRegions(j);
//mexPrintf("regions detected, colour #%d.\n", j);
// display colour to be scanned for
//printf("-------------------------------\n");
//printf("R|G|B: %3d|%3d|%3d\n", retREG[j]->colour.red,
// retREG[j]->colour.green,
// retREG[j]->colour.blue);
/* reset the number of detected regions */
numREG = 0;
retREG[j]->nREG = 0;
/* detect regions */
while(reg && reg->area>minAREA && numREG<maxREG) {
retREG[j]->cen_x[numREG] = (unsigned int)reg->cen_x;
retREG[j]->cen_y[numREG] = (unsigned int)reg->cen_y;
retREG[j]->x1[numREG] = (unsigned int)reg->x1;
retREG[j]->y1[numREG] = (unsigned int)reg->y1;
retREG[j]->x2[numREG] = (unsigned int)reg->x2;
retREG[j]->y2[numREG] = (unsigned int)reg->y2;
// display
//printf("Area of region %d: %d\n", numREG, reg->area);
//printf("cX|cY: %3d|%3d\n",retREG[j]->cen_x[numREG], retREG[j]->cen_y[numREG]);
retREG[j]->nREG = ++numREG;
reg = reg->next;
} /* while */
//printf("-------------------------------\n");
/*******************************/
/* display regional data (opt) */
/*******************************/
/* do we need to display image and regional data? */
if(retIMG) {
// is there any data to be displayed?
if(numREG) {
double x1, y1, x2, y2, r, g, b;
unsigned int i;
r = (double)(retREG[j]->colour.red/255);
g = (double)(retREG[j]->colour.green/255);
b = (double)(retREG[j]->colour.blue/255);
for(i=0; i<numREG; i++) {
// update patch object (centroid) ================================
// determine 'centroid' coordinates
x1 = (double)(retREG[j]->cen_x[i] - 1);
x2 = (double)(retREG[j]->cen_x[i] + 1);
y1 = (double)(retREG[j]->cen_y[i] - 1);
y2 = (double)(retREG[j]->cen_y[i] + 1);
// new x-data
*(pVect5ArgData[0] + 0) = x1;
*(pVect5ArgData[0] + 1) = x2;
*(pVect5ArgData[0] + 2) = x2;
*(pVect5ArgData[0] + 3) = x1;
*(pVect5ArgData[0] + 4) = x1;
/* call MATLAB to change property 'XData' of the selected 'patch' object (centroid) */
SetArgsXData[0] = FGretCEN[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsXData, "set");
// new y-data
*(pVect5ArgData[0] + 0) = y1;
*(pVect5ArgData[0] + 1) = y1;
*(pVect5ArgData[0] + 2) = y2;
*(pVect5ArgData[0] + 3) = y2;
*(pVect5ArgData[0] + 4) = y1;
/* call MATLAB to change property 'YData' of the selected 'patch' object (centroid) */
SetArgsYData[0] = FGretCEN[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsYData, "set");
// new 'face colour' [r g b]
*(pVect3ArgData + 0) = r;
*(pVect3ArgData + 1) = g;
*(pVect3ArgData + 2) = b;
/* call MATLAB to define change the property 'FaceColor' of the selected 'patch' object */
//mexPrintf("mdlOutput: before 'set(FaceColor)'.\n");
//mexPrintf("mdlOutput: SetArgsFaceColor[0] = %0x\n", SetArgsFaceColor[0]);
//mexPrintf("mdlOutput: SetArgsFaceColor[1] = %0x\n", SetArgsFaceColor[1]);
//mexPrintf("mdlOutput: SetArgsFaceColor[2] = %0x\n", SetArgsFaceColor[2]);
SetArgsFaceColor[0] = FGretCEN[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsFaceColor, "set");
//mexPrintf("mdlOutput: after 'set(FaceColor)'.\n");
// update plot object (boundary box) ============================
// determine 'boundary box' coordinates
x1 = (double)(retREG[j]->x1[i]);
x2 = (double)(retREG[j]->x2[i]);
y1 = (double)(retREG[j]->y1[i]);
y2 = (double)(retREG[j]->y2[i]);
// new x-data
*(pVect5ArgData[0] + 0) = x1;
*(pVect5ArgData[0] + 1) = x2;
*(pVect5ArgData[0] + 2) = x2;
*(pVect5ArgData[0] + 3) = x1;
*(pVect5ArgData[0] + 4) = x1;
/* call MATLAB to change property 'XData' of the selected 'patch' object (centroid) */
SetArgsXData[0] = FGretBOX[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsXData, "set");
// new y-data
*(pVect5ArgData[0] + 0) = y1;
*(pVect5ArgData[0] + 1) = y1;
*(pVect5ArgData[0] + 2) = y2;
*(pVect5ArgData[0] + 3) = y2;
*(pVect5ArgData[0] + 4) = y1;
/* call MATLAB to change property 'YData' of the selected 'patch' object (centroid) */
SetArgsYData[0] = FGretBOX[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsYData, "set");
} /* for (region) */
// remove 'invisible' objects
for(i=numREG; i<maxREG; i++) {
// update patch object (centroid) ================================
/* call MATLAB to change property 'XData' of the selected 'patch' object (centroid) to 'NaN' */
SetArgsXDataNaN[0] = FGretCEN[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsXDataNaN, "set");
/* call MATLAB to change property 'YData' of the selected 'patch' object (centroid) to 'NaN' */
SetArgsYDataNaN[0] = FGretCEN[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsYDataNaN, "set");
// update plot object (boundary box) ============================
/* call MATLAB to change property 'XData' of the selected 'patch' object (centroid) to 'NaN' */
SetArgsXDataNaN[0] = FGretBOX[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsXDataNaN, "set");
/* call MATLAB to change property 'YData' of the selected 'patch' object (centroid) to 'NaN' */
SetArgsYDataNaN[0] = FGretBOX[i]; // current 'patch' object (centroid)
mexCallMATLAB(0, NULL, 3, SetArgsYDataNaN, "set");
} /* for (invisible regions) */
} /* numREG */
} /* retIMG */
} /* for (colour) */
/* output coordinates */
{
real_T *outx = ssGetOutputPortRealSignal(S, 0);
real_T *outy = ssGetOutputPortRealSignal(S, 1);
// dectected something -> output centroid co-ordinates
for(unsigned int j=0; j<numCOLS; j++) {
// has colour 'j' been detected?
if(retREG[j]->nREG > 0) {
// yes...
// reset output variable
cen_av_x[j] = 0;
cen_av_y[j] = 0;
// sum up all centroids
for(unsigned int i=0; i<retREG[j]->nREG; i++) {
cen_av_x[j] = cen_av_x[j] + retREG[j]->cen_x[i];
cen_av_y[j] = cen_av_y[j] + retREG[j]->cen_y[i];
}
// calculate averaged centroid
cen_av_x[j] = (uint_T)((real_T)cen_av_x[j]/retREG[j]->nREG);
cen_av_y[j] = (uint_T)((real_T)cen_av_y[j]/retREG[j]->nREG);
outx[j] = (real_T)cen_av_x[j];
outy[j] = (real_T)cen_av_y[j];
}
else {
// nothing detected -> output negative of the previously stored value
outx[j] = -(real_T)cen_av_x[j];
outy[j] = -(real_T)cen_av_y[j];
}
} /* for */
}
//mexPrintf("mdlOutput: out.\n");
}
/*
* mdlTerminate - called when the simulation is terminated.
*
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was allocated
* in mdlInitializeConditions, this is the place to free it.
*/
static void mdlTerminate (SimStruct *S)
{
/* delete grabber object, free memory (when stopping) */
mySetupCamera(CAMERA_STOP);
// destroy class 'vision'
mySetupVision(CAMERA_STOP, 0, 0);
// free dynamically allocated memory of all regional data structures
for(unsigned int j=0; j<numCOLS; j++) {
delete retREG[j];
//mexPrintf("freed retREG[%d].\n", j);
}
if(retIMG) {
// (re-)enable 'closereq' property of the current figure
mexCallMATLAB(0, NULL, 3, SetArgsCloseRequestFcnOn, "set");
// close figure
mexCallMATLAB(0, NULL, 1, &FGretIMGfigureHdl, "close");
// MATLAB crashes if we do this... estrange. (fw-01-08)
#ifdef ERASE
/* free memory of persistent display variables */
mxDestroyArray(FGretIMG);
mxDestroyArray(Vect5U16Arg[0]);
mxDestroyArray(Vect5U16Arg[1]);
mxDestroyArray(Vect3U16Arg);
mxDestroyArray(SetPropStrings[0]);
mxDestroyArray(SetPropStrings[1]);
mxDestroyArray(SetPropStrings[2]);
mxDestroyArray(SetPropStrings[3]);
mxDestroyArray(SetPropStrings[4]);
mxDestroyArray(SetPropStrings[5]);
mxDestroyArray(SetPropStrings[6]);
mxDestroyArray(SetPropStrings[7]);
#endif
}
}
// the define 'MATLAB_MEX_FILE' has to be specified when recompiling this module to a DLL.
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -