#include <mil.h>
#define CONTOUR_IMAGE M_IMAGE_PATH MIL_TEXT("Wafer.mim")
void HorizontalBlurImage(MIL_ID MilSystem, MIL_ID MilImage, MIL_INT Size);
#define DERIVATIVE_X 0
#define DERIVATIVE_Y 1
MIL_ID CreateDerivativeKernel(MIL_ID MilSystem, MIL_INT DimX, MIL_INT DimY, MIL_INT KernelType);
void CalculateChildDerivative(MIL_ID MilImage,
MIL_ID Derivative,
MIL_ID Kernel,
MIL_INT OffsetX,
MIL_INT OffsetY,
MIL_INT SizeX,
MIL_INT SizeY);
#define ROI1_OFFSET_X 280
#define ROI1_OFFSET_Y 55
#define ROI1_SIZE_X 100
#define ROI1_SIZE_Y 120
#define ROI2_OFFSET_X 235
#define ROI2_OFFSET_Y 325
#define ROI2_SIZE_X 60
#define ROI2_SIZE_Y 60
#define ROI3_OFFSET_X 50
#define ROI3_OFFSET_Y 50
#define ROI3_SIZE_X 135
#define ROI3_SIZE_Y 65
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilImage,
GraphicList,
MilEdgeContext,
MilEdgeResult;
MIL_ID DerivativeKernel,
DerivativeX,
DerivativeY;
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);
MbufRestore(CONTOUR_IMAGE, MilSystem, &MilImage);
HorizontalBlurImage(MilSystem, MilImage, 10);
MdispSelect(MilDisplay, MilImage);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MgraControl(M_DEFAULT, M_BACKGROUND_MODE, M_TRANSPARENT);
MosPrintf(MIL_TEXT("\nEDGE MODULE:\n"));
MosPrintf(MIL_TEXT("------------\n\n"));
MosPrintf(MIL_TEXT("This program extracts image contours in ROIs using\n")
MIL_TEXT("user-defined custom partial derivatives.\n"));
MbufInquire(MilImage, M_SIZE_X, &SizeX);
MbufInquire(MilImage, M_SIZE_Y, &SizeY);
MbufAlloc2d(MilSystem, SizeX, SizeY, 16 + M_SIGNED, M_IMAGE + M_PROC, &DerivativeX);
MbufClear(DerivativeX, 0);
MbufAlloc2d(MilSystem, SizeX, SizeY, 16 + M_SIGNED, M_IMAGE + M_PROC, &DerivativeY);
MbufClear(DerivativeY, 0);
MgraText(M_DEFAULT, GraphicList, ROI1_OFFSET_X + 1, ROI1_OFFSET_Y + 1, MIL_TEXT("1"));
MgraRect(M_DEFAULT, GraphicList, ROI1_OFFSET_X, ROI1_OFFSET_Y, ROI1_OFFSET_X + ROI1_SIZE_X, ROI1_OFFSET_Y + ROI1_SIZE_Y);
DerivativeKernel = CreateDerivativeKernel(MilSystem, 2, 5, DERIVATIVE_Y);
CalculateChildDerivative(MilImage, DerivativeY, DerivativeKernel, ROI1_OFFSET_X, ROI1_OFFSET_Y, ROI1_SIZE_X, ROI1_SIZE_Y);
MbufFree(DerivativeKernel);
MgraText(M_DEFAULT, GraphicList, ROI2_OFFSET_X + 1, ROI2_OFFSET_Y + 1, MIL_TEXT("2"));
MgraRect(M_DEFAULT, GraphicList, ROI2_OFFSET_X, ROI2_OFFSET_Y, ROI2_OFFSET_X + ROI2_SIZE_X, ROI2_OFFSET_Y + ROI2_SIZE_Y);
DerivativeKernel = CreateDerivativeKernel(MilSystem, 15, 2, DERIVATIVE_X);
CalculateChildDerivative(MilImage, DerivativeX, DerivativeKernel, ROI2_OFFSET_X, ROI2_OFFSET_Y, ROI2_SIZE_X, ROI2_SIZE_Y);
MbufFree(DerivativeKernel);
MgraText(M_DEFAULT, GraphicList, ROI3_OFFSET_X + 1, ROI3_OFFSET_Y + 1, MIL_TEXT("3"));
MgraRect(M_DEFAULT, GraphicList, ROI3_OFFSET_X, ROI3_OFFSET_Y, ROI3_OFFSET_X + ROI3_SIZE_X, ROI3_OFFSET_Y + ROI3_SIZE_Y);
DerivativeKernel = CreateDerivativeKernel(MilSystem, 15, 5, DERIVATIVE_X);
CalculateChildDerivative(MilImage, DerivativeX, DerivativeKernel, ROI3_OFFSET_X, ROI3_OFFSET_Y, ROI3_SIZE_X, ROI3_SIZE_Y);
MbufFree(DerivativeKernel);
DerivativeKernel = CreateDerivativeKernel(MilSystem, 5, 5, DERIVATIVE_Y);
CalculateChildDerivative(MilImage, DerivativeY, DerivativeKernel, ROI3_OFFSET_X, ROI3_OFFSET_Y, ROI3_SIZE_X, ROI3_SIZE_Y);
MbufFree(DerivativeKernel);
MedgeAlloc(MilSystem, M_CONTOUR, M_DEFAULT, &MilEdgeContext);
MedgeAllocResult(MilSystem, M_DEFAULT, &MilEdgeResult);
MedgeCalculate(MilEdgeContext, M_NULL, DerivativeX, DerivativeY, M_NULL, MilEdgeResult, M_DEFAULT);
MgraColor(M_DEFAULT, M_COLOR_RED);
MedgeDraw(M_DEFAULT, MilEdgeResult, GraphicList, M_DRAW_EDGES, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("\nThe extracted edges are displayed.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n"));
MosGetch();
MgraFree(GraphicList);
MbufFree(MilImage);
MbufFree(DerivativeX);
MbufFree(DerivativeY);
MedgeFree(MilEdgeContext);
MedgeFree(MilEdgeResult);
MdispFree(MilDisplay);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
void HorizontalBlurImage(MIL_ID MilSystem, MIL_ID MilImage, MIL_INT Size)
{
MIL_ID Kernel;
MbufAlloc1d(MilSystem, Size, 8 + M_UNSIGNED, M_KERNEL, &Kernel);
MbufClear(Kernel, 1);
MbufControl(Kernel, M_NORMALIZATION_FACTOR, Size);
MimConvolve(MilImage, MilImage, Kernel);
MbufFree(Kernel);
}
MIL_ID CreateDerivativeKernel(MIL_ID MilSystem, MIL_INT DimX, MIL_INT DimY, MIL_INT KernelType)
{
MIL_ID Kernel;
MIL_ID KernelChild;
MIL_INT KernelSizeX = (DimX / 2) * 2 + 1;
MIL_INT KernelSizeY = (DimY / 2) * 2 + 1;
MbufAlloc2d(MilSystem, KernelSizeX, KernelSizeY, 8 + M_SIGNED, M_KERNEL, &Kernel);
MbufClear(Kernel, 0);
if (KernelType == DERIVATIVE_X)
{
MbufChild2d(Kernel, 0, 0, KernelSizeX / 2, KernelSizeY, &KernelChild);
MbufClear(KernelChild, -1);
MbufChildMove(KernelChild, KernelSizeX / 2 + 1, 0, KernelSizeX / 2, KernelSizeY, M_DEFAULT);
MbufClear(KernelChild, +1);
MbufFree(KernelChild);
}
else if (KernelType == DERIVATIVE_Y)
{
MbufChild2d(Kernel, 0, 0, KernelSizeX, KernelSizeY / 2, &KernelChild);
MbufClear(KernelChild, -1);
MbufChildMove(KernelChild, 0, KernelSizeY / 2 + 1, KernelSizeX, KernelSizeY / 2, M_DEFAULT);
MbufClear(KernelChild, +1);
MbufFree(KernelChild);
}
else
{
MosPrintf(MIL_TEXT("\n WARNING: Invalid function 'KernelType' value.\n"));
}
MIL_INT NormFactor = (KernelSizeX * KernelSizeY) / 2;
MbufControl(Kernel, M_NORMALIZATION_FACTOR, NormFactor);
MbufControl(Kernel, M_OVERSCAN, M_TRANSPARENT);
return Kernel;
}
void CalculateChildDerivative(MIL_ID MilImage,
MIL_ID Derivative,
MIL_ID Kernel,
MIL_INT OffsetX,
MIL_INT OffsetY,
MIL_INT SizeX,
MIL_INT SizeY)
{
MIL_ID MilImageChild, DerivativeChild;
MbufChild2d(MilImage, OffsetX, OffsetY, SizeX, SizeY, &MilImageChild);
MbufChild2d(Derivative, OffsetX, OffsetY, SizeX, SizeY, &DerivativeChild);
MimConvolve(MilImageChild, DerivativeChild, Kernel);
MbufFree(MilImageChild);
MbufFree(DerivativeChild);
}