using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MModelTracking
{
class Program
{
private const int MODEL_WIDTH = 128;
private const int MODEL_HEIGHT = 128;
static MIL_INT MODEL_POS_X_INIT(MIL_ID TargetImage) { return (MIL.MbufInquire(TargetImage, MIL.M_SIZE_X, MIL.M_NULL) / 2); }
static MIL_INT MODEL_POS_Y_INIT(MIL_ID TargetImage) { return (MIL.MbufInquire(TargetImage, MIL.M_SIZE_Y, MIL.M_NULL) / 2); }
private const double MODEL_MIN_MATCH_SCORE = 50.0;
private const int DRAW_COLOR = 0xFF;
private const int RUN_PAT_TRACKING_EXAMPLE = MIL.M_YES;
private const int RUN_MOD_TRACKING_EXAMPLE = MIL.M_YES;
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilDigitizer = MIL.M_NULL;
MIL_ID MilDisplayImage = MIL.M_NULL;
MIL_ID MilModelImage = MIL.M_NULL;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, ref MilDigitizer, ref MilDisplayImage);
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilDisplayImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilDisplayImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_PROC, ref MilModelImage);
Console.Write("\nMODEL TRACKING:\n");
Console.Write("---------------\n\n");
GetModelImage(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage);
if (RUN_PAT_TRACKING_EXAMPLE == MIL.M_YES)
{
MpatTrackingExample(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage);
}
if (RUN_MOD_TRACKING_EXAMPLE == MIL.M_YES)
{
MmodTrackingExample(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage);
}
MIL.MbufFree(MilModelImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilDisplayImage);
}
static void GetModelImage(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer, MIL_ID MilDisplayImage, MIL_ID MilModelImage)
{
MIL_ID MilOverlayImage = MIL.M_NULL;
double DrawColor = DRAW_COLOR;
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE);
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT);
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, ref MilOverlayImage);
MIL.MgraColor(MIL.M_DEFAULT, DrawColor);
MIL.MgraRect(MIL.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));
Console.Write("Model definition:\n\n");
Console.Write("Place a unique model to find in the marked rectangle.\n");
Console.Write("Press <Enter> to continue.\n\n");
MIL.MdigGrabContinuous(MilDigitizer, MilDisplayImage);
Console.ReadKey();
MIL.MdigHalt(MilDigitizer);
MIL.MbufCopy(MilDisplayImage, MilModelImage);
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_DISABLE);
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT);
}
static void MpatTrackingExample(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer, MIL_ID MilDisplayImage, MIL_ID MilModelImage)
{
MIL_ID[] MilImage = new MIL_ID[2] { MIL.M_NULL, MIL.M_NULL };
MIL_ID Model = MIL.M_NULL;
MIL_ID Result = MIL.M_NULL;
double DrawColor = DRAW_COLOR;
MIL_INT Found = 0;
int NbFindDone = 0;
double OrgX = 0.0;
double OrgY = 0.0;
double x = 0.0;
double y = 0.0;
double Score = 0.0;
double Time = 0.0;
Console.Write("\nGRAYSCALE PATTERN MATCHING:\n");
Console.Write("---------------------------\n\n");
MIL.MbufCopy(MilModelImage, MilDisplayImage);
MIL.MpatAllocModel(MilSystem, MilModelImage, MODEL_POS_X_INIT(MilModelImage) - (MODEL_WIDTH / 2), MODEL_POS_Y_INIT(MilModelImage) - (MODEL_HEIGHT / 2), MODEL_WIDTH, MODEL_HEIGHT, MIL.M_NORMALIZED, ref Model);
MIL.MpatAllocResult(MilSystem, 1, ref Result);
MIL.MgraColor(MIL.M_DEFAULT, DrawColor);
MIL.MpatDraw(MIL.M_DEFAULT, Model, MilDisplayImage, MIL.M_DRAW_BOX, MIL.M_DEFAULT, MIL.M_ORIGINAL);
MIL.MpatSetAcceptance(Model, MODEL_MIN_MATCH_SCORE);
MIL.MpatSetSpeed(Model, MIL.M_HIGH);
MIL.MpatSetAccuracy(Model, MIL.M_LOW);
MIL.MpatPreprocModel(MilModelImage, Model, MIL.M_DEFAULT);
MIL.MpatInquire(Model, MIL.M_ORIGINAL_X, ref OrgX);
MIL.MpatInquire(Model, MIL.M_ORIGINAL_Y, ref OrgY);
Console.Write("A Grayscale Model was defined.\n");
Console.Write("Model dimensions: {0} x {1}.\n", MODEL_WIDTH, MODEL_HEIGHT);
Console.Write("Model center: X={0:0.00}, Y={0:0.00}.\n", OrgX, OrgY);
Console.Write("Model is scale and rotation dependant.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, ref MilImage[0]);
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, ref MilImage[1]);
Console.Write("\nContinuously finding the Grayscale model.\n");
Console.Write("Press <Enter> to stop.\n\n");
MIL.MdigControl(MilDigitizer, MIL.M_GRAB_MODE, MIL.M_ASYNCHRONOUS);
MIL.MdigGrab(MilDigitizer, MilImage[NbFindDone % 2]);
MIL.MdigGrab(MilDigitizer, MilImage[NbFindDone % 2]);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET, ref Time);
do
{
MIL.MdigGrab(MilDigitizer, MilImage[(NbFindDone + 1) % 2]);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref Time);
MIL.MpatFindModel(MilImage[NbFindDone % 2], Model, Result);
MIL.MpatGetNumber(Result, ref Found);
MIL.MpatGetResult(Result, MIL.M_POSITION_X, ref x);
MIL.MpatGetResult(Result, MIL.M_POSITION_Y, ref y);
MIL.MpatGetResult(Result, MIL.M_SCORE, ref Score);
if (Found > 0)
{
MIL.MpatDraw(MIL.M_DEFAULT, Result, MilImage[NbFindDone % 2], MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT);
Console.Write("Found: X={0,7:0.00}, Y={1,7:0.00}, Score={2,5:0.0}% ({3:0.0} fps). \r", x, y, Score, (NbFindDone + 1) / Time);
}
else
{
Console.Write("Not found ! (score<{0,5:0.0}%) ({1:0.0} fps). \r", MODEL_MIN_MATCH_SCORE, (NbFindDone + 1) / Time);
}
MIL.MbufCopy(MilImage[NbFindDone % 2], MilDisplayImage);
NbFindDone++;
}
while (!Console.KeyAvailable);
Console.ReadKey();
Console.Write("\n\n");
MIL.MdigGrabWait(MilDigitizer, MIL.M_GRAB_END);
MIL.MpatFree(Result);
MIL.MpatFree(Model);
MIL.MbufFree(MilImage[1]);
MIL.MbufFree(MilImage[0]);
}
private const int MODEL_MAX_OCCURRENCES = 16;
static void MmodTrackingExample(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_ID MilDigitizer, MIL_ID MilDisplayImage, MIL_ID MilModelImage)
{
MIL_ID[] MilImage = new MIL_ID[2] { MIL.M_NULL, MIL.M_NULL };
MIL_ID SearchContext = MIL.M_NULL;
MIL_ID Result = MIL.M_NULL;
double DrawColor = DRAW_COLOR;
MIL_INT Found = 0;
int NbFindDone = 0;
double OrgX = 0.0;
double OrgY = 0.0;
double[] Score = new double[MODEL_MAX_OCCURRENCES];
double[] x = new double[MODEL_MAX_OCCURRENCES];
double[] y = new double[MODEL_MAX_OCCURRENCES];
double[] Angle = new double[MODEL_MAX_OCCURRENCES];
double[] Scale = new double[MODEL_MAX_OCCURRENCES];
double Time = 0.0;
Console.Write("\nGEOMETRIC MODEL FINDER (scale and rotation independent):\n");
Console.Write("--------------------------------------------------------\n\n");
MIL.MbufCopy(MilModelImage, MilDisplayImage);
MIL.MmodAlloc(MilSystem, MIL.M_GEOMETRIC, MIL.M_DEFAULT, ref SearchContext);
MIL.MmodDefine(SearchContext, MIL.M_IMAGE, MilModelImage, (double)MODEL_POS_X_INIT(MilModelImage) - (MODEL_WIDTH / 2), (double)MODEL_POS_Y_INIT(MilModelImage) - (MODEL_HEIGHT / 2), MODEL_WIDTH, MODEL_HEIGHT);
MIL.MmodAllocResult(MilSystem, MIL.M_DEFAULT, ref Result);
MIL.MgraColor(MIL.M_DEFAULT, DrawColor);
MIL.MmodDraw(MIL.M_DEFAULT, SearchContext, MilDisplayImage, MIL.M_DRAW_BOX, MIL.M_DEFAULT, MIL.M_ORIGINAL);
MIL.MmodControl(SearchContext, MIL.M_CONTEXT, MIL.M_SPEED, MIL.M_VERY_HIGH);
MIL.MmodControl(SearchContext, MIL.M_DEFAULT, MIL.M_ACCEPTANCE, MODEL_MIN_MATCH_SCORE);
MIL.MmodPreprocess(SearchContext, MIL.M_DEFAULT);
MIL.MmodInquire(SearchContext, MIL.M_DEFAULT, MIL.M_ORIGINAL_X, ref OrgX);
MIL.MmodInquire(SearchContext, MIL.M_DEFAULT, MIL.M_ORIGINAL_Y, ref OrgY);
Console.Write("The Geometric target model was defined.\n");
Console.Write("Model dimensions: {0} x {1}.\n", MODEL_WIDTH, MODEL_HEIGHT);
Console.Write("Model center: X={0:0.00}, Y={0:0.00}.\n", OrgX, OrgY);
Console.Write("Model is scale and rotation independent.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, ref MilImage[0]);
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, ref MilImage[1]);
Console.Write("\nContinuously finding the Geometric Model.\n");
Console.Write("Press a <Enter> to stop.\n\n");
MIL.MdigControl(MilDigitizer, MIL.M_GRAB_MODE, MIL.M_ASYNCHRONOUS);
MIL.MdigGrab(MilDigitizer, MilImage[NbFindDone % 2]);
MIL.MdigGrab(MilDigitizer, MilImage[NbFindDone % 2]);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET, ref Time);
do
{
MIL.MdigGrab(MilDigitizer, MilImage[(NbFindDone + 1) % 2]);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref Time);
MIL.MmodFind(SearchContext, MilImage[NbFindDone % 2], Result);
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, ref Found);
if ((Found >= 1) && (Found < MODEL_MAX_OCCURRENCES))
{
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_X, x);
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_Y, y);
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_SCALE, Scale);
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_ANGLE, Angle);
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_SCORE, Score);
MIL.MmodDraw(MIL.M_DEFAULT, Result, MilImage[NbFindDone % 2], MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION + MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
Console.Write("Found: X={0,6:0.0}, Y={1,6:0.0}, Angle={2,6:0.0}, Scale={3,5:0.00}, Score={4,5:0.0}% ({5,5:0.0} fps).\r", x[0], y[0], Angle[0], Scale[0], Score[0], (NbFindDone + 1) / Time);
}
else
{
Console.Write("Not found! (score<{0,5:0.0}%) ({1,5:0.0} fps).\r", MODEL_MIN_MATCH_SCORE, (NbFindDone + 1) / Time);
}
MIL.MbufCopy(MilImage[NbFindDone % 2], MilDisplayImage);
NbFindDone++;
}
while (!Console.KeyAvailable);
Console.ReadKey();
Console.Write("\n\n");
MIL.MdigGrabWait(MilDigitizer, MIL.M_GRAB_END);
MIL.MmodFree(Result);
MIL.MmodFree(SearchContext);
MIL.MbufFree(MilImage[1]);
MIL.MbufFree(MilImage[0]);
}
}
}