/*************************************************************************************/ /* * File name: MMod.c * * Synopsis: This program uses the Geometric Model Finder module to define geometric * models and searches for these models in a target image. A simple single model * example (1 model, 1 occurrence, good search conditions) is presented first, * followed by a more complex example (multiple models, multiple occurrences in a * complex scene with bad search conditions). */ #include #include /* Example functions declarations. */ void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay); void MultipleModelsExample(MIL_ID MilSystem, MIL_ID MilDisplay); /*****************************************************************************/ /* Main. ******************************************************************************/ void main(void) { MIL_ID MilApplication, /* Application identifier. */ MilSystem, /* System Identifier. */ MilDisplay; /* Display identifier. */ /* Allocate defaults. */ MappAllocDefault(M_SETUP, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); /* Run single model example. */ SingleModelExample(MilSystem, MilDisplay); /* Run multiple model example. */ MultipleModelsExample(MilSystem, MilDisplay); /* Free defaults. */ MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); } /*****************************************************************************/ /* Single model example. */ /* Source MIL image file specifications. */ #define SINGLE_MODEL_IMAGE M_IMAGE_PATH MIL_TEXT("SingleModel.mim") /* Target MIL image file specifications. */ #define SINGLE_MODEL_TARGET_IMAGE M_IMAGE_PATH MIL_TEXT("SingleTarget.mim") /* Search speed: M_VERY_HIGH for faster search, M_MEDIUM for precision and robustness. */ #define SINGLE_MODEL_SEARCH_SPEED M_VERY_HIGH /* Model specifications. */ #define MODEL_OFFSETX 176L #define MODEL_OFFSETY 136L #define MODEL_SIZEX 128L #define MODEL_SIZEY 128L #define MODEL_MAX_OCCURRENCES 16L void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay) { MIL_ID MilImage, /* Image buffer identifier.*/ MilOverlayImage; /* Overlay image. */ MIL_ID MilSearchContext, /* Search context */ MilResult; /* Result identifier. */ long Model[MODEL_MAX_OCCURRENCES], /* Model index. */ ModelDrawColor = M_COLOR_RED; /* Model draw color. */ long NumResults = 0L; /* Number of results found.*/ double Score[MODEL_MAX_OCCURRENCES], /* Model correlation score.*/ XPosition[MODEL_MAX_OCCURRENCES], /* Model X position. */ YPosition[MODEL_MAX_OCCURRENCES], /* Model Y position. */ Angle[MODEL_MAX_OCCURRENCES], /* Model occurrence angle. */ Scale[MODEL_MAX_OCCURRENCES], /* Model occurrence scale. */ Time = 0.0; /* Bench variable. */ int i; /* Loop variable. */ /* Restore the model image and display it */ MbufRestore(SINGLE_MODEL_IMAGE, MilSystem, &MilImage); MdispSelect(MilDisplay, MilImage); /* Prepare for overlay annotation. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Allocate a Geometric Model Finder context. */ MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext); /* Allocate a result buffer. */ MmodAllocResult(MilSystem, M_DEFAULT, &MilResult); /* Define the model. */ MmodDefine(MilSearchContext, M_IMAGE, MilImage, MODEL_OFFSETX, MODEL_OFFSETY, MODEL_SIZEX, MODEL_SIZEY); /* Set the search speed. */ MmodControl(MilSearchContext, M_CONTEXT, M_SPEED, SINGLE_MODEL_SEARCH_SPEED); /* Preprocess the search context. */ MmodPreprocess(MilSearchContext, M_DEFAULT); /* Draw box and position it in the source image to show the model. */ MgraColor(M_DEFAULT, ModelDrawColor); MmodDraw(M_DEFAULT, MilSearchContext, MilOverlayImage, M_DRAW_BOX+M_DRAW_POSITION, 0, M_ORIGINAL); /* Pause to show the model. */ printf("\nGEOMETRIC MODEL FINDER:\n"); printf("-----------------------\n\n"); printf("A model context was defined with the model in the displayed image.\n"); printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Load the single model target image. */ MbufLoad(SINGLE_MODEL_TARGET_IMAGE, MilImage); /* Dummy first find operation for better function timing accuracy (model cache effet,...). */ MmodFind(MilSearchContext, MilImage, MilResult); /* Reset the timer. */ MappTimer(M_TIMER_RESET+M_SYNCHRONOUS, M_NULL); /* Find the model. */ MmodFind(MilSearchContext, MilImage, MilResult); /* Read the find time. */ MappTimer(M_TIMER_READ+M_SYNCHRONOUS, &Time); /* Get the number of models found. */ MmodGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_LONG, &NumResults); /* If a model was found above the acceptance threshold. */ if ( (NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES) ) { /* Get the results of the single model. */ MmodGetResult(MilResult, M_DEFAULT, M_INDEX+M_TYPE_LONG, Model); MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition); MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition); MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle); MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale); MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score); /* Print the results for each model found. */ printf("The model was found in the target image:\n\n"); printf("Result Model X Position Y Position Angle Scale Score\n\n"); for (i=0; i to continue.\n\n"); getch(); /* Free MIL objects. */ MbufFree(MilImage); MmodFree(MilSearchContext); MmodFree(MilResult); } /***************************************************************************** /* Multiple models example. */ /* Source MIL image file specifications. */ #define MULTI_MODELS_IMAGE M_IMAGE_PATH MIL_TEXT("MultipleModel.mim") /* Target MIL image file specifications. */ #define MULTI_MODELS_TARGET_IMAGE M_IMAGE_PATH MIL_TEXT("MultipleTarget.mim") /* Search speed: M_VERY_HIGH for faster search, M_MEDIUM for precision and robustness. */ #define MULTI_MODELS_SEARCH_SPEED M_VERY_HIGH /* Number of models. */ #define NUMBER_OF_MODELS 3L #define MODELS_MAX_OCCURRENCES 16L /* Model 1 specifications. */ #define MODEL0_OFFSETX 34L #define MODEL0_OFFSETY 93L #define MODEL0_SIZEX 214L #define MODEL0_SIZEY 76L #define MODEL0_DRAW_COLOR M_COLOR_RED /* Model 2 specifications. */ #define MODEL1_OFFSETX 73L #define MODEL1_OFFSETY 232L #define MODEL1_SIZEX 150L #define MODEL1_SIZEY 154L #define MODEL1_REFERENCEX 23L #define MODEL1_REFERENCEY 127L #define MODEL1_DRAW_COLOR M_COLOR_GREEN /* Model 3 specifications. */ #define MODEL2_OFFSETX 308L #define MODEL2_OFFSETY 39L #define MODEL2_SIZEX 175L #define MODEL2_SIZEY 357L #define MODEL2_REFERENCEX 62L #define MODEL2_REFERENCEY 150L #define MODEL2_DRAW_COLOR M_COLOR_BLUE /* Models array specifications. */ #define MODELS_ARRAY_SIZE 3L #define MODELS_OFFSETX {MODEL0_OFFSETX, MODEL1_OFFSETX, MODEL2_OFFSETX} #define MODELS_OFFSETY {MODEL0_OFFSETY, MODEL1_OFFSETY, MODEL2_OFFSETY} #define MODELS_SIZEX {MODEL0_SIZEX, MODEL1_SIZEX, MODEL2_SIZEX} #define MODELS_SIZEY {MODEL0_SIZEY, MODEL1_SIZEY, MODEL2_SIZEY} #define MODELS_DRAW_COLOR {MODEL0_DRAW_COLOR, MODEL1_DRAW_COLOR, MODEL2_DRAW_COLOR} void MultipleModelsExample(MIL_ID MilSystem, MIL_ID MilDisplay) { MIL_ID MilImage, /* Image buffer identifier. */ MilOverlayImage; /* Overlay image. */ MIL_ID MilSearchContext, /* Search context */ MilResult; /* Result identifier. */ long Models[MODELS_MAX_OCCURRENCES], /* Model indices. */ ModelsOffsetX[MODELS_ARRAY_SIZE] = MODELS_OFFSETX, /* Model X offsets array. */ ModelsOffsetY[MODELS_ARRAY_SIZE] = MODELS_OFFSETY, /* Model Y offsets array. */ ModelsSizeX[MODELS_ARRAY_SIZE] = MODELS_SIZEX, /* Model X sizes array. */ ModelsSizeY[MODELS_ARRAY_SIZE] = MODELS_SIZEY; /* Model Y sizes array. */ long ModelsDrawColor[MODELS_ARRAY_SIZE]=MODELS_DRAW_COLOR, /* Model drawing colors. */ NumResults = 0L; /* Number of results found. */ double Score[MODELS_MAX_OCCURRENCES], /* Model correlation scores.*/ XPosition[MODELS_MAX_OCCURRENCES], /* Model X positions. */ YPosition[MODELS_MAX_OCCURRENCES], /* Model Y positions. */ Angle[MODELS_MAX_OCCURRENCES], /* Model occurrence angles. */ Scale[MODELS_MAX_OCCURRENCES], /* Model occurrence scales. */ Time = 0.0; /* Time variable. */ int i; /* Loop variable */ /* Restore the model image and display it. */ MbufRestore(MULTI_MODELS_IMAGE, MilSystem, &MilImage); MdispSelect(MilDisplay, MilImage); /* Prepare for overlay annotation. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Allocate a geometric model finder. */ MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext); /* Allocate a result buffer. */ MmodAllocResult(MilSystem, M_DEFAULT, &MilResult); /* Define the models. */ for (i=0; i1) /* Change the reference point of the second model. */ MmodControl(MilSearchContext, 1, M_REFERENCE_X, MODEL1_REFERENCEX); MmodControl(MilSearchContext, 1, M_REFERENCE_Y, MODEL1_REFERENCEY); #if (NUMBER_OF_MODELS>2) /* Change the reference point of the third model. */ MmodControl(MilSearchContext, 2, M_REFERENCE_X, MODEL2_REFERENCEX); MmodControl(MilSearchContext, 2, M_REFERENCE_Y, MODEL2_REFERENCEY); #endif #endif /* Preprocess the search context. */ MmodPreprocess(MilSearchContext, M_DEFAULT); /* Draw boxes and positions in the source image to identify the models. */ for (i=0; i to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Load the complex target image. */ MbufLoad(MULTI_MODELS_TARGET_IMAGE, MilImage); /* Dummy first find operation for better function timing accuracy (model cache effet,...). */ MmodFind(MilSearchContext, MilImage, MilResult); /* Reset the timer. */ MappTimer(M_TIMER_RESET+M_SYNCHRONOUS, M_NULL); /* Find the models. */ MmodFind(MilSearchContext, MilImage, MilResult); /* Read the find time. */ MappTimer(M_TIMER_READ+M_SYNCHRONOUS, &Time); /* Get the number of models found. */ MmodGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_LONG, &NumResults); /* If the models were found above the acceptance threshold. */ if( (NumResults >= 1) && (NumResults <= MODELS_MAX_OCCURRENCES) ) { /* Get the results for each model. */ MmodGetResult(MilResult, M_DEFAULT, M_INDEX+M_TYPE_LONG, Models); MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition); MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition); MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle); MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale); MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score); /* Print information about the target image. */ printf("The models were found in the target image although their is:\n"); printf(" Full rotation\n Small scale change\n Contrast variation\n"); printf(" Specular reflection\n Occlusion\n Multiple models\n"); printf(" Multiple occurrences\n\n"); /* Print the results for the found models. */ printf("Result Model X Position Y Position Angle Scale Score\n\n"); for (i=0; i to end.\n\n"); getch(); /* Free MIL objects. */ MbufFree(MilImage); MmodFree(MilSearchContext); MmodFree(MilResult); }