#include <mil.h>
#include "defectdetectionprocfunc.h"
#include "defectdetectionexample.h"
static const MIL_INT WINDOW_TITLE_HEIGHT = 60;
static const MIL_INT IMAGE_SEPARATION_X = 30;
static const MIL_INT TARGET_IMAGE_SEPARATION_Y = 30;
static const MIL_DOUBLE MODEL_OFFSET_X = M_DEFAULT;
static const MIL_DOUBLE MODEL_OFFSET_Y = M_DEFAULT;
static const MIL_DOUBLE MODEL_SIZE_X = M_DEFAULT;
static const MIL_DOUBLE MODEL_SIZE_Y = M_DEFAULT;
static const MIL_INT HIST_BORDER = WINDOW_TITLE_HEIGHT;
static const MIL_INT DEFECT_SIZE = 5;
static const MIL_INT CLEAN_MORPH_SIZE = 1;
static const MIL_DOUBLE GRADIENT_MASK_SMOOTHNESS = 50;
static const MIL_DOUBLE TRIANGLE_LOWER_CUTOFF = 2;
static const MIL_DOUBLE TRIANGLE_UPPER_CUTOFF = 255;
static const MIL_DOUBLE BIN_CUMULATIVE_VALUE = 95.0;
static const MIL_DOUBLE FIXED_DIFF_THRESHOLD = 10;
static const MIL_DOUBLE NORMAL_VARIATIONS = 20;
CDefectDetectionExampleMngr::CDefectDetectionExampleMngr(MIL_CONST_TEXT_PTR SystemDescriptor)
: m_MilApplication (M_NULL),
m_MilSystem (M_NULL),
m_MilDisplay (M_NULL),
m_MilDisplayImage (M_NULL),
m_MilGraList (M_NULL),
m_MilGraContextTemplate (M_NULL),
m_MilGraContextTarget (M_NULL),
m_MilGraContextWarpedTarget (M_NULL),
m_MilGraContextDifference (M_NULL),
m_MilGraContextInspection (M_NULL),
m_MilHistDisplay (M_NULL),
m_MilHistDisplayImage (M_NULL),
m_MilHistDispGraphicList (M_NULL),
m_MilHistResult (M_NULL),
m_MilDispTemplateImage (M_NULL),
m_MilTemplateImage (M_NULL),
m_MilTargetImage (M_NULL),
m_MilWarpedTargetImage (M_NULL),
m_MilDifferenceImage (M_NULL),
m_MilInspectionImage (M_NULL),
m_MilTemplateGradientMask (M_NULL),
m_MilTemplateLumImage (M_NULL),
m_MilTargetLumImage (M_NULL),
m_MilDifferenceGrayImage (M_NULL),
m_MilStructElement (M_NULL),
m_MilModContext (M_NULL),
m_MilModResult (M_NULL),
m_MilBlobResult (M_NULL),
m_BlobDrawLabel (M_NULL),
m_MilBlobFeatureList (M_NULL),
m_MilFixturingOffset (M_NULL),
m_TemplateSizeX (0),
m_TemplateSizeY (0),
m_DiffMethod (enAbsoluteDiff),
m_BinMethod (enBiModal),
m_OverlayTransparentColor (0),
m_IsInteractive (false),
m_ImageSpacing (IMAGE_SEPARATION_X),
m_ModOccDrawStartIndex (0)
{
MappAlloc(M_NULL, M_DEFAULT, &m_MilApplication);
MsysAlloc(M_DEFAULT, SystemDescriptor, M_DEFAULT, M_DEFAULT, &m_MilSystem);
MdispAlloc(m_MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &m_MilDisplay);
MdispControl(m_MilDisplay, M_SCALE_DISPLAY, M_ENABLE);
MgraAllocList(m_MilSystem, M_DEFAULT, &m_MilGraList);
MdispControl(m_MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, m_MilGraList);
MdispAlloc(m_MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &m_MilHistDisplay);
MbufAlloc2d(m_MilSystem, 256 + 2*HIST_BORDER, 256 + 2*HIST_BORDER, 8+M_UNSIGNED, M_IMAGE+M_DISP, &m_MilHistDisplayImage);
MbufClear(m_MilHistDisplayImage, 0);
MgraAllocList(m_MilSystem, M_DEFAULT, &m_MilHistDispGraphicList);
InitHistogram();
MdispControl(m_MilHistDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, m_MilHistDispGraphicList);
MimAllocResult(m_MilSystem, 256, M_HIST_LIST, &m_MilHistResult);
MbufAlloc2d(m_MilSystem, DEFECT_SIZE + 1, DEFECT_SIZE + 1, 32+M_UNSIGNED, M_STRUCT_ELEMENT, &m_MilStructElement);
MbufClear(m_MilStructElement, 0);
MmodAlloc(m_MilSystem, M_GEOMETRIC, M_DEFAULT, &m_MilModContext);
MmodAllocResult(m_MilSystem, M_DEFAULT, &m_MilModResult);
MblobAllocResult(m_MilSystem, &m_MilBlobResult);
MblobAllocFeatureList(m_MilSystem, &m_MilBlobFeatureList);
McalAlloc(m_MilSystem, M_FIXTURING_OFFSET, M_DEFAULT, &m_MilFixturingOffset);
}
CDefectDetectionExampleMngr::~CDefectDetectionExampleMngr()
{
FreeDisplayImages();
FreeWorkImages();
if(m_MilStructElement != M_NULL)
MbufFree(m_MilStructElement);
if(m_MilModContext != M_NULL)
{
MmodFree(m_MilModContext);
MmodFree(m_MilModResult);
}
if(m_MilBlobResult != M_NULL)
{
MblobFree(m_MilBlobResult);
MblobFree(m_MilBlobFeatureList);
}
if(m_MilFixturingOffset != M_NULL)
McalFree(m_MilFixturingOffset);
if(m_MilHistResult)
MimFree(m_MilHistResult);
if(m_MilHistDispGraphicList != M_NULL)
MgraFree(m_MilHistDispGraphicList);
if(m_MilGraList != M_NULL)
MgraFree(m_MilGraList);
if(m_MilHistDisplayImage != M_NULL)
MbufFree(m_MilHistDisplayImage);
if(m_MilHistDisplay != M_NULL)
MdispFree(m_MilHistDisplay);
if (m_MilDisplay != M_NULL)
MdispFree(m_MilDisplay);
if (m_MilSystem != M_NULL)
MsysFree(m_MilSystem);
if (m_MilApplication != M_NULL)
MappFree(m_MilApplication);
}
void CDefectDetectionExampleMngr::FreeDisplayImages()
{
if (m_MilDisplayImage != M_NULL)
{
MbufFree(m_MilDispTemplateImage);
MbufFree(m_MilTargetImage);
MbufFree(m_MilWarpedTargetImage);
MbufFree(m_MilDifferenceImage);
MbufFree(m_MilInspectionImage);
MbufFree(m_MilTargetLumImage);
MgraFree(m_MilGraContextTemplate);
MgraFree(m_MilGraContextTarget);
MgraFree(m_MilGraContextWarpedTarget);
MgraFree(m_MilGraContextDifference);
MgraFree(m_MilGraContextInspection);
MbufFree(m_MilDisplayImage);
}
}
void CDefectDetectionExampleMngr::FreeWorkImages()
{
if (m_MilTemplateGradientMask != M_NULL)
{
MbufFree(m_MilTemplateGradientMask);
MbufFree(m_MilTemplateLumGradientMask);
MbufFree(m_MilTemplateLumImage);
MbufFree(m_MilDifferenceGrayImage);
MbufFree(m_MilTemplateImage);
}
}
bool CDefectDetectionExampleMngr::LoadTemplate(MIL_CONST_TEXT_PTR TemplateImagePath)
{
bool LoadStatus = false;
FreeWorkImages();
if(TemplateImagePath == M_INTERACTIVE)
MosPrintf(MIL_TEXT("Please choose a template image.\n\n"));
MappControl(M_ERROR, M_PRINT_DISABLE);
MIL_ID MilUserTemplate = MbufRestore(TemplateImagePath, m_MilSystem, M_NULL);
MappControl(M_ERROR, M_PRINT_ENABLE);
if(MilUserTemplate != M_NULL)
{
MbufInquire(MilUserTemplate, M_SIZE_X, &m_TemplateSizeX);
MbufInquire(MilUserTemplate, M_SIZE_Y, &m_TemplateSizeY);
if (m_TemplateSizeX >= 16 && m_TemplateSizeX <= 1024 &&
m_TemplateSizeY >= 16 && m_TemplateSizeY <= 1024)
{
MbufAllocColor(m_MilSystem, 3, m_TemplateSizeX, m_TemplateSizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &m_MilTemplateImage);
MbufCopy(MilUserTemplate, m_MilTemplateImage);
MbufAlloc2d(m_MilSystem, m_TemplateSizeX, m_TemplateSizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &m_MilDifferenceGrayImage);
CreateLuminanceImage(m_MilTemplateImage, m_TemplateSizeX, m_TemplateSizeY, &m_MilTemplateLumImage);
CreateGradientMaskImage(m_MilTemplateImage, m_MilTemplateLumImage, GRADIENT_MASK_SMOOTHNESS, &m_MilTemplateGradientMask, &m_MilTemplateLumGradientMask);
LoadStatus = DefineModelAndFixture(m_MilTemplateLumImage, m_MilModContext, m_MilFixturingOffset, MODEL_OFFSET_X, MODEL_OFFSET_Y, MODEL_SIZE_X, MODEL_SIZE_Y);
}
else
{
MosPrintf(MIL_TEXT("Unable to create model from the template image.\n")
MIL_TEXT("The template image sizes must be between 16 and 1024.\n\n"));
}
MbufFree(MilUserTemplate);
}
else
MosPrintf(MIL_TEXT("Unable to load template image.\n\n"));
return LoadStatus;
}
bool CDefectDetectionExampleMngr::InitializeExample(MIL_CONST_TEXT_PTR TargetImagePath, DifferenceExtractionMethod DiffMethod, BinarizationMethod BinMethod)
{
m_DiffMethod = DiffMethod;
m_BinMethod = BinMethod;
if(TargetImagePath == M_INTERACTIVE)
MosPrintf(MIL_TEXT("Please choose a target image.\n\n"));
MappControl(M_ERROR, M_PRINT_DISABLE);
MIL_ID MilUserTarget = MbufRestore(TargetImagePath, m_MilSystem, M_NULL);
MappControl(M_ERROR, M_PRINT_ENABLE);
if(MilUserTarget != M_NULL)
{
MIL_INT TargetSizeX = MbufInquire(MilUserTarget, M_SIZE_X, M_NULL);
MIL_INT TargetSizeY = MbufInquire(MilUserTarget, M_SIZE_Y, M_NULL);
ResetDisplay(TargetSizeX, TargetSizeY);
MbufCopy(m_MilTemplateImage, m_MilDispTemplateImage);
MbufCopy(MilUserTarget, m_MilTargetImage);
MbufFree(MilUserTarget);
CreateLuminanceImage(m_MilTargetImage, TargetSizeX, TargetSizeY, &m_MilTargetLumImage);
McalUniform(m_MilWarpedTargetImage, 0, 0, 1, 1, 0, M_DEFAULT);
ShowTargetAndTemplate(TargetImagePath);
return true;
}
else
{
MosPrintf(MIL_TEXT("Unable to load target image.\n\n"));
return false;
}
}
void CDefectDetectionExampleMngr::Run(MIL_CONST_TEXT_PTR CaseInformationText)
{
m_NbOccurences = FindModel(m_MilModContext, m_MilTargetLumImage, m_MilModResult);
if(m_NbOccurences > 0)
{
ShowAllOccurrences();
for(MIL_INT OccurrenceIdx = 0; OccurrenceIdx < m_NbOccurences; OccurrenceIdx++)
{
ClearDefectDetection();
AlignImageBasedOnFixture(m_MilTargetImage, m_MilWarpedTargetImage, m_MilFixturingOffset, m_MilModResult, M_RESULT_MOD, OccurrenceIdx);
ShowWarpedOccurrence(OccurrenceIdx);
DifferenceExtractionMethod DiffMethod = m_DiffMethod;
BinarizationMethod BinMethod = m_BinMethod;
MosPrintf(MIL_TEXT("%s\n\n"), CaseInformationText);
m_ChangeDiff = true;
m_ChangeBin = true;
do
{
if(m_ChangeDiff)
{
ExtractDifferences(m_MilTemplateImage,
m_MilTemplateLumImage,
m_MilTemplateGradientMask,
m_MilTemplateLumGradientMask,
m_MilWarpedTargetImage,
m_MilDifferenceGrayImage,
m_MilStructElement,
DiffMethod);
m_ChangeBin = true;
}
if(m_ChangeBin)
{
m_BinValue = ExtractDefects(m_MilDifferenceGrayImage,
m_MilBlobResult,
m_MilBlobFeatureList,
TRIANGLE_LOWER_CUTOFF,
TRIANGLE_UPPER_CUTOFF,
BIN_CUMULATIVE_VALUE,
NORMAL_VARIATIONS,
FIXED_DIFF_THRESHOLD,
CLEAN_MORPH_SIZE,
BinMethod);
}
ShowDefectDetection();
ShowHistogram();
m_ChangeBin = false;
m_ChangeDiff = false;
}while(AskForMethod(DiffMethod, BinMethod));
MdispSelect(m_MilHistDisplay, M_NULL);
}
}
else
{
MosPrintf(MIL_TEXT("No occurrence of the model were found in the target image\n")
MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
}
void CDefectDetectionExampleMngr::ResetDisplay(MIL_INT TargetSizeX, MIL_INT TargetSizeY)
{
MIL_INT InspectionChildOffsetX = 4 * IMAGE_SEPARATION_X + 3* m_TemplateSizeX;
MIL_INT DispSizeX, TargetChildOffsetX;
if(InspectionChildOffsetX + m_TemplateSizeX > TargetSizeX + IMAGE_SEPARATION_X)
{
DispSizeX = InspectionChildOffsetX + m_TemplateSizeX + IMAGE_SEPARATION_X;
TargetChildOffsetX = (DispSizeX - TargetSizeX)/2;
m_ImageSpacing = IMAGE_SEPARATION_X;
}
else
{
DispSizeX = TargetSizeX + 2 * IMAGE_SEPARATION_X;
TargetChildOffsetX = IMAGE_SEPARATION_X;
m_ImageSpacing = (DispSizeX - 4 * m_TemplateSizeX)/5;
}
MIL_INT WarpedChildOffsetX = 2 * m_ImageSpacing + m_TemplateSizeX;
MIL_INT DifferenceChildOffsetX = WarpedChildOffsetX + m_ImageSpacing + m_TemplateSizeX;
InspectionChildOffsetX = DifferenceChildOffsetX + m_ImageSpacing + m_TemplateSizeX;
MIL_INT TargetChildOffsetY = 3 * WINDOW_TITLE_HEIGHT + m_TemplateSizeY + TARGET_IMAGE_SEPARATION_Y;
MIL_INT DispSizeY = TargetChildOffsetY + TargetSizeY + IMAGE_SEPARATION_X;
FreeDisplayImages();
MgraClear(M_DEFAULT, m_MilGraList);
MbufAllocColor(m_MilSystem, 3, DispSizeX, DispSizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, &m_MilDisplayImage);
MbufChild2d(m_MilDisplayImage, m_ImageSpacing, WINDOW_TITLE_HEIGHT, m_TemplateSizeX, m_TemplateSizeY, &m_MilDispTemplateImage);
MbufChild2d(m_MilDisplayImage, TargetChildOffsetX, TargetChildOffsetY, TargetSizeX, TargetSizeY, &m_MilTargetImage);
MbufChild2d(m_MilDisplayImage, WarpedChildOffsetX, WINDOW_TITLE_HEIGHT, m_TemplateSizeX, m_TemplateSizeY, &m_MilWarpedTargetImage);
MbufChild2d(m_MilDisplayImage, DifferenceChildOffsetX, WINDOW_TITLE_HEIGHT, m_TemplateSizeX, m_TemplateSizeY, &m_MilDifferenceImage);
MbufChild2d(m_MilDisplayImage, InspectionChildOffsetX, WINDOW_TITLE_HEIGHT, m_TemplateSizeX, m_TemplateSizeY, &m_MilInspectionImage);
MbufClear(m_MilDisplayImage, 0);
MdispSelect(m_MilDisplay, m_MilDisplayImage);
MgraAlloc(m_MilSystem, &m_MilGraContextTemplate);
MgraControl(m_MilGraContextTemplate, M_DRAW_OFFSET_X, - (MIL_DOUBLE)m_ImageSpacing);
MgraControl(m_MilGraContextTemplate, M_DRAW_OFFSET_Y, - WINDOW_TITLE_HEIGHT);
MgraAlloc(m_MilSystem, &m_MilGraContextTarget);
MgraControl(m_MilGraContextTarget, M_DRAW_OFFSET_X, - (MIL_DOUBLE)TargetChildOffsetX);
MgraControl(m_MilGraContextTarget, M_DRAW_OFFSET_Y, - (MIL_DOUBLE)TargetChildOffsetY);
MgraAlloc(m_MilSystem, &m_MilGraContextWarpedTarget);
MgraControl(m_MilGraContextWarpedTarget, M_DRAW_OFFSET_X, - (MIL_DOUBLE)WarpedChildOffsetX);
MgraControl(m_MilGraContextWarpedTarget, M_DRAW_OFFSET_Y, - WINDOW_TITLE_HEIGHT);
MgraAlloc(m_MilSystem, &m_MilGraContextDifference);
MgraControl(m_MilGraContextDifference, M_DRAW_OFFSET_X, - (MIL_DOUBLE)DifferenceChildOffsetX);
MgraControl(m_MilGraContextDifference, M_DRAW_OFFSET_Y, - WINDOW_TITLE_HEIGHT);
MgraAlloc(m_MilSystem, &m_MilGraContextInspection);
MgraControl(m_MilGraContextInspection, M_DRAW_OFFSET_X, - (MIL_DOUBLE)InspectionChildOffsetX);
MgraControl(m_MilGraContextInspection, M_DRAW_OFFSET_Y, - WINDOW_TITLE_HEIGHT);
DrawDisplayChildInfo(m_MilGraContextTemplate , m_TemplateSizeX, m_TemplateSizeY, MIL_TEXT("Template Image"));
DrawDisplayChildInfo(m_MilGraContextTarget , TargetSizeX , TargetSizeY , MIL_TEXT("Target Image"));
DrawDisplayChildInfo(m_MilGraContextWarpedTarget, m_TemplateSizeX, m_TemplateSizeY, MIL_TEXT("Warped Target"));
DrawDisplayChildInfo(m_MilGraContextDifference , m_TemplateSizeX, m_TemplateSizeY, MIL_TEXT("Difference Image"));
DrawDisplayChildInfo(m_MilGraContextInspection , m_TemplateSizeX, m_TemplateSizeY, MIL_TEXT("Inspection Image"));
}
void CDefectDetectionExampleMngr::DrawDisplayChildInfo(MIL_ID MilGraContext, MIL_INT ImageSizeX, MIL_INT ImageSizeY, MIL_CONST_TEXT_PTR ImageName)
{
MgraControl(MilGraContext, M_TEXT_ALIGN_HORIZONTAL, M_CENTER);
MgraControl(MilGraContext, M_TEXT_ALIGN_VERTICAL, M_BOTTOM);
MgraColor(MilGraContext, M_COLOR_CYAN);
MgraText(MilGraContext, m_MilGraList, ImageSizeX/2, -WINDOW_TITLE_HEIGHT/2, ImageName);
MgraColor(MilGraContext, M_COLOR_WHITE);
MgraRectAngle(MilGraContext, m_MilGraList, ImageSizeX/2, ImageSizeY/2, ImageSizeX + m_ImageSpacing, ImageSizeY + WINDOW_TITLE_HEIGHT, 0, M_CENTER_AND_DIMENSION);
}
void CDefectDetectionExampleMngr::CreateLuminanceImage(MIL_ID MilRGBSrc, MIL_INT SizeX, MIL_INT SizeY, MIL_ID *MilLumImagePtr)
{
MbufAlloc2d(m_MilSystem, SizeX, SizeY, 8+M_UNSIGNED, M_IMAGE+M_PROC, MilLumImagePtr);
MimConvert(MilRGBSrc, *MilLumImagePtr, M_RGB_TO_L);
}
void CDefectDetectionExampleMngr::ShowTargetAndTemplate(MIL_CONST_TEXT_PTR TargetImagePath)
{
MIL_CONST_TEXT_PTR TargetImageName = GetFileName(TargetImagePath);
MosPrintf(MIL_TEXT("The target image %s has been loaded \n"), TargetImageName);
MosPrintf(MIL_TEXT("and is shown in the display.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraColor(m_MilGraContextTemplate, M_COLOR_YELLOW);
McalDraw(m_MilGraContextTemplate, m_MilFixturingOffset, m_MilGraList, M_DRAW_FIXTURING_OFFSET, M_DEFAULT, M_DEFAULT);
MgraColor(m_MilGraContextTemplate, M_COLOR_GREEN);
McalDraw(m_MilGraContextTemplate, M_NULL, m_MilGraList, M_DRAW_PIXEL_COORDINATE_SYSTEM+M_DRAW_FRAME, M_DEFAULT, M_DEFAULT);
MgraColor(m_MilGraContextTemplate, M_COLOR_RED);
MmodDraw(m_MilGraContextTemplate, m_MilModContext, m_MilGraList, M_DRAW_EDGES, M_DEFAULT, M_ORIGINAL);
MosPrintf(MIL_TEXT("The model's edges and the fixturing offset are shown in the \n"));
MosPrintf(MIL_TEXT("template display.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
void CDefectDetectionExampleMngr::ShowAllOccurrences()
{
MgraColor(m_MilGraContextTarget, M_COLOR_GREEN);
for(MIL_INT i = 0; i < m_NbOccurences; i++)
{
MmodDraw(m_MilGraContextTarget, m_MilModResult, m_MilGraList, M_DRAW_BOX, i, M_DEFAULT);
MmodDraw(m_MilGraContextTarget, m_MilModResult, m_MilGraList, M_DRAW_POSITION, i, M_DEFAULT);
if(i == 0)
m_ModOccDrawStartIndex = MgraInquireList(m_MilGraList, M_LIST, M_DEFAULT, M_NUMBER_OF_GRAPHICS, M_NULL) - 2;
}
MosPrintf(MIL_TEXT("The occurrences found in the target image are shown in the target\n"));
MosPrintf(MIL_TEXT("display.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
void CDefectDetectionExampleMngr::ShowWarpedOccurrence(MIL_INT OccurrenceIdx)
{
for(MIL_INT i = 0, GraphicIndex = m_ModOccDrawStartIndex; i < m_NbOccurences; i++)
{
MgraControlList(m_MilGraList, M_GRAPHIC_INDEX(GraphicIndex++), M_DEFAULT, M_COLOR, i == OccurrenceIdx? M_COLOR_RED : M_COLOR_GREEN);
MgraControlList(m_MilGraList, M_GRAPHIC_INDEX(GraphicIndex++), M_DEFAULT, M_COLOR, i == OccurrenceIdx? M_COLOR_RED : M_COLOR_GREEN);
}
MgraColor(m_MilGraContextTarget, M_COLOR_RED);
McalDraw(m_MilGraContextTarget, m_MilTargetImage, m_MilGraList, M_DRAW_RELATIVE_COORDINATE_SYSTEM+M_DRAW_FRAME, M_DEFAULT, M_DEFAULT);
MgraColor(m_MilGraContextWarpedTarget, M_COLOR_RED);
McalDraw(m_MilGraContextWarpedTarget, m_MilWarpedTargetImage, m_MilGraList, M_DRAW_RELATIVE_COORDINATE_SYSTEM+M_DRAW_FRAME, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("The occurrence %i has been warped.\n"), (int)OccurrenceIdx);
MosPrintf(MIL_TEXT("Will now proceed with the defect detection.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
void CDefectDetectionExampleMngr::ShowDefectDetection()
{
if(m_ChangeDiff)
{
MbufCopy(m_MilDifferenceGrayImage, m_MilDifferenceImage);
MbufCopy(m_MilDifferenceGrayImage, m_MilInspectionImage);
}
if(m_ChangeBin)
{
if(m_BlobDrawLabel)
MgraControlList(m_MilGraList, M_GRAPHIC_LABEL(m_BlobDrawLabel), M_DEFAULT, M_DELETE, M_DEFAULT);
MgraColor(m_MilGraContextInspection, M_COLOR_RED);
MblobDraw(m_MilGraContextInspection, m_MilBlobResult, m_MilGraList, M_DRAW_BLOBS, M_DEFAULT, M_DEFAULT);
MgraInquireList(m_MilGraList, M_LIST, M_DEFAULT, M_LAST_LABEL, &m_BlobDrawLabel);
}
}
void CDefectDetectionExampleMngr::ClearDefectDetection()
{
MIL_INT NbGraphics = MgraInquireList(m_MilGraList, M_LIST, M_DEFAULT, M_NUMBER_OF_GRAPHICS, M_NULL);
for(MIL_INT GraphicIndex = m_ModOccDrawStartIndex + m_NbOccurences*2; GraphicIndex < NbGraphics; GraphicIndex++)
MgraControlList(m_MilGraList, M_GRAPHIC_INDEX(GraphicIndex), M_DEFAULT, M_DELETE, M_DEFAULT);
m_BlobDrawLabel = M_NULL;
}
void CDefectDetectionExampleMngr::PrintMethodChoices(MIL_CONST_TEXT_PTR MethodTypeTag,
MIL_CONST_TEXT_PTR *MethodsTags,
MIL_INT NbMethod,
MIL_TEXT_CHAR StartChar)
{
MosPrintf(MIL_TEXT("%s method:\n"), MethodTypeTag);
for(MIL_INT i=0; i<NbMethod; i++)
MosPrintf(MIL_TEXT(" %c. %s\n"), StartChar+i, MethodsTags[i]);
MosPrintf(MIL_TEXT("\n"));
}
void CDefectDetectionExampleMngr::PrintCurrentMethods(MIL_TEXT_CHAR DiffStartChar, MIL_TEXT_CHAR BinStartChar, DifferenceExtractionMethod DiffMethod, BinarizationMethod BinMethod)
{
MosPrintf(MIL_TEXT("Current methods for difference extraction and binarization:\n"));
MosPrintf(MIL_TEXT("%c. %s\n"), DiffStartChar+DiffMethod, DifferenceExtractionMethodsTags[DiffMethod]);
MosPrintf(MIL_TEXT("%c. %s\n\n"), BinStartChar+BinMethod, BinarizationMethodsTags[BinMethod]);
}
bool CDefectDetectionExampleMngr::AskForInteractive()
{
MosPrintf(MIL_TEXT("Do you want to run the example in interactive mode? (Y or N)\n\n"));
do
{
switch(MosGetch())
{
case 'y':
case 'Y':
m_IsInteractive = true;
return true;
case 'n':
case 'N':
return false;
}
} while(1);
return true;
}
bool CDefectDetectionExampleMngr::AskForMethod(DifferenceExtractionMethod &DiffMethod, BinarizationMethod &BinMethod)
{
if(m_IsInteractive)
{
PrintCurrentMethods(MIL_TEXT('1'), MIL_TEXT('A'), DiffMethod, BinMethod);
PrintMethodChoices(MIL_TEXT("Difference extraction"), DifferenceExtractionMethodsTags, enNbDiffMethod, MIL_TEXT('1'));
PrintMethodChoices(MIL_TEXT("Binarization"), BinarizationMethodsTags, enNbBinMethod, MIL_TEXT('A'));
MosPrintf(MIL_TEXT("Other:\n"));
MosPrintf(MIL_TEXT(" Q. Continue\n\n"));
MosPrintf(MIL_TEXT("Your choice : "));
MIL_TEXT_CHAR Choice;
while(!m_ChangeDiff && !m_ChangeBin)
{
Choice = (MIL_TEXT_CHAR)MosGetch();
switch(Choice)
{
case '1':
m_ChangeDiff = true;
DiffMethod = enAbsoluteDiff;
break;
case '2':
m_ChangeDiff = true;
DiffMethod = enColDistance;
break;
case '3':
m_ChangeDiff = true;
DiffMethod = enBottomHat;
break;
case '4':
m_ChangeDiff = true;
DiffMethod = enTopHat;
break;
case 'a':
case 'A':
m_ChangeBin = true;
BinMethod = enBiModal;
break;
case 'b':
case 'B':
m_ChangeBin = true;
BinMethod = enTriangleBisection;
break;
case 'c':
case 'C':
m_ChangeBin = true;
BinMethod = enCumulHistPercentage;
break;
case 'd':
case 'D':
m_ChangeBin = true;
BinMethod = enFixed;
break;
case 'q':
case 'Q':
MosPrintf(MIL_TEXT("Q. Continue\n\n"));
return false;
default:
break;
}
}
if(m_ChangeDiff)
MosPrintf(MIL_TEXT("%c. %s\n\n"), Choice, DifferenceExtractionMethodsTags[DiffMethod]);
else if(m_ChangeBin)
MosPrintf(MIL_TEXT("%c. %s\n\n"), Choice, BinarizationMethodsTags[BinMethod]);
return true;
}
else
{
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
return false;
}
}
void CDefectDetectionExampleMngr::ShowHistogram()
{
MimHistogram(m_MilDifferenceGrayImage, m_MilHistResult);
MIL_INT Histogram[256];
MimGetResult(m_MilHistResult, M_VALUE+M_TYPE_MIL_INT, Histogram);
MIL_INT MaxHistValue = -MIL_INT_MAX;
for(MIL_INT i = ((MIL_INT) TRIANGLE_LOWER_CUTOFF); i < 256; i++)
MaxHistValue = Histogram[i] > MaxHistValue ? Histogram[i] : MaxHistValue;
if(MaxHistValue < 1) { MaxHistValue = 1; }
MIL_DOUBLE YPixelScale = 256.0 / MaxHistValue;
MdispControl(m_MilHistDisplay, M_UPDATE_GRAPHIC_LIST, M_DISABLE);
for(MIL_INT i = 0; i < 256; i++)
{
MIL_DOUBLE Width = Histogram[i]*YPixelScale;
MgraControlList(m_MilHistDispGraphicList,
M_GRAPHIC_INDEX(i),
M_DEFAULT,
M_RECTANGLE_WIDTH,
Width > 256.0 ? 256.0 : Width);
}
MgraControlList(m_MilHistDispGraphicList, M_GRAPHIC_INDEX(256), M_DEFAULT, M_POSITION_X, (MIL_DOUBLE) (HIST_BORDER + m_BinValue + 0.5));
MIL_TEXT_CHAR BinValueString[4];
MosSprintf(BinValueString, 4, MIL_TEXT("%i"), (int)m_BinValue);
MgraControlList(m_MilHistDispGraphicList, M_GRAPHIC_INDEX(260), M_DEFAULT, M_DELETE, M_DEFAULT);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_CENTER);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_VERTICAL, M_TOP);
MgraColor(M_DEFAULT, M_COLOR_BLUE);
MgraText(M_DEFAULT, m_MilHistDispGraphicList, HIST_BORDER + m_BinValue + 0.5, 256 + HIST_BORDER + 5, BinValueString);
MdispControl(m_MilHistDisplay, M_UPDATE_GRAPHIC_LIST, M_ENABLE);
MdispSelect(m_MilHistDisplay, m_MilHistDisplayImage);
}
void CDefectDetectionExampleMngr::InitHistogram()
{
MgraColor(M_DEFAULT, M_COLOR_GREEN);
for(MIL_INT i = 0; i < 256; i++)
MgraRectAngle(M_DEFAULT, m_MilHistDispGraphicList, i+HIST_BORDER, 256.0 + HIST_BORDER, 1, 1, 90, M_CORNER_AND_DIMENSION + M_FILLED);
MgraColor(M_DEFAULT, M_COLOR_BLUE);
MgraLine(M_DEFAULT, m_MilHistDispGraphicList, HIST_BORDER, HIST_BORDER/2, HIST_BORDER, 261.0+HIST_BORDER);
MgraColor(M_DEFAULT, M_COLOR_RED);
MgraLine(M_DEFAULT, m_MilHistDispGraphicList, HIST_BORDER, 256.0 + HIST_BORDER, HIST_BORDER, HIST_BORDER/2);
MgraLine(M_DEFAULT, m_MilHistDispGraphicList, HIST_BORDER, 256.0 + HIST_BORDER, 256.0 + 3*HIST_BORDER/2, 256.0 + HIST_BORDER);
MgraColor(M_DEFAULT, M_COLOR_CYAN);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_CENTER);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_VERTICAL, M_TOP);
MgraText(M_DEFAULT, m_MilHistDispGraphicList, 128.0 + HIST_BORDER, 0, MIL_TEXT("Histogram"));
MgraControl(M_DEFAULT, M_TEXT_ALIGN_HORIZONTAL, M_CENTER);
MgraControl(M_DEFAULT, M_TEXT_ALIGN_VERTICAL, M_TOP);
MgraText(M_DEFAULT, m_MilHistDispGraphicList, HIST_BORDER, 256.0 + HIST_BORDER + 5, MIL_TEXT(""));
}