#include <mil.h>
#define MIL_IMAGE_TEXT M_IMAGE_PATH MIL_TEXT("VariousCodeReadings/FlippedDatamatrix2.mim")
#define SMOOTHNESS_VALUE 90.0
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("Image Flattening\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This program demonstrates how to flatten a source image using\n")
MIL_TEXT("a combination of linear filtering and arithmetic.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, system, display, buffer, image processing.\n\n"));
}
void SimpleFlatten1(MIL_ID MilSystem, MIL_ID Src, MIL_ID Dst, MIL_ID MilFilter, MIL_DOUBLE FilterSmoothness, MIL_DOUBLE Alpha)
{
MIL_INT Sx = MbufInquire(Src, M_SIZE_X, M_NULL);
MIL_INT Sy = MbufInquire(Src, M_SIZE_Y, M_NULL);
MIL_ID Temp = MbufAlloc2d(MilSystem, Sx, Sy, 32 + M_FLOAT, M_IMAGE + M_PROC, M_NULL);
MimControl(MilFilter, M_FILTER_SMOOTHNESS, FilterSmoothness);
MimConvolve(Src, Temp, MilFilter);
MimArith(Temp, 127, Temp, M_SUB_CONST);
MimArith(Temp, -Alpha, Temp, M_MULT_CONST);
MimArith(Src, Temp, Dst, M_ADD + M_SATURATION);
MbufFree(Temp);
}
void SimpleFlatten2(MIL_ID MilSystem, MIL_ID Src, MIL_ID Dst, MIL_ID MilFilter, MIL_DOUBLE FilterSmoothness)
{
MIL_INT Sx = MbufInquire(Src, M_SIZE_X, M_NULL);
MIL_INT Sy = MbufInquire(Src, M_SIZE_Y, M_NULL);
MIL_ID Temp = MbufAlloc2d(MilSystem, Sx, Sy, 32 + M_FLOAT, M_IMAGE + M_PROC, M_NULL);
MimControl(MilFilter, M_FILTER_SMOOTHNESS, FilterSmoothness);
MimConvolve(Src, Temp, MilFilter);
MimArith(Temp, 1, Temp, M_ADD_CONST + M_SATURATION);
MimArith(Src, Temp, Temp, M_DIV);
MimArith(Temp, 128, Dst, M_MULT_CONST + M_SATURATION);
MbufFree(Temp);
}
void LocalNormalization(MIL_ID MilSystem, MIL_ID Src, MIL_ID Dst, MIL_ID MilFilter, MIL_DOUBLE FilterSmoothness)
{
MIL_INT Sx = MbufInquire(Src, M_SIZE_X, M_NULL);
MIL_INT Sy = MbufInquire(Src, M_SIZE_Y, M_NULL);
MIL_ID Temp1 = MbufAlloc2d(MilSystem, Sx, Sy, 32 + M_FLOAT, M_IMAGE + M_PROC, M_NULL);
MIL_ID Temp2 = MbufAlloc2d(MilSystem, Sx, Sy, 32 + M_FLOAT, M_IMAGE + M_PROC, M_NULL);
MimControl(MilFilter, M_FILTER_SMOOTHNESS, FilterSmoothness);
MimConvolve(Src, Temp1, MilFilter);
MimArith(Src, Temp1, Temp1, M_SUB);
MimArith(Temp1, M_NULL, Temp2, M_SQUARE);
MimConvolve(Temp2, Temp2, MilFilter);
MimArith(Temp2, M_NULL, Temp2, M_SQUARE_ROOT);
MimArith(Temp2, 1.0, Temp2, M_ADD_CONST);
MimArith(Temp1, Temp2, Temp1, M_DIV);
MimArith(Temp1, 2.0, Temp1, M_ADD_CONST);
MimArith(Temp1, 64, Dst, M_MULT_CONST + M_SATURATION);
MbufFree(Temp1);
MbufFree(Temp2);
}
int MosMain()
{
PrintHeader();
MIL_ID MilApplication,
MilSystem,
MilImage,
MilIIRFilterId,
MilSrcChild,
MilFlattenChild,
MilDisplay;
MIL_INT SizeX, SizeY;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
MgraColor(M_DEFAULT, 255);
MosPrintf(MIL_TEXT("An image is loaded and flattened using several strategies.\n"));
MbufDiskInquire(MIL_IMAGE_TEXT, M_SIZE_X, &SizeX);
MbufDiskInquire(MIL_IMAGE_TEXT, M_SIZE_Y, &SizeY);
MbufAlloc2d(MilSystem, 2 * SizeX, 2 * SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilImage);
MbufChild2d(MilImage, 0, 0, SizeX, SizeY, &MilSrcChild);
MbufClear(MilImage, 0);
MimAlloc(MilSystem, M_LINEAR_FILTER_IIR_CONTEXT, M_DEFAULT, &MilIIRFilterId);
MimControl(MilIIRFilterId, M_FILTER_TYPE, M_SHEN);
MimControl(MilIIRFilterId, M_FILTER_OPERATION, M_SMOOTH);
MbufLoad(MIL_IMAGE_TEXT, MilSrcChild);
MosPrintf(MIL_TEXT("\nMethod 1: A fraction of the deviation to the estimated image luminance is\n")
MIL_TEXT(" subtracted from the source image. Locally, the contrast information\n")
MIL_TEXT(" is similar to the source image's local contrast.\n\n"));
MbufChild2d(MilImage, SizeX, 0, SizeX, SizeY, &MilFlattenChild);
MgraText(M_DEFAULT, MilFlattenChild, 10, 10, MIL_TEXT("Method 1"));
SimpleFlatten1(MilSystem, MilSrcChild, MilFlattenChild, MilIIRFilterId, SMOOTHNESS_VALUE, 0.8);
MosPrintf(MIL_TEXT("\nMethod 2: The source image is divided by the estimation of the image's\n")
MIL_TEXT(" luminance. The resulting image has enhanced local contrast\n"));
MIL_TEXT(" information.\n\n");
MbufChildMove(MilFlattenChild, 0, SizeY, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MgraText(M_DEFAULT, MilFlattenChild, 10, 10, MIL_TEXT("Method 2"));
SimpleFlatten2(MilSystem, MilSrcChild, MilFlattenChild, MilIIRFilterId, SMOOTHNESS_VALUE);
MosPrintf(MIL_TEXT("\nMethod 3: A local contrast image, obtained by subtracting the\n")
MIL_TEXT(" estimated luminance, is normalized by the local intensity\n")
MIL_TEXT(" variation. In the resulting image, the amplitude of all\n")
MIL_TEXT(" the local contrast information is similar.\n\n"));
MbufChildMove(MilFlattenChild, SizeX, SizeY, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MgraText(M_DEFAULT, MilFlattenChild, 10, 10, MIL_TEXT("Method 3"));
LocalNormalization(MilSystem, MilSrcChild, MilFlattenChild, MilIIRFilterId, SMOOTHNESS_VALUE);
MdispSelect(MilDisplay, MilImage);
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n\n"));
MosGetch();
MimFree(MilIIRFilterId);
MbufFree(MilSrcChild);
MbufFree(MilFlattenChild);
MbufFree(MilImage);
MdispFree(MilDisplay);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}