#include <mil.h>
void TextBinarization(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_CONST_TEXT_PTR Filename);
void PlasticCupBinarization(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_CONST_TEXT_PTR Filename);
void HardDiskBinarization(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_CONST_TEXT_PTR Filename);
#define IMAGE_TEXT M_IMAGE_PATH MIL_TEXT("Preprocessing/PrintedText.mim")
#define IMAGE_CUP M_IMAGE_PATH MIL_TEXT("Preprocessing/PlasticCup.mim")
#define IMAGE_DISK M_IMAGE_PATH MIL_TEXT("Preprocessing/HardDisk.mim")
#define CUP_INTERIOR_X_BEGIN 145
#define CUP_INTERIOR_Y_BEGIN 165
#define CUP_INTERIOR_X_LENGTH 300
#define CUP_INTERIOR_Y_LENGTH 190
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("BinarizeAdaptive\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This program demonstrates how to perform\n")
MIL_TEXT("adaptive binarization.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, system, display, buffer,\n")
MIL_TEXT("graphic, image processing.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
int MosMain()
{
PrintHeader();
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
TextBinarization(MilSystem, MilDisplay, IMAGE_TEXT);
PlasticCupBinarization(MilSystem, MilDisplay, IMAGE_CUP);
HardDiskBinarization(MilSystem, MilDisplay, IMAGE_DISK);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
void TextBinarization(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_CONST_TEXT_PTR Filename)
{
MIL_ID MilImage,
MilOverlayImage,
MilSubImage00,
MilSubImage01,
MilContext;
MIL_INT Type, SizeX, SizeY, OverlayClearColor;
MbufDiskInquire(Filename, M_SIZE_X, &SizeX);
MbufDiskInquire(Filename, M_SIZE_Y, &SizeY);
MbufDiskInquire(Filename, M_TYPE, &Type);
MbufAlloc2d(MilSystem, SizeX * 2, SizeY,
Type, M_IMAGE + M_PROC + M_DISP, &MilImage);
MbufChild2d(MilImage, 0L, 0L, SizeX, SizeY, &MilSubImage00);
MbufChild2d(MilImage, SizeX, 0L, SizeX, SizeY, &MilSubImage01);
MbufLoad(Filename, MilSubImage00);
MimBinarize(MilSubImage00, MilSubImage01, M_BIMODAL + M_GREATER, M_NULL, M_NULL);
MdispSelect (MilDisplay, MilImage);
MdispInquire(MilDisplay, M_TRANSPARENT_COLOR, &OverlayClearColor);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MbufClear (MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraText(M_DEFAULT, MilOverlayImage, 0, 0, MIL_TEXT(" Source image"));
MgraText(M_DEFAULT, MilOverlayImage, SizeX, 0, MIL_TEXT(" Global binarization"));
MosPrintf(MIL_TEXT("The image has been segmented using a global bimodal binarization. \n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MimAlloc(MilSystem, M_BINARIZE_ADAPTIVE_CONTEXT,
M_DEFAULT, &MilContext);
MimControl(MilContext, M_THRESHOLD_MODE, M_NIBLACK);
MimControl(MilContext, M_FOREGROUND_VALUE, M_FOREGROUND_BLACK);
MimControl(MilContext, M_MINIMUM_CONTRAST, 6);
MimControl(MilContext, M_GLOBAL_MIN, 65);
MimBinarizeAdaptive(MilContext, MilSubImage00,
M_NULL, M_NULL, MilSubImage01, M_NULL, M_DEFAULT);
MbufClear(MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraText(M_DEFAULT, MilOverlayImage, 0, 0, MIL_TEXT(" Source image"));
MgraText(M_DEFAULT, MilOverlayImage, SizeX, 0, MIL_TEXT(" Local adaptive binarization"));
MosPrintf(MIL_TEXT("The image has been segmented using Niblack\'s local adaptive binarization. \n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MimFree(MilContext);
MbufFree(MilSubImage00);
MbufFree(MilSubImage01);
MbufFree(MilImage);
}
void PlasticCupBinarization(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_CONST_TEXT_PTR Filename)
{
MIL_ID MilImage,
MilOverlayImage,
MilZoneOfInterest00,
MilZoneOfInterest01,
MilSubImage00,
MilSubImage01,
MilContext;
MIL_INT Type, SizeX, SizeY, OverlayClearColor;
MbufDiskInquire(Filename, M_SIZE_X, &SizeX);
MbufDiskInquire(Filename, M_SIZE_Y, &SizeY);
MbufDiskInquire(Filename, M_TYPE, &Type);
MbufAlloc2d(MilSystem, SizeX * 2, SizeY,
Type, M_IMAGE + M_PROC + M_DISP, &MilImage);
MbufChild2d(MilImage, 0L, 0L, SizeX, SizeY, &MilSubImage00);
MbufChild2d(MilImage, SizeX, 0L, SizeX, SizeY, &MilSubImage01);
MbufChild2d(MilSubImage00, CUP_INTERIOR_X_BEGIN, CUP_INTERIOR_Y_BEGIN,
CUP_INTERIOR_X_LENGTH, CUP_INTERIOR_Y_LENGTH, &MilZoneOfInterest00);
MbufChild2d(MilSubImage01, CUP_INTERIOR_X_BEGIN, CUP_INTERIOR_Y_BEGIN,
CUP_INTERIOR_X_LENGTH, CUP_INTERIOR_Y_LENGTH, &MilZoneOfInterest01);
MdispSelect(MilDisplay, MilImage);
MdispInquire(MilDisplay, M_TRANSPARENT_COLOR, &OverlayClearColor);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MbufClear(MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraRect(M_DEFAULT,
MilOverlayImage,
CUP_INTERIOR_X_BEGIN,
CUP_INTERIOR_Y_BEGIN,
CUP_INTERIOR_X_BEGIN + CUP_INTERIOR_X_LENGTH,
CUP_INTERIOR_Y_BEGIN + CUP_INTERIOR_Y_LENGTH);
MbufLoad(Filename, MilSubImage00);
MimBinarize(MilZoneOfInterest00, MilZoneOfInterest01, M_BIMODAL + M_GREATER, M_NULL, M_NULL);
MimClose(MilZoneOfInterest01, MilZoneOfInterest01, 1, M_BINARY);
MimRank(MilZoneOfInterest01, MilZoneOfInterest01,
M_3X3_RECT, M_MEDIAN, M_BINARY);
MimResize(MilZoneOfInterest01, MilSubImage01,
M_FILL_DESTINATION, M_FILL_DESTINATION, M_BILINEAR);
MgraText(M_DEFAULT, MilOverlayImage, 0, 0, MIL_TEXT(" Source image"));
MgraText(M_DEFAULT, MilOverlayImage, SizeX, 0, MIL_TEXT(" Global binarization"));
MosPrintf(MIL_TEXT("The image has been segmented using a global bimodal binarization. \n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MimAlloc(MilSystem, M_BINARIZE_ADAPTIVE_CONTEXT,
M_DEFAULT, &MilContext);
MimControl(MilContext, M_THRESHOLD_MODE, M_PSEUDOMEDIAN);
MimControl(MilContext, M_FOREGROUND_VALUE, M_FOREGROUND_WHITE);
MimControl(MilContext, M_GLOBAL_OFFSET, 10);
MimBinarizeAdaptive(MilContext, MilZoneOfInterest00,
M_NULL, M_NULL, MilZoneOfInterest01, M_NULL, M_DEFAULT);
MimClose(MilZoneOfInterest01, MilZoneOfInterest01, 1, M_BINARY);
MimRank(MilZoneOfInterest01, MilZoneOfInterest01,
M_3X3_RECT, M_MEDIAN, M_BINARY);
MimResize(MilZoneOfInterest01, MilSubImage01,
M_FILL_DESTINATION, M_FILL_DESTINATION, M_BILINEAR);
MbufClear(MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MgraRect(M_DEFAULT,
MilOverlayImage,
CUP_INTERIOR_X_BEGIN,
CUP_INTERIOR_Y_BEGIN,
CUP_INTERIOR_X_BEGIN + CUP_INTERIOR_X_LENGTH,
CUP_INTERIOR_Y_BEGIN + CUP_INTERIOR_Y_LENGTH);
MgraText(M_DEFAULT, MilOverlayImage, 0, 0, MIL_TEXT(" Source image"));
MgraText(M_DEFAULT, MilOverlayImage, SizeX, 0, MIL_TEXT(" Local adaptive binarization"));
MosPrintf(MIL_TEXT("The image has been segmented using the pseudomedian local adaptive binarization.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MbufClear(MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MimFree(MilContext);
MbufFree(MilZoneOfInterest00);
MbufFree(MilZoneOfInterest01);
MbufFree(MilSubImage00);
MbufFree(MilSubImage01);
MbufFree(MilImage);
}
void HardDiskBinarization(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_CONST_TEXT_PTR Filename)
{
MIL_ID MilImage,
MilOverlayImage,
MilSubImage00,
MilSubImage01,
MilContext;
MIL_INT Type, SizeX, SizeY, OverlayClearColor;
MbufDiskInquire(Filename, M_SIZE_X, &SizeX);
MbufDiskInquire(Filename, M_SIZE_Y, &SizeY);
MbufDiskInquire(Filename, M_TYPE, &Type);
MbufAlloc2d(MilSystem, SizeX * 2, SizeY,
Type, M_IMAGE + M_PROC + M_DISP, &MilImage);
MbufChild2d(MilImage, 0L, 0L, SizeX, SizeY, &MilSubImage00);
MbufChild2d(MilImage, SizeX, 0L, SizeX, SizeY, &MilSubImage01);
MbufLoad(Filename, MilSubImage00);
MimBinarize(MilSubImage00, MilSubImage01, M_BIMODAL + M_GREATER, M_NULL, M_NULL);
MimRank(MilSubImage01, MilSubImage01,
M_3X3_RECT, M_MEDIAN, M_BINARY);
MdispSelect(MilDisplay, MilImage);
MdispInquire(MilDisplay, M_TRANSPARENT_COLOR, &OverlayClearColor);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MbufClear(MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraText(M_DEFAULT, MilOverlayImage, 0, 0, MIL_TEXT(" Source image"));
MgraText(M_DEFAULT, MilOverlayImage, SizeX, 0, MIL_TEXT(" Global binarization"));
MosPrintf(MIL_TEXT("The image has been segmented using a global bimodal binarization. \n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MimAlloc(MilSystem, M_BINARIZE_ADAPTIVE_CONTEXT, M_DEFAULT, &MilContext);
MimControl(MilContext, M_THRESHOLD_MODE, M_NIBLACK);
MimControl(MilContext, M_FOREGROUND_VALUE, M_FOREGROUND_BLACK);
MimControl(MilContext, M_MINIMUM_CONTRAST, 1.65);
MimControl(MilContext, M_NIBLACK_BIAS, 0.3);
MimControl(MilContext, M_AVERAGE_MODE, M_GAUSSIAN);
MimBinarizeAdaptive(MilContext, MilSubImage00,
M_NULL, M_NULL, MilSubImage01, M_NULL, M_DEFAULT);
MimRank(MilSubImage01, MilSubImage01,
M_3X3_RECT, M_MEDIAN, M_BINARY);
MdispSelect(MilDisplay, MilImage);
MbufClear(MilOverlayImage, (MIL_DOUBLE) OverlayClearColor);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraText(M_DEFAULT, MilOverlayImage, 0, 0, MIL_TEXT(" Source image"));
MgraText(M_DEFAULT, MilOverlayImage, SizeX, 0, MIL_TEXT(" Local adaptive binarization"));
MosPrintf(MIL_TEXT("The image has been binarized using Niblack\'s local adaptive binarization with a Gaussian average mode. \n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n\n"));
MosGetch();
MimFree(MilContext);
MbufFree(MilSubImage00);
MbufFree(MilSubImage01);
MbufFree(MilImage);
}