#include <mil.h>
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n"));
MosPrintf(MIL_TEXT("CircleShapeFinder\n\n"));
MosPrintf(MIL_TEXT("[SYNOPSIS]\n"));
MosPrintf(MIL_TEXT("This example uses circle finder to define circle models and search for circles\n"));
MosPrintf(MIL_TEXT("in target images. A simple circle finder example is presented first (multiple\n"));
MosPrintf(MIL_TEXT("occurrences and a small radius range with good search conditions), followed by\n"));
MosPrintf(MIL_TEXT("more complex examples (multiple occurrences and a large radius range in a\n"));
MosPrintf(MIL_TEXT("complex scene with bad search conditions).\n\n"));
MosPrintf(MIL_TEXT("[MODULES USED]\n"));
MosPrintf(MIL_TEXT("Modules used: application, system, display,\n calibration, geometric model finder.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
void SimpleCircleSearchExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void ComplexCircleSearchExample1(MIL_ID MilSystem, MIL_ID MilDisplay);
void ComplexCircleSearchExample2(MIL_ID MilSystem, MIL_ID MilDisplay);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
PrintHeader();
SimpleCircleSearchExample(MilSystem, MilDisplay);
ComplexCircleSearchExample1(MilSystem, MilDisplay);
ComplexCircleSearchExample2(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define SIMPLE_CIRCLE_SEARCH_TARGET_IMAGE M_IMAGE_PATH MIL_TEXT("/CircleShapeFinder/SimpleCircleSearchTarget.mim")
#define NUMBER_OF_MODELS 18L
#define MODEL_RADIUS 30.0
#define MODEL_MAX_OCCURRENCES 23L
void SimpleCircleSearchExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
GraphicList;
MIL_ID MilSearchContext,
MilResult;
MIL_DOUBLE PositionDrawColor = M_COLOR_RED;
MIL_DOUBLE ModelDrawColor = M_COLOR_GREEN;
MIL_DOUBLE BoxDrawColor = M_COLOR_BLUE;
MIL_INT NumResults = 0L;
MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES],
XPosition[MODEL_MAX_OCCURRENCES],
YPosition[MODEL_MAX_OCCURRENCES],
Radius[MODEL_MAX_OCCURRENCES],
Time = 0.0;
int i;
MbufRestore(SIMPLE_CIRCLE_SEARCH_TARGET_IMAGE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MmodAlloc(MilSystem, M_SHAPE_CIRCLE, M_DEFAULT, &MilSearchContext);
MmodAllocResult(MilSystem, M_SHAPE_CIRCLE, &MilResult);
MmodDefine(MilSearchContext, M_DEFAULT, M_DEFAULT, MODEL_RADIUS,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
MmodControl(MilSearchContext, 0, M_NUMBER, NUMBER_OF_MODELS);
MmodPreprocess(MilSearchContext, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER + M_TYPE_MIL_INT, &NumResults);
MosPrintf(MIL_TEXT("\nUsing circle finder in a simple situation:\n"));
MosPrintf(MIL_TEXT("------------------------------------------\n\n"));
MosPrintf(MIL_TEXT("A circle model was defined with "));
MosPrintf(MIL_TEXT("a nominal radius of %-3.1f%.\n\n"), MODEL_RADIUS);
if ((NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES))
{
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_RADIUS, Radius);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
MosPrintf(MIL_TEXT("The circles were found in the target image:\n\n"));
MosPrintf(MIL_TEXT("Result X-Position Y-Position Radius Score\n\n"));
for (i = 0; i<NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-13.2f%-13.2f%-8.2f%-5.2f%%\n"), i, XPosition[i],
YPosition[i], Radius[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time was %.1f ms.\n\n"), Time*1000.0);
MgraColor(M_DEFAULT, PositionDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, BoxDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_BOX, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_EDGES, M_DEFAULT, M_DEFAULT);
}
else
{
MosPrintf(MIL_TEXT("The model was not found or the number of models ")
MIL_TEXT("found is greater than\n"));
MosPrintf(MIL_TEXT("the specified maximum number of occurrence !\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraFree(GraphicList);
MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}
#define COMPLEX_CIRCLE_SEARCH_TARGET_IMAGE_1 M_IMAGE_PATH MIL_TEXT("/CircleShapeFinder/ComplexCircleSearchTarget1.mim")
#define NUMBER_OF_MODELS_1 4L
#define MODEL_RADIUS_1 100.0
#define SMOOTHNESS_VALUE_1 75.0
#define MIN_SCALE_FACTOR_VALUE_1 0.1
void ComplexCircleSearchExample1(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
GraphicList;
MIL_ID MilSearchContext,
MilResult;
MIL_DOUBLE PositionDrawColor = M_COLOR_RED;
MIL_DOUBLE ModelDrawColor = M_COLOR_GREEN;
MIL_INT NumResults = 0L;
MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES],
XPosition[MODEL_MAX_OCCURRENCES],
YPosition[MODEL_MAX_OCCURRENCES],
Radius[MODEL_MAX_OCCURRENCES],
Time = 0.0;
int i;
MbufRestore(COMPLEX_CIRCLE_SEARCH_TARGET_IMAGE_1, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MmodAlloc(MilSystem, M_SHAPE_CIRCLE, M_DEFAULT, &MilSearchContext);
MmodAllocResult(MilSystem, M_SHAPE_CIRCLE, &MilResult);
MmodDefine(MilSearchContext, M_DEFAULT, M_DEFAULT, MODEL_RADIUS_1,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
MmodControl(MilSearchContext, M_CONTEXT, M_DETAIL_LEVEL, M_VERY_HIGH);
MmodControl(MilSearchContext, M_CONTEXT, M_SMOOTHNESS, SMOOTHNESS_VALUE_1);
MmodControl(MilSearchContext, 0, M_SCALE_MIN_FACTOR, MIN_SCALE_FACTOR_VALUE_1);
MmodControl(MilSearchContext, M_DEFAULT, M_NUMBER, NUMBER_OF_MODELS_1);
MmodPreprocess(MilSearchContext, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER + M_TYPE_MIL_INT, &NumResults);
MosPrintf(MIL_TEXT("\nUsing circle finder in a complex situation:\n"));
MosPrintf(MIL_TEXT("-------------------------------------------\n\n"));
MosPrintf(MIL_TEXT("A circle model was defined with "));
MosPrintf(MIL_TEXT("a nominal radius of %-3.1f%.\n\n"), MODEL_RADIUS_1);
if ((NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES))
{
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_RADIUS, Radius);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
MosPrintf(MIL_TEXT("The circles were found in the target "));
MosPrintf(MIL_TEXT("image, despite the following complexities:\n "));
MosPrintf(MIL_TEXT("\t. High scale range\n"));
MosPrintf(MIL_TEXT("\t. Low contrast\n"));
MosPrintf(MIL_TEXT("\t. Noisy edges\n\n"));
MosPrintf(MIL_TEXT("Result X-Position Y-Position Radius Score\n\n"));
for (i = 0; i<NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-13.2f%-13.2f%-8.2f%-5.2f%%\n"),
i, XPosition[i], YPosition[i], Radius[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time was %.1f ms.\n\n"), Time*1000.0);
MgraColor(M_DEFAULT, PositionDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_EDGES, M_DEFAULT, M_DEFAULT);
}
else
{
MosPrintf(MIL_TEXT("The circles were not found or the number of ")
MIL_TEXT("circles found is greater than\n"));
MosPrintf(MIL_TEXT("the defined value of maximum occurrences !\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraFree(GraphicList);
MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}
#define COMPLEX_CIRCLE_SEARCH_TARGET_IMAGE_2 M_IMAGE_PATH MIL_TEXT("/CircleShapeFinder/ComplexCircleSearchTarget2.mim")
#define COMPLEX_CIRCLE_SEARCH_CALIBRATION_2 M_IMAGE_PATH MIL_TEXT("/CircleShapeFinder/ComplexCircleSearchCalibration2.mca")
#define NUMBER_OF_MODELS_2 23L
#define MODEL_RADIUS_2 1.0
#define SMOOTHNESS_VALUE_2 65.0
#define ACCEPTANCE_VALUE_2 50.0
#define MIN_SEPARATION_SCALE_VALUE_2 1.5
#define MIN_SEPARATION_XY_VALUE_2 30.0
void ComplexCircleSearchExample2(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
MilCalibration,
GraphicList;
MIL_ID MilSearchContext,
MilResult;
MIL_DOUBLE PositionDrawColor = M_COLOR_RED;
MIL_DOUBLE ModelDrawColor = M_COLOR_GREEN;
MIL_INT NumResults = 0L;
MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES],
XPosition[MODEL_MAX_OCCURRENCES],
YPosition[MODEL_MAX_OCCURRENCES],
Radius[MODEL_MAX_OCCURRENCES],
Time = 0.0;
int i;
MbufRestore(COMPLEX_CIRCLE_SEARCH_TARGET_IMAGE_2, MilSystem, &MilImage);
McalRestore(COMPLEX_CIRCLE_SEARCH_CALIBRATION_2, MilSystem, M_DEFAULT, &MilCalibration);
McalAssociate(MilCalibration, MilImage, M_DEFAULT);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MmodAlloc(MilSystem, M_SHAPE_CIRCLE, M_DEFAULT, &MilSearchContext);
MmodAllocResult(MilSystem, M_SHAPE_CIRCLE, &MilResult);
MmodDefine(MilSearchContext, M_DEFAULT, M_DEFAULT, MODEL_RADIUS_2,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
MmodControl(MilSearchContext, M_CONTEXT, M_DETAIL_LEVEL, M_VERY_HIGH);
MmodControl(MilSearchContext, M_CONTEXT, M_SMOOTHNESS, SMOOTHNESS_VALUE_2);
MmodControl(MilSearchContext, M_DEFAULT, M_ACCEPTANCE, ACCEPTANCE_VALUE_2);
MmodControl(MilSearchContext, 0, M_MIN_SEPARATION_SCALE, MIN_SEPARATION_SCALE_VALUE_2);
MmodControl(MilSearchContext, 0, M_MIN_SEPARATION_X, MIN_SEPARATION_XY_VALUE_2);
MmodControl(MilSearchContext, 0, M_MIN_SEPARATION_Y, MIN_SEPARATION_XY_VALUE_2);
MmodControl(MilSearchContext, 0, M_POLARITY, M_REVERSE);
MmodControl(MilSearchContext, M_DEFAULT, M_NUMBER, NUMBER_OF_MODELS_2);
MmodPreprocess(MilSearchContext, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
MmodFind(MilSearchContext, MilImage, MilResult);
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER + M_TYPE_MIL_INT, &NumResults);
MosPrintf(MIL_TEXT("\nUsing circle finder with a calibrated target:\n"));
MosPrintf(MIL_TEXT("---------------------------------------------\n\n"));
MosPrintf(MIL_TEXT("A circle model was defined with "));
MosPrintf(MIL_TEXT("a nominal radius of %-3.1f%.\n\n"), MODEL_RADIUS_2);
if ((NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES))
{
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_RADIUS, Radius);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
MosPrintf(MIL_TEXT("The circles were found in the calibrated target "));
MosPrintf(MIL_TEXT("image, despite the following\ncomplexities:\n"));
MosPrintf(MIL_TEXT("\t. Occlusion.\n"));
MosPrintf(MIL_TEXT("\t. Low contrast.\n"));
MosPrintf(MIL_TEXT("\t. Noisy edges.\n\n"));
MosPrintf(MIL_TEXT("Result X-Position Y-Position Radius Score\n\n"));
for (i = 0; i<NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-13.2f%-13.2f%-8.2f%-5.2f%%\n"),
i, XPosition[i], YPosition[i], Radius[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time was %.1f ms.\n\n"), Time*1000.0);
MgraColor(M_DEFAULT, PositionDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList, M_DRAW_EDGES, M_DEFAULT, M_DEFAULT);
}
else
{
MosPrintf(MIL_TEXT("The circles were not found or the number of ")
MIL_TEXT("circles found is greater than\n"));
MosPrintf(MIL_TEXT("the defined value of maximum occurrences !\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
McalFree(MilCalibration);
MgraFree(GraphicList);
MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}