#include <mil.h>
#define RUN_SINGLE_MEASUREMENT_EXAMPLE M_YES
#define RUN_MULTIPLE_MEASUREMENT_EXAMPLE M_YES
#define RUN_FIXTURED_MEASUREMENT_EXAMPLE M_YES
void SingleMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void MultipleMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void FixturedMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MosPrintf(MIL_TEXT("\nMEASUREMENT MODULE:\n"));
MosPrintf(MIL_TEXT("-------------------\n\n"));
#if (RUN_SINGLE_MEASUREMENT_EXAMPLE)
SingleMeasurementExample(MilSystem, MilDisplay);
#endif
#if (RUN_MULTIPLE_MEASUREMENT_EXAMPLE)
MultipleMeasurementExample(MilSystem, MilDisplay);
#endif
#if (RUN_FIXTURED_MEASUREMENT_EXAMPLE)
FixturedMeasurementExample(MilSystem, MilDisplay);
#endif
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define MEAS_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("lead.mim")
#define MEAS_BOX_WIDTH 128
#define MEAS_BOX_HEIGHT 100
#define MEAS_BOX_POS_X 166
#define MEAS_BOX_POS_Y 130
#define STRIPE_POLARITY_LEFT M_POSITIVE
#define STRIPE_POLARITY_RIGHT M_NEGATIVE
#define STRIPE_WIDTH 45L
#define STRIPE_WIDTH_VARIATION 10L
void SingleMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
MilGraphicList,
StripeMarker;
MIL_DOUBLE StripeCenterX,
StripeCenterY,
StripeWidth,
StripeAngle;
MIL_DOUBLE CrossColor = M_COLOR_YELLOW,
BoxColor = M_COLOR_RED;
MbufRestore(MEAS_IMAGE_FILE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList);
MmeasAllocMarker(MilSystem, M_STRIPE, M_DEFAULT, &StripeMarker);
MmeasSetMarker(StripeMarker, M_POLARITY, STRIPE_POLARITY_LEFT, STRIPE_POLARITY_RIGHT);
MmeasSetScore(StripeMarker,
M_STRIPE_WIDTH_SCORE,
STRIPE_WIDTH - STRIPE_WIDTH_VARIATION,
STRIPE_WIDTH - STRIPE_WIDTH_VARIATION,
STRIPE_WIDTH + STRIPE_WIDTH_VARIATION,
STRIPE_WIDTH + STRIPE_WIDTH_VARIATION,
M_DEFAULT,
M_DEFAULT,
M_DEFAULT);
MmeasSetMarker(StripeMarker, M_BOX_ANGLE_MODE, M_ENABLE, M_NULL);
MmeasSetMarker(StripeMarker, M_BOX_ORIGIN, MEAS_BOX_POS_X, MEAS_BOX_POS_Y);
MmeasSetMarker(StripeMarker, M_BOX_SIZE, MEAS_BOX_WIDTH, MEAS_BOX_HEIGHT);
MgraColor(M_DEFAULT, BoxColor);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_SEARCH_REGION,
M_DEFAULT, M_MARKER);
MosPrintf(MIL_TEXT("Position, width and angle of the stripe ")
MIL_TEXT("in the highlighted box\n"));
MosPrintf(MIL_TEXT("will be calculated and the center "));
MosPrintf(MIL_TEXT("and edges will be marked.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraClear(M_DEFAULT, MilGraphicList);
MmeasFindMarker(M_DEFAULT, MilImage , StripeMarker, M_DEFAULT);
MmeasGetResult(StripeMarker, M_POSITION, &StripeCenterX, &StripeCenterY);
MmeasGetResult(StripeMarker, M_STRIPE_WIDTH, &StripeWidth, M_NULL);
MmeasGetResult(StripeMarker, M_ANGLE, &StripeAngle, M_NULL);
MgraColor(M_DEFAULT, BoxColor);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_SEARCH_REGION,
M_DEFAULT, M_RESULT);
MgraColor(M_DEFAULT, CrossColor);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_POSITION, M_DEFAULT,
M_RESULT);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList,
M_DRAW_POSITION+M_EDGE_FIRST+M_EDGE_SECOND, M_DEFAULT, M_RESULT);
MosPrintf(MIL_TEXT("The stripe in the image is at position %.2f,%.2f and\n"),
StripeCenterX, StripeCenterY);
MosPrintf(MIL_TEXT("is %.2f pixels wide with an angle of %.2f degrees.\n"),
StripeWidth, StripeAngle);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, M_NULL);
MgraFree(MilGraphicList);
MmeasFree(StripeMarker);
MbufFree(MilImage);
}
#define MULT_MEAS_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("chip.mim")
#define MULT_MEAS_BOX_WIDTH 230
#define MULT_MEAS_BOX_HEIGHT 7
#define MULT_MEAS_BOX_POS_X 220
#define MULT_MEAS_BOX_POS_Y 171
#define MULT_STRIPES_ORIENTATION M_VERTICAL
#define MULT_STRIPES_POLARITY_LEFT M_POSITIVE
#define MULT_STRIPES_POLARITY_RIGHT M_NEGATIVE
#define MULT_STRIPES_NUMBER 12
void MultipleMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
MilGraphicList,
StripeMarker;
MIL_DOUBLE MeanAngle,
MeanWidth,
MeanSpacing;
MIL_DOUBLE CrossColor = M_COLOR_YELLOW,
BoxColor = M_COLOR_RED;
MbufRestore(MULT_MEAS_IMAGE_FILE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList);
MmeasAllocMarker(MilSystem, M_STRIPE, M_DEFAULT, &StripeMarker);
MmeasSetMarker(StripeMarker, M_NUMBER, MULT_STRIPES_NUMBER, M_NULL);
MmeasSetMarker(StripeMarker, M_POLARITY, MULT_STRIPES_POLARITY_LEFT,
MULT_STRIPES_POLARITY_RIGHT);
MmeasSetMarker(StripeMarker, M_ORIENTATION, MULT_STRIPES_ORIENTATION, M_NULL);
MmeasSetMarker(StripeMarker, M_BOX_ORIGIN, MULT_MEAS_BOX_POS_X, MULT_MEAS_BOX_POS_Y);
MmeasSetMarker(StripeMarker, M_BOX_SIZE, MULT_MEAS_BOX_WIDTH, MULT_MEAS_BOX_HEIGHT);
MgraColor(M_DEFAULT, BoxColor);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_SEARCH_REGION,
M_DEFAULT, M_MARKER);
MosPrintf(MIL_TEXT("The position and angle of a row of pins on a chip ")
MIL_TEXT("will be calculated.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MmeasFindMarker(M_DEFAULT, MilImage, StripeMarker, M_POSITION + M_ANGLE + M_STRIPE_WIDTH);
MgraColor(M_DEFAULT, BoxColor);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_SEARCH_REGION,
M_DEFAULT, M_RESULT);
MgraColor(M_DEFAULT, CrossColor);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_POSITION, M_ALL, M_RESULT);
MmeasGetResult(StripeMarker, M_ANGLE + M_MEAN, &MeanAngle, M_NULL);
MmeasGetResult(StripeMarker, M_STRIPE_WIDTH + M_MEAN, &MeanWidth, M_NULL);
MmeasGetResult(StripeMarker, M_SPACING + M_MEAN, &MeanSpacing, M_NULL);
MosPrintf(MIL_TEXT("The center and angle of each pin have been marked.\n\n"));
MosPrintf(MIL_TEXT("The statistics for the pins are:\n"));
MosPrintf(MIL_TEXT("Average angle : %5.2f\n"), MeanAngle);
MosPrintf(MIL_TEXT("Average width : %5.2f\n"), MeanWidth);
MosPrintf(MIL_TEXT("Average spacing : %5.2f\n"), MeanSpacing);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, M_NULL);
MgraFree(MilGraphicList);
MmeasFree(StripeMarker);
MbufFree(MilImage);
}
#define FIXTURED_MEAS_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Fuse.mim")
#define FIXTURED_MEAS_BOX_OFFSET_X 400
#define FIXTURED_MEAS_BOX_OFFSET_Y 290
#define FIXTURED_MEAS_BOX_WIDTH 100
#define FIXTURED_MEAS_BOX_HEIGHT 15
#define FIXTURED_MODEL_OFFSET_X 395
#define FIXTURED_MODEL_OFFSET_Y 200
#define FIXTURED_MODEL_SIZE_X 110
#define FIXTURED_MODEL_SIZE_Y 120
#define FIXTURED_IMAGE_SIZE_X 512
#define FIXTURED_IMAGE_SIZE_Y 384
#define FIXTURED_STRIPE_POLARITY_LEFT M_POSITIVE
#define FIXTURED_STRIPE_POLARITY_RIGHT M_OPPOSITE
void FixturedMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilSourceImage,
MilImage,
MilModContext,
MilModResult,
MilFixturingOffset,
StripeMarker;
MIL_ID MilGraphicList;
MIL_DOUBLE StripeWidth,
PositionX,
PositionY;
MIL_INT SizeX,
SizeY,
NbOccurrences,
NbStripes,
Index;
MIL_TEXT_CHAR TextIndex[128];
MbufRestore(FIXTURED_MEAS_IMAGE_FILE, MilSystem, &MilSourceImage);
MbufInquire(MilSourceImage, M_SIZE_X, &SizeX);
MbufInquire(MilSourceImage, M_SIZE_Y, &SizeY);
MbufAlloc2d(MilSystem, SizeX, SizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &MilImage);
MimConvert(MilSourceImage, MilImage, M_RGB_TO_L);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicList);
MdispSelect(MilDisplay, MilImage);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList);
MmeasAllocMarker(MilSystem, M_STRIPE, M_DEFAULT, &StripeMarker);
MmeasSetMarker(StripeMarker, M_SEARCH_REGION_INPUT_UNITS, M_WORLD, M_NULL);
McalUniform(MilImage, 0.0, 0.0, 1.0, 1.0, 0.0, M_DEFAULT);
MmeasSetMarker(StripeMarker, M_BOX_ORIGIN, FIXTURED_MEAS_BOX_OFFSET_X,
FIXTURED_MEAS_BOX_OFFSET_Y);
MmeasSetMarker(StripeMarker, M_POLARITY, FIXTURED_STRIPE_POLARITY_LEFT,
FIXTURED_STRIPE_POLARITY_RIGHT);
MmeasSetMarker(StripeMarker, M_SEARCH_REGION_CLIPPING, M_ENABLE, M_NULL);
MmeasSetMarker(StripeMarker, M_BOX_SIZE, FIXTURED_MEAS_BOX_WIDTH,
FIXTURED_MEAS_BOX_HEIGHT);
MgraColor(M_DEFAULT, M_COLOR_BLUE);
MgraControl(M_DEFAULT, M_INPUT_UNITS, M_WORLD);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_SEARCH_REGION,
M_DEFAULT, M_MARKER);
MgraControl(M_DEFAULT, M_INPUT_UNITS, M_PIXEL);
MosPrintf(MIL_TEXT("A measurement stripe marker (in blue) is defined.\n"));
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilModContext);
MmodAllocResult(MilSystem, M_DEFAULT, &MilModResult);
MmodDefine(MilModContext, M_IMAGE, MilImage,
FIXTURED_MODEL_OFFSET_X, FIXTURED_MODEL_OFFSET_Y,
FIXTURED_MODEL_SIZE_X, FIXTURED_MODEL_SIZE_Y);
MmodControl(MilModContext, M_DEFAULT, M_NUMBER, M_ALL);
MmodControl(MilModContext, M_CONTEXT, M_SPEED, M_VERY_HIGH);
MmodPreprocess(MilModContext, M_DEFAULT);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MmodDraw(M_DEFAULT, MilModContext, MilGraphicList, M_DRAW_BOX+M_DRAW_POSITION,
M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("A Model Finder model (in green) is defined to\n"));
MosPrintf(MIL_TEXT("further fixture the measurement operation.\n\n"));
MosPrintf(MIL_TEXT("The stripe marker determines the gap between\n"));
MosPrintf(MIL_TEXT("the fuse connectors. Model Finder tracks the\n"));
MosPrintf(MIL_TEXT("fuses while the attached fixturing automatically\n"));
MosPrintf(MIL_TEXT("relocates the marker relative to the found fuses.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
McalAlloc(MilSystem, M_FIXTURING_OFFSET, M_DEFAULT, &MilFixturingOffset);
McalFixture(M_NULL, MilFixturingOffset, M_LEARN_OFFSET, M_MODEL_MOD,
MilModContext, 0, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MmodFind(MilModContext, MilImage, MilModResult);
MgraClear(M_DEFAULT, MilGraphicList);
MmodDraw(M_DEFAULT, MilModResult, MilGraphicList, M_DRAW_POSITION+M_DRAW_BOX,
M_DEFAULT, M_DEFAULT);
MmodGetResult(MilModResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NbOccurrences);
MosPrintf(MIL_TEXT("Locating the parts: %d occurrences are found.\n"), NbOccurrences);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MosPrintf(MIL_TEXT("The measurement tool is moved relative to each piece.\n"));
MosPrintf(MIL_TEXT("A graphic list is used to display the results with\n"));
MosPrintf(MIL_TEXT("subpixel annotations.\n\n"));
MgraClear(M_DEFAULT, MilGraphicList);
for(Index=0; Index<NbOccurrences; Index++)
{
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MmodDraw(M_DEFAULT, MilModResult, MilGraphicList, M_DRAW_POSITION,
Index, M_DEFAULT);
MmodGetResult(MilModResult, Index, M_POSITION_X+M_TYPE_MIL_DOUBLE, &PositionX);
MmodGetResult(MilModResult, Index, M_POSITION_Y+M_TYPE_MIL_DOUBLE, &PositionY);
MosSprintf(TextIndex, 128, MIL_TEXT("%d"), Index);
MgraText(M_DEFAULT, MilGraphicList, PositionX-20, PositionY, TextIndex);
McalFixture(MilImage, MilFixturingOffset, M_MOVE_RELATIVE, M_RESULT_MOD,
MilModResult, Index, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MmeasFindMarker(M_DEFAULT, MilImage, StripeMarker, M_POSITION+M_STRIPE_WIDTH);
MmeasGetResult(StripeMarker, M_NUMBER+M_TYPE_MIL_INT, &NbStripes, M_NULL);
if(NbStripes==1)
{
MmeasGetResult(StripeMarker, M_STRIPE_WIDTH, &StripeWidth, M_NULL);
MgraColor(M_DEFAULT, M_COLOR_BLUE);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_SEARCH_REGION,
M_DEFAULT, M_RESULT);
MgraColor(M_DEFAULT, M_COLOR_RED);
MmeasDraw(M_DEFAULT, StripeMarker, MilGraphicList, M_DRAW_WIDTH,
M_DEFAULT, M_RESULT);
MosPrintf(MIL_TEXT("The gap (in red) of occurrence %d is %.2f pixels wide.\n"),
Index, StripeWidth);
}
else
{
MosPrintf(MIL_TEXT("The gap of occurrence %d could not be measured.\n"), Index);
}
}
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
MgraFree(MilGraphicList);
MmeasFree(StripeMarker);
MmodFree(MilModContext);
MmodFree(MilModResult);
McalFree(MilFixturingOffset);
MbufFree(MilImage);
MbufFree(MilSourceImage);
}