#include <mil.h>
#include <vector>
#define NUM_OF_MODELS 4
#define MODEL_SIZE_X 126
#define MODEL_SIZE_Y 126
#define EXAMPLE_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("MpatFindMultipleModels/")
#define MODEL_IMAGE_NAME MIL_TEXT("Model_%d.mim")
#define MODEL_IMAGE_FILE EXAMPLE_IMAGE_PATH MODEL_IMAGE_NAME
#define TARGET_IMAGE_FILE EXAMPLE_IMAGE_PATH MIL_TEXT("Buttons.mim")
#define ACCCEPTANCE 60
#define MODEL_NAME_LENGTH 200
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("MpatFindMultipleModels\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This example demonstrates how to locate multiple models with the\n")
MIL_TEXT("Pattern Matching module using two different search mode controls:\n\n")
MIL_TEXT("\t1 M_FIND_ALL_MODELS. This finds all occurrences for each\n")
MIL_TEXT("\t model.\n\n")
MIL_TEXT("\t2 M_FIND_BEST_MODELS. This finds the best model for each\n")
MIL_TEXT("\t occurrence.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, buffer, display, graphics,\n")
MIL_TEXT("image processing, pattern matching, 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,
MilModelMaskImage,
MilPatContext,
MilPatResult;
MIL_TEXT_CHAR ModelImagesSource[NUM_OF_MODELS][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_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_SIZE_X,
(TargetImageSizeY/NUM_OF_MODELS), &MilRightSubImage);
MbufAlloc2d(MilSystem, MODEL_SIZE_X, MODEL_SIZE_Y, 8+M_UNSIGNED, M_IMAGE+M_PROC,
&MilModelMaskImage);
MpatAlloc(MilSystem, M_NORMALIZED, M_DEFAULT, &MilPatContext);
MpatAllocResult(MilSystem, M_DEFAULT, &MilPatResult);
for (i = 0; i < NUM_OF_MODELS; i++)
{
MosSprintf(ModelImagesSource[i], MODEL_NAME_LENGTH, MODEL_IMAGE_FILE, i);
if (i==0)
{
MbufRestore(ModelImagesSource[i], MilSystem, &MilModelImage);
MimBinarize(MilModelImage, MilModelMaskImage, M_BIMODAL+M_GREATER, M_NULL, M_NULL);
MblobReconstruct(MilModelMaskImage, M_NULL, MilModelMaskImage, M_FILL_HOLES,
M_FOREGROUND_ZERO);
MimErode(MilModelMaskImage, MilModelMaskImage, 4, M_BINARY);
}
else
{
MbufLoad(ModelImagesSource[i], MilModelImage);
}
MpatDefine(MilPatContext, M_REGULAR_MODEL, MilModelImage, 0, 0, MODEL_SIZE_X,
MODEL_SIZE_Y, M_DEFAULT);
MpatControl(MilPatContext, i, M_ACCEPTANCE, ACCCEPTANCE);
MpatMask(MilPatContext, i, MilModelMaskImage, M_DONT_CARE, M_DEFAULT);
MbufChildMove(MilRightSubImage, TargetImageSizeX, i*(TargetImageSizeY/NUM_OF_MODELS),
MODEL_SIZE_X, MODEL_SIZE_Y, M_DEFAULT);
MgraColor(M_DEFAULT,M_COLOR_RED);
MpatDraw(M_DEFAULT, MilPatContext, MilRightSubImage, M_DRAW_IMAGE+M_DRAW_DONT_CARE, i,
M_DEFAULT);
MgraColor(M_DEFAULT,M_COLOR_GREEN);
MosSprintf(ModelImagesSource[i], MODEL_NAME_LENGTH, MODEL_IMAGE_NAME, i);
ModelImagesSource[i][7]=0;
MgraText(M_DEFAULT, MilRightSubImage, 0,
MODEL_SIZE_Y- MgraInquire(M_DEFAULT, M_FONT_SIZE, M_NULL),
MIL_CONST_TEXT_PTR(ModelImagesSource[i]));
}
MosPrintf(MIL_TEXT("A target image containing multiple occurrences of %i objects is\n"),
NUM_OF_MODELS);
MosPrintf(MIL_TEXT("displayed. %i models (one for each object) are also defined and\n"),
NUM_OF_MODELS);
MosPrintf(MIL_TEXT("displayed on the right. A don't care mask, drawn in red, is\napplied ")
MIL_TEXT("to each model to limit the matching process to the\n")
MIL_TEXT("object pixels only.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MosPrintf(MIL_TEXT("\n1. M_FIND_ALL_MODELS\n")
MIL_TEXT("--------------------\n\n"));
MpatControl(MilPatContext, M_ALL, M_NUMBER, M_ALL);
MpatPreprocess(MilPatContext, M_DEFAULT, MilTargetImage);
MpatFind(MilPatContext, MilTargetImage, MilPatResult);
MpatGetResult(MilPatResult, M_GENERAL, M_NUMBER + M_TYPE_MIL_INT, &TotalNumFound);
MosPrintf(MIL_TEXT("M_FIND_ALL_MODELS is used to find all occurrences for each\nmodel"));
MosPrintf(MIL_TEXT(" in the target image. A total of %i occurrences are found.\n\n"),
TotalNumFound);
if(TotalNumFound)
{
std::vector<MIL_INT> Indexes(TotalNumFound);
MpatGetResult(MilPatResult, M_ALL, M_INDEX + M_TYPE_MIL_INT, &Indexes.front());
MIL_INT ModelIndex, OccurrenceIndex=0, NumFoundPerModel;
for(ModelIndex = 0; ModelIndex< NUM_OF_MODELS && OccurrenceIndex < TotalNumFound;
ModelIndex++)
{
NumFoundPerModel=0;
for(; OccurrenceIndex< TotalNumFound; OccurrenceIndex++)
{
if(Indexes[OccurrenceIndex] == ModelIndex)
{
MpatDraw(M_DEFAULT, MilPatResult, MilGraphicList, M_DRAW_BOX+M_DRAW_POSITION,
OccurrenceIndex, M_DEFAULT);
NumFoundPerModel++;
}
else
{
break;
}
}
MosPrintf(MIL_TEXT("For Model_%i, %i occurrences are found and displayed.\n\n"),
ModelIndex, NumFoundPerModel);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT,MilGraphicList);
}
}
MosPrintf(MIL_TEXT("\n2. M_FIND_BEST_MODELS\n")
MIL_TEXT("---------------------\n\n"));
MpatControl(MilPatContext, M_CONTEXT, M_SEARCH_MODE, M_FIND_BEST_MODELS);
MpatPreprocess(MilPatContext, M_DEFAULT, MilTargetImage);
MpatFind(MilPatContext, MilTargetImage, MilPatResult);
MpatGetResult(MilPatResult, M_GENERAL, M_NUMBER + M_TYPE_MIL_INT, &TotalNumFound);
MosPrintf(MIL_TEXT("M_FIND_BEST_MODELS is used to find the best model for each\n"));
MosPrintf(MIL_TEXT("occurrence in the target image. A total of %i occurrences\n"),
TotalNumFound);
MosPrintf(MIL_TEXT("are found.\n\n"));
if(TotalNumFound)
{
std::vector<MIL_INT> Indexes(TotalNumFound);
MpatGetResult(MilPatResult, M_ALL, M_INDEX + M_TYPE_MIL_INT, &Indexes.front());
MIL_TEXT_CHAR ModelIndexChar[2] = {0};
MIL_DOUBLE posX, posY;
for(i = 0; i < TotalNumFound; i++)
{
MpatDraw(M_DEFAULT, MilPatResult, MilGraphicList, M_DRAW_BOX+M_DRAW_POSITION, i,
M_DEFAULT);
MpatGetResult(MilPatResult, i, M_POSITION_X, &posX);
MpatGetResult(MilPatResult, i, M_POSITION_Y, &posY);
MosSprintf(ModelIndexChar, 2, MIL_TEXT("%d"), Indexes[i]);
MgraText(M_DEFAULT, MilLeftSubImage, posX, posY, MIL_CONST_TEXT_PTR(ModelIndexChar));
}
}
MosPrintf(MIL_TEXT("The %i found occurrences are displayed and the model index\n"),
TotalNumFound);
MosPrintf(MIL_TEXT("for each occurrence, is drawn.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to finish.\n"));
MosGetch();
MbufFree(MilModelImage);
MpatFree(MilPatResult);
MpatFree(MilPatContext);
MbufFree(MilModelMaskImage);
MbufFree(MilRightSubImage);
MbufFree(MilLeftSubImage);
MgraFree(MilGraphicList);
MbufFree(MilDispImage);
MbufFree(MilTargetImage);
MdispFree(MilDisplay);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}