#include <mil.h>
void AddColorSamples(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_ID MilColorContext,
MIL_ID MilDispColorImageChild);
void DisplayColorSamplesUtil(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_ID MilColorContext,
MIL_ID MilDispColorImageChild,
MIL_INT ActiveIndex);
#define EXAMPLE_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("ProductIdentificationOasis/")
#define _HIGHLIGHT_ALL_SAMPLES -1
#define _HIGHLIGHT_NONE_SAMPLE -2
#define _SAMPLE_DISPLAY_DELAY 500
static const MIL_INT NUM_SAMPLES = 10;
static MIL_CONST_TEXT_PTR SAMPLE_IMAGE_LIST_FILENAME[NUM_SAMPLES] =
{
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample01.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample02.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample03.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample04.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample05.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample06.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample07.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample08.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample09.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisSample10.mim")
};
static const MIL_INT NUM_PRODUCT = 10;
static MIL_CONST_TEXT_PTR PRODUCT_IMAGE_LIST_FILENAME[NUM_PRODUCT] =
{
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget01.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget02.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget03.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget04.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget05.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget06.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget07.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget08.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget09.mim"),
EXAMPLE_IMAGE_PATH MIL_TEXT("OasisTarget10.mim")
};
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilDispColorImage,
MilDispColorImageChild,
MilColorContext,
MilColorResult,
MilProductImage;
MIL_INT ImageSizeX,
ImageSizeY,
SampleMargin,
Index,
BestMatchSampleIndex;
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
MosPrintf(MIL_TEXT("\nThis program shows color-based product identification on a conveyor.\n\n"));
MosPrintf(MIL_TEXT("\nOnly color information of the images is used."));
MosPrintf(MIL_TEXT("\nFirst, product samples are added to a MIL color context."));
MosPrintf(MIL_TEXT("\nThis context is then used to identify the product"));
MosPrintf(MIL_TEXT("\nin target images under various conditions.\n"));
MosPrintf(MIL_TEXT("\nNote: the OASIS Trademark (Registered) is reproduced with"));
MosPrintf(MIL_TEXT("\nthe consent of its owner, Industries Lassonde Inc.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to define product samples"));
MosGetch();
MbufDiskInquire(SAMPLE_IMAGE_LIST_FILENAME[0], M_SIZE_X, &ImageSizeX);
MbufDiskInquire(SAMPLE_IMAGE_LIST_FILENAME[0], M_SIZE_Y, &ImageSizeY);
SampleMargin = 2*ImageSizeX/(NUM_SAMPLES/2);
MbufAllocColor(MilSystem, 3, ImageSizeX+SampleMargin, ImageSizeY,
8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &MilDispColorImage);
MbufClear(MilDispColorImage, M_COLOR_BLACK);
MdispSelect(MilDisplay, MilDispColorImage);
McolAlloc(MilSystem, M_COLOR_MATCHING, M_RGB, M_DEFAULT, M_DEFAULT, &MilColorContext);
MbufChild2d(MilDispColorImage, 0, 0, ImageSizeX, ImageSizeY, &MilDispColorImageChild);
AddColorSamples(MilSystem, MilDisplay, MilColorContext, MilDispColorImageChild);
MosPrintf(MIL_TEXT("\n\nNew product samples have been added to the color context.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n"));
MosGetch();
MbufClear(MilDispColorImageChild, M_COLOR_BLACK);
DisplayColorSamplesUtil(MilSystem, MilDisplay, MilColorContext, MilDispColorImageChild,
_HIGHLIGHT_NONE_SAMPLE);
McolSetMethod(MilColorContext, M_HISTOGRAM_MATCHING, M_DEFAULT, M_DEFAULT, M_DEFAULT);
McolAllocResult(MilSystem, M_COLOR_MATCHING_RESULT, M_DEFAULT, &MilColorResult);
MosPrintf(MIL_TEXT("\n\nThe color tool is preprocessing the color information for each sample...\n"));
MgraColor(M_DEFAULT, M_RGB888(255, 255, 255));
MgraText(M_DEFAULT, MilDispColorImageChild, 50, 50, MIL_TEXT("Preprocessing in progress..."));
McolPreprocess(MilColorContext, M_DEFAULT);
MbufClear(MilDispColorImageChild, M_COLOR_BLACK);
MosPrintf(MIL_TEXT("\nPress <Enter> to identify the product in target images.\n\n"));
MosGetch();
for(Index=0; Index<NUM_PRODUCT; Index++)
{
MbufRestore(PRODUCT_IMAGE_LIST_FILENAME[Index], MilSystem, &MilProductImage);
MbufCopy(MilProductImage, MilDispColorImageChild);
McolMatch(MilColorContext, MilProductImage, M_DEFAULT, M_NULL, MilColorResult, M_DEFAULT);
McolGetResult(MilColorResult, M_ALL, M_GENERAL,
M_BEST_MATCH_INDEX + M_TYPE_MIL_INT, &BestMatchSampleIndex);
DisplayColorSamplesUtil(MilSystem, MilDisplay, MilColorContext, MilDispColorImageChild,
BestMatchSampleIndex);
MosPrintf(MIL_TEXT("The sample product #%d has been found.\n"), (int)BestMatchSampleIndex+1);
MosPrintf(MIL_TEXT("Press <Enter> for the next target image.\n\n"));
MosGetch();
MbufFree(MilProductImage);
}
MosPrintf(MIL_TEXT("\nAll products on the conveyer have been identified.\n\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to exit.\n"));
MosGetch();
MbufFree(MilDispColorImageChild);
MbufFree(MilDispColorImage);
McolFree(MilColorResult);
McolFree(MilColorContext);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void DisplayColorSamplesUtil(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_ID MilColorContext,
MIL_ID MilDispColorImageChild,
MIL_INT ActiveIndex)
{
MIL_ID MilBufferId,
MilDispColorImage,
MilDispColorChildIcon;
const MIL_INT HALF_SAMPLES = NUM_SAMPLES/2;
MIL_INT ImageChildSizeX,
ImageChildSizeY,
IconSizeX,
IconSizeY,
NbSamples,
i,j,k;
bool InMainDisplay = (ActiveIndex==_HIGHLIGHT_ALL_SAMPLES);
MbufInquire(MilDispColorImageChild, M_PARENT_ID, &MilDispColorImage);
MbufInquire(MilDispColorImageChild, M_SIZE_X, &ImageChildSizeX);
MbufInquire(MilDispColorImageChild, M_SIZE_Y, &ImageChildSizeY);
if(InMainDisplay)
MilBufferId = MilDispColorImageChild;
else
MbufAllocColor(MilSystem, 3, ImageChildSizeX, ImageChildSizeY, 8, M_IMAGE+M_PROC, &MilBufferId);
IconSizeX = ImageChildSizeX/HALF_SAMPLES;
IconSizeY = ImageChildSizeY/HALF_SAMPLES;
McolInquire(MilColorContext, M_CONTEXT, M_NUMBER_OF_SAMPLES+M_TYPE_MIL_INT, &NbSamples);
for(i=0; i<NbSamples ; i++)
{
MdispControl(MilDisplay, M_UPDATE, M_DISABLE);
McolDraw(M_DEFAULT, MilColorContext, MilBufferId, M_DRAW_SAMPLE, M_DEFAULT, M_SAMPLE_INDEX(i), M_DEFAULT);
j=0;
k=i;
if(i>(HALF_SAMPLES-1))
{
k = k-HALF_SAMPLES;
j = 1;
}
MbufChild2d(MilDispColorImage, ImageChildSizeX+j*IconSizeX, k*IconSizeY,
IconSizeX, IconSizeY, &MilDispColorChildIcon);
MimResize(MilBufferId, MilDispColorChildIcon,
M_FILL_DESTINATION, M_FILL_DESTINATION, M_DEFAULT);
if((i!=ActiveIndex)&&(ActiveIndex!=_HIGHLIGHT_ALL_SAMPLES))
MimArith(MilDispColorChildIcon, 4, MilDispColorChildIcon, M_DIV_CONST);
if(ActiveIndex==_HIGHLIGHT_ALL_SAMPLES)
{
MdispControl(MilDisplay, M_UPDATE, M_ENABLE);
MosSleep(_SAMPLE_DISPLAY_DELAY);
}
MbufFree(MilDispColorChildIcon);
}
MdispControl(MilDisplay, M_UPDATE, M_ENABLE);
if(!InMainDisplay)
MbufFree(MilBufferId);
}
void AddColorSamples(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_ID MilColorContext,
MIL_ID MilDispColorImageChild)
{
MIL_INT i;
MIL_ID MilBufferId;
for(i=0; i<NUM_SAMPLES ; i++)
{
MbufRestore(SAMPLE_IMAGE_LIST_FILENAME[i], MilSystem, &MilBufferId);
McolDefine(MilColorContext, MilBufferId, M_DEFAULT, M_IMAGE, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT);
MbufFree(MilBufferId);
}
DisplayColorSamplesUtil(MilSystem, MilDisplay, MilColorContext, MilDispColorImageChild,
_HIGHLIGHT_ALL_SAMPLES);
};