📄 imgproctest.cpp
字号:
}
}
/* open display... */
{
unsigned int i;
int fi; // field index
char myEvalStr[100];
// get IEEE representation of 'NaN'
myNaN = mxGetNaN();
/* display image (at this stage an arbitrarily filled frame...) */
mexCallMATLAB(1, &FGretIMGhandle, 1, &FGretIMG, "image");
//mexMakeArrayPersistent(FGretIMGhandle); /* IMPORTANT!! (FW-03-03) */
// use fixed 'image' axes
mexEvalString("axis image");
//mexEvalString("axis image ij;"); /* use this one if the picture is upside down... */
// put axis on hold...
mexEvalString("hold on;");
//mexEvalString("set(gca, 'NextPlot', 'add');"); /* equivalent */
// set image dimensions to (iWidth x iHeight)
sprintf(myEvalStr, "axis(0.5 + [0 %d 0 %d]);", frameWidth, frameHeight);
mexEvalString(myEvalStr);
// set up 'patch command' call-up parameters ----------------------
// 1st call-up parameter: [NaN NaN NaN NaN NaN]
PatchArgs[0] = mxCreateNumericMatrix(1, 5, mxDOUBLE_CLASS, mxREAL);
pArgs = mxGetPr(PatchArgs[0]); // beginning of data area
// fill array with [NaN NaN NaN NaN NaN]
*(pArgs + 0) = (double)(10.0);
*(pArgs + 1) = (double)(50.0);
*(pArgs + 2) = (double)(50.0);
*(pArgs + 3) = (double)(10.0);
*(pArgs + 4) = (double)(10.0);
// 2nd call-up parameter: [NaN NaN NaN NaN NaN]
PatchArgs[1] = mxCreateNumericMatrix(1, 5, mxDOUBLE_CLASS, mxREAL);
pArgs = mxGetPr(PatchArgs[1]); // beginning of data area
// fill array with [NaN NaN NaN NaN NaN]
*(pArgs + 0) = (double)(10.0);
*(pArgs + 1) = (double)(10.0);
*(pArgs + 2) = (double)(50.0);
*(pArgs + 3) = (double)(50.0);
*(pArgs + 4) = (double)(10.0);
// 3rd call-up parameter: [0 0 0]
PatchArgs[2] = mxCreateNumericMatrix(1, 3, mxDOUBLE_CLASS, mxREAL);
pArgs = mxGetPr(PatchArgs[2]); // beginning of data area
// fill array with [0 0 0]
for(fi=0; fi<3; fi++) {
// set entry to 'NaN'
*(pArgs + fi) = (double)0.0;
}
// set up 'plot command' call-up parameters ----------------------
PlotArgs[0] = PatchArgs[0];
PlotArgs[1] = PatchArgs[1];
PlotArgs[2] = mxCreateString("w-"); // colour of boundary box: always 'white'
// first call-up parameter of 'set' command: 'XData' / 'YData' / 'FaceColor'
SetArgStrings[0] = mxCreateString("XData");
SetArgStrings[1] = mxCreateString("YData");
SetArgStrings[2] = mxCreateString("FaceColor");
SetArgStrings[3] = mxCreateString("CData");
// prepare display of centroids
for(i=0; i<maxREG; i++) {
/* create numeric array FGretCEN (centroid object handles) */
FGretCEN[i] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL);
//mexPrintf("FGretCEN created\n");
/* ensure that MATLAB doesn't free data associated with FGretCEN (between subsequent calls to mdl-functions) */
mexMakeArrayPersistent(FGretCEN[i]); /* IMPORTANT!! (FW-03-03) */
/* call MATLAB to define 'patch' object and return its handle to variable FGretCEN[i] */
mexCallMATLAB(1, &FGretCEN[i], 3, PatchArgs, "patch");
} /* for (region) */
// prepare display of boundary boxes
for(i=0; i<maxREG; i++) {
/* create numeric array FGretBOX (boundary box object handles) */
FGretBOX[i] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL);
//mexPrintf("FGretBOX created\n");
/* ensure that MATLAB doesn't free data associated with FGretBOX (between subsequent calls to mdl-functions) */
mexMakeArrayPersistent(FGretBOX[i]); /* IMPORTANT!! (FW-03-03) */
/* call MATLAB to define 'plot' object and return its handle to variable FGretBOX[i] */
mexCallMATLAB(1, &FGretBOX[i], 3, PlotArgs, "plot");
} /* for (region) */
/* update display */
sprintf(myEvalStr, "drawnow");
mexEvalString(myEvalStr);
Sleep(1000);
// change colour of patch objects (FaceColor)
for(i=0; i<maxREG; i++) {
SetArgs[0] = FGretCEN[i];
SetArgs[1] = SetArgStrings[2]; // 'FaceColor'
SetArgs[2] = PatchArgs[2]; // [1 2 3]
// fill array with [0 0 0]
pArgs = mxGetPr(SetArgs[2]); // beginning of data area
*(pArgs + 0) = (double)0.1; // R
*(pArgs + 1) = (double)0.3; // G
*(pArgs + 2) = (double)0.5; // B
/* call MATLAB to define change the colour of the selected 'patch' object */
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
} /* for (region) */
/* update display */
sprintf(myEvalStr, "drawnow");
mexEvalString(myEvalStr);
Sleep(1000);
// scramble colours of image
SetArgs[0] = FGretIMGhandle;
SetArgs[1] = SetArgStrings[3]; // 'CData'
SetArgs[2] = FGretIMG; // ... the data
// scramble colours...
for(row=0; row<frameHeight; row++) {
for(col=0; col<frameWidth; col++) {
MATbuf[row+col*frameHeight+0*frameWidth*frameHeight] = img[row*frameWidth+col].green;
MATbuf[row+col*frameHeight+1*frameWidth*frameHeight] = img[row*frameWidth+col].blue;
MATbuf[row+col*frameHeight+2*frameWidth*frameHeight] = img[row*frameWidth+col].red;
}
}
/* call MATLAB to define change the colour of the selected 'patch' object */
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
/* update display */
sprintf(myEvalStr, "drawnow");
mexEvalString(myEvalStr);
Sleep(1000);
for(i=0; i<maxREG; i++) {
// update patch object (centroid) & plot object (boundary box)
// new... fw-01-08
//
SetArgs[2] = PatchArgs[0]; // [1 2 3 4 5]
pArgs = mxGetPr(SetArgs[2]); // beginning of data area
// update x-data
*(pArgs + 0) *= 1.1;
*(pArgs + 1) *= 1.1;
*(pArgs + 2) *= 1.1;
*(pArgs + 3) *= 1.1;
*(pArgs + 4) *= 1.1;
/* call MATLAB to define change the property 'XData' of the selected 'patch' object */
SetArgs[0] = FGretCEN[i]; // select 'patch' object
SetArgs[1] = SetArgStrings[0]; // select 'XData' property
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
/* call MATLAB to define change the property 'XData' of the selected 'patch' object */
SetArgs[0] = FGretBOX[i]; // select 'plot' object
mexPrintf("Before first 'set'.\n");
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
mexPrintf("After first 'set'.\n");
SetArgs[2] = PatchArgs[1]; // [1 2 3 4 5]
pArgs = mxGetPr(SetArgs[2]); // beginning of data area
// update y-data
*(pArgs + 0) *= 1.1;
*(pArgs + 1) *= 1.1;
*(pArgs + 2) *= 1.1;
*(pArgs + 3) *= 1.1;
*(pArgs + 4) *= 1.1;
/* call MATLAB to define change the property 'YData' of the selected 'patch' object */
SetArgs[0] = FGretCEN[i]; // select 'patch' object
SetArgs[1] = SetArgStrings[1]; // select 'YData' property
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
/* call MATLAB to define change the property 'XData' of the selected 'patch' object */
SetArgs[0] = FGretBOX[i]; // select 'plot' object
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
#ifdef ERASE
// update [r g b]
SetArgs[2] = PatchArgs[2]; // [1 2 3]
pArgs = mxGetPr(SetArgs[2]); // beginning of data area
*(pArgs + 0) = r;
*(pArgs + 1) = g;
*(pArgs + 2) = b;
/* call MATLAB to define change the property 'YData' of the selected 'patch' object */
SetArgs[0] = FGretCEN[i]; // select 'patch' object
SetArgs[1] = SetArgStrings[2]; // select 'FaceData' property
mexCallMATLAB(0, NULL, 3, SetArgs, "set");
#endif
} /* for (region) */
}
}
// 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 + -