using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Matrox.MatroxImagingLibrary;
namespace MImLocatePeak1d
{
class Program
{
private const string SEQUENCE_FILE = MIL.M_IMAGE_PATH + "HandWithLaser.avi";
private const int LINE_WIDTH_AVERAGE = 20;
private const int LINE_WIDTH_DELTA = 20;
private const double MIN_CONTRAST = 100.0;
private const int NB_FIXED_POINT = 4;
private static readonly int D3D_DISPLAY_SIZE_X = 640;
private static readonly int D3D_DISPLAY_SIZE_Y = 480;
private static readonly double D3D_MESH_SCALING_X = -1.0;
private static readonly double D3D_MESH_SCALING_Y = 4.0;
private static readonly double D3D_MESH_SCALING_Z = -0.13;
[DllImport("mdispd3d.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr MdepthD3DAlloc(MIL_ID DepthBuffer, MIL_ID IntensityBuffer, MIL_INT DisplaySizeX, MIL_INT DisplaySizeY, double ScaleX, double ScaleY, double ScaleZ, double MinZ, double MaxZ, double MaxDistanceZ, IntPtr WindowHandle);
[DllImport("mdispd3d.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void MdispD3DFree(IntPtr DispHandle);
[DllImport("mdispd3d.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void MdispD3DShow(IntPtr DispHandle);
[DllImport("mdispd3d.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void MdispD3DHide(IntPtr DispHandle);
[DllImport("mdispd3d.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void MdispD3DPrintHelp(IntPtr DispHandle);
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 MilImage = MIL.M_NULL;
MIL_ID MilPosYImage = MIL.M_NULL;
MIL_ID MilValImage = MIL.M_NULL;
MIL_ID MilContext = MIL.M_NULL;
MIL_ID MilLocatePeak = MIL.M_NULL;
MIL_ID MilExtreme = MIL.M_NULL;
MIL_INT SizeX = 0;
MIL_INT SizeY = 0;
MIL_INT NumberOfImages = 0;
double FrameRate = 0.0;
int n = 0;
double PreviousTime = 0.0;
double StartTime = 0.0;
double EndTime = 0.0;
double TotalProcessTime = 0.0;
double WaitTime = 0.0;
MIL_INT[] ExtremePosY = new MIL_INT[] { 0, 0 };
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_SIZE_X, ref SizeX);
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_SIZE_Y, ref SizeY);
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_NUMBER_OF_IMAGES, ref NumberOfImages);
MIL.MbufDiskInquire(SEQUENCE_FILE, MIL.M_FRAME_RATE, ref FrameRate);
MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, ref MilImage);
MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_DISP, ref MilDisplayImage);
MIL.MbufAlloc2d(MilSystem, SizeX, NumberOfImages, 16 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, ref MilPosYImage);
MIL.MbufAlloc2d(MilSystem, SizeX, NumberOfImages, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, ref MilValImage);
MIL.MimAlloc(MilSystem, MIL.M_LOCATE_PEAK_1D_CONTEXT, MIL.M_DEFAULT, ref MilContext);
MIL.MimAllocResult(MilSystem, MIL.M_DEFAULT, MIL.M_LOCATE_PEAK_1D_RESULT, ref MilLocatePeak);
MIL.MdispSelect(MilDisplay, MilDisplayImage);
Console.Write("\nEXTRACTING 3D IMAGE FROM A LASER LINE (SHEET-OF-LIGHT):\n");
Console.Write("-------------------------------------------------------\n\n");
Console.Write("The position of a laser line is being extracted from an image\n");
Console.Write("to generate a depth image.\n\n");
MIL.MbufImportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_OPEN);
MIL.MimLocatePeak1d(MilContext, MilImage, MilLocatePeak, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_PREPROCESS, MIL.M_DEFAULT);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref PreviousTime);
TotalProcessTime = 0.0;
for (n = 0; n < NumberOfImages; n++)
{
MIL.MbufImportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_LOAD, MIL.M_NULL, ref MilImage, MIL.M_DEFAULT, 1, MIL.M_READ);
MIL.MbufCopy(MilImage, MilDisplayImage);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref StartTime);
MIL.MimLocatePeak1d(MilContext, MilImage, MilLocatePeak, LINE_WIDTH_AVERAGE, LINE_WIDTH_DELTA, MIN_CONTRAST, MIL.M_DEFAULT, MIL.M_DEFAULT);
MIL.MimDraw(MIL.M_DEFAULT, MilLocatePeak, MIL.M_NULL, MilPosYImage, MIL.M_DRAW_DEPTH_MAP_ROW, n, MIL.M_NULL, MIL.M_FIXED_POINT + NB_FIXED_POINT);
MIL.MimDraw(MIL.M_DEFAULT, MilLocatePeak, MIL.M_NULL, MilValImage, MIL.M_DRAW_INTENSITY_MAP_ROW, n, MIL.M_NULL, MIL.M_FIXED_POINT + NB_FIXED_POINT);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref EndTime);
TotalProcessTime += EndTime - StartTime;
WaitTime = (1.0 / FrameRate) - (EndTime - PreviousTime);
if (WaitTime > 0)
{
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_WAIT, ref WaitTime);
}
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref PreviousTime);
}
MIL.MbufImportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_CLOSE);
Console.Write("{0} images processed in {1,7:0.00} s ({2,7:0.00} ms/image).\n", NumberOfImages, TotalProcessTime, TotalProcessTime / (double)NumberOfImages * 1000.0);
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
Console.Write("The reconstructed images are being displayed.\n");
for (n = 0; n < NumberOfImages; n++)
{
MIL.MbufClear(MilImage, 0);
MIL.MimDraw(MIL.M_DEFAULT, MilPosYImage, MilValImage, MilImage, MIL.M_DRAW_PEAKS + MIL.M_1D_COLUMNS + MIL.M_LINES, (double)n, 1, MIL.M_FIXED_POINT + NB_FIXED_POINT);
MIL.MbufCopy(MilImage, MilDisplayImage);
}
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
IntPtr DispHandle;
DispHandle = MdepthD3DAlloc(MilPosYImage, MilValImage, D3D_DISPLAY_SIZE_X, D3D_DISPLAY_SIZE_Y, D3D_MESH_SCALING_X, D3D_MESH_SCALING_Y, D3D_MESH_SCALING_Z, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, IntPtr.Zero);
if (DispHandle != IntPtr.Zero)
{
Console.Write("The depth buffer is displayed using D3D.\n");
MIL.MdispControl(MilDisplay, MIL.M_WINDOW_SHOW, MIL.M_DISABLE);
MdispD3DShow(DispHandle);
MdispD3DPrintHelp(DispHandle);
Console.Write("Press <Enter> to end.\n");
Console.ReadKey();
MdispD3DHide(DispHandle);
MdispD3DFree(DispHandle);
}
else
{
Console.Write("The depth buffer is displayed using MIL.\n");
MIL.MimAllocResult(MilSystem, MIL.M_DEFAULT, MIL.M_STAT_LIST, ref MilExtreme);
MIL.MimStat(MilPosYImage, MilExtreme, MIL.M_MIN + MIL.M_MAX, MIL.M_NOT_EQUAL, 0xFFFF, MIL.M_NULL);
MIL.MimGetResult(MilExtreme, MIL.M_MIN + MIL.M_TYPE_MIL_INT, ref ExtremePosY[0]);
MIL.MimGetResult(MilExtreme, MIL.M_MAX + MIL.M_TYPE_MIL_INT, ref ExtremePosY[1]);
MIL.MimFree(MilExtreme);
MIL.MbufFree(MilDisplayImage);
MIL.MbufAlloc2d(MilSystem,
(MIL_INT)((double)SizeX * (D3D_MESH_SCALING_X > 0 ? D3D_MESH_SCALING_X : -D3D_MESH_SCALING_X)),
(MIL_INT)((double)NumberOfImages * D3D_MESH_SCALING_Y),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP,
ref MilDisplayImage);
MIL.MdispSelect(MilDisplay, MilDisplayImage);
MIL.MimClip(MilPosYImage, MilPosYImage, MIL.M_GREATER, (double)ExtremePosY[1], MIL.M_NULL, (double)ExtremePosY[1], MIL.M_NULL);
MIL.MimArith(MilPosYImage, (double)ExtremePosY[0], MilPosYImage, MIL.M_SUB_CONST);
MIL.MimArith(MilPosYImage, ((ExtremePosY[1] - ExtremePosY[0]) / 255.0) + 1, MilPosYImage, MIL.M_DIV_CONST);
MIL.MimResize(MilPosYImage, MilDisplayImage, MIL.M_FILL_DESTINATION, MIL.M_FILL_DESTINATION, MIL.M_BILINEAR);
Console.Write("Press <Enter> to end.\n");
Console.ReadKey();
}
MIL.MimFree(MilLocatePeak);
MIL.MimFree(MilContext);
MIL.MbufFree(MilImage);
MIL.MbufFree(MilDisplayImage);
MIL.MbufFree(MilPosYImage);
MIL.MbufFree(MilValImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}