/*****************************************************************************/ /* * File name: MMeas.c * * Synopsis: This program consist of 2 examples that use the Measurement module * to calculate the position, width and angle of objects in an image. * The first one measures the position, width and angle of a stripe * in an image, and marks its center and edges. The second one measures * the average position, width and angle of a row of pins on a chip. */ #include #include /* Example selection. */ #define RUN_SINGLE_MEASUREMENT_EXAMPLE M_YES #define RUN_MULTIPLE_MEASUREMENT_EXAMPLE M_YES /* Example functions declarations. */ void SingleMeasurementExample(MIL_ID MilSystem, MIL_ID MilDisplay); void MultipleMeasurementExample(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); /* Print module name. */ printf("\nMEASUREMENT MODULE:\n"); printf("-------------------\n\n"); #if (RUN_SINGLE_MEASUREMENT_EXAMPLE) SingleMeasurementExample(MilSystem, MilDisplay); #endif #if (RUN_MULTIPLE_MEASUREMENT_EXAMPLE) MultipleMeasurementExample(MilSystem, MilDisplay); #endif /* Free defaults. */ MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); } /***************************************************************************** Single measurement example. *****************************************************************************/ /* Source MIL image file specification. */ #define MEAS_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Lead.mim") /* Processing region specification. */ #define MEAS_BOX_WIDTH 128 #define MEAS_BOX_HEIGHT 100 #define MEAS_BOX_POS_X 166 #define MEAS_BOX_POS_Y 130 /* Target stripe typical specifications. */ #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, /* Image buffer identifier. */ MilOverlayImage, /* Overlay image. */ StripeMarker; /* Stripe marker identifier.*/ double StripeCenterX, /* Stripe X center position.*/ StripeCenterY, /* Stripe Y center position.*/ StripeWidth, /* Stripe width. */ StripeAngle; /* Stripe angle. */ long CrossColor = M_COLOR_YELLOW, /* Cross drawing color. */ BoxColor = M_COLOR_RED; /* Box drawing color. */ /* Restore and display the source image. */ MbufRestore(MEAS_IMAGE_FILE, MilSystem, &MilImage); MdispSelect(MilDisplay, MilImage); /* Prepare for overlay annotation. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Allocate a stripe marker. */ MmeasAllocMarker(MilSystem, M_STRIPE, M_DEFAULT, &StripeMarker); /* Specify the stripe characteristics. */ MmeasSetMarker(StripeMarker, M_POLARITY, STRIPE_POLARITY_LEFT, STRIPE_POLARITY_RIGHT); MmeasSetMarker(StripeMarker, M_WIDTH, STRIPE_WIDTH, M_NULL); MmeasSetMarker(StripeMarker, M_WIDTH_VARIATION, STRIPE_WIDTH_VARIATION, M_NULL); MmeasSetMarker(StripeMarker, M_BOX_ANGLE_MODE, M_ENABLE, M_NULL); /* Specify the search box size and position. */ MmeasSetMarker(StripeMarker, M_BOX_ORIGIN, MEAS_BOX_POS_X, MEAS_BOX_POS_Y); MmeasSetMarker(StripeMarker, M_BOX_SIZE, MEAS_BOX_WIDTH, MEAS_BOX_HEIGHT); /* Draw the contour of the measurement box. */ MgraColor(M_DEFAULT, BoxColor); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_BOX, M_DEFAULT, M_MARKER); /* Pause to show the original image. */ printf("Position, width and angle of the stripe in the highlighted box\n"); printf("will be calculated and the center and edges will be marked.\n"); printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Find the stripe and measure its width and angle. */ MmeasFindMarker(M_DEFAULT, MilImage , StripeMarker, M_DEFAULT); /* Get the stripe position, width and angle. */ MmeasGetResult(StripeMarker, M_POSITION, &StripeCenterX, &StripeCenterY); MmeasGetResult(StripeMarker, M_WIDTH, &StripeWidth, M_NULL); MmeasGetResult(StripeMarker, M_ANGLE, &StripeAngle, M_NULL); /* Draw the contour of the found measurement box. */ MgraColor(M_DEFAULT, BoxColor); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_BOX, M_DEFAULT, M_RESULT); /* Draw a cross on the center, left edge and right edge of the found stripe. */ MgraColor(M_DEFAULT, CrossColor); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_POSITION, M_DEFAULT, M_RESULT); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_POSITION+M_EDGE_FIRST+M_EDGE_SECOND, M_DEFAULT, M_RESULT); /* Print the result. */ printf("The stripe in the image is at position %.2f,%.2f and\n", StripeCenterX, StripeCenterY); printf("is %.2f pixels wide with an angle of %.2f degrees.\n", StripeWidth, StripeAngle); printf("Press to continue.\n\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Free all allocations. */ MmeasFree(StripeMarker); MbufFree(MilImage); } /***************************************************************************** Multiple measurement example. *****************************************************************************/ /* Source MIL image file specification. */ #define MULT_MEAS_IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Chip.mim") /* Processing region specification. */ #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 /* Target stripe specifications. */ #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, /* Image buffer identifier. */ MilOverlayImage, /* Overlay image. */ StripeMarker; /* Stripe marker identifier. */ double MeanAngle, /* Stripe mean angle. */ MeanWidth, /* Stripe mean width. */ MeanSpacing; /* Stripe mean spacing. */ long CrossColor = M_COLOR_YELLOW, /* Cross drawing color. */ BoxColor = M_COLOR_RED; /* Box drawing color. */ /* Restore and display the source image. */ MbufRestore(MULT_MEAS_IMAGE_FILE, 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 stripe marker. */ MmeasAllocMarker(MilSystem, M_STRIPE, M_DEFAULT, &StripeMarker); /* Specify the stripe characteristics. */ 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); /* Specify the measurement box size and position. */ 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); /* Draw the contour of the measurement box. */ MgraColor(M_DEFAULT, BoxColor); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_BOX, M_DEFAULT, M_MARKER); /* Pause to show the original image. */ printf("The position and angle of a row of pins on a chip will be calculated.\n"); printf("Press to continue.\n\n"); getch(); /* Find the stripe and measure its width and angle. */ MmeasFindMarker(M_DEFAULT, MilImage, StripeMarker, M_POSITION + M_ANGLE + M_WIDTH); /* Draw the contour of the measurement box. */ MgraColor(M_DEFAULT, BoxColor); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_BOX, M_DEFAULT, M_RESULT); /* Draw a cross at the center of each stripe found. */ MgraColor(M_DEFAULT, CrossColor); MmeasDraw(M_DEFAULT, StripeMarker, MilOverlayImage, M_DRAW_POSITION, M_ALL, M_RESULT); /* Get the stripe's width, angle and spacing. */ MmeasGetResult(StripeMarker, M_ANGLE + M_MEAN, &MeanAngle, M_NULL); MmeasGetResult(StripeMarker, M_WIDTH + M_MEAN, &MeanWidth, M_NULL); MmeasGetResult(StripeMarker, M_SPACING + M_MEAN, &MeanSpacing, M_NULL); /* Print the results. */ printf("The center and angle of each pin have been marked.\n\n"); printf("The statistics for the pins are:\n"); printf("Average angle : %5.2f\n", MeanAngle); printf("Average width : %5.2f\n", MeanWidth); printf("Average spacing : %5.2f\n\n", MeanSpacing); printf("Press to end.\n"); getch(); /* Clear the overlay image. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Free all allocations. */ MmeasFree(StripeMarker); MbufFree(MilImage); }