#include <mil.h>
void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void MultipleModelsExample(MIL_ID MilSystem, MIL_ID MilDisplay);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
SingleModelExample(MilSystem, MilDisplay);
MultipleModelsExample(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define SINGLE_MODEL_IMAGE M_IMAGE_PATH MIL_TEXT("SingleModel.mim")
#define SINGLE_MODEL_TARGET_IMAGE M_IMAGE_PATH MIL_TEXT("SingleTarget.mim")
#define SINGLE_MODEL_SEARCH_SPEED M_VERY_HIGH
#define MODEL_OFFSETX 176L
#define MODEL_OFFSETY 136L
#define MODEL_SIZEX 128L
#define MODEL_SIZEY 128L
#define MODEL_MAX_OCCURRENCES 16L
void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
GraphicList;
MIL_ID MilSearchContext,
MilResult;
MIL_DOUBLE ModelDrawColor = M_COLOR_RED;
MIL_INT Model[MODEL_MAX_OCCURRENCES],
NumResults = 0L;
MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES],
XPosition[MODEL_MAX_OCCURRENCES],
YPosition[MODEL_MAX_OCCURRENCES],
Angle[MODEL_MAX_OCCURRENCES],
Scale[MODEL_MAX_OCCURRENCES],
Time = 0.0;
int i;
MbufRestore(SINGLE_MODEL_IMAGE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext);
MmodAllocResult(MilSystem, M_DEFAULT, &MilResult);
MmodDefine(MilSearchContext, M_IMAGE, MilImage,
MODEL_OFFSETX, MODEL_OFFSETY, MODEL_SIZEX, MODEL_SIZEY);
MmodControl(MilSearchContext, M_CONTEXT, M_SPEED, SINGLE_MODEL_SEARCH_SPEED);
MmodPreprocess(MilSearchContext, M_DEFAULT);
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilSearchContext, GraphicList,
M_DRAW_BOX+M_DRAW_POSITION, 0, M_ORIGINAL);
MosPrintf(MIL_TEXT("\nGEOMETRIC MODEL FINDER:\n"));
MosPrintf(MIL_TEXT("-----------------------\n\n"));
MosPrintf(MIL_TEXT("A model context was defined with "));
MosPrintf(MIL_TEXT("the model in the displayed image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, GraphicList);
MbufLoad(SINGLE_MODEL_TARGET_IMAGE, MilImage);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
if ( (NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES) )
{
MmodGetResult(MilResult, M_DEFAULT, M_INDEX+M_TYPE_MIL_INT, Model);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle);
MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
MosPrintf(MIL_TEXT("The model was found in the target image:\n\n"));
MosPrintf(MIL_TEXT("Result Model X Position Y Position ")
MIL_TEXT("Angle Scale Score\n\n"));
for (i=0; i<NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-8d%-13.2f%-13.2f%-8.2f%-8.2f%-5.2f%%\n"),
i, Model[i], XPosition[i], YPosition[i],
Angle[i], Scale[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time is %.1f ms\n\n"), Time*1000.0);
for (i=0; i<NumResults; i++)
{
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList,
M_DRAW_EDGES+M_DRAW_BOX+M_DRAW_POSITION, i, M_DEFAULT);
}
}
else
{
MosPrintf(MIL_TEXT("The model was not found or the number of models ")
MIL_TEXT("found is greater than\n"));
MosPrintf(MIL_TEXT("the specified maximum number of occurrence !\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraFree(GraphicList);
MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}
#define MULTI_MODELS_IMAGE M_IMAGE_PATH MIL_TEXT("MultipleModel.mim")
#define MULTI_MODELS_TARGET_IMAGE M_IMAGE_PATH MIL_TEXT("MultipleTarget.mim")
#define MULTI_MODELS_SEARCH_SPEED M_VERY_HIGH
#define NUMBER_OF_MODELS 3L
#define MODELS_MAX_OCCURRENCES 16L
#define MODEL0_OFFSETX 34L
#define MODEL0_OFFSETY 93L
#define MODEL0_SIZEX 214L
#define MODEL0_SIZEY 76L
#define MODEL0_DRAW_COLOR M_COLOR_RED
#define MODEL1_OFFSETX 73L
#define MODEL1_OFFSETY 232L
#define MODEL1_SIZEX 150L
#define MODEL1_SIZEY 154L
#define MODEL1_REFERENCEX 23L
#define MODEL1_REFERENCEY 127L
#define MODEL1_DRAW_COLOR M_COLOR_GREEN
#define MODEL2_OFFSETX 308L
#define MODEL2_OFFSETY 39L
#define MODEL2_SIZEX 175L
#define MODEL2_SIZEY 357L
#define MODEL2_REFERENCEX 62L
#define MODEL2_REFERENCEY 150L
#define MODEL2_DRAW_COLOR M_COLOR_BLUE
#define MODELS_ARRAY_SIZE 3L
#define MODELS_OFFSETX {MODEL0_OFFSETX, MODEL1_OFFSETX, MODEL2_OFFSETX}
#define MODELS_OFFSETY {MODEL0_OFFSETY, MODEL1_OFFSETY, MODEL2_OFFSETY}
#define MODELS_SIZEX {MODEL0_SIZEX, MODEL1_SIZEX, MODEL2_SIZEX}
#define MODELS_SIZEY {MODEL0_SIZEY, MODEL1_SIZEY, MODEL2_SIZEY}
#define MODELS_DRAW_COLOR {MODEL0_DRAW_COLOR, MODEL1_DRAW_COLOR, MODEL2_DRAW_COLOR}
void MultipleModelsExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
GraphicList;
MIL_ID MilSearchContext,
MilResult;
MIL_INT Models[MODELS_MAX_OCCURRENCES],
ModelsOffsetX[MODELS_ARRAY_SIZE] = MODELS_OFFSETX,
ModelsOffsetY[MODELS_ARRAY_SIZE] = MODELS_OFFSETY,
ModelsSizeX[MODELS_ARRAY_SIZE] = MODELS_SIZEX,
ModelsSizeY[MODELS_ARRAY_SIZE] = MODELS_SIZEY;
MIL_DOUBLE ModelsDrawColor[MODELS_ARRAY_SIZE]=MODELS_DRAW_COLOR;
MIL_INT NumResults = 0L;
MIL_DOUBLE Score[MODELS_MAX_OCCURRENCES],
XPosition[MODELS_MAX_OCCURRENCES],
YPosition[MODELS_MAX_OCCURRENCES],
Angle[MODELS_MAX_OCCURRENCES],
Scale[MODELS_MAX_OCCURRENCES],
Time = 0.0;
int i;
MbufRestore(MULTI_MODELS_IMAGE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext);
MmodAllocResult(MilSystem, M_DEFAULT, &MilResult);
for (i=0; i<NUMBER_OF_MODELS; i++)
{
MmodDefine(MilSearchContext, M_IMAGE, MilImage,
(MIL_DOUBLE)ModelsOffsetX[i], (MIL_DOUBLE)ModelsOffsetY[i],
(MIL_DOUBLE)ModelsSizeX[i], (MIL_DOUBLE)ModelsSizeY[i]);
}
MmodControl(MilSearchContext, M_CONTEXT, M_SPEED, MULTI_MODELS_SEARCH_SPEED);
MmodControl(MilSearchContext, M_CONTEXT, M_SMOOTHNESS, 75);
MmodControl(MilSearchContext, M_DEFAULT, M_ACCEPTANCE, 40);
MmodControl(MilSearchContext, M_DEFAULT, M_CERTAINTY, 60);
MmodControl(MilSearchContext, M_DEFAULT, M_NUMBER, 2);
#if (NUMBER_OF_MODELS>1)
MmodControl(MilSearchContext, 1, M_REFERENCE_X, MODEL1_REFERENCEX);
MmodControl(MilSearchContext, 1, M_REFERENCE_Y, MODEL1_REFERENCEY);
#if (NUMBER_OF_MODELS>2)
MmodControl(MilSearchContext, 2, M_REFERENCE_X, MODEL2_REFERENCEX);
MmodControl(MilSearchContext, 2, M_REFERENCE_Y, MODEL2_REFERENCEY);
#endif
#endif
MmodPreprocess(MilSearchContext, M_DEFAULT);
for (i=0; i<NUMBER_OF_MODELS; i++)
{
MgraColor(M_DEFAULT, ModelsDrawColor[i]);
MmodDraw( M_DEFAULT, MilSearchContext, GraphicList,
M_DRAW_BOX+M_DRAW_POSITION, i, M_ORIGINAL);
}
MosPrintf(MIL_TEXT("A model context was defined with the ")
MIL_TEXT("models in the displayed image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, GraphicList);
MbufLoad(MULTI_MODELS_TARGET_IMAGE, MilImage);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
if( (NumResults >= 1) && (NumResults <= MODELS_MAX_OCCURRENCES) )
{
MmodGetResult(MilResult, M_DEFAULT, M_INDEX+M_TYPE_MIL_INT, Models);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle);
MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
MosPrintf(MIL_TEXT("The models were found in the target "));
MosPrintf(MIL_TEXT("image although there is:\n "));
MosPrintf(MIL_TEXT("Full rotation\n Small scale change\n "));
MosPrintf(MIL_TEXT("Contrast variation\n Specular reflection\n "));
MosPrintf(MIL_TEXT("Occlusion\n Multiple models\n"));
MosPrintf(MIL_TEXT(" Multiple occurrences\n\n"));
MosPrintf(MIL_TEXT("Result Model X Position Y Position ")
MIL_TEXT("Angle Scale Score\n\n"));
for (i=0; i<NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-8d%-13.2f%-13.2f%-8.2f%-8.2f%-5.2f%%\n"),
i, Models[i], XPosition[i], YPosition[i],
Angle[i], Scale[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time is %.1f ms\n\n"), Time*1000.0);
for (i=0; i < NumResults; i++)
{
MgraColor(M_DEFAULT, ModelsDrawColor[Models[i]]);
MmodDraw(M_DEFAULT, MilResult,GraphicList,
M_DRAW_EDGES+M_DRAW_POSITION, i, M_DEFAULT);
}
}
else
{
MosPrintf(MIL_TEXT("The models were not found or the number of ")
MIL_TEXT("models found is greater than\n"));
MosPrintf(MIL_TEXT("the defined value of maximum occurrences !\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
MgraFree(GraphicList);
MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}