#include <mil.h>
#include <math.h>
void SearchModelExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void SearchModelAngleRangeExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void SearchModelAtAngleExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void AutoAllocationModelExample(MIL_ID MilSystem, MIL_ID MilDisplay);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MosPrintf(MIL_TEXT("\nGRAYSCALE PATTERN MATCHING:\n"));
MosPrintf(MIL_TEXT("---------------------------\n\n"));
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
SearchModelExample(MilSystem, MilDisplay);
SearchModelAngleRangeExample(MilSystem, MilDisplay);
SearchModelAtAngleExample(MilSystem, MilDisplay);
AutoAllocationModelExample(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define FIND_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim")
#define FIND_MODEL_X_POS 153L
#define FIND_MODEL_Y_POS 132L
#define FIND_MODEL_WIDTH 128L
#define FIND_MODEL_HEIGHT 128L
#define FIND_MODEL_X_CENTER (FIND_MODEL_X_POS+(FIND_MODEL_WIDTH -1)/2.0)
#define FIND_MODEL_Y_CENTER (FIND_MODEL_Y_POS+(FIND_MODEL_HEIGHT-1)/2.0)
#define FIND_SHIFT_X 4.5
#define FIND_SHIFT_Y 7.5
#define FIND_MODEL_MIN_MATCH_SCORE 70.0
#define FIND_MODEL_MIN_ACCURACY 0.1
#define Absolute(x) (((x) < 0.0) ? -(x) : (x))
void SearchModelExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
GraphicList,
ContextId,
Result;
MIL_INT NumResults;
MIL_DOUBLE XOrg = 0.0, YOrg = 0.0;
MIL_DOUBLE x = 0.0, y = 0.0;
MIL_DOUBLE ErrX = 0.0, ErrY = 0.0;
MIL_DOUBLE Score = 0.0;
MIL_DOUBLE Time = 0.0;
MIL_DOUBLE AnnotationColor = M_COLOR_GREEN;
MbufRestore(FIND_IMAGE_FILE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MpatAlloc(MilSystem, M_NORMALIZED, M_DEFAULT, &ContextId);
MpatDefine(ContextId, M_REGULAR_MODEL, MilImage, FIND_MODEL_X_POS,
FIND_MODEL_Y_POS, FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT, M_DEFAULT);
MpatControl(ContextId, M_DEFAULT, M_ACCURACY, M_HIGH);
MpatControl(ContextId, M_DEFAULT, M_SPEED, M_HIGH);
MpatPreprocess(ContextId, M_DEFAULT, MilImage);
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, ContextId, GraphicList, M_DRAW_BOX + M_DRAW_POSITION,
M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("\nA %ldx%ld model was defined in the source image.\n"),
FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT);
MosPrintf(MIL_TEXT("It will be found in an image shifted ")
MIL_TEXT("by %.2f in X and %.2f in Y.\n"), FIND_SHIFT_X, FIND_SHIFT_Y);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, GraphicList);
MimTranslate(MilImage, MilImage, FIND_SHIFT_X, FIND_SHIFT_Y, M_DEFAULT);
MpatAllocResult(MilSystem, M_DEFAULT, &Result);
MpatFind(ContextId, MilImage, Result);
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
MpatFind(ContextId, MilImage, Result);
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
MpatGetResult(Result, M_GENERAL, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
if(NumResults == 1L)
{
MpatGetResult(Result, M_DEFAULT, M_POSITION_X, &x);
MpatGetResult(Result, M_DEFAULT, M_POSITION_Y, &y);
MpatGetResult(Result, M_DEFAULT, M_SCORE, &Score);
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, Result, GraphicList, M_DRAW_BOX + M_DRAW_POSITION,
M_DEFAULT, M_DEFAULT);
ErrX = fabs((FIND_MODEL_X_CENTER + FIND_SHIFT_X) - x);
ErrY = fabs((FIND_MODEL_Y_CENTER + FIND_SHIFT_Y) - y);
MpatInquire(ContextId, M_DEFAULT, M_ORIGINAL_X, &XOrg);
MpatInquire(ContextId, M_DEFAULT, M_ORIGINAL_Y, &YOrg);
MosPrintf(MIL_TEXT("Search results:\n"));
MosPrintf(MIL_TEXT("---------------------------------------------------\n"));
MosPrintf(MIL_TEXT("The model is found to be shifted by \tX:%.2f, Y:%.2f.\n"),
x - XOrg, y - YOrg);
MosPrintf(MIL_TEXT("The model position error is \t\tX:%.2f, Y:%.2f\n"),
ErrX, ErrY);
MosPrintf(MIL_TEXT("The model match score is \t\t%.1f\n"), Score);
MosPrintf(MIL_TEXT("The search time is \t\t\t%.3f ms\n\n"), Time*1000.0);
if (
(Absolute((x - XOrg) - FIND_SHIFT_X) > FIND_MODEL_MIN_ACCURACY) ||
(Absolute((y - YOrg) - FIND_SHIFT_Y) > FIND_MODEL_MIN_ACCURACY) ||
(Score < FIND_MODEL_MIN_MATCH_SCORE)
)
MosPrintf(MIL_TEXT("Results verification error !\n"));
}
else
MosPrintf(MIL_TEXT("Model not found !\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, GraphicList);
MgraFree(GraphicList);
MpatFree(Result);
MpatFree(ContextId);
MbufFree(MilImage);
}
#define ROTATED_FIND_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim")
#define ROTATED_FIND_ROTATION_DELTA_ANGLE 10
#define ROTATED_FIND_ROTATION_ANGLE_STEP 1
#define ROTATED_FIND_RAD_PER_DEG 0.01745329251
#define ROTATED_FIND_MODEL_X_POS 153L
#define ROTATED_FIND_MODEL_Y_POS 132L
#define ROTATED_FIND_MODEL_WIDTH 128L
#define ROTATED_FIND_MODEL_HEIGHT 128L
#define ROTATED_FIND_MODEL_X_CENTER ROTATED_FIND_MODEL_X_POS+ \
(ROTATED_FIND_MODEL_WIDTH -1)/2.0
#define ROTATED_FIND_MODEL_Y_CENTER ROTATED_FIND_MODEL_Y_POS+ \
(ROTATED_FIND_MODEL_HEIGHT-1)/2.0
#define ROTATED_FIND_MIN_POSITION_ACCURACY 0.10
#define ROTATED_FIND_MIN_ANGLE_ACCURACY 0.25
#define ROTATED_FIND_ANGLE_DELTA_POS ROTATED_FIND_ROTATION_DELTA_ANGLE
#define ROTATED_FIND_ANGLE_DELTA_NEG ROTATED_FIND_ROTATION_DELTA_ANGLE
void RotateModelCenter(MIL_ID Buffer,
MIL_DOUBLE *X,
MIL_DOUBLE *Y,
MIL_DOUBLE Angle);
MIL_DOUBLE CalculateAngleDist(MIL_DOUBLE Angle1,
MIL_DOUBLE Angle2);
void SearchModelAngleRangeExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilSourceImage,
MilTargetImage,
MilDisplayImage,
GraphicList,
MilContextId,
MilResult;
MIL_DOUBLE RealX = 0.0,
RealY = 0.0,
RealAngle = 0.0,
X = 0.0,
Y = 0.0,
Angle = 0.0,
Score = 0.0,
Time = 0.0,
ErrX = 0.0,
ErrY = 0.0,
ErrAngle = 0.0,
SumErrX = 0.0,
SumErrY = 0.0,
SumErrAngle = 0.0,
SumTime = 0.0;
MIL_INT NumResults;
MIL_INT NbFound = 0;
MIL_DOUBLE AnnotationColor = M_COLOR_GREEN;
MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilSourceImage);
MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilTargetImage);
MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilDisplayImage);
MdispSelect(MilDisplay, MilDisplayImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MpatAlloc(MilSystem, M_NORMALIZED, M_DEFAULT, &MilContextId);
MpatDefine(MilContextId, M_REGULAR_MODEL + M_CIRCULAR_OVERSCAN, MilSourceImage,
ROTATED_FIND_MODEL_X_POS, ROTATED_FIND_MODEL_Y_POS,
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT, M_DEFAULT);
MpatControl(MilContextId, M_DEFAULT, M_SPEED, M_MEDIUM);
MpatControl(MilContextId, M_DEFAULT, M_ACCURACY, M_HIGH);
MpatControl(MilContextId, M_DEFAULT, M_SEARCH_ANGLE_MODE, M_ENABLE);
MpatControl(MilContextId, M_DEFAULT, M_SEARCH_ANGLE_DELTA_NEG, ROTATED_FIND_ANGLE_DELTA_NEG);
MpatControl(MilContextId, M_DEFAULT, M_SEARCH_ANGLE_DELTA_POS, ROTATED_FIND_ANGLE_DELTA_POS);
MpatControl(MilContextId, M_DEFAULT, M_SEARCH_ANGLE_ACCURACY, ROTATED_FIND_MIN_ANGLE_ACCURACY);
MpatControl(MilContextId, M_DEFAULT, M_SEARCH_ANGLE_INTERPOLATION_MODE, M_BILINEAR);
MpatPreprocess(MilContextId, M_DEFAULT, MilSourceImage);
MpatAllocResult(MilSystem, M_DEFAULT, &MilResult);
MpatDraw(M_DEFAULT, MilContextId, GraphicList, M_DRAW_BOX + M_DRAW_POSITION,
M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("\nA %ldx%ld model was defined in the source image.\n"),
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT);
MosPrintf(MIL_TEXT("It will be searched in images rotated from %d degree ")
MIL_TEXT("to %d degree.\n"), -ROTATED_FIND_ROTATION_DELTA_ANGLE,
ROTATED_FIND_ROTATION_DELTA_ANGLE);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MpatFind(MilContextId, MilSourceImage, MilResult);
MpatGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
if(NumResults == 1L)
{
RealAngle = ROTATED_FIND_ROTATION_DELTA_ANGLE;
while(RealAngle >= -ROTATED_FIND_ROTATION_DELTA_ANGLE)
{
MimRotate(MilSourceImage, MilTargetImage, RealAngle, M_DEFAULT,
M_DEFAULT, M_DEFAULT, M_DEFAULT, M_BILINEAR + M_OVERSCAN_CLEAR);
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
MpatFind(MilContextId, MilTargetImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
MgraClear(M_DEFAULT, GraphicList);
MpatGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
if(NumResults == 1L)
{
MpatGetResult(MilResult, M_DEFAULT, M_POSITION_X, &X);
MpatGetResult(MilResult, M_DEFAULT, M_POSITION_Y, &Y);
MpatGetResult(MilResult, M_DEFAULT, M_ANGLE, &Angle);
MpatGetResult(MilResult, M_DEFAULT, M_SCORE, &Score);
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, MilResult, GraphicList,
M_DRAW_BOX + M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MbufCopy(MilTargetImage, MilDisplayImage);
ErrAngle = CalculateAngleDist(Angle, RealAngle);
RotateModelCenter(MilSourceImage, &RealX, &RealY, RealAngle);
ErrX = fabs(X - RealX);
ErrY = fabs(Y - RealY);
SumErrAngle += ErrAngle;
SumErrX += ErrX;
SumErrY += ErrY;
SumTime += Time;
NbFound++;
if ((ErrX > ROTATED_FIND_MIN_POSITION_ACCURACY) ||
(ErrY > ROTATED_FIND_MIN_POSITION_ACCURACY) ||
(ErrAngle > ROTATED_FIND_MIN_ANGLE_ACCURACY))
{
MosPrintf(MIL_TEXT("Model accuracy error at angle %.1f !\n\n"), RealAngle);
MosPrintf(MIL_TEXT("Errors are X:%.3f, Y:%.3f and Angle:%.2f\n\n"),
ErrX, ErrY, ErrAngle);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
}
else
{
MosPrintf(MIL_TEXT("Model was not found at angle %.1f !\n\n"), RealAngle);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
RealAngle -= ROTATED_FIND_ROTATION_ANGLE_STEP;
}
MosPrintf(MIL_TEXT("\nSearch statistics for the model ")
MIL_TEXT("found in the rotated images.\n"));
MosPrintf(MIL_TEXT("------------------------------")
MIL_TEXT("------------------------------\n"));
MosPrintf(MIL_TEXT("The average position error is \t\tX:%.3f, Y:%.3f\n"),
SumErrX / NbFound, SumErrY / NbFound);
MosPrintf(MIL_TEXT("The average angle error is \t\t%.3f\n"), SumErrAngle / NbFound);
MosPrintf(MIL_TEXT("The average search time is \t\t%.3f ms\n\n"),
SumTime*1000.0 / NbFound);
}
else
{
MosPrintf(MIL_TEXT("Model was not found!\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraFree(GraphicList);
MpatFree(MilResult);
MpatFree(MilContextId);
MbufFree(MilTargetImage);
MbufFree(MilSourceImage);
MbufFree(MilDisplayImage);
}
void RotateModelCenter(MIL_ID Buffer,
MIL_DOUBLE *X,
MIL_DOUBLE *Y,
MIL_DOUBLE Angle)
{
MIL_INT BufSizeX = MbufInquire(Buffer, M_SIZE_X, M_NULL);
MIL_INT BufSizeY = MbufInquire(Buffer, M_SIZE_Y, M_NULL);
MIL_DOUBLE RadAngle = Angle * ROTATED_FIND_RAD_PER_DEG;
MIL_DOUBLE CosAngle = cos(RadAngle);
MIL_DOUBLE SinAngle = sin(RadAngle);
MIL_DOUBLE OffSetX = (BufSizeX - 1) / 2.0F;
MIL_DOUBLE OffSetY = (BufSizeY - 1) / 2.0F;
*X = (ROTATED_FIND_MODEL_X_CENTER - OffSetX)*CosAngle +
(ROTATED_FIND_MODEL_Y_CENTER - OffSetY)*SinAngle + OffSetX;
*Y = (ROTATED_FIND_MODEL_Y_CENTER - OffSetY)*CosAngle -
(ROTATED_FIND_MODEL_X_CENTER - OffSetX)*SinAngle + OffSetY;
}
MIL_DOUBLE CalculateAngleDist(MIL_DOUBLE Angle1, MIL_DOUBLE Angle2)
{
MIL_DOUBLE dist = fabs(Angle1 - Angle2);
while(dist >= 360.0)
dist -= 360.0;
if(dist > 180.0)
dist = 360.0 - dist;
return dist;
}
void SearchModelAtAngleExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilSourceImage,
MilTargetImage,
ContextId,
GraphicList,
MilResult;
MIL_DOUBLE Time = 0.0;
MIL_INT NbFound = 0;
MIL_DOUBLE AnnotationColor = M_COLOR_GREEN;
MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilSourceImage);
MdispSelect(MilDisplay, MilSourceImage);
MbufAlloc2d(MilSystem, MbufInquire(MilSourceImage, M_SIZE_X, M_NULL),
MbufInquire(MilSourceImage, M_SIZE_Y, M_NULL),
8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &MilTargetImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MpatAlloc(MilSystem, M_NORMALIZED, M_DEFAULT, &ContextId);
MpatDefine(ContextId, M_REGULAR_MODEL, MilSourceImage,
ROTATED_FIND_MODEL_X_POS, ROTATED_FIND_MODEL_Y_POS,
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT, M_DEFAULT);
MpatControl(ContextId, M_DEFAULT, M_SEARCH_ANGLE_MODE, M_ENABLE);
MpatControl(ContextId, M_DEFAULT, M_SEARCH_ANGLE_DELTA_NEG, 0);
MpatControl(ContextId, M_DEFAULT, M_SEARCH_ANGLE_DELTA_POS, 0);
MpatControl(ContextId, 0, M_SEARCH_ANGLE, ROTATED_FIND_ROTATION_DELTA_ANGLE);
MpatPreprocess(ContextId, M_DEFAULT, MilSourceImage);
MpatAllocResult(MilSystem, M_DEFAULT, &MilResult);
MpatDraw(M_DEFAULT, ContextId, GraphicList, M_DRAW_BOX + M_DRAW_POSITION,
M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("\nA %ldx%ld model was defined in the source image.\n"),
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT);
MosPrintf(MIL_TEXT("It will be searched in an image rotated at %d degrees.\n"),
-ROTATED_FIND_ROTATION_DELTA_ANGLE);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimRotate(MilSourceImage, MilTargetImage, ROTATED_FIND_ROTATION_DELTA_ANGLE, M_DEFAULT,
M_DEFAULT, M_DEFAULT, M_DEFAULT, M_BILINEAR + M_OVERSCAN_CLEAR);
MdispSelect(MilDisplay, MilTargetImage);
MpatFind(ContextId, MilTargetImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
MpatFind(ContextId, MilTargetImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MgraClear(M_DEFAULT, GraphicList);
MpatGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NbFound);
if (NbFound== 1L)
{
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("A search model at a specific angle has been found in the rotated image.\n"));
MosPrintf(MIL_TEXT("The search time is %.3f ms.\n\n"), Time*1000.0);
}
else
{
MosPrintf(MIL_TEXT("Model was not found!\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MdispControl(MilDisplay, M_OVERLAY_SHOW, M_DISABLE);
MpatFree(MilResult);
MpatFree(ContextId);
MgraFree(GraphicList);
MbufFree(MilTargetImage);
MbufFree(MilSourceImage);
}
#define AUTO_MODEL_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Wafer.mim")
#define AUTO_MODEL_TARGET_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("WaferShifted.mim")
#define AUTO_MODEL_WIDTH 64L
#define AUTO_MODEL_HEIGHT 64L
void AutoAllocationModelExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
MilSubImage,
GraphicList,
ContextId,
Result;
MIL_INT AllocError;
MIL_INT NumResults;
MIL_INT ImageWidth, ImageHeight;
MIL_DOUBLE OrgX = 0.0, OrgY = 0.0;
MIL_DOUBLE x = 0.0, y = 0.0, Score = 0.0;
MIL_DOUBLE AnnotationColor = M_COLOR_GREEN;
MbufRestore(AUTO_MODEL_IMAGE_FILE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MbufInquire(MilImage, M_SIZE_X, &ImageWidth);
MbufInquire(MilImage, M_SIZE_Y, &ImageHeight);
MbufChild2d(MilImage, ImageWidth/2, ImageHeight/2,
ImageWidth/2, ImageHeight/2, &MilSubImage);
MgraControl(M_DEFAULT, M_DRAW_OFFSET_X, (MIL_DOUBLE)-(ImageWidth/2));
MgraControl(M_DEFAULT, M_DRAW_OFFSET_Y, (MIL_DOUBLE)-(ImageHeight/2));
MpatAlloc(MilSystem, M_NORMALIZED, M_DEFAULT, &ContextId);
MpatDefine(ContextId, M_AUTO_MODEL, MilSubImage, M_DEFAULT, M_DEFAULT,
AUTO_MODEL_WIDTH, AUTO_MODEL_HEIGHT, M_DEFAULT);
MpatControl(ContextId, M_DEFAULT, M_ACCURACY, M_HIGH);
MappGetError(M_DEFAULT, M_CURRENT, &AllocError);
if(!AllocError)
{
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, ContextId, GraphicList, M_DRAW_BOX + M_DRAW_POSITION,
M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("A model was automatically defined in the image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, GraphicList);
MbufLoad(AUTO_MODEL_TARGET_IMAGE_FILE, MilImage);
MpatAllocResult(MilSystem, M_DEFAULT, &Result);
MpatPreprocess(ContextId, M_DEFAULT, MilSubImage);
MpatFind(ContextId, MilSubImage, Result);
MpatGetResult(Result, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
if(NumResults == 1L)
{
MpatGetResult(Result, M_DEFAULT, M_POSITION_X, &x);
MpatGetResult(Result, M_DEFAULT, M_POSITION_Y, &y);
MpatGetResult(Result, M_DEFAULT, M_SCORE, &Score);
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, Result, GraphicList, M_DRAW_BOX + M_DRAW_POSITION,
M_DEFAULT, M_DEFAULT);
MpatInquire(ContextId, M_DEFAULT, M_ORIGINAL_X, &OrgX);
MpatInquire(ContextId, M_DEFAULT, M_ORIGINAL_Y, &OrgY);
MosPrintf(MIL_TEXT("An image misaligned by 50 pixels in X and 20 pixels ")
MIL_TEXT("in Y was loaded.\n\n"));
MosPrintf(MIL_TEXT("The image is found to be shifted by %.2f in X, ")
MIL_TEXT("and %.2f in Y.\n"), x - OrgX, y - OrgY);
MosPrintf(MIL_TEXT("Model match score is %.1f percent.\n"), Score);
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
}
else
{
MosPrintf(MIL_TEXT("Error: Pattern not found properly.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
}
MpatFree(Result);
MpatFree(ContextId);
}
else
{
MosPrintf(MIL_TEXT("Error: Automatic model definition failed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
}
MgraControl(M_DEFAULT, M_DRAW_OFFSET_X, 0.0);
MgraControl(M_DEFAULT, M_DRAW_OFFSET_Y, 0.0);
MgraFree(GraphicList);
MbufFree(MilSubImage);
MbufFree(MilImage);
}