#include <mil.h>
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n"));
MosPrintf(MIL_TEXT("DeadPixelCorrection\n\n"));
MosPrintf(MIL_TEXT("[SYNOPSIS]\n"));
MosPrintf(MIL_TEXT("This example shows how to correct dead pixels in an image.\n\n"));
MosPrintf(MIL_TEXT("[MODULES USED]\n"));
MosPrintf(MIL_TEXT("Modules used: application, system, display, buffer, graphic, image processing.\n"));
}
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("CircuitsBoard.mim")
#define BOX_SIZE 12.0
void DrawBoxAroundDeadPixels(MIL_ID DisplayOverlay,
MIL_DOUBLE BoxColor,
MIL_INT NumberOfDeadPixel,
MIL_INT PixelPosX[],
MIL_INT PixelPosY[]);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilDeadPixelContext,
MilOverlayImage,
MilSourceImage;
MIL_INT DeadPixelArrayX[] = {100, 65, 245, 404, 404, 404, 404, 404, 404, 405, 403, 403, 404, 145, 123, 123, 481, 476};
MIL_INT DeadPixelArrayY[] = {150, 156, 168, 113, 114, 115, 116, 117, 118, 118, 118, 119, 119, 32, 124, 123, 442, 476};
MIL_INT NumberOfDeadPixel = sizeof(DeadPixelArrayX)/sizeof(MIL_INT);
MappAlloc(M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
PrintHeader();
MbufRestore(IMAGE_FILE, MilSystem, &MilSourceImage);
MgraColor(M_DEFAULT, M_COLOR_WHITE);
MgraDots(M_DEFAULT, MilSourceImage, NumberOfDeadPixel, DeadPixelArrayX, DeadPixelArrayY, M_DEFAULT);
MdispSelect(MilDisplay, MilSourceImage);
MdispControl(MilDisplay, M_OVERLAY, M_ENABLE);
MilOverlayImage = MdispInquire(MilDisplay, M_OVERLAY_ID, M_NULL);
DrawBoxAroundDeadPixels(MilOverlayImage, M_COLOR_RED, NumberOfDeadPixel, DeadPixelArrayX, DeadPixelArrayY);
MosPrintf(MIL_TEXT("A source image with dead pixels is displayed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimAlloc(MilSystem, M_DEAD_PIXEL_CONTEXT, M_DEFAULT, &MilDeadPixelContext);
MimPut(MilDeadPixelContext, M_XY_DEAD_PIXELS+M_TYPE_MIL_INT , NumberOfDeadPixel, DeadPixelArrayX, DeadPixelArrayY, M_DEFAULT);
MimDeadPixelCorrection(MilDeadPixelContext, MilSourceImage, MilSourceImage, M_DEFAULT);
DrawBoxAroundDeadPixels(MilOverlayImage, M_COLOR_GREEN, NumberOfDeadPixel, DeadPixelArrayX, DeadPixelArrayY);
MosPrintf(MIL_TEXT("The dead pixels have been corrected.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to terminate.\n\n"));
MosGetch();
MimFree(MilDeadPixelContext);
MbufFree(MilSourceImage);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void DrawBoxAroundDeadPixels(MIL_ID DisplayOverlay,
MIL_DOUBLE BoxColor,
MIL_INT NumberOfDeadPixel,
MIL_INT PixelPosX[],
MIL_INT PixelPosY[])
{
bool* ActivePixel = new bool[NumberOfDeadPixel];
for (MIL_INT Pixel = 0; Pixel < NumberOfDeadPixel; Pixel++)
ActivePixel[Pixel]=true;
MgraColor(M_DEFAULT, BoxColor);
for (MIL_INT Pixel = 0; Pixel < NumberOfDeadPixel; Pixel++)
{
if(ActivePixel[Pixel])
{
MIL_DOUBLE BoxStartX = PixelPosX[Pixel]-BOX_SIZE;
MIL_DOUBLE BoxStartY = PixelPosY[Pixel]-BOX_SIZE;
MIL_DOUBLE BoxEndX = PixelPosX[Pixel]+BOX_SIZE;
MIL_DOUBLE BoxEndY = PixelPosY[Pixel]+BOX_SIZE;
MgraRect(M_DEFAULT, DisplayOverlay, BoxStartX, BoxStartY, BoxEndX, BoxEndY);
for (MIL_INT NextPixel = Pixel+1; NextPixel < NumberOfDeadPixel; NextPixel++)
{
bool InBoxRangeX = (PixelPosX[NextPixel]>BoxStartX)&&(PixelPosX[NextPixel]<BoxEndX);
bool InBoxRangeY = (PixelPosY[NextPixel]>BoxStartY)&&(PixelPosY[NextPixel]<BoxEndY);
ActivePixel[NextPixel]=!(InBoxRangeX&&InBoxRangeY);
}
}
}
delete[] ActivePixel;
}