#include <mil.h>
struct STestParameters
{
MIL_ID MilDisplay,
MilGraphicsList,
MilGraphicsContext,
MilBinImage,
MilBlobFeatureList,
MilBlobResult;
MIL_INT RegionLabel;
};
void InteractivityExample(MIL_ID MilSystem, MIL_ID MilDisplay);
MIL_INT MFTYPE HookHandler(MIL_INT HookType, MIL_ID EventId, void* UserData);
void CountObjects(MIL_ID MilDisplay, MIL_ID MilGraphicsList,
MIL_ID MilGraphicsContext, MIL_ID MilBinImage,
MIL_ID MilBlobFeatureList, MIL_ID MilBlobResult);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay,
M_NULL, M_NULL);
MosPrintf(MIL_TEXT("\nINTERACTIVE REGIONS AND SUBPIXEL ANNOTATIONS:\n"));
MosPrintf(MIL_TEXT("---------------------------------------------\n\n"));
MosPrintf(MIL_TEXT("This program determines the number of blobs in a region\n"));
MosPrintf(MIL_TEXT("defined interactively by the user. The extracted blob's\n"));
MosPrintf(MIL_TEXT("features are drawn with subpixel accuracy in a zoomable\n"));
MosPrintf(MIL_TEXT("display.\n\n"));
InteractivityExample(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Seals.mim")
#define IMAGE_THRESHOLD_VALUE 110L
#define RECTANGLE_POSITION_X 160
#define RECTANGLE_POSITION_Y 310
#define RECTANGLE_WIDTH 200
#define RECTANGLE_HEIGHT 175
#define RECTANGLE_ANGLE 0
void InteractivityExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilImage,
MilGraphicsList,
MilGraphicsContext,
MilBinImage,
MilBlobFeatureList,
MilBlobResult;
MIL_INT SizeX,
SizeY,
RegionLabel;
STestParameters DataStructure;
MbufRestore(IMAGE_FILE, MilSystem, &MilImage);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicsList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicsList);
MgraAlloc(MilSystem, &MilGraphicsContext);
MdispControl(MilDisplay, M_GRAPHIC_LIST_INTERACTIVE, M_ENABLE);
MgraRectAngle(MilGraphicsContext, MilGraphicsList, RECTANGLE_POSITION_X,
RECTANGLE_POSITION_Y, RECTANGLE_WIDTH, RECTANGLE_HEIGHT, RECTANGLE_ANGLE,
M_CENTER_AND_DIMENSION);
MgraInquireList(MilGraphicsList, M_LIST, M_DEFAULT, M_LAST_LABEL, &RegionLabel);
MgraControl(MilGraphicsContext, M_SELECTABLE, M_DISABLE);
MbufInquire(MilImage, M_SIZE_X, &SizeX);
MbufInquire(MilImage, M_SIZE_Y, &SizeY);
MbufAlloc2d(MilSystem, SizeX, SizeY, 1+M_UNSIGNED, M_IMAGE+M_PROC, &MilBinImage);
MimBinarize(MilImage, MilBinImage, M_FIXED+M_LESS, IMAGE_THRESHOLD_VALUE, M_NULL);
MblobAllocFeatureList(MilSystem, &MilBlobFeatureList);
MblobAllocResult(MilSystem, &MilBlobResult);
MblobSelectFeature(MilBlobFeatureList, M_CENTER_OF_GRAVITY);
MblobSelectFeature(MilBlobFeatureList, M_BOX);
MgraControlList(MilGraphicsList, M_GRAPHIC_LABEL(RegionLabel), M_DEFAULT,
M_GRAPHIC_SELECTED, M_TRUE);
CountObjects(MilDisplay, MilGraphicsList, MilGraphicsContext, MilBinImage,
MilBlobFeatureList, MilBlobResult);
DataStructure.MilDisplay = MilDisplay;
DataStructure.MilGraphicsList = MilGraphicsList;
DataStructure.MilGraphicsContext = MilGraphicsContext;
DataStructure.MilBinImage = MilBinImage;
DataStructure.RegionLabel = RegionLabel;
DataStructure.MilBlobFeatureList = MilBlobFeatureList;
DataStructure.MilBlobResult = MilBlobResult;
MgraHookFunction(MilGraphicsList, M_GRAPHIC_MODIFIED, &HookHandler, &DataStructure);
if(MdispInquire(MilDisplay, M_DISPLAY_TYPE, M_NULL) != M_AUXILIARY)
{
MosPrintf(MIL_TEXT("You can try using your mouse to interactively modify the\n"));
MosPrintf(MIL_TEXT("displayed region, such as moving, resizing, or rotating the\n"));
MosPrintf(MIL_TEXT("region. If you do so, the results and annotations will be\n"));
MosPrintf(MIL_TEXT("immediately updated.\n\n"));
}
else
{
MosPrintf(MIL_TEXT("***NOTE: This example does not implement the interactivity\n"));
MosPrintf(MIL_TEXT(" with an auxiliary display.\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to exit.\n"));
MosGetch();
MgraHookFunction(MilGraphicsList, M_GRAPHIC_MODIFIED+M_UNHOOK,
&HookHandler, &DataStructure);
MblobFree(MilBlobResult);
MblobFree(MilBlobFeatureList);
MbufFree(MilBinImage);
MgraFree(MilGraphicsContext);
MgraFree(MilGraphicsList);
MbufFree(MilImage);
}
MIL_INT MFTYPE HookHandler(MIL_INT HookType, MIL_ID EventId, void* UserData)
{
STestParameters* pDataStructure = (STestParameters*)(UserData);
MIL_INT ModifiedGraphicLabel;
MgraGetHookInfo(EventId, M_GRAPHIC_LABEL_VALUE, &ModifiedGraphicLabel);
if (ModifiedGraphicLabel == pDataStructure->RegionLabel)
{
CountObjects(pDataStructure->MilDisplay,
pDataStructure->MilGraphicsList,
pDataStructure->MilGraphicsContext,
pDataStructure->MilBinImage,
pDataStructure->MilBlobFeatureList,
pDataStructure->MilBlobResult);
}
return M_NULL;
}
#define MAX_TEXT_SIZE 100
void CountObjects(MIL_ID MilDisplay, MIL_ID MilGraphicsList, MIL_ID MilGraphicsContext,
MIL_ID MilBinImage, MIL_ID MilBlobFeatureList, MIL_ID MilBlobResult)
{
MIL_INT NumberOfBlobs,
NumberOfPrimitives,
Index;
MIL_TEXT_CHAR TextLabel[MAX_TEXT_SIZE];
MdispControl(MilDisplay, M_UPDATE, M_DISABLE);
MgraInquireList(MilGraphicsList, M_LIST, M_DEFAULT, M_NUMBER_OF_GRAPHICS,
&NumberOfPrimitives);
for(Index = NumberOfPrimitives-1; Index > 0; Index--)
{
MgraControlList(MilGraphicsList, M_GRAPHIC_INDEX(Index), M_DEFAULT, M_DELETE,
M_DEFAULT);
}
MbufSetRegion(MilBinImage, MilGraphicsList, M_DEFAULT, M_RASTERIZE+M_FILL_REGION,
M_DEFAULT);
MblobCalculate(MilBinImage, M_NULL, MilBlobFeatureList, MilBlobResult);
MblobGetNumber(MilBlobResult, &NumberOfBlobs);
MgraControl(MilGraphicsContext, M_INPUT_UNITS, M_DISPLAY);
MosSprintf(TextLabel, MAX_TEXT_SIZE, MIL_TEXT(" Number of blobs found: %2i "),
NumberOfBlobs);
MgraColor(MilGraphicsContext, M_COLOR_WHITE);
MgraText(MilGraphicsContext, MilGraphicsList, 10, 10, TextLabel);
MgraControl(MilGraphicsContext, M_INPUT_UNITS, M_PIXEL);
MgraColor(MilGraphicsContext, M_COLOR_RED);
MblobDraw(MilGraphicsContext, MilBlobResult, MilGraphicsList, M_DRAW_CENTER_OF_GRAVITY,
M_INCLUDED_BLOBS, M_DEFAULT);
MgraColor(MilGraphicsContext, M_COLOR_GREEN);
MblobDraw(MilGraphicsContext, MilBlobResult, MilGraphicsList, M_DRAW_BOX,
M_INCLUDED_BLOBS, M_DEFAULT);
MdispControl(MilDisplay, M_UPDATE, M_ENABLE);
}