#include <mil.h>
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("FoodInspectionMango\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This example shows how to determine the ripeness of a mango using color\n")
MIL_TEXT("matching. In the example, a mango is considered ripe when the area of the\n")
MIL_TEXT("pixels that matched with the green sample is less than a certain percentage\n")
MIL_TEXT("of the total matching area of the mango.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: Application, system, image processing, color\n\n")
MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
#define EXAMPLE_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("FoodInspectionMango/")
static const MIL_INT NB_IMAGES = 10;
static MIL_CONST_TEXT_PTR NUMBERED_IMAGE_FILE_NAME = EXAMPLE_IMAGE_PATH MIL_TEXT("Mango_%i.jpg");
static MIL_CONST_TEXT_PTR COLOR_CONTEXT_PATH = EXAMPLE_IMAGE_PATH MIL_TEXT("MangoColor.mcol");
static const MIL_INT NB_COLOR_SAMPLES = 5;
static MIL_CONST_TEXT_PTR SAMPLE_STRING[NB_COLOR_SAMPLES] =
{
MIL_TEXT("Dark Red"),
MIL_TEXT("Bright Red"),
MIL_TEXT("Yellow"),
MIL_TEXT("Orange"),
MIL_TEXT("Green")
};
static const MIL_INT GREEN_INDEX = 4;
static const MIL_INT NB_ERODE_ITER = 10;
static const MIL_DOUBLE GREEN_THRESHOLD = 30;
static const MIL_INT PASS_FAIL_OFFSET = 30;
static const MIL_INT COLUMN_WIDTH = 16;
void DrawSample(MIL_ID MilColContext, MIL_ID MilDest, MIL_UINT SampleIdx, MIL_INT OffsetX, MIL_INT OffsetY, MIL_INT SamplePatchSize);
void PrintResultTable(MIL_DOUBLE SamplesScores[NB_COLOR_SAMPLES]);
int MosMain(void)
{
MIL_ID MilApplication = MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MIL_ID MilSystem = MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MIL_ID MilDisplay = MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
MIL_ID MilGraList = MgraAllocList(MilSystem, M_DEFAULT, M_NULL);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraList);
MIL_ID MilColContext = McolRestore(COLOR_CONTEXT_PATH, MilSystem, M_DEFAULT, M_NULL);
MIL_ID MilColResult = McolAllocResult(MilSystem, M_DEFAULT, M_DEFAULT, M_NULL);
McolPreprocess(MilColContext, M_DEFAULT);
MosPrintf(MIL_TEXT("FOOD INSPECTION MANGO:\n"));
MosPrintf(MIL_TEXT("---------------------------------------\n\n"));
PrintHeader();
MIL_TEXT_CHAR ImageFilePath[512];
MosSprintf(ImageFilePath, 512, NUMBERED_IMAGE_FILE_NAME, 0);
MIL_INT ImageSizeX = MbufDiskInquire(ImageFilePath, M_SIZE_X, M_NULL);
MIL_INT ImageSizeY = MbufDiskInquire(ImageFilePath, M_SIZE_Y, M_NULL);
MIL_INT SamplePatchSize = ImageSizeY / NB_COLOR_SAMPLES;
MIL_ID MilDisplayImage = MbufAllocColor(MilSystem, 3, 2*ImageSizeX + SamplePatchSize, ImageSizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, M_NULL);
MIL_ID MilInspectionImage = MbufChild2d(MilDisplayImage, 0, 0, ImageSizeX, ImageSizeY, M_NULL);
MIL_ID MilInspectionBlue = MbufChildColor2d(MilInspectionImage, M_BLUE, 1, 1, ImageSizeX-2, ImageSizeY-2, M_NULL);
MIL_ID MilResultImage = MbufChild2d(MilDisplayImage, ImageSizeX, 0, ImageSizeX, ImageSizeY, M_NULL);
MbufLoad(ImageFilePath, MilInspectionImage);
MbufClear(MilResultImage, 0);
MIL_ID MilBinaryImage = MbufAlloc2d(MilSystem, ImageSizeX, ImageSizeY, 1+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilBinaryChild = MbufChild2d(MilBinaryImage, 1, 1, ImageSizeX-2, ImageSizeY-2, M_NULL);
MIL_ID MilAreaImage = MbufAlloc2d(MilSystem, ImageSizeX, ImageSizeY, 1+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_CENTER);
MgraColor(M_DEFAULT, M_COLOR_RED);
MgraFont(M_DEFAULT, M_FONT_DEFAULT_MEDIUM);
MgraText(M_DEFAULT, MilGraList, ImageSizeX / 2, 5, MIL_TEXT("Inspection Image"));
MgraText(M_DEFAULT, MilGraList, 3*ImageSizeX / 2, 5, MIL_TEXT("Result Image"));
MIL_UINT SampleIdx;
MIL_INT OffsetY = 0;
MgraControl(M_DEFAULT, M_TEXT_ALIGN_VERTICAL, M_CENTER);
MgraControl(M_DEFAULT, M_BACKGROUND_MODE, M_TRANSPARENT);
MgraFont (M_DEFAULT, M_FONT_DEFAULT_SMALL);
for(SampleIdx = 0; SampleIdx < NB_COLOR_SAMPLES-1; SampleIdx++, OffsetY += SamplePatchSize)
{
DrawSample(MilColContext, MilDisplayImage, SampleIdx, 2*ImageSizeX, OffsetY, SamplePatchSize);
}
DrawSample(MilColContext, MilDisplayImage, SampleIdx, 2*ImageSizeX, OffsetY, ImageSizeY - OffsetY);
MgraControl(M_DEFAULT, M_BACKGROUND_MODE, M_OPAQUE);
MdispSelect(MilDisplay, MilDisplayImage);
MosPrintf(MIL_TEXT("The average colors of the samples used to qualify the\n")
MIL_TEXT("ripeness of the mangos are displayed. The maximum green\n")
MIL_TEXT("area allowed for a mango to pass is set at %.2f%%.\n")
MIL_TEXT("We will now proceed to qualify some mangos.\n\n")
MIL_TEXT("Press <Enter> to continue.\n\n"),
GREEN_THRESHOLD);
MosGetch();
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_RIGHT);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_VERTICAL, M_BOTTOM);
MgraFont (M_DEFAULT, M_FONT_DEFAULT_LARGE);
for(MIL_UINT ImageIdx = 0; ImageIdx < NB_IMAGES; ImageIdx++)
{
MdispControl(MilDisplay, M_UPDATE, M_DISABLE);
MbufClear(MilResultImage, 0);
MosSprintf(ImageFilePath, 512, NUMBERED_IMAGE_FILE_NAME, (int)ImageIdx);
MbufLoad(ImageFilePath, MilInspectionImage);
MosPrintf(MIL_TEXT("Inspecting mango image %i\n\n"), (int)ImageIdx);
MimBinarize(MilInspectionBlue, MilBinaryChild, M_BIMODAL+M_LESS, M_NULL, M_NULL);
MimErode(MilBinaryImage, MilAreaImage, NB_ERODE_ITER, M_BINARY);
McolMatch(MilColContext, MilInspectionImage, M_DEFAULT, MilAreaImage, MilColResult, M_DEFAULT);
MIL_DOUBLE SamplesScores[NB_COLOR_SAMPLES];
McolGetResult(MilColResult, M_DEFAULT, M_ALL, M_SCORE, SamplesScores);
McolDraw(M_DEFAULT, MilColResult, MilResultImage, M_DRAW_PIXEL_MATCH_USING_COLOR, M_DEFAULT, M_DEFAULT, M_DEFAULT);
bool IsNotRipe = SamplesScores[GREEN_INDEX] > GREEN_THRESHOLD;
MgraColor(M_DEFAULT, IsNotRipe ? M_COLOR_RED : M_COLOR_GREEN);
MgraText(M_DEFAULT, MilDisplayImage, 2*ImageSizeX - PASS_FAIL_OFFSET, ImageSizeY - PASS_FAIL_OFFSET, IsNotRipe ? MIL_TEXT("FAIL") : MIL_TEXT("PASS"));
MdispControl(MilDisplay, M_UPDATE, M_ENABLE);
PrintResultTable(SamplesScores);
MosPrintf(MIL_TEXT("Mango ripeness : %s\n\n"), IsNotRipe ? MIL_TEXT("FAIL") : MIL_TEXT("PASS"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
MbufFree(MilBinaryChild);
MbufFree(MilBinaryImage);
MbufFree(MilAreaImage);
MbufFree(MilResultImage);
MbufFree(MilInspectionBlue);
MbufFree(MilInspectionImage);
MbufFree(MilDisplayImage);
McolFree(MilColResult);
McolFree(MilColContext);
MgraFree(MilGraList);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void DrawSample(MIL_ID MilColContext, MIL_ID MilDest, MIL_UINT SampleIdx, MIL_INT OffsetX, MIL_INT OffsetY, MIL_INT SamplePatchSize)
{
MIL_INT SampleColorR = McolInquire(MilColContext, M_SAMPLE_INDEX(SampleIdx), M_SAMPLE_AVERAGE_COLOR_BAND_0, M_NULL);
MIL_INT SampleColorG = McolInquire(MilColContext, M_SAMPLE_INDEX(SampleIdx), M_SAMPLE_AVERAGE_COLOR_BAND_1, M_NULL);
MIL_INT SampleColorB = McolInquire(MilColContext, M_SAMPLE_INDEX(SampleIdx), M_SAMPLE_AVERAGE_COLOR_BAND_2, M_NULL);
MgraColor(M_DEFAULT, M_RGB888(SampleColorR, SampleColorG, SampleColorB));
MgraRectAngle(M_DEFAULT, MilDest, OffsetX, OffsetY, SamplePatchSize, SamplePatchSize, 0, M_CORNER_AND_DIMENSION+M_FILLED);
MgraColor(M_DEFAULT, M_COLOR_BLACK);
MgraText(M_DEFAULT, MilDest, OffsetX + SamplePatchSize/2, OffsetY + SamplePatchSize/2, SAMPLE_STRING[SampleIdx]);
}
void PrintResultTable(MIL_DOUBLE SamplesScores[NB_COLOR_SAMPLES])
{
MosPrintf(MIL_TEXT("+----------------+----------------+----------------+\n")
MIL_TEXT("| Sample Index | Sample Tag | Score(%%) |\n"));
for(MIL_INT SampleIdx = 0; SampleIdx < NB_COLOR_SAMPLES; SampleIdx++)
{
MosPrintf(MIL_TEXT("+----------------+----------------+----------------+\n")
MIL_TEXT("|%*i|%*s|%*.2f|\n"),
COLUMN_WIDTH,
(int)SampleIdx,
COLUMN_WIDTH,
SAMPLE_STRING[SampleIdx],
COLUMN_WIDTH,
SamplesScores[SampleIdx]);
}
MosPrintf(MIL_TEXT("+----------------+----------------+----------------+\n\n"));
}