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