/*****************************************************************************/ /* * File name: MEdge.c * * Synopsis: This program uses the MIL Edge Finder module to find and measure * the outer diameter of good seals in a target image. */ #include #include /* Source MIL image file specifications. */ #define CONTOUR_IMAGE M_IMAGE_PATH MIL_TEXT("Seals.mim") #define CONTOUR_MAX_RESULTS 100 #define CONTOUR_MAXIMUM_ELONGATION 0.8 #define CONTOUR_DRAW_COLOR M_COLOR_GREEN #define CONTOUR_LABEL_COLOR M_COLOR_RED /* Main function. */ void main(void) { MIL_ID MilApplication, /* Application identifier. */ MilSystem, /* System Identifier. */ MilDisplay, /* Display identifier. */ MilImage, /* Image buffer identifier. */ MilOverlayImage, /* Overlay image. */ MilEdgeContext, /* Edge context. */ MilEdgeResult; /* Edge result identifier. */ long EdgeDrawColor = CONTOUR_DRAW_COLOR, /* Edge draw color. */ LabelDrawColor = CONTOUR_LABEL_COLOR, /* Text draw color. */ NumEdgeFound = 0L, /* Number of edges found. */ NumResults = 0L, /* Number of results found. */ i; /* Loop variable. */ double MeanFeretDiameter[CONTOUR_MAX_RESULTS]; /* Edge mean Feret diameter. */ /* Allocate defaults. */ MappAllocDefault(M_SETUP, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL); /* Restore the image and display it. */ MbufRestore(CONTOUR_IMAGE, MilSystem, &MilImage); MdispSelect(MilDisplay, MilImage); /* Prepare for overlay annotations. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Pause to show the original image. */ printf("\nEDGE MODULE:\n"); printf("------------\n\n"); printf("This program determines the outer seal diameters in the displayed image \n"); printf("by detecting and analyzing contours with the Edge Finder module.\n"); printf("Press to continue.\n\n"); getch(); /* Allocate a Edge Finder context. */ MedgeAlloc(MilSystem, M_CONTOUR, M_DEFAULT, &MilEdgeContext); /* Allocate a result buffer. */ MedgeAllocResult(MilSystem, M_DEFAULT, &MilEdgeResult); /* Enable features to compute. */ MedgeControl(MilEdgeContext, M_MOMENT_ELONGATION, M_ENABLE); MedgeControl(MilEdgeContext, M_FERET_MEAN_DIAMETER+M_SORT1_DOWN, M_ENABLE); /* Calculate edges and features. */ MedgeCalculate(MilEdgeContext, MilImage, M_NULL, M_NULL, M_NULL, MilEdgeResult, M_DEFAULT); /* Get the number of edges found. */ MedgeGetResult(MilEdgeResult, M_DEFAULT, M_NUMBER_OF_CHAINS+M_TYPE_LONG, &NumEdgeFound, M_NULL); /* Draw edges in the source image to show the result. */ MgraColor(M_DEFAULT, EdgeDrawColor); MedgeDraw(M_DEFAULT, MilEdgeResult, MilOverlayImage, M_DRAW_EDGES, M_DEFAULT, M_DEFAULT); /* Pause to show the edges. */ printf("%d edges were found in the image.\n", NumEdgeFound); printf("Press to continue.\n\n"); getch(); /* Exclude elongated edges. */ MedgeSelect(MilEdgeResult, M_EXCLUDE, M_MOMENT_ELONGATION, M_LESS, CONTOUR_MAXIMUM_ELONGATION, M_NULL); /* Exclude inner chains. */ MedgeSelect(MilEdgeResult, M_EXCLUDE, M_INCLUDED_EDGES, M_INSIDE_BOX, M_NULL, M_NULL); /* Draw remaining edges and their index to show the result. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); MgraColor(M_DEFAULT, EdgeDrawColor); MedgeDraw(M_DEFAULT, MilEdgeResult, MilOverlayImage, M_DRAW_EDGES, M_DEFAULT, M_DEFAULT); /* Pause to show the results. */ printf("Elongated edges and inner edges of each seal were removed.\n"); printf("Press to continue.\n\n"); getch(); /* Get the number of edges found. */ MedgeGetResult(MilEdgeResult, M_DEFAULT, M_NUMBER_OF_CHAINS+M_TYPE_LONG, &NumResults, M_NULL); /* If the right number of edges were found. */ if ( (NumResults >= 1) && (NumResults <= CONTOUR_MAX_RESULTS) ) { /* Draw the index of each edge. */ MgraColor(M_DEFAULT, LabelDrawColor); MedgeDraw(M_DEFAULT, MilEdgeResult, MilOverlayImage, M_DRAW_INDEX, M_DEFAULT, M_DEFAULT); /* Get the mean Feret diameters. */ MedgeGetResult(MilEdgeResult, M_DEFAULT, M_FERET_MEAN_DIAMETER, &MeanFeretDiameter, M_NULL); /* Print the results. */ printf("Mean diameter of the %ld outer edges are:\n\n", NumResults); printf("Index Mean diameter \n"); for (i=0; i to end.\n"); getch(); /* Free MIL objects. */ MbufFree(MilImage); MedgeFree(MilEdgeContext); MedgeFree(MilEdgeResult); /* Free defaults. */ MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); }