#include <mil.h>
#define MODEL_WIDTH 128L
#define MODEL_HEIGHT 128L
#define MODEL_POS_X_INIT(TargetImage) (MbufInquire(TargetImage, M_SIZE_X, M_NULL)/2)
#define MODEL_POS_Y_INIT(TargetImage) (MbufInquire(TargetImage, M_SIZE_Y, M_NULL)/2)
#define MODEL_MIN_MATCH_SCORE 50.0
#define DRAW_COLOR 0xFF
#define RUN_PAT_TRACKING_EXAMPLE M_YES
#define RUN_MOD_TRACKING_EXAMPLE M_YES
void MpatTrackingExample(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer,
MIL_ID MilDisplayImage, MIL_ID MilModelImage);
void MmodTrackingExample(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer,
MIL_ID MilDisplayImage, MIL_ID MilModelImage);
void GetModelImage(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer,
MIL_ID MilDisplayImage, MIL_ID MilModelImage);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilDigitizer,
MilDisplayImage,
MilModelImage;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay,
&MilDigitizer, &MilDisplayImage);
MbufAlloc2d(MilSystem,
MbufInquire(MilDisplayImage, M_SIZE_X, M_NULL),
MbufInquire(MilDisplayImage, M_SIZE_Y, M_NULL),
8, M_IMAGE+M_PROC, &MilModelImage);
MosPrintf(MIL_TEXT("\nMODEL TRACKING:\n"));
MosPrintf(MIL_TEXT("---------------\n\n"));
GetModelImage(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage);
#if (RUN_PAT_TRACKING_EXAMPLE)
MpatTrackingExample(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage);
#endif
#if (RUN_MOD_TRACKING_EXAMPLE)
MmodTrackingExample(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage);
#endif
MbufFree(MilModelImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilDisplayImage);
return 0;
}
void GetModelImage(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer,
MIL_ID MilDisplayImage, MIL_ID MilModelImage)
{
MIL_ID MilOverlayImage;
MIL_DOUBLE DrawColor = DRAW_COLOR;
MdispControl(MilDisplay, M_OVERLAY, M_ENABLE);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MgraColor(M_DEFAULT, DrawColor);
MgraRect(M_DEFAULT, MilOverlayImage,
MODEL_POS_X_INIT(MilOverlayImage) - (MODEL_WIDTH/2),
MODEL_POS_Y_INIT(MilOverlayImage) - (MODEL_HEIGHT/2),
MODEL_POS_X_INIT(MilOverlayImage) + (MODEL_WIDTH/2),
MODEL_POS_Y_INIT(MilOverlayImage) + (MODEL_HEIGHT/2));
MosPrintf(MIL_TEXT("Model definition:\n\n"));
MosPrintf(MIL_TEXT("Place a unique model to find in the marked rectangle.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MdigGrabContinuous(MilDigitizer, MilDisplayImage);
MosGetch();
MdigHalt(MilDigitizer);
MbufCopy(MilDisplayImage, MilModelImage);
MdispControl(MilDisplay, M_OVERLAY, M_DISABLE);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
}
void MpatTrackingExample(MIL_ID MilSystem, MIL_ID MilDisplay,
MIL_ID MilDigitizer, MIL_ID MilDisplayImage, MIL_ID MilModelImage)
{
MIL_ID MilImage[2],
Model,
Result;
MIL_DOUBLE DrawColor = DRAW_COLOR;
MIL_INT Found;
MIL_INT NbFindDone = 0;
MIL_DOUBLE OrgX = 0.0, OrgY = 0.0;
MIL_DOUBLE x, y, Score = 0.0;
MIL_DOUBLE Time = 0.0;
MosPrintf(MIL_TEXT("\nGRAYSCALE PATTERN MATCHING:\n"));
MosPrintf(MIL_TEXT("---------------------------\n\n"));
MbufCopy(MilModelImage, MilDisplayImage);
MpatAllocModel(MilSystem, MilModelImage,
MODEL_POS_X_INIT(MilModelImage) - (MODEL_WIDTH/2),
MODEL_POS_Y_INIT(MilModelImage) - (MODEL_HEIGHT/2),
MODEL_WIDTH, MODEL_HEIGHT, M_NORMALIZED, &Model);
MpatAllocResult(MilSystem, 1L, &Result);
MgraColor(M_DEFAULT, DrawColor);
MpatDraw(M_DEFAULT, Model, MilDisplayImage, M_DRAW_BOX, M_DEFAULT, M_ORIGINAL);
MpatSetAcceptance(Model,MODEL_MIN_MATCH_SCORE);
MpatSetSpeed(Model,M_HIGH);
MpatSetAccuracy(Model,M_LOW);
MpatPreprocModel(MilModelImage, Model, M_DEFAULT);
MpatInquire(Model, M_ORIGINAL_X, &OrgX);
MpatInquire(Model, M_ORIGINAL_Y, &OrgY);
MosPrintf(MIL_TEXT("A Grayscale Model was defined.\n"));
MosPrintf(MIL_TEXT("Model dimensions: %ld x %ld.\n"), MODEL_WIDTH, MODEL_HEIGHT);
MosPrintf(MIL_TEXT("Model center: X=%.2f, Y=%.2f.\n"), OrgX, OrgY);
MosPrintf(MIL_TEXT("Model is scale and rotation dependant.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufAlloc2d(MilSystem,
MbufInquire(MilModelImage, M_SIZE_X, M_NULL),
MbufInquire(MilModelImage, M_SIZE_Y, M_NULL),
8,
M_IMAGE+M_GRAB+M_PROC,
&MilImage[0]);
MbufAlloc2d(MilSystem,
MbufInquire(MilModelImage, M_SIZE_X, M_NULL),
MbufInquire(MilModelImage, M_SIZE_Y, M_NULL),
8,
M_IMAGE+M_GRAB+M_PROC,
&MilImage[1]);
MosPrintf(MIL_TEXT("\nContinuously finding the Grayscale model.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to stop.\n\n"));
MdigControl(MilDigitizer, M_GRAB_MODE, M_ASYNCHRONOUS);
MdigGrab(MilDigitizer, MilImage[NbFindDone%2]);
MdigGrab(MilDigitizer, MilImage[NbFindDone%2]);
MappTimer(M_DEFAULT, M_TIMER_RESET, &Time);
do
{
MdigGrab(MilDigitizer, MilImage[(NbFindDone+1)%2]);
MappTimer(M_DEFAULT, M_TIMER_READ, &Time);
MpatFindModel(MilImage[NbFindDone%2], Model, Result);
MpatGetNumber(Result, &Found);
MpatGetResult(Result, M_POSITION_X, &x);
MpatGetResult(Result, M_POSITION_Y, &y);
MpatGetResult(Result, M_SCORE, &Score);
if (Found)
{
MpatDraw(M_DEFAULT, Result, MilImage[NbFindDone%2],
M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("Found: X=%7.2f, Y=%7.2f, Score=%5.1f%% (%.1f fps). \r"),
x, y, Score, (NbFindDone+1)/Time);
}
else
{
MosPrintf(MIL_TEXT("Not found ! (score<%5.1f%%) (%.1f fps). \r"),
MODEL_MIN_MATCH_SCORE, (NbFindDone+1)/Time);
}
MbufCopy(MilImage[NbFindDone%2], MilDisplayImage);
NbFindDone++;
}
while (!MosKbhit());
MosGetch();
MosPrintf(MIL_TEXT("\n\n"));
MdigGrabWait(MilDigitizer, M_GRAB_END);
MpatFree(Result);
MpatFree(Model);
MbufFree(MilImage[1]);
MbufFree(MilImage[0]);
}
#define MODEL_MAX_OCCURRENCES 16L
void MmodTrackingExample(MIL_ID MilSystem, MIL_ID MilDisplay,
MIL_ID MilDigitizer, MIL_ID MilDisplayImage, MIL_ID MilModelImage)
{
MIL_ID MilImage[2],
SearchContext,
Result;
MIL_DOUBLE DrawColor = DRAW_COLOR;
MIL_INT Found;
MIL_INT NbFindDone=0;
MIL_DOUBLE OrgX = 0.0, OrgY = 0.0;
MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES],
x[MODEL_MAX_OCCURRENCES],
y[MODEL_MAX_OCCURRENCES],
Angle[MODEL_MAX_OCCURRENCES],
Scale[MODEL_MAX_OCCURRENCES];
MIL_DOUBLE Time = 0.0;
MosPrintf(MIL_TEXT("\nGEOMETRIC MODEL FINDER (scale and rotation independent):\n"));
MosPrintf(MIL_TEXT("--------------------------------------------------------\n\n"));
MbufCopy(MilModelImage, MilDisplayImage);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &SearchContext);
MmodDefine(SearchContext, M_IMAGE, MilModelImage,
(MIL_DOUBLE)MODEL_POS_X_INIT(MilModelImage) - (MODEL_WIDTH/2),
(MIL_DOUBLE)MODEL_POS_Y_INIT(MilModelImage) - (MODEL_HEIGHT/2),
MODEL_WIDTH, MODEL_HEIGHT);
MmodAllocResult(MilSystem, M_DEFAULT, &Result);
MgraColor(M_DEFAULT, DrawColor);
MmodDraw(M_DEFAULT, SearchContext, MilDisplayImage, M_DRAW_BOX, M_DEFAULT, M_ORIGINAL);
MmodControl(SearchContext, M_CONTEXT, M_SPEED, M_VERY_HIGH);
MmodControl(SearchContext, M_DEFAULT, M_ACCEPTANCE, MODEL_MIN_MATCH_SCORE);
MmodPreprocess(SearchContext, M_DEFAULT);
MmodInquire(SearchContext, M_DEFAULT, M_ORIGINAL_X, &OrgX);
MmodInquire(SearchContext, M_DEFAULT, M_ORIGINAL_Y, &OrgY);
MosPrintf(MIL_TEXT("The Geometric target model was defined.\n"));
MosPrintf(MIL_TEXT("Model dimensions: %ld x %ld.\n"), MODEL_WIDTH, MODEL_HEIGHT);
MosPrintf(MIL_TEXT("Model center: X=%.2f, Y=%.2f.\n"), OrgX, OrgY);
MosPrintf(MIL_TEXT("Model is scale and rotation independent.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufAlloc2d(MilSystem,
MbufInquire(MilModelImage, M_SIZE_X, M_NULL),
MbufInquire(MilModelImage, M_SIZE_Y, M_NULL),
8,
M_IMAGE+M_GRAB+M_PROC,
&MilImage[0]);
MbufAlloc2d(MilSystem,
MbufInquire(MilModelImage, M_SIZE_X, M_NULL),
MbufInquire(MilModelImage, M_SIZE_Y, M_NULL),
8,
M_IMAGE+M_GRAB+M_PROC,
&MilImage[1]);
MosPrintf(MIL_TEXT("\nContinuously finding the Geometric Model.\n"));
MosPrintf(MIL_TEXT("Press a <Enter> to stop.\n\n"));
MdigControl(MilDigitizer, M_GRAB_MODE, M_ASYNCHRONOUS);
MdigGrab(MilDigitizer, MilImage[NbFindDone%2]);
MdigGrab(MilDigitizer, MilImage[NbFindDone%2]);
MappTimer(M_DEFAULT, M_TIMER_RESET, &Time);
do
{
MdigGrab(MilDigitizer, MilImage[(NbFindDone+1)%2]);
MappTimer(M_DEFAULT, M_TIMER_READ, &Time);
MmodFind(SearchContext, MilImage[NbFindDone%2], Result);
MmodGetResult(Result, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &Found);
if ( (Found >= 1) && (Found < MODEL_MAX_OCCURRENCES) )
{
MmodGetResult(Result, M_DEFAULT, M_POSITION_X, x);
MmodGetResult(Result, M_DEFAULT, M_POSITION_Y, y);
MmodGetResult(Result, M_DEFAULT, M_SCALE, Scale);
MmodGetResult(Result, M_DEFAULT, M_ANGLE, Angle);
MmodGetResult(Result, M_DEFAULT, M_SCORE, Score);
MmodDraw(M_DEFAULT, Result, MilImage[NbFindDone%2],
M_DRAW_BOX+M_DRAW_POSITION+M_DRAW_EDGES, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("Found: X=%6.1f, Y=%6.1f, Angle=%6.1f, Scale=%5.2f, ")
MIL_TEXT("Score=%5.1f%% (%5.1f fps).\r"),
x[0], y[0], Angle[0], Scale[0], Score[0], (NbFindDone+1)/Time);
}
else
{
MosPrintf(MIL_TEXT("Not found! (score<%5.1f%%) ")
MIL_TEXT("(%5.1f fps).\r"),MODEL_MIN_MATCH_SCORE, (NbFindDone+1)/Time);
}
MbufCopy(MilImage[NbFindDone%2], MilDisplayImage);
NbFindDone++;
}
while (!MosKbhit());
MosGetch();
MosPrintf(MIL_TEXT("\n\n"));
MdigGrabWait(MilDigitizer, M_GRAB_END);
MmodFree(Result);
MmodFree(SearchContext);
MbufFree(MilImage[1]);
MbufFree(MilImage[0]);
}