#include <mil.h>
void CalculateTextureStatistics(MIL_ID MilSystem, MIL_ID MilDisplayImage, MIL_ID MilOverlay, MIL_CONST_TEXT_PTR ImageFilename);
#define IMAGE_FABRIC M_IMAGE_PATH MIL_TEXT("Preprocessing/Fabric.mim")
#define IMAGE_ROUGH_FABRIC M_IMAGE_PATH MIL_TEXT("Preprocessing/RoughFabric.mim")
#define IMAGE_TOWEL M_IMAGE_PATH MIL_TEXT("Preprocessing/Towel.mim")
#define IMAGE_DOT M_IMAGE_PATH MIL_TEXT("Preprocessing/DotMatrixSerial.mim")
#define IMAGE_STRUCTURED_NOISE M_IMAGE_PATH MIL_TEXT("noise.mim")
#define IMAGE_LARGE_WAFER M_IMAGE_PATH MIL_TEXT("LargeWafer.mim")
#define STRING_LENGTH_MAX 128
#define TEXTURE_SIZE 256
#define X_DISPLACEMENT_OFFSET 2
#define Y_DISPLACEMENT_OFFSET 0
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("TextureStatistics\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This example demonstrates how to calculate texture statistics.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, buffer, display, \n")
MIL_TEXT("graphic, image processing, system.\n\n"));
}
int MosMain()
{
PrintHeader();
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilOverlay,
MilOverlayChild,
MilImage,
MilSampleImage;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
MbufAlloc2d(MilSystem,
4 * TEXTURE_SIZE,
3 * TEXTURE_SIZE,
32 + M_UNSIGNED,
M_PROC + M_DISP + M_IMAGE,
&MilImage);
MdispSelect(MilDisplay, MilImage);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlay);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_RIGHT );
MgraControl(M_DEFAULT, M_BACKGROUND_MODE, M_TRANSPARENT);
MgraControl(M_DEFAULT, M_COLOR, M_COLOR_LIGHT_BLUE );
MbufChild2d(MilImage,
0,
0,
2 * TEXTURE_SIZE,
TEXTURE_SIZE,
&MilSampleImage);
MbufChildColor2d(MilOverlay,
M_ALL_BANDS,
0,
0,
2 * TEXTURE_SIZE,
TEXTURE_SIZE,
&MilOverlayChild);
CalculateTextureStatistics(MilSystem, MilSampleImage, MilOverlayChild, IMAGE_FABRIC);
MbufChildMove(MilSampleImage, 2 * TEXTURE_SIZE, 0, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
MbufChildMove(MilOverlayChild, 2 * TEXTURE_SIZE, 0, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
CalculateTextureStatistics(MilSystem, MilSampleImage, MilOverlayChild, IMAGE_DOT);
MbufChildMove(MilSampleImage, 0, TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
MbufChildMove(MilOverlayChild, 0, TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
CalculateTextureStatistics(MilSystem, MilSampleImage, MilOverlayChild, IMAGE_TOWEL);
MbufChildMove(MilSampleImage, 2 * TEXTURE_SIZE, TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
MbufChildMove(MilOverlayChild, 2 * TEXTURE_SIZE, TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
CalculateTextureStatistics(MilSystem, MilSampleImage, MilOverlayChild, IMAGE_ROUGH_FABRIC);
MbufChildMove(MilSampleImage, 0, 2 * TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
MbufChildMove(MilOverlayChild, 0, 2 * TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
CalculateTextureStatistics(MilSystem, MilSampleImage, MilOverlayChild, IMAGE_LARGE_WAFER);
MbufChildMove(MilSampleImage, 2 * TEXTURE_SIZE, 2 * TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
MbufChildMove(MilOverlayChild, 2 * TEXTURE_SIZE, 2 * TEXTURE_SIZE, 2 * TEXTURE_SIZE, TEXTURE_SIZE, M_DEFAULT);
CalculateTextureStatistics(MilSystem, MilSampleImage, MilOverlayChild, IMAGE_STRUCTURED_NOISE);
MosPrintf(MIL_TEXT("The co-occurrence matrices (GLCM) have been calculated for\n")
MIL_TEXT("each texture sample and are displayed.\n\n")
MIL_TEXT("Texture metrics (Haralick statistics) derived from the GLCM\n")
MIL_TEXT("are then calculated and printed for each sample.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_DEFAULT );
MgraControl(M_DEFAULT, M_BACKGROUND_MODE, M_DEFAULT);
MgraControl(M_DEFAULT, M_COLOR, M_DEFAULT );
MbufFree(MilOverlayChild);
MbufFree(MilSampleImage);
MbufFree(MilImage);
MdispFree(MilDisplay);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
void CalculateTextureStatistics(MIL_ID MilSystem,
MIL_ID MilImage,
MIL_ID MilOverlay,
MIL_CONST_TEXT_PTR ImageFilename)
{
MIL_ID MilTextureImage,
TextureDisplaySample,
CooccurrenceMatrixId,
MilContext,
MilResult;
MimAlloc(MilSystem, M_STATISTICS_CONTEXT, M_DEFAULT, &MilContext);
MimAllocResult(MilSystem, M_DEFAULT, M_STATISTICS_RESULT, &MilResult);
MIL_INT Type, SizeX, SizeY;
MbufDiskInquire(ImageFilename, M_SIZE_X, &SizeX);
MbufDiskInquire(ImageFilename, M_SIZE_Y, &SizeY);
MbufDiskInquire(ImageFilename, M_TYPE, &Type);
MbufAlloc2d(MilSystem, SizeX, SizeY,
Type, M_IMAGE + M_PROC, &MilTextureImage);
MbufChild2d(MilImage, 0, 0, TEXTURE_SIZE, TEXTURE_SIZE, &TextureDisplaySample);
MbufChild2d(MilImage, TEXTURE_SIZE, 0, TEXTURE_SIZE, TEXTURE_SIZE, &CooccurrenceMatrixId);
MbufLoad(ImageFilename, MilTextureImage);
const MIL_INT SizeDifferenceX = (SizeX - TEXTURE_SIZE) / 2;
const MIL_INT SizeDifferenceY = (SizeY - TEXTURE_SIZE) / 2;
const MIL_INT SrcOffsetX = SizeDifferenceX > 0 ? SizeDifferenceX : 0;
const MIL_INT SrcOffsetY = SizeDifferenceY > 0 ? SizeDifferenceY : 0;
const MIL_INT DstOffsetX = SizeDifferenceX > 0 ? 0 : -SizeDifferenceX;
const MIL_INT DstOffsetY = SizeDifferenceY > 0 ? 0 : -SizeDifferenceY;
const MIL_INT SizeXToCopy = SizeX > TEXTURE_SIZE ? TEXTURE_SIZE : SizeX;
const MIL_INT SizeYToCopy = SizeY > TEXTURE_SIZE ? TEXTURE_SIZE : SizeY;
MbufCopyColor2d(MilTextureImage,
TextureDisplaySample,
M_ALL_BANDS,
SrcOffsetX,
SrcOffsetY,
M_ALL_BANDS,
DstOffsetX,
DstOffsetY,
SizeXToCopy,
SizeYToCopy);
MimControl(MilContext, M_GLCM_PAIR_OFFSET_X, X_DISPLACEMENT_OFFSET);
MimControl(MilContext, M_GLCM_PAIR_OFFSET_Y, Y_DISPLACEMENT_OFFSET);
MimControl(MilContext, M_STAT_GLCM_ENERGY, M_ENABLE);
MimControl(MilContext, M_STAT_GLCM_CONTRAST, M_ENABLE);
MimControl(MilContext, M_STAT_GLCM_CORRELATION, M_ENABLE);
MimControl(MilContext, M_STAT_GLCM_ENTROPY, M_ENABLE);
MimControl(MilContext, M_STAT_GLCM_DISSIMILARITY, M_ENABLE);
MimControl(MilContext, M_STAT_GLCM_HOMOGENEITY, M_ENABLE);
MimStatCalculate(MilContext, MilTextureImage, MilResult, M_DEFAULT);
MIL_DOUBLE Energy = 0,
Entropy = 0,
Contrast = 0,
Dissimilarity = 0,
Homogeneity = 0,
Correlation = 0,
Max = 0;
MimGetResult(MilResult, M_STAT_GLCM_ENERGY, &Energy);
MimGetResult(MilResult, M_STAT_GLCM_CONTRAST, &Contrast);
MimGetResult(MilResult, M_STAT_GLCM_ENTROPY, &Entropy);
MimGetResult(MilResult, M_STAT_GLCM_DISSIMILARITY, &Dissimilarity);
MimGetResult(MilResult, M_STAT_GLCM_HOMOGENEITY, &Homogeneity);
MimGetResult(MilResult, M_STAT_GLCM_CORRELATION, &Correlation);
MIL_TEXT_CHAR TextToDisplay[STRING_LENGTH_MAX];
MosSprintf(TextToDisplay, STRING_LENGTH_MAX, MIL_TEXT("Dissimilarity: %4.2f"), Dissimilarity);
MgraText(M_DEFAULT, MilOverlay, 510, 0, TextToDisplay);
MosSprintf(TextToDisplay, STRING_LENGTH_MAX, MIL_TEXT("Homogeneity: %4.2f"), Homogeneity);
MgraText(M_DEFAULT, MilOverlay, 510, 15, TextToDisplay);
MosSprintf(TextToDisplay, STRING_LENGTH_MAX, MIL_TEXT("Correlation: %4.2f"), Correlation);
MgraText(M_DEFAULT, MilOverlay, 510, 30, TextToDisplay);
MosSprintf(TextToDisplay, STRING_LENGTH_MAX, MIL_TEXT("Contrast: %4.2f"), Contrast);
MgraText(M_DEFAULT, MilOverlay, 510, 45, TextToDisplay);
MosSprintf(TextToDisplay, STRING_LENGTH_MAX, MIL_TEXT("Entropy: %4.2f"), Entropy);
MgraText(M_DEFAULT, MilOverlay, 510, 60, TextToDisplay);
MosSprintf(TextToDisplay, STRING_LENGTH_MAX, MIL_TEXT("Energy: %4.2f"), Energy);
MgraText(M_DEFAULT, MilOverlay, 510, 75, TextToDisplay);
MimDraw(M_DEFAULT,
MilResult,
M_NULL,
CooccurrenceMatrixId,
M_DRAW_GLCM_MATRIX,
M_NULL,
M_NULL,
M_DEFAULT);
MimControl(MilContext, M_STAT_GLCM_ENERGY, M_DISABLE);
MimControl(MilContext, M_STAT_GLCM_CONTRAST, M_DISABLE);
MimControl(MilContext, M_STAT_GLCM_CORRELATION, M_DISABLE);
MimControl(MilContext, M_STAT_GLCM_ENTROPY, M_DISABLE);
MimControl(MilContext, M_STAT_GLCM_DISSIMILARITY, M_DISABLE);
MimControl(MilContext, M_STAT_GLCM_HOMOGENEITY, M_DISABLE);
MimControl(MilContext, M_STAT_MAX, M_ENABLE);
MimStatCalculate(MilContext, CooccurrenceMatrixId, MilResult, M_DEFAULT);
MimGetResult(MilResult, M_STAT_MAX, &Max);
MimArith(CooccurrenceMatrixId, 255.0 / Max, CooccurrenceMatrixId, M_MULT_CONST + M_FLOAT_PROC);
MimFree(MilResult);
MimFree(MilContext);
MbufFree(TextureDisplaySample);
MbufFree(CooccurrenceMatrixId);
MbufFree(MilTextureImage);
}