#include <mil.h>
void FixturedBeadExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void PredefinedBeadExample(MIL_ID MilSystem, MIL_ID MilDisplay);
#define USER_POSITION_COLOR M_COLOR_RED
#define USER_TEMPLATE_COLOR M_COLOR_CYAN
#define TRAINED_BEAD_WIDTH_COLOR M_RGB888(255, 128, 0)
#define MODEL_FINDER_COLOR M_COLOR_GREEN
#define COORDINATE_SYSTEM_COLOR M_RGB888(164, 164, 0)
#define RESULT_SEARCH_BOX_COLOR M_COLOR_CYAN
#define PASS_BEAD_WIDTH_COLOR M_COLOR_GREEN
#define PASS_BEAD_POSITION_COLOR M_COLOR_GREEN
#define FAIL_NOT_FOUND_COLOR M_COLOR_RED
#define FAIL_SMALL_WIDTH_COLOR M_RGB888(255, 128, 0)
#define FAIL_EDGE_OFFSET_COLOR M_COLOR_RED
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
FixturedBeadExample(MilSystem, MilDisplay);
PredefinedBeadExample(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define IMAGE_FILE_TRAINING M_IMAGE_PATH MIL_TEXT("BeadTraining.mim")
#define IMAGE_FILE_TARGET M_IMAGE_PATH MIL_TEXT("BeadTarget.mim")
#define NUMBER_OF_TRAINING_POINTS 13
static MIL_DOUBLE TrainingPointsX[NUMBER_OF_TRAINING_POINTS] =
{180, 280, 400, 430, 455, 415, 370, 275, 185, 125, 105, 130, 180};
static MIL_DOUBLE TrainingPointsY[NUMBER_OF_TRAINING_POINTS] =
{190, 215, 190, 200, 260, 330, 345, 310, 340, 305, 265, 200, 190};
#define MAX_ANGLE_CORRECTION_VALUE 20.0
#define MAX_DEVIATION_OFFSET_VALUE 25.0
#define WIDTH_DELTA_NEG_VALUE 2.0
#define MODEL_ORIGIN_X 145
#define MODEL_ORIGIN_Y 115
#define MODEL_SIZE_X 275
#define MODEL_SIZE_Y 60
void FixturedBeadExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilGraList,
MilImageTraining,
MilImageTarget,
MilBeadContext,
MilBeadResult,
MilModelFinderContext,
MilModelFinderResult,
MilFixturingOffset;
MIL_DOUBLE NominalWidth,
AvWidth,
GapCov,
MaxGap,
Score;
MbufRestore(IMAGE_FILE_TRAINING, MilSystem, &MilImageTraining);
MbufRestore(IMAGE_FILE_TARGET, MilSystem, &MilImageTarget);
MdispSelect(MilDisplay, MilImageTraining);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraList);
MosPrintf(MIL_TEXT("\nFIXTURED BEAD INSPECTION:\n"));
MosPrintf(MIL_TEXT("-------------------------\n\n"));
MosPrintf(MIL_TEXT("This program performs a bead inspection on a mechanical part.\n"));
MosPrintf(MIL_TEXT("In the first step, a bead template context is trained using an "));
MosPrintf(MIL_TEXT("image.\nIn the second step, a mechanical part, at an arbitrary "));
MosPrintf(MIL_TEXT("angle and with\na defective bead, is inspected.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbeadAlloc(MilSystem, M_DEFAULT, M_DEFAULT, &MilBeadContext);
MbeadAllocResult(MilSystem, M_DEFAULT, &MilBeadResult);
MbeadTemplate(MilBeadContext, M_ADD, M_DEFAULT, M_TEMPLATE_LABEL(1),
NUMBER_OF_TRAINING_POINTS, TrainingPointsX, TrainingPointsY, M_NULL, M_DEFAULT);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_TEMPLATE_INPUT_UNITS, M_WORLD);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_ANGLE_ACCURACY_MAX_DEVIATION,
MAX_ANGLE_CORRECTION_VALUE);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_OFFSET_MAX,
MAX_DEVIATION_OFFSET_VALUE);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_WIDTH_DELTA_NEG,
WIDTH_DELTA_NEG_VALUE);
MgraColor(M_DEFAULT, USER_TEMPLATE_COLOR);
MbeadDraw(M_DEFAULT, MilBeadContext, MilGraList, M_DRAW_POSITION_POLYLINE,
M_USER, M_ALL, M_ALL, M_DEFAULT);
MgraColor(M_DEFAULT, USER_POSITION_COLOR);
MbeadDraw(M_DEFAULT, MilBeadContext, MilGraList, M_DRAW_POSITION,
M_USER, M_ALL, M_ALL, M_DEFAULT);
MosPrintf(MIL_TEXT("The initial points specified by the user (in red) are\n"));
MosPrintf(MIL_TEXT("used to train the bead information from an image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
McalUniform(MilImageTraining, 0, 0, 1, 1, 0, M_DEFAULT);
MbeadTrain(MilBeadContext, MilImageTraining, M_DEFAULT);
MgraColor(M_DEFAULT, TRAINED_BEAD_WIDTH_COLOR);
MbeadDraw(M_DEFAULT, MilBeadContext, MilGraList, M_DRAW_WIDTH,
M_TRAINED, M_TEMPLATE_LABEL(1), M_ALL, M_DEFAULT);
MbeadInquire(MilBeadContext,M_TEMPLATE_LABEL(1), M_TRAINED_WIDTH_NOMINAL,
&NominalWidth);
MosPrintf(MIL_TEXT("The template has been trained and is displayed in orange.\n"));
MosPrintf(MIL_TEXT("Its nominal trained width is %.2f pixels.\n\n"), NominalWidth);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilModelFinderContext);
MmodAllocResult(MilSystem, M_DEFAULT, &MilModelFinderResult);
MmodDefine(MilModelFinderContext, M_IMAGE, MilImageTraining,
MODEL_ORIGIN_X, MODEL_ORIGIN_Y, MODEL_SIZE_X, MODEL_SIZE_Y);
MmodPreprocess(MilModelFinderContext, M_DEFAULT);
McalAlloc(MilSystem, M_FIXTURING_OFFSET, M_DEFAULT, &MilFixturingOffset);
McalFixture(M_NULL, MilFixturingOffset, M_LEARN_OFFSET, M_MODEL_MOD,
MilModelFinderContext, 0, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, MODEL_FINDER_COLOR);
MmodDraw(M_DEFAULT, MilModelFinderContext, MilGraList, M_DRAW_BOX+M_DRAW_POSITION,
M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("A Model Finder model (in green) is also defined to\n"));
MosPrintf(MIL_TEXT("further fixture the bead verification operation.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, MilGraList);
MdispSelect(MilDisplay, MilImageTarget);
MmodFind(MilModelFinderContext, MilImageTarget, MilModelFinderResult);
MgraColor(M_DEFAULT, MODEL_FINDER_COLOR);
MmodDraw(M_DEFAULT, MilModelFinderResult, MilGraList, M_DRAW_BOX+M_DRAW_POSITION,
M_DEFAULT, M_DEFAULT);
McalFixture(MilImageTarget, MilFixturingOffset, M_MOVE_RELATIVE, M_RESULT_MOD,
MilModelFinderResult, 0, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, COORDINATE_SYSTEM_COLOR);
McalDraw(M_DEFAULT, M_NULL, MilGraList, M_DRAW_RELATIVE_COORDINATE_SYSTEM,
M_DEFAULT, M_DEFAULT);
MbeadVerify(MilBeadContext, MilImageTarget, MilBeadResult, M_DEFAULT);
MgraColor(M_DEFAULT, RESULT_SEARCH_BOX_COLOR);
MbeadDraw(M_DEFAULT, MilBeadResult, MilGraList, M_DRAW_SEARCH_BOX, M_ALL,
M_ALL, M_ALL, M_DEFAULT);
MosPrintf(MIL_TEXT("The mechanical part's position and angle (in green) were "));
MosPrintf(MIL_TEXT("located\nusing Model Finder, and the bead's search boxes "));
MosPrintf(MIL_TEXT("(in cyan) were\npositioned accordingly.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, MilGraList);
MgraColor(M_DEFAULT, COORDINATE_SYSTEM_COLOR);
McalDraw(M_DEFAULT, M_NULL, MilGraList, M_DRAW_RELATIVE_COORDINATE_SYSTEM,
M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, PASS_BEAD_WIDTH_COLOR);
MbeadDraw(M_DEFAULT, MilBeadResult, MilGraList, M_DRAW_WIDTH, M_PASS,
M_TEMPLATE_LABEL(1), M_ALL, M_DEFAULT);
MgraColor(M_DEFAULT, FAIL_NOT_FOUND_COLOR);
MbeadDraw(M_DEFAULT, MilBeadResult, MilGraList, M_DRAW_SEARCH_BOX,
M_FAIL_NOT_FOUND, M_ALL, M_ALL, M_DEFAULT);
MgraColor(M_DEFAULT, FAIL_SMALL_WIDTH_COLOR);
MbeadDraw(M_DEFAULT, MilBeadResult, MilGraList, M_DRAW_SEARCH_BOX,
M_FAIL_WIDTH_MIN, M_TEMPLATE_LABEL(1), M_ALL, M_DEFAULT);
MbeadGetResult(MilBeadResult, M_TEMPLATE_LABEL(1), M_GENERAL, M_SCORE, &Score);
MbeadGetResult(MilBeadResult, M_TEMPLATE_LABEL(1), M_GENERAL, M_GAP_COVERAGE, &GapCov);
MbeadGetResult(MilBeadResult, M_TEMPLATE_LABEL(1), M_GENERAL, M_WIDTH_AVERAGE, &AvWidth);
MbeadGetResult(MilBeadResult, M_TEMPLATE_LABEL(1), M_GENERAL, M_GAP_MAX_LENGTH, &MaxGap);
MosPrintf(MIL_TEXT("The bead has been inspected:\n"));
MosPrintf(MIL_TEXT(" -Passing bead sections (green) cover %.2f%% of the bead\n"), Score);
MosPrintf(MIL_TEXT(" -Missing bead sections (red) cover %.2f%% of the bead\n"), GapCov);
MosPrintf(MIL_TEXT(" -Sections outside the specified tolerances are drawn in orange\n"));
MosPrintf(MIL_TEXT(" -The bead's average width is %.2f pixels\n"), AvWidth);
MosPrintf(MIL_TEXT(" -The bead's longest gap section is %.2f pixels\n"), MaxGap);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n"));
MosGetch();
MmodFree(MilModelFinderContext);
MmodFree(MilModelFinderResult);
MbeadFree(MilBeadContext);
MbeadFree(MilBeadResult);
McalFree(MilFixturingOffset);
MbufFree(MilImageTraining);
MbufFree(MilImageTarget);
MgraFree(MilGraList);
}
#define CAP_FILE_TARGET M_IMAGE_PATH MIL_TEXT("Cap.mim")
#define CIRCLE_CENTER_X 330.0
#define CIRCLE_CENTER_Y 230.0
#define CIRCLE_RADIUS 120.0
#define EDGE_THRESHOLD_VALUE 25.0
#define MAX_CONTOUR_DEVIATION_OFFSET 5.0
#define MAX_CONTOUR_FOUND_OFFSET 25.0
void PredefinedBeadExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilOverlayImage,
MilImageTarget,
MilBeadContext,
MilBeadResult;
MIL_DOUBLE MaximumOffset;
MbufRestore(CAP_FILE_TARGET, MilSystem, &MilImageTarget);
MdispSelect(MilDisplay, MilImageTarget);
MdispControl(MilDisplay, M_OVERLAY, M_ENABLE);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_TRANSPARENT_COLOR);
MosPrintf(MIL_TEXT("\nPREDEFINED BEAD INSPECTION:\n"));
MosPrintf(MIL_TEXT("---------------------------\n\n"));
MosPrintf(MIL_TEXT("This program performs a bead inspection of a bottle\n"));
MosPrintf(MIL_TEXT("cap's contour using a predefined circular bead.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbeadAlloc(MilSystem, M_DEFAULT, M_DEFAULT, &MilBeadContext);
MbeadAllocResult(MilSystem, M_DEFAULT, &MilBeadResult);
MbeadTemplate(MilBeadContext, M_ADD, M_BEAD_EDGE, M_TEMPLATE_LABEL(1),
0, M_NULL, M_NULL, M_NULL, M_DEFAULT);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_TRAINING_PATH, M_CIRCLE);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_TEMPLATE_CIRCLE_CENTER_X,
CIRCLE_CENTER_X);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_TEMPLATE_CIRCLE_CENTER_Y,
CIRCLE_CENTER_Y);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_TEMPLATE_CIRCLE_RADIUS,
CIRCLE_RADIUS);
MbeadControl(MilBeadContext, M_TEMPLATE_LABEL(1), M_THRESHOLD_VALUE,
EDGE_THRESHOLD_VALUE);
MbeadControl(MilBeadContext, M_ALL, M_WIDTH_NOMINAL_MODE, M_USER_DEFINED);
MbeadControl(MilBeadContext, M_ALL, M_FAIL_WARNING_OFFSET, MAX_CONTOUR_FOUND_OFFSET);
MbeadControl(MilBeadContext, M_ALL, M_OFFSET_MAX, MAX_CONTOUR_DEVIATION_OFFSET);
MgraColor(M_DEFAULT, USER_TEMPLATE_COLOR);
MbeadDraw(M_DEFAULT, MilBeadContext, MilOverlayImage, M_DRAW_POSITION,
M_USER, M_ALL, M_ALL, M_DEFAULT);
MbeadTrain(MilBeadContext, M_NULL, M_DEFAULT);
MgraColor(M_DEFAULT, TRAINED_BEAD_WIDTH_COLOR);
MbeadDraw(M_DEFAULT, MilBeadContext, MilOverlayImage, M_DRAW_SEARCH_BOX,
M_TRAINED, M_ALL, M_ALL, M_DEFAULT);
MosPrintf(MIL_TEXT("A circular template that was parametrically defined by the\n"));
MosPrintf(MIL_TEXT("user is displayed (in cyan). The template has been trained\n"));
MosPrintf(MIL_TEXT("and the resulting search is displayed (in orange).\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbeadVerify(MilBeadContext, MilImageTarget, MilBeadResult, M_DEFAULT);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_TRANSPARENT_COLOR);
MgraColor(M_DEFAULT, PASS_BEAD_POSITION_COLOR);
MbeadDraw(M_DEFAULT, MilBeadResult, MilOverlayImage, M_DRAW_POSITION, M_PASS,
M_ALL, M_ALL, M_DEFAULT);
MgraColor(M_DEFAULT, FAIL_EDGE_OFFSET_COLOR);
MbeadDraw(M_DEFAULT, MilBeadResult, MilOverlayImage, M_DRAW_POSITION, M_FAIL_OFFSET,
M_ALL, M_ALL, M_DEFAULT);
MbeadGetResult(MilBeadResult, M_TEMPLATE_LABEL(1), M_GENERAL, M_OFFSET_MAX,
&MaximumOffset);
MosPrintf(MIL_TEXT("The bottle cap shape has been inspected:\n"));
MosPrintf(MIL_TEXT(" -Sections outside the specified offset"));
MosPrintf(MIL_TEXT(" tolerance are drawn in red\n"));
MosPrintf(MIL_TEXT(" -The maximum offset value is %0.2f pixels.\n\n"), MaximumOffset);
MosPrintf(MIL_TEXT("Press <Enter> to terminate.\n"));
MosGetch();
MbeadFree(MilBeadContext);
MbeadFree(MilBeadResult);
MbufFree(MilImageTarget);
}