#include <mil.h>
#include <vector>
#define MODEL_IMAGE_SIZE_X 239
#define MODEL_IMAGE_SIZE_Y 241
#define MODEL_POSITION_X 22
#define MODEL_POSITION_Y 22
#define MODEL_SIZE_X 199
#define MODEL_SIZE_Y 152
#define MODEL_RECT_POSITION_X 7
#define MODEL_RECT_POSITION_Y 91
#define MODEL_RECT_SIZE_X 98
#define MODEL_RECT_SIZE_Y 209
#define MODEL_RECT_ANGLE 62.5
#define EXAMPLE_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("MmodDefineModelAtAngle/")
#define MODEL_IMAGE_NAME MIL_TEXT("Model.mim")
#define MODEL_IMAGE_FILE EXAMPLE_IMAGE_PATH MODEL_IMAGE_NAME
#define TARGET_IMAGE_FILE EXAMPLE_IMAGE_PATH MIL_TEXT("Hook.mim")
#define ACCCEPTANCE 60
#define MODEL_NAME_LENGTH 200
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("MmodDefineModelAtAngle\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This example demonstrates how to define a model at angle using the\n")
MIL_TEXT("possibility to add model with an associated region to a context.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, buffer, display, graphics,\n")
MIL_TEXT("image processing, model finder, system.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilGraphicList,
MilDispImage,
MilRightSubImage,
MilLeftSubImage,
MilTargetImage,
MilModelImage,
MilModContext,
MilModResult;
MIL_TEXT_CHAR ModelImageSource[MODEL_NAME_LENGTH];
MIL_INT TotalNumFound, i;
PrintHeader();
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
MbufRestore(TARGET_IMAGE_FILE, MilSystem, &MilTargetImage);
MIL_ID TargetImageSizeX = MbufInquire(MilTargetImage, M_SIZE_X, M_NULL);
MIL_ID TargetImageSizeY = MbufInquire(MilTargetImage, M_SIZE_Y, M_NULL);
MbufAllocColor(MilSystem, 3, TargetImageSizeX + MODEL_IMAGE_SIZE_X, TargetImageSizeY,
8 + M_UNSIGNED, M_IMAGE + M_DISP + M_PROC, &MilDispImage);
MbufClear(MilDispImage, 0L);
MdispSelect(MilDisplay, MilDispImage);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList);
MbufChild2d(MilDispImage, 0L, 0L, TargetImageSizeX, TargetImageSizeY, &MilLeftSubImage);
MbufCopy(MilTargetImage, MilLeftSubImage);
MbufChild2d(MilDispImage, TargetImageSizeX, 0L, MODEL_IMAGE_SIZE_X, TargetImageSizeY, &MilRightSubImage);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilModContext);
MmodAllocResult(MilSystem, M_DEFAULT, &MilModResult);
MosSprintf(ModelImageSource, MODEL_NAME_LENGTH, MODEL_IMAGE_FILE);
MbufRestore(ModelImageSource, MilSystem, &MilModelImage);
MbufCopy(MilModelImage, MilRightSubImage);
MmodDefine(MilModContext, M_IMAGE, MilModelImage,
MODEL_POSITION_X,
MODEL_POSITION_Y,
MODEL_SIZE_X,
MODEL_SIZE_Y);
MmodControl(MilModContext, 0, M_ACCEPTANCE, ACCCEPTANCE);
MbufChildMove(MilRightSubImage, TargetImageSizeX, 0, MODEL_IMAGE_SIZE_X, MODEL_IMAGE_SIZE_Y, M_DEFAULT);
MgraColor(M_DEFAULT,M_COLOR_RED);
MmodDraw(M_DEFAULT, MilModContext, MilRightSubImage, M_DRAW_BOX, 0, M_ORIGINAL);
MgraColor(M_DEFAULT,M_COLOR_GREEN);
MosSprintf(ModelImageSource, MODEL_NAME_LENGTH, MODEL_IMAGE_NAME);
ModelImageSource[5] = 0;
MgraText(M_DEFAULT, MilRightSubImage, 0, MODEL_IMAGE_SIZE_Y- MgraInquire(M_DEFAULT, M_FONT_SIZE, M_NULL), MIL_CONST_TEXT_PTR(ModelImageSource));
MosPrintf(MIL_TEXT("A target image containing multiple occurrences of the object is displayed.\n"));
MosPrintf(MIL_TEXT("A Model is also defined in the source image and displayed on the right.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MmodControl(MilModContext, M_ALL, M_NUMBER, M_ALL);
MmodPreprocess(MilModContext, M_DEFAULT);
MmodFind(MilModContext, MilTargetImage, MilModResult);
MmodGetResult(MilModResult, M_GENERAL, M_NUMBER + M_TYPE_MIL_INT, &TotalNumFound);
MosPrintf(MIL_TEXT("A total of %i occurrences are found.\n\n"), TotalNumFound);
if(TotalNumFound)
{
MIL_TEXT_CHAR ModelScoreChar[5] = {0};
MIL_DOUBLE posX, posY, Score;
for(i = 0; i < TotalNumFound; i++)
{
MmodDraw(M_DEFAULT, MilModResult, MilGraphicList, M_DRAW_BOX+M_DRAW_POSITION, i, M_DEFAULT);
MmodGetResult(MilModResult, i, M_POSITION_X, &posX);
MmodGetResult(MilModResult, i, M_POSITION_Y, &posY);
MmodGetResult(MilModResult, i, M_SCORE, &Score);
MosSprintf(ModelScoreChar, 5, MIL_TEXT("%.1f"), Score);
MgraText(M_DEFAULT, MilGraphicList, posX, posY, MIL_CONST_TEXT_PTR(ModelScoreChar));
}
}
MosPrintf(MIL_TEXT("The %i found occurrences are displayed and the score for each\n"),TotalNumFound);
MosPrintf(MIL_TEXT("occurrence, is drawn. Scores are low with the current defined\n"));
MosPrintf(MIL_TEXT("model, which includes an unwanted piece from another object.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MosPrintf(MIL_TEXT("\nMmodDefine (Model with an associated region)\n")
MIL_TEXT("---------------------------------------------\n\n"));
MbufCopy(MilModelImage, MilRightSubImage);
MgraClear(M_DEFAULT, MilGraphicList);
MmodDefine(MilModContext, M_DELETE, M_ALL, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MIL_ID GraphCtx = MgraAlloc(MilSystem, M_NULL);
MIL_ID RotatedRectGraphList = MgraAllocList(MilSystem, M_DEFAULT, M_NULL);
MgraRectAngle(GraphCtx, RotatedRectGraphList,
MODEL_RECT_POSITION_X,
MODEL_RECT_POSITION_Y,
MODEL_RECT_SIZE_X,
MODEL_RECT_SIZE_Y,
MODEL_RECT_ANGLE,
M_CORNER_AND_DIMENSION);
MbufSetRegion(MilModelImage, RotatedRectGraphList, M_DEFAULT, M_NO_RASTERIZE + M_FILL_REGION, M_DEFAULT);
MmodDefine(MilModContext, M_IMAGE, MilModelImage, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, M_COLOR_RED);
MmodDraw(M_DEFAULT, MilModContext, MilRightSubImage, M_DRAW_BOX, 0, M_ORIGINAL);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MosSprintf(ModelImageSource, MODEL_NAME_LENGTH, MODEL_IMAGE_NAME);
ModelImageSource[5] = 0;
MgraText(M_DEFAULT, MilRightSubImage, 0, MODEL_IMAGE_SIZE_Y - 3 * MgraInquire(M_DEFAULT, M_FONT_SIZE, M_NULL), MIL_CONST_TEXT_PTR(ModelImageSource));
MgraText(M_DEFAULT, MilRightSubImage, 0, MODEL_IMAGE_SIZE_Y - 2 * MgraInquire(M_DEFAULT, M_FONT_SIZE, M_NULL), MIL_TEXT("With an associated region."));
MosPrintf(MIL_TEXT("A target image containing multiple occurrences of the object is displayed.\n"));
MosPrintf(MIL_TEXT("A Model with an associated region is also defined in the source image\n"));
MosPrintf(MIL_TEXT("and displayed on the right.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MmodControl(MilModContext, M_ALL, M_NUMBER, M_ALL);
MmodPreprocess(MilModContext, M_DEFAULT);
MmodFind(MilModContext, MilTargetImage, MilModResult);
MmodGetResult(MilModResult, M_GENERAL, M_NUMBER + M_TYPE_MIL_INT, &TotalNumFound);
MosPrintf(MIL_TEXT("A total of %i occurrences are found.\n\n"), TotalNumFound);
if(TotalNumFound)
{
MIL_TEXT_CHAR ModelScoreChar[5] = {0};
MIL_DOUBLE posX, posY, Score;
for(i = 0; i < TotalNumFound; i++)
{
MmodDraw(M_DEFAULT, MilModResult, MilGraphicList, M_DRAW_BOX + M_DRAW_POSITION, i, M_DEFAULT);
MmodGetResult(MilModResult, i, M_POSITION_X, &posX);
MmodGetResult(MilModResult, i, M_POSITION_Y, &posY);
MmodGetResult(MilModResult, i, M_SCORE, &Score);
MosSprintf(ModelScoreChar, 5, MIL_TEXT("%.1f"), Score);
MgraText(M_DEFAULT, MilGraphicList, posX, posY, MIL_CONST_TEXT_PTR(ModelScoreChar));
}
}
MosPrintf(MIL_TEXT("The %i found occurrences are displayed and the score for each\n"), TotalNumFound);
MosPrintf(MIL_TEXT("occurrence, is drawn. Scores are higher with the model defined\n"));
MosPrintf(MIL_TEXT("at an angle that excludes an unwanted piece from another object.\n\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to finish.\n"));
MosGetch();
MgraFree(RotatedRectGraphList);
MgraFree(GraphCtx);
MbufFree(MilModelImage);
MmodFree(MilModResult);
MmodFree(MilModContext);
MbufFree(MilRightSubImage);
MbufFree(MilLeftSubImage);
MgraFree(MilGraphicList);
MbufFree(MilDispImage);
MbufFree(MilTargetImage);
MdispFree(MilDisplay);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}