/***************************************************************************************/ /* * File name: MPat.c * * Synopsis: This program contains 3 examples of the pattern matching module: * * The first example defines a model and then searches for it in a shifted * version of the image (without rotation). * * The second example defines a model and then searches for it in a * rotated version of the image. * * The third example automatically allocates a model in a wafer image and finds * its horizontal and vertical displacement. */ #include #include #include /* Example functions declarations. */ void SearchModelExample(MIL_ID MilSystem, MIL_ID MilDisplay); void SearchRotatedModelExample(MIL_ID MilSystem, MIL_ID MilDisplay); void AutoAllocationModelExample(MIL_ID MilSystem, MIL_ID MilDisplay); /***************************************************************************** Main. *****************************************************************************/ void main(void) { MIL_ID MilApplication, /* Application identifier. */ MilSystem, /* System identifier. */ MilDisplay; /* Display identifier. */ printf("\nGRAYSCALE PATTERN MATCHING:\n"); printf("---------------------------\n\n"); /* Allocate defaults. */ MappAllocDefault(M_SETUP, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); /* Run the search at 0 degree example. */ SearchModelExample(MilSystem, MilDisplay); /* Run the search over 360 degrees example. */ SearchRotatedModelExample(MilSystem, MilDisplay); /* Run the automatic model allocation example. */ AutoAllocationModelExample(MilSystem, MilDisplay); /* Free defaults. */ MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); } /*****************************************************************************/ /* Find model in shifted version of the image example. */ /* Source image file name. */ #define FIND_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim") /* Model position and size. */ #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) /* Target image shifting values. */ #define FIND_SHIFT_X 4.5 #define FIND_SHIFT_Y 7.5 /* Minimum match score to determine acceptability of model (default). */ #define FIND_MODEL_MIN_MATCH_SCORE 70.0 /* Minimum accuracy for the search. */ #define FIND_MODEL_MIN_ACCURACY 0.1 /* Absolute value macro. */ #define Absolute(x) (((x) < 0.0) ? -(x) : (x)) void SearchModelExample(MIL_ID MilSystem, MIL_ID MilDisplay) { MIL_ID MilImage, /* Image buffer identifier. */ MilOverlayImage, /* Overlay image. */ Model, /* Model identifier. */ Result; /* Result identifier. */ double XOrg = 0.0, YOrg = 0.0; /* Original model position. */ double x = 0.0, y = 0.0; /* Model position. */ double ErrX = 0.0, ErrY = 0.0; /* Model error position. */ double Score = 0.0; /* Model correlation score. */ double Time = 0.0; /* Model search time. */ long AnnotationColor = M_COLOR_RED; /* Drawing color. */ /* Restore source image into an automatically allocated image buffer. */ MbufRestore(FIND_IMAGE_FILE, MilSystem, &MilImage); /* Display the image buffer. */ MdispSelect(MilDisplay, MilImage); /* Prepare for overlay annotations. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Allocate a normalized grayscale model. */ MpatAllocModel(MilSystem, MilImage, FIND_MODEL_X_POS, FIND_MODEL_Y_POS, FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT, M_NORMALIZED, &Model); /* Set the search accuracy to high. */ MpatSetAccuracy(Model, M_HIGH); /* Set the search model speed to high. */ MpatSetSpeed(Model, M_HIGH); /* Preprocess the model. */ MpatPreprocModel(MilImage, Model, M_DEFAULT); /* Draw a box around the model in the model image. */ MgraColor(M_DEFAULT, AnnotationColor); MpatDraw(M_DEFAULT, Model, MilOverlayImage, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_ORIGINAL); /* Pause to show the original image and model position. */ printf("\nA %ldx%ld model was defined in the source image.\n", FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT); printf("It will be found in an image shifted by %.2f in X and %.2f in Y.\n", FIND_SHIFT_X, FIND_SHIFT_Y); printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Translate the image on a subpixel level. */ MimTranslate(MilImage, MilImage, FIND_SHIFT_X, FIND_SHIFT_Y, M_DEFAULT); /* Allocate result buffer. */ MpatAllocResult(MilSystem, 1L, &Result); /* First, perform a dummy find operation for better function timing accuracy (model cache effect,...) and reset the timer. */ MpatFindModel(MilImage, Model, Result); MappTimer(M_TIMER_RESET+M_SYNCHRONOUS, M_NULL); /* Find the model in the target buffer. */ MpatFindModel(MilImage, Model, Result); /* Read the time spent in MpatFindModel. */ MappTimer(M_TIMER_READ+M_SYNCHRONOUS, &Time); /* If one model was found above the acceptance threshold. */ if (MpatGetNumber(Result, M_NULL) == 1L) { /* Read results and draw a box around the model occurrence. */ 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, MilOverlayImage, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_DEFAULT); /* Calculate the position errors in X and Y and inquire original model position. */ 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); /* Print out the search result of the model in the original image. */ printf("Search results:\n"); printf("---------------------------------------------------\n"); printf("The model was found to have been shifted by\tX:%.2f, Y:%.2f.\n", x-XOrg, y-YOrg); printf("The model position error is \t\tX:%.2f, Y:%.2f\n", ErrX, ErrY); printf("The model match score is \t\t%.1f\n", Score); printf("The search time is \t\t\t%.3f ms\n\n", Time*1000.0); /* Verify the results. */ 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) ) printf("Results verification error !\n"); } else printf("Model not found !\n"); /* Wait for a key to be pressed. */ printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Free all allocations. */ MpatFree(Result); MpatFree(Model); MbufFree(MilImage); } /*****************************************************************************/ /* Find rotated model example. */ /* Source image file name. */ #define ROTATED_FIND_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim") /* Image rotation values. */ #define ROTATED_FIND_ROTATION_DELTA_ANGLE 10 #define ROTATED_FIND_ROTATION_ANGLE_STEP 1 #define ROTATED_FIND_RAD_PER_DEG 0.01745329251 /* Model position and size. */ #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 /* Minimum accuracy for the search position. */ #define ROTATED_FIND_MIN_POSITION_ACCURACY 0.10 /* Minimum accuracy for the search angle. */ #define ROTATED_FIND_MIN_ANGLE_ACCURACY 0.25 /* Angle range to search. */ #define ROTATED_FIND_ANGLE_DELTA_POS ROTATED_FIND_ROTATION_DELTA_ANGLE #define ROTATED_FIND_ANGLE_DELTA_NEG ROTATED_FIND_ROTATION_DELTA_ANGLE /* Prototypes of utility functions. */ void RotateModelCenter(MIL_ID Buffer, double *X, double *Y, double Angle); double CalculateAngleDist(double Angle1, double Angle2); void SearchRotatedModelExample(MIL_ID MilSystem, MIL_ID MilDisplay) { MIL_ID MilSourceImage, /* Model image buffer identifier. */ MilTargetImage, /* Target image buffer identifier. */ MilDisplayImage, /* Target image buffer identifier. */ MilOverlayImage, /* Overlay image. */ MilModel, /* Model identifier. */ MilResult; /* Result identifier. */ double RealX = 0.0, /* Model real position in x. */ RealY = 0.0, /* Model real position in y. */ RealAngle = 0.0, /* Model real angle. */ X = 0.0, /* Model position in x found. */ Y = 0.0, /* Model position in y found. */ Angle = 0.0, /* Model angle found. */ Score = 0.0, /* Model correlation score. */ Time = 0.0, /* Model search time. */ ErrX = 0.0, /* Model error position in x. */ ErrY = 0.0, /* Model error position in y. */ ErrAngle = 0.0, /* Model error angle. */ SumErrX = 0.0, /* Model total error position in x. */ SumErrY = 0.0, /* Model total error position in y. */ SumErrAngle = 0.0, /* Model total error angle. */ SumTime = 0.0; /* Model total search time. */ long NbFound = 0, /* Number of models found. */ AnnotationColor = M_COLOR_RED; /* Drawing color. */ /* Load target image into image buffers and display it. */ MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilSourceImage); MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilTargetImage); MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, &MilDisplayImage); MdispSelect(MilDisplay, MilDisplayImage); /* Prepare for overlay annotations. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Allocate a normalized grayscale model. */ 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); /* Set the search model speed. */ MpatSetSpeed(MilModel, M_MEDIUM); /* Set the position search accuracy. */ MpatSetAccuracy(MilModel, M_HIGH); /* Activate the search model angle mode. */ MpatSetAngle(MilModel, M_SEARCH_ANGLE_MODE, M_ENABLE); /* Set the search model range angle. */ MpatSetAngle(MilModel, M_SEARCH_ANGLE_DELTA_NEG, ROTATED_FIND_ANGLE_DELTA_NEG); MpatSetAngle(MilModel, M_SEARCH_ANGLE_DELTA_POS, ROTATED_FIND_ANGLE_DELTA_POS); /* Set the search model angle accuracy. */ MpatSetAngle(MilModel, M_SEARCH_ANGLE_ACCURACY, ROTATED_FIND_MIN_ANGLE_ACCURACY); /* Set the search model angle interpolation mode to bilinear. */ MpatSetAngle(MilModel, M_SEARCH_ANGLE_INTERPOLATION_MODE, M_BILINEAR); /* Preprocess the model. */ MpatPreprocModel(MilSourceImage, MilModel, M_DEFAULT); /* Allocate a result buffer. */ MpatAllocResult(MilSystem, 1L, &MilResult); /* Draw the original model position */ MpatDraw(M_DEFAULT, MilModel, MilOverlayImage, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_ORIGINAL); /* Pause to show the original image and model position. */ printf("\nA %ldx%ld model was defined in the source image.\n", ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT); printf("It will be searched in images rotated from %ld degree to %ld degree.\n", -ROTATED_FIND_ROTATION_DELTA_ANGLE, ROTATED_FIND_ROTATION_DELTA_ANGLE); printf("Press to continue.\n\n"); getch(); /* First, perform a dummy find operation to verify the model for better function timing accuracy. */ MpatFindModel(MilSourceImage, MilModel, MilResult); /* If the model was found above the acceptance threshold. */ if (MpatGetNumber(MilResult, M_NULL) == 1L) { /* Search for the model in images at different angles. */ RealAngle = ROTATED_FIND_ROTATION_DELTA_ANGLE; while (RealAngle >= -ROTATED_FIND_ROTATION_DELTA_ANGLE) { /* Rotate the image from the model image to target image. */ MimRotate(MilSourceImage, MilTargetImage, RealAngle, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_BILINEAR+M_OVERSCAN_CLEAR); /* Reset the timer. */ MappTimer(M_TIMER_RESET+M_SYNCHRONOUS, M_NULL); /* Find the model in the target image. */ MpatFindModel(MilTargetImage, MilModel, MilResult); /* Read the time spent in MpatFindModel(). */ MappTimer(M_TIMER_READ+M_SYNCHRONOUS, &Time); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* If one model was found above the acceptance threshold. */ if (MpatGetNumber(MilResult, M_NULL) == 1L) { /* Read results and draw a box around model occurrence. */ 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, MilOverlayImage, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_DEFAULT); MbufCopy(MilTargetImage, MilDisplayImage); /* Calculate the angle error and the position errors for statistics. */ 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++; /* Verify the precision for the position and the angle. */ if ((ErrX > ROTATED_FIND_MIN_POSITION_ACCURACY) || (ErrY > ROTATED_FIND_MIN_POSITION_ACCURACY) || (ErrAngle > ROTATED_FIND_MIN_ANGLE_ACCURACY)) { printf("Model accuracy error at angle %.1f !\n\n", RealAngle); printf("Errors are X:%.3f, Y:%.3f and Angle:%.2f\n\n", ErrX, ErrY, ErrAngle); printf("Press to continue.\n\n"); getch(); } } else { printf("Model was not found at angle %.1f !\n\n", RealAngle); printf("Press to continue.\n\n"); getch(); } RealAngle -= ROTATED_FIND_ROTATION_ANGLE_STEP; } /* Print out the search result statistics */ /* of the models found in rotated images. */ printf("\nSearch statistics for the model found in the rotated images.\n"); printf("------------------------------------------------------------\n"); printf("The average position error is \t\tX:%.3f, Y:%.3f\n", SumErrX/NbFound, SumErrY/NbFound); printf("The average angle error is \t\t%.3f\n", SumErrAngle/NbFound); printf("The average search time is \t\t%.3f ms\n\n", SumTime*1000.0/NbFound); } else { printf("Model was not found!\n\n"); } /* Wait for a key to be pressed. */ printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Free all allocations. */ MpatFree(MilResult); MpatFree(MilModel); MbufFree(MilTargetImage); MbufFree(MilSourceImage); MbufFree(MilDisplayImage); } /* Calculate the rotated center of the model to compare the accuracy with * the center of the occurrence found during pattern matching. */ void RotateModelCenter(MIL_ID Buffer, double *X, double *Y, double Angle) { long BufSizeX = MbufInquire(Buffer, M_SIZE_X, M_NULL); long BufSizeY = MbufInquire(Buffer, M_SIZE_Y, M_NULL); double RadAngle = Angle * ROTATED_FIND_RAD_PER_DEG; double CosAngle = cos(RadAngle); double SinAngle = sin(RadAngle); double OffSetX = (BufSizeX-1)/2.0F; 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; } /* Calculate the absolute difference between the real angle * and the angle found. */ double CalculateAngleDist(double Angle1, double Angle2) { double dist = fabs(Angle1 - Angle2); while(dist >= 360.0) dist -= 360.0; if(dist > 180.0) dist = 360.0 - dist; return dist; } /*****************************************************************************/ /* Automatic model allocation example. */ /* Source and target images file specifications. */ #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_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim") //#define AUTO_MODEL_TARGET_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim") /* Model width and height */ #define AUTO_MODEL_WIDTH 64L #define AUTO_MODEL_HEIGHT 64L void AutoAllocationModelExample(MIL_ID MilSystem, MIL_ID MilDisplay) { MIL_ID MilImage, /* Image buffer identifier. */ MilOverlayImage, /* Overlay image. */ MilSubImage, /* Sub-image buffer identifier. */ MilOverlaySubImage, /* Overlay sub-image buffer identifier. */ Model, /* Model identifier. */ Result; /* Result buffer identifier. */ long AllocError; /* Allocation error variable. */ long ImageWidth, ImageHeight; /* Target image dimensions */ double OrgX=0.0, OrgY=0.0; /* Original center of model. */ double x=0.0, y=0.0, Score=0.0; /* Result variables. */ long AnnotationColor = M_COLOR_RED; /* Drawing color. */ /* Load model image into an image buffer. */ MbufRestore(AUTO_MODEL_IMAGE_FILE, MilSystem, &MilImage); /* Display the image and prepare for overlay annotations. */ MdispSelect(MilDisplay, MilImage); MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Restrict the region to be processed to the bottom right corner of the image. */ MbufInquire(MilImage, M_SIZE_X, &ImageWidth); MbufInquire(MilImage, M_SIZE_Y, &ImageHeight); MbufChild2d(MilImage, ImageWidth/2, ImageHeight/2, ImageWidth/2, ImageHeight/2, &MilSubImage); MbufChild2d(MilOverlayImage, ImageWidth/2, ImageHeight/2, ImageWidth/2, ImageHeight/2, &MilOverlaySubImage); /* Automatically allocate a normalized grayscale type model. */ MpatAllocAutoModel(MilSystem, MilSubImage, AUTO_MODEL_WIDTH, AUTO_MODEL_HEIGHT, M_DEFAULT, M_DEFAULT, M_NORMALIZED, M_DEFAULT, &Model); /* Set the search accuracy to high. */ MpatSetAccuracy(Model, M_HIGH); /* Check for that model allocation was successful. */ MappGetError(M_CURRENT, &AllocError); if (!AllocError) { /* Draw a box around the model. */ MgraColor(M_DEFAULT, AnnotationColor); MpatDraw(M_DEFAULT, Model, MilOverlaySubImage, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_ORIGINAL); printf("A model was automatically defined in the image.\n"); printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Load target image into an image buffer. */ MbufLoad(AUTO_MODEL_TARGET_IMAGE_FILE, MilImage); /* Allocate result. */ MpatAllocResult(MilSystem, 1L, &Result); /* Find model. */ MpatFindModel(MilSubImage, Model, Result); /* If one model was found above the acceptance threshold set. */ if (MpatGetNumber(Result, M_NULL) == 1L) { /* Get results. */ MpatGetResult(Result, M_POSITION_X, &x); MpatGetResult(Result, M_POSITION_Y, &y); MpatGetResult(Result, M_SCORE, &Score); /* Draw a box around the occurrence. */ MgraColor(M_DEFAULT, AnnotationColor); MpatDraw(M_DEFAULT, Result, MilOverlaySubImage, M_DRAW_BOX+M_DRAW_POSITION, M_DEFAULT, M_DEFAULT); /* Analyze and print results. */ MpatInquire(Model,M_ORIGINAL_X,&OrgX); MpatInquire(Model,M_ORIGINAL_Y,&OrgY); printf("An image misaligned by 50 pixels in X and 20 pixels in Y was loaded.\n\n"); printf("The image was found to be shifted by %.2f in X, and %.2f in Y.\n",x-OrgX, y-OrgY); printf("Model match score is %.1f percent.\n", Score); printf("Press to end.\n\n"); getch(); } else { printf("Error: Pattern not found properly.\n"); printf("Press to end.\n\n"); getch(); } /* Free result buffer and model. */ MpatFree(Result); MpatFree(Model); } else { printf("Error: Automatic model definition failed.\n"); printf("Press to end.\n\n"); getch(); } /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Free child buffer and defaults. */ MbufFree(MilSubImage); MbufFree(MilOverlaySubImage); MbufFree(MilImage); }