#include <mil.h>
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("pills.mim")
#define WATERSHED_MINIMAL_GRADIENT_VARIATION 45
#define WATERSHED_MINIMAL_DISTANCE_VARIATION 2
#define PIXEL_FETCH_POSITION_X 2
#define PIXEL_FETCH_POSITION_Y 2
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilImage,
MilImageWatershed;
MIL_INT32 lFetchedValue = 0;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem,
&MilDisplay, M_NULL, M_NULL);
MbufRestore(IMAGE_FILE, MilSystem, &MilImageWatershed);
MbufRestore(IMAGE_FILE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MosPrintf(MIL_TEXT("\nSEGMENTATION:\n"));
MosPrintf(MIL_TEXT("-------------\n\n"));
MosPrintf(MIL_TEXT("An edge detection followed by a watershed will be used to remove\n"));
MosPrintf(MIL_TEXT("the background.\nPress <Enter> to continue.\n\n"));
MosGetch();
MimEdgeDetect(MilImageWatershed, MilImageWatershed, M_NULL, M_SOBEL,
M_REGULAR_EDGE_DETECT, M_NULL);
MimWatershed(MilImageWatershed, M_NULL, MilImageWatershed,
WATERSHED_MINIMAL_GRADIENT_VARIATION, M_MINIMA_FILL+M_BASIN);
MbufGet2d(MilImageWatershed, PIXEL_FETCH_POSITION_X, PIXEL_FETCH_POSITION_Y,
1, 1, &lFetchedValue);
MimClip(MilImageWatershed, MilImageWatershed, M_EQUAL, lFetchedValue, 0, 0, 0);
MimClip(MilImageWatershed, MilImage, M_NOT_EQUAL, 0, 0, 0xFF, 0);
MosPrintf(MIL_TEXT("A distance transformation followed by a watershed will be used \n"));
MosPrintf(MIL_TEXT("to separate the touching pills.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimDistance(MilImage, MilImageWatershed, M_CHAMFER_3_4);
MimWatershed(MilImageWatershed, M_NULL, MilImageWatershed,
WATERSHED_MINIMAL_DISTANCE_VARIATION,
M_STRAIGHT_WATERSHED+M_MAXIMA_FILL+M_SKIP_LAST_LEVEL+M_WATERSHED);
MimArith(MilImageWatershed, MilImage, MilImage, M_AND);
MosPrintf(MIL_TEXT("Here are the segmented pills.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
MbufFree(MilImageWatershed);
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}