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