#include <mil.h>
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("/Preprocessing/InterlacedBird.mim")
#define MOTION_DETECT_NUM_FRAMES 3
#define MOTION_DETECT_THRESHOLD 20
#define NUM_GRAB_IMAGES (MOTION_DETECT_NUM_FRAMES+1)
#define GRAB_DEINTERLACE_METHOD M_DISCARD
void GrabDeinterlace(MIL_ID MilSystem);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay,
MilDisplayImage,
MilSourceImage,
MilDeinterlaceContext;
MIL_INT ImageSizeBand,
ImageWidth,
ImageHeight,
ImageType;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MbufRestore(IMAGE_FILE, MilSystem, &MilSourceImage);
MbufAllocColor(MilSystem, MbufInquire(MilSourceImage, M_SIZE_BAND, &ImageSizeBand),
MbufInquire(MilSourceImage, M_SIZE_X , &ImageWidth),
MbufInquire(MilSourceImage, M_SIZE_Y , &ImageHeight),
MbufInquire(MilSourceImage, M_TYPE , &ImageType),
M_IMAGE+M_PROC+M_DISP, &MilDisplayImage);
MbufCopy(MilSourceImage, MilDisplayImage);
MdispSelect(MilDisplay, MilDisplayImage);
MimAlloc(MilSystem, M_DEINTERLACE_CONTEXT, M_DEFAULT, &MilDeinterlaceContext);
MosPrintf(MIL_TEXT("\nDEINTERLACING:\n"));
MosPrintf(MIL_TEXT("----------------\n\n"));
MosPrintf(MIL_TEXT("This image has been grabbed using an interlaced camera.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimControl(MilDeinterlaceContext, M_DEINTERLACE_TYPE, M_DISCARD);
MimDeinterlace(MilDeinterlaceContext, &MilSourceImage, &MilDisplayImage, 1, 1, M_DEFAULT);
MosPrintf(MIL_TEXT("The image has been deinterlaced using the DISCARD algorithm.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilSourceImage);
MbufFree(MilDisplayImage);
MdispFree(MilDisplay);
MimFree(MilDeinterlaceContext);
if (MsysInquire(MilSystem, M_DIGITIZER_NUM, M_NULL))
{
GrabDeinterlace(MilSystem);
}
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
void GrabDeinterlace(MIL_ID MilSystem)
{
MIL_ID MilDigitizer,
MilDisplay,
MilDisplayImage,
MilDeinterlaceContext,
MilGrabImages[NUM_GRAB_IMAGES],
MilPreviousImages[MOTION_DETECT_NUM_FRAMES];
MIL_INT ImageSizeBand,
ImageWidth,
ImageHeight;
MIL_INT i,
CurrentGrabIndex,
NextGrabIndex;
MdigAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDigitizer);
if (MilDigitizer == M_NULL)
return;
MdigInquire(MilDigitizer, M_SIZE_BAND, &ImageSizeBand);
MdigInquire(MilDigitizer, M_SIZE_X , &ImageWidth );
MdigInquire(MilDigitizer, M_SIZE_Y , &ImageHeight );
for (i = 0; i < NUM_GRAB_IMAGES; i++)
{
MbufAllocColor(MilSystem,
ImageSizeBand,
ImageWidth,
ImageHeight,
8+M_UNSIGNED,
M_IMAGE+M_GRAB+M_PROC,
&MilGrabImages[i]);
}
MimAlloc(MilSystem, M_DEINTERLACE_CONTEXT, M_DEFAULT, &MilDeinterlaceContext);
MbufAllocColor(MilSystem,
ImageSizeBand,
ImageWidth,
ImageHeight,
8+M_UNSIGNED,
M_IMAGE+M_GRAB+M_DISP+M_PROC,
&MilDisplayImage);
MdispAlloc(MilSystem,
M_DEFAULT,
MIL_TEXT("M_DEFAULT"),
M_DEFAULT,
&MilDisplay);
MbufClear(MilDisplayImage, 0);
MdispSelect(MilDisplay, MilDisplayImage);
MosPrintf(MIL_TEXT("Normal live grab is displayed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MdigGrabContinuous(MilDigitizer, MilDisplayImage);
MosGetch();
MdigHalt(MilDigitizer);
MimControl(MilDeinterlaceContext, M_DEINTERLACE_TYPE, GRAB_DEINTERLACE_METHOD);
if (GRAB_DEINTERLACE_METHOD == M_ADAPTIVE_DISCARD ||
GRAB_DEINTERLACE_METHOD == M_ADAPTIVE_AVERAGE)
{
MimControl(MilDeinterlaceContext, M_MOTION_DETECT_NUM_FRAMES, MOTION_DETECT_NUM_FRAMES);
MimControl(MilDeinterlaceContext, M_MOTION_DETECT_THRESHOLD , MOTION_DETECT_THRESHOLD );
}
MdigControl(MilDigitizer, M_GRAB_MODE, M_ASYNCHRONOUS);
MdigGrab(MilDigitizer, MilGrabImages[0]);
CurrentGrabIndex = 0;
NextGrabIndex = 1;
MosPrintf(MIL_TEXT("Deinterlaced live grab is displayed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to stop.\n\n"));
while (!MosKbhit())
{
MdigGrab(MilDigitizer, MilGrabImages[NextGrabIndex]);
if ((GRAB_DEINTERLACE_METHOD != M_ADAPTIVE_DISCARD) &&
(GRAB_DEINTERLACE_METHOD != M_ADAPTIVE_AVERAGE))
{
MimDeinterlace(MilDeinterlaceContext, &MilGrabImages[CurrentGrabIndex],
&MilDisplayImage, 1, 1, M_DEFAULT);
}
else
{
for (i = 0; i < MOTION_DETECT_NUM_FRAMES; i++)
{
MIL_INT PreviousGrabIndex = NextGrabIndex - MOTION_DETECT_NUM_FRAMES + i;
if (PreviousGrabIndex < 0)
PreviousGrabIndex += NUM_GRAB_IMAGES;
MilPreviousImages[i] = MilGrabImages[PreviousGrabIndex];
}
MimDeinterlace(MilDeinterlaceContext, MilPreviousImages, &MilDisplayImage,
MOTION_DETECT_NUM_FRAMES, 1, M_DEFAULT);
}
CurrentGrabIndex = NextGrabIndex;
NextGrabIndex = (NextGrabIndex+1) % NUM_GRAB_IMAGES;
}
MosGetch();
MosPrintf(MIL_TEXT("Last deinterlaced image displayed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
MimFree(MilDeinterlaceContext);
MbufFree(MilDisplayImage);
MdispFree(MilDisplay);
for (i = 0; i < NUM_GRAB_IMAGES; i++)
MbufFree(MilGrabImages[i]);
MdigFree(MilDigitizer);
}