#include <mil.h>
#include <math.h>
void SearchModelExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void SearchRotatedModelExample(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);
SearchRotatedModelExample(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,
Model,
Result;
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);
MpatAllocModel(MilSystem, MilImage, FIND_MODEL_X_POS, FIND_MODEL_Y_POS,
FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT, M_NORMALIZED, &Model);
MpatSetAccuracy(Model, M_HIGH);
MpatSetSpeed(Model, M_HIGH);
MpatPreprocModel(MilImage, Model, M_DEFAULT);
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, Model, 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, 1L, &Result);
MpatFindModel(MilImage, Model, Result);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
MpatFindModel(MilImage, Model, Result);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
if (MpatGetNumber(Result, M_NULL) == 1L)
{
MpatGetResult(Result, M_POSITION_X, &x);
MpatGetResult(Result, M_POSITION_Y, &y);
MpatGetResult(Result, 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(Model, M_ORIGINAL_X, &XOrg);
MpatInquire(Model, 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(Model);
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 SearchRotatedModelExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilSourceImage,
MilTargetImage,
MilDisplayImage,
GraphicList,
MilModel,
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 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);
MpatAllocModel(MilSystem, MilSourceImage,
ROTATED_FIND_MODEL_X_POS, ROTATED_FIND_MODEL_Y_POS,
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT,
M_NORMALIZED+M_CIRCULAR_OVERSCAN, &MilModel);
MpatSetSpeed(MilModel, M_MEDIUM);
MpatSetAccuracy(MilModel, M_HIGH);
MpatSetAngle(MilModel, M_SEARCH_ANGLE_MODE, M_ENABLE);
MpatSetAngle(MilModel, M_SEARCH_ANGLE_DELTA_NEG, ROTATED_FIND_ANGLE_DELTA_NEG);
MpatSetAngle(MilModel, M_SEARCH_ANGLE_DELTA_POS, ROTATED_FIND_ANGLE_DELTA_POS);
MpatSetAngle(MilModel, M_SEARCH_ANGLE_ACCURACY, ROTATED_FIND_MIN_ANGLE_ACCURACY);
MpatSetAngle(MilModel, M_SEARCH_ANGLE_INTERPOLATION_MODE, M_BILINEAR);
MpatPreprocModel(MilSourceImage, MilModel, M_DEFAULT);
MpatAllocResult(MilSystem, 1L, &MilResult);
MpatDraw(M_DEFAULT, MilModel, 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 %ld degree ")
MIL_TEXT("to %ld degree.\n"),-ROTATED_FIND_ROTATION_DELTA_ANGLE,
ROTATED_FIND_ROTATION_DELTA_ANGLE);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MpatFindModel(MilSourceImage, MilModel, MilResult);
if (MpatGetNumber(MilResult, M_NULL) == 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);
MpatFindModel(MilTargetImage, MilModel, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MgraClear(M_DEFAULT, GraphicList);
if (MpatGetNumber(MilResult, M_NULL) == 1L)
{
MpatGetResult(MilResult, M_POSITION_X, &X);
MpatGetResult(MilResult, M_POSITION_Y, &Y);
MpatGetResult(MilResult, M_ANGLE, &Angle);
MpatGetResult(MilResult, 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(MilModel);
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;
}
#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,
Model,
Result;
MIL_INT AllocError;
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));
MpatAllocAutoModel(MilSystem, MilSubImage, AUTO_MODEL_WIDTH, AUTO_MODEL_HEIGHT,
M_DEFAULT, M_DEFAULT, M_NORMALIZED, M_DEFAULT, &Model);
MpatSetAccuracy(Model, M_HIGH);
MappGetError(M_DEFAULT, M_CURRENT, &AllocError);
if (!AllocError)
{
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, Model, 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, 1L, &Result);
MpatFindModel(MilSubImage, Model, Result);
if (MpatGetNumber(Result, M_NULL) == 1L)
{
MpatGetResult(Result, M_POSITION_X, &x);
MpatGetResult(Result, M_POSITION_Y, &y);
MpatGetResult(Result, M_SCORE, &Score);
MgraColor(M_DEFAULT, AnnotationColor);
MpatDraw(M_DEFAULT, Result, GraphicList, M_DRAW_BOX+M_DRAW_POSITION,
M_DEFAULT, M_DEFAULT);
MpatInquire(Model,M_ORIGINAL_X,&OrgX);
MpatInquire(Model,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(Model);
}
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);
}