'
'
'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports Matrox.MatroxImagingLibrary
Namespace M3dmap
Friend Class Program
<DllImport("mdispd3d.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function MdepthD3DAlloc(ByVal DepthBuffer As MIL_ID, ByVal IntensityBuffer As MIL_ID, ByVal DisplaySizeX As MIL_INT, ByVal DisplaySizeY As MIL_INT, ByVal ScaleX As Double, ByVal ScaleY As Double, ByVal ScaleZ As Double, ByVal MinZ As Double, ByVal MaxZ As Double, ByVal MaxDistanceZ As Double, ByVal WindowHandle As IntPtr) As IntPtr
End Function
<DllImport("mdispd3d.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Sub MdispD3DFree(ByVal DispHandle As IntPtr)
End Sub
<DllImport("mdispd3d.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Sub MdispD3DShow(ByVal DispHandle As IntPtr)
End Sub
<DllImport("mdispd3d.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Sub MdispD3DHide(ByVal DispHandle As IntPtr)
End Sub
<DllImport("mdispd3d.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Sub MdispD3DPrintHelp(ByVal DispHandle As IntPtr)
End Sub
Shared Sub Main(ByVal args() As String)
Dim MilApplication As MIL_ID = MIL.M_NULL
Dim MilSystem As MIL_ID = MIL.M_NULL
Dim MilDisplay As MIL_ID = MIL.M_NULL
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
DepthCorrectionExample(MilSystem, MilDisplay)
CalibratedCameraExample(MilSystem, MilDisplay)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Const REFERENCE_PLANES_SEQUENCE_FILE As String = MIL.M_IMAGE_PATH & "ReferencePlanes.avi"
Private Const OBJECT_SEQUENCE_FILE As String = MIL.M_IMAGE_PATH & "ScannedObject.avi"
Private Const PEAK_WIDTH_NOMINAL As Integer = 10
Private Const PEAK_WIDTH_DELTA As Integer = 8
Private Const MIN_CONTRAST As Integer = 140
Private Shared ReadOnly CORRECTED_DEPTHS() As Double = {1.25, 2.5, 3.75, 5.0}
Private Const SCALE_FACTOR As Double = 10000.0
Private Const CALIB_TEXT_POS_X As Double = 400
Private Const CALIB_TEXT_POS_Y As Double = 15
Private Shared Sub DepthCorrectionExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilDepthMap As MIL_ID = MIL.M_NULL
Dim MilLaser As MIL_ID = MIL.M_NULL
Dim MilCalibScan As MIL_ID = MIL.M_NULL
Dim MilScan As MIL_ID = MIL.M_NULL
Dim SizeX As MIL_INT = 0
Dim SizeY As MIL_INT = 0
Dim NbReferencePlanes As MIL_INT = 0
Dim NbObjectImages As MIL_INT = 0
Dim n As MIL_INT = 0
Dim FrameRate As Double = 0.0
Dim StartTime As Double = 0.0
Dim EndTime As Double = 0.0
Dim WaitTime As Double = 0.0
MIL.MbufDiskInquire(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_SIZE_X, SizeX)
MIL.MbufDiskInquire(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_SIZE_Y, SizeY)
MIL.MbufDiskInquire(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_NUMBER_OF_IMAGES, NbReferencePlanes)
MIL.MbufDiskInquire(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_FRAME_RATE, FrameRate)
MIL.MbufDiskInquire(OBJECT_SEQUENCE_FILE, MIL.M_NUMBER_OF_IMAGES, NbObjectImages)
MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_DISP + MIL.M_PROC, MilImage)
MIL.MbufClear(MilImage, 0.0)
Console.WriteLine()
Console.WriteLine("DEPTH ANALYSIS:")
Console.WriteLine("---------------")
Console.WriteLine()
Console.WriteLine("This program performs a surface inspection to detect depth")
Console.WriteLine("defects on a wood surface using a laser (sheet-of-light) profiling system.")
Console.WriteLine()
Console.WriteLine("Press <Enter> to continue.")
Console.WriteLine()
Console.ReadKey()
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, MilOverlayImage)
MIL.MgraControl(MIL.M_DEFAULT, MIL.M_BACKGROUND_MODE, MIL.M_TRANSPARENT)
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_WHITE)
MIL.M3dmapAlloc(MilSystem, MIL.M_LASER, MIL.M_DEPTH_CORRECTION, MilLaser)
MIL.M3dmapAllocResult(MilSystem, MIL.M_LASER_CALIBRATION_DATA, MIL.M_DEFAULT, MilCalibScan)
Dim MilPeakLocator As MIL_ID = MIL.M_NULL
MIL.M3dmapInquire(MilLaser, MIL.M_CONTEXT, MIL.M_LOCATE_PEAK_1D_CONTEXT_ID + MIL.M_TYPE_MIL_ID, MilPeakLocator)
MIL.MimControl(MilPeakLocator, MIL.M_PEAK_WIDTH_NOMINAL, PEAK_WIDTH_NOMINAL)
MIL.MimControl(MilPeakLocator, MIL.M_PEAK_WIDTH_DELTA, PEAK_WIDTH_DELTA)
MIL.MimControl(MilPeakLocator, MIL.M_MINIMUM_CONTRAST, MIN_CONTRAST)
MIL.MbufImportSequence(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, CType(MIL.M_NULL, IntPtr), MIL.M_NULL, MIL.M_NULL, MIL.M_OPEN)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, StartTime)
For n = 0 To NbReferencePlanes - 1
Dim CalibString As String
MIL.MbufImportSequence(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_LOAD, MIL.M_NULL, MilImage, MIL.M_DEFAULT, 1, MIL.M_READ)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
CalibString = String.Format("Reference plane {0}: {1:0.00} mm", n + 1, CORRECTED_DEPTHS(CType(n, Integer)))
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, CALIB_TEXT_POS_X, CALIB_TEXT_POS_Y, CalibString)
MIL.M3dmapControl(MilLaser, MIL.M_DEFAULT, MIL.M_CORRECTED_DEPTH, CORRECTED_DEPTHS(CType(n, Integer)) * SCALE_FACTOR)
MIL.M3dmapAddScan(MilLaser, MilCalibScan, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, EndTime)
WaitTime = (1.0 / FrameRate) - (EndTime - StartTime)
If WaitTime > 0 Then
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_WAIT, WaitTime)
End If
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, StartTime)
Next n
MIL.MbufImportSequence(REFERENCE_PLANES_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, CType(MIL.M_NULL, IntPtr), MIL.M_NULL, MIL.M_NULL, MIL.M_CLOSE)
MIL.M3dmapCalibrate(MilLaser, MilCalibScan, MIL.M_NULL, MIL.M_DEFAULT)
Console.WriteLine("The laser profiling system has been calibrated using 4 reference")
Console.WriteLine("planes of known heights.")
Console.WriteLine()
Console.WriteLine("Press <Enter> to continue.")
Console.WriteLine()
Console.ReadKey()
Console.WriteLine("The wood surface is being scanned.")
Console.WriteLine()
MIL.M3dmapFree(MilCalibScan)
MilCalibScan = MIL.M_NULL
MIL.M3dmapAllocResult(MilSystem, MIL.M_DEPTH_CORRECTED_DATA, MIL.M_DEFAULT, MilScan)
MIL.MbufDiskInquire(OBJECT_SEQUENCE_FILE, MIL.M_FRAME_RATE, FrameRate)
MIL.MbufImportSequence(OBJECT_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, CType(MIL.M_NULL, IntPtr), MIL.M_NULL, MIL.M_NULL, MIL.M_OPEN)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, StartTime)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
For n = 0 To NbObjectImages - 1
MIL.MbufImportSequence(OBJECT_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_LOAD, MIL.M_NULL, MilImage, MIL.M_DEFAULT, 1, MIL.M_READ)
MIL.M3dmapAddScan(MilLaser, MilScan, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, EndTime)
WaitTime = (1.0 / FrameRate) - (EndTime - StartTime)
If WaitTime > 0 Then
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_WAIT, WaitTime)
End If
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, StartTime)
Next n
MIL.MbufImportSequence(OBJECT_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, CType(MIL.M_NULL, IntPtr), MIL.M_NULL, MIL.M_NULL, MIL.M_CLOSE)
MIL.MbufAlloc2d(MilSystem, SizeX, NbObjectImages, 16 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, MilDepthMap)
MIL.M3dmapExtract(MilScan, MilDepthMap, MIL.M_NULL, MIL.M_CORRECTED_DEPTH_MAP, MIL.M_DEFAULT, MIL.M_DEFAULT)
SetupColorDisplay(MilSystem, MilDisplay, MIL.MbufInquire(MilDepthMap, MIL.M_SIZE_BIT, MIL.M_NULL))
MIL.MdispSelect(MilDisplay, MilDepthMap)
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, MilOverlayImage)
Console.WriteLine("The pseudo-color depth map of the surface is displayed.")
Console.WriteLine()
Console.WriteLine("Press <Enter> to continue.")
Console.WriteLine()
Console.ReadKey()
PerformBlobAnalysis(MilSystem, MilDisplay, MilOverlayImage, MilDepthMap)
Console.WriteLine("Press <Enter> to continue.")
Console.WriteLine()
Console.ReadKey()
MIL.MdispLut(MilDisplay, MIL.M_DEFAULT)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
MIL.M3dmapFree(MilScan)
MIL.M3dmapFree(MilLaser)
MIL.MbufFree(MilDepthMap)
MIL.MbufFree(MilImage)
End Sub
Private Const EXPECTED_HEIGHT As Double = 3.4
Private Const DEFECT_THRESHOLD As Double = 0.2
Private Const SATURATED_DEFECT As Double = 1.0
Private Const MIN_BLOB_RADIUS As Integer = 3
Private Const TEXT_H_OFFSET_1 As Double = -50
Private Const TEXT_V_OFFSET_1 As Double = -6
Private Const TEXT_H_OFFSET_2 As Double = -30
Private Const TEXT_V_OFFSET_2 As Double = 6
Private Shared Sub PerformBlobAnalysis(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID, ByVal MilOverlayImage As MIL_ID, ByVal MilDepthMap As MIL_ID)
Dim MilBinImage As MIL_ID = MIL.M_NULL
Dim MilBlobFeatureList As MIL_ID = MIL.M_NULL
Dim MilBlobResult As MIL_ID = MIL.M_NULL
Dim SizeX As MIL_INT = 0
Dim SizeY As MIL_INT = 0
Dim TotalBlobs As MIL_INT = 0
Dim n As MIL_INT = 0
Dim MinPixels() As MIL_INT = Nothing
Dim DefectThreshold As Double = 0.0
Dim CogX() As Double = Nothing
Dim CogY() As Double = Nothing
MIL.MbufInquire(MilDepthMap, MIL.M_SIZE_X, SizeX)
MIL.MbufInquire(MilDepthMap, MIL.M_SIZE_Y, SizeY)
MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, MilBinImage)
DefectThreshold = (EXPECTED_HEIGHT - DEFECT_THRESHOLD) * SCALE_FACTOR
MIL.MimBinarize(MilDepthMap, MilBinImage, MIL.M_FIXED + MIL.M_LESS_OR_EQUAL, DefectThreshold, MIL.M_NULL)
MIL.MimOpen(MilBinImage, MilBinImage, MIN_BLOB_RADIUS, MIL.M_BINARY)
MIL.MblobAllocFeatureList(MilSystem, MilBlobFeatureList)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_CENTER_OF_GRAVITY)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_MIN_PIXEL)
MIL.MblobAllocResult(MilSystem, MilBlobResult)
MIL.MblobCalculate(MilBinImage, MilDepthMap, MilBlobFeatureList, MilBlobResult)
MIL.MblobGetNumber(MilBlobResult, TotalBlobs)
Console.WriteLine("Number of defects: {0}", TotalBlobs)
CogX = New Double(CType(TotalBlobs, Integer) - 1) {}
CogY = New Double(CType(TotalBlobs, Integer) - 1) {}
MinPixels = New MIL_INT(CType(TotalBlobs, Integer) - 1) {}
If CogX IsNot Nothing AndAlso CogY IsNot Nothing AndAlso MinPixels IsNot Nothing Then
MIL.MblobGetResult(MilBlobResult, MIL.M_CENTER_OF_GRAVITY_X, CogX)
MIL.MblobGetResult(MilBlobResult, MIL.M_CENTER_OF_GRAVITY_Y, CogY)
MIL.MblobGetResult(MilBlobResult, MIL.M_MIN_PIXEL + MIL.M_TYPE_MIL_INT, MinPixels)
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_RED)
MIL.MblobDraw(MIL.M_DEFAULT, MilBlobResult, MilOverlayImage, MIL.M_DRAW_BLOBS, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_WHITE)
For n = 0 To TotalBlobs - 1
Dim DepthOfDefect As Double
Dim DepthString As String
DepthOfDefect = EXPECTED_HEIGHT - (CType(MinPixels(CType(n, Integer)), Double) / SCALE_FACTOR)
DepthString = String.Format("{0:0.00} mm", DepthOfDefect)
Console.WriteLine("Defect #{0}: depth ={1,5:0.00} mm", n, DepthOfDefect)
Console.WriteLine()
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, CogX(CType(n, Integer)) + TEXT_H_OFFSET_1, CogY(CType(n, Integer)) + TEXT_V_OFFSET_1, "Defect depth")
MIL.MgraText(MIL.M_DEFAULT, MilOverlayImage, CogX(CType(n, Integer)) + TEXT_H_OFFSET_2, CogY(CType(n, Integer)) + TEXT_V_OFFSET_2, DepthString)
Next n
Else
Console.WriteLine("Error: Not enough memory.")
End If
MIL.MblobFree(MilBlobResult)
MIL.MblobFree(MilBlobFeatureList)
MIL.MbufFree(MilBinImage)
End Sub
Private Const BLUE_HUE As Double = 171.0
Private Const RED_HUE As Double = 0.0
Private Const FULL_SATURATION As Integer = 255
Private Const HALF_LUMINANCE As Integer = 128
Private Shared Sub SetupColorDisplay(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID, ByVal SizeBit As MIL_INT)
Dim MilRampLut1Band As MIL_ID = MIL.M_NULL
Dim MilRampLut3Band As MIL_ID = MIL.M_NULL
Dim MilColorImage As MIL_ID = MIL.M_NULL
Dim DefectGrayLevel As MIL_INT = 0
Dim ExpectedGrayLevel As MIL_INT = 0
Dim NbGrayLevels As MIL_INT
NbGrayLevels = CType(1 << CType(SizeBit, Integer), MIL_INT)
MIL.MbufAlloc1d(MilSystem, NbGrayLevels, 8 + MIL.M_UNSIGNED, MIL.M_LUT, MilRampLut1Band)
DefectGrayLevel = CType((EXPECTED_HEIGHT - SATURATED_DEFECT) * SCALE_FACTOR, MIL_INT)
ExpectedGrayLevel = CType(EXPECTED_HEIGHT * SCALE_FACTOR, MIL_INT)
MIL.MgenLutRamp(MilRampLut1Band, 0, RED_HUE, DefectGrayLevel, RED_HUE)
MIL.MgenLutRamp(MilRampLut1Band, DefectGrayLevel, RED_HUE, ExpectedGrayLevel, BLUE_HUE)
MIL.MgenLutRamp(MilRampLut1Band, ExpectedGrayLevel, BLUE_HUE, NbGrayLevels - 1, BLUE_HUE)
MIL.MbufAllocColor(MilSystem, 3, NbGrayLevels, 1, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE, MilColorImage)
MIL.MbufClear(MilColorImage, MIL.M_RGB888(0, FULL_SATURATION, HALF_LUMINANCE))
MIL.MbufCopyColor2d(MilRampLut1Band, MilColorImage, 0, 0, 0, 0, 0, 0, NbGrayLevels, 1)
MIL.MimConvert(MilColorImage, MilColorImage, MIL.M_HSL_TO_RGB)
MIL.MbufAllocColor(MilSystem, 3, NbGrayLevels, 1, 8 + MIL.M_UNSIGNED, MIL.M_LUT, MilRampLut3Band)
MIL.MbufCopy(MilColorImage, MilRampLut3Band)
MIL.MdispLut(MilDisplay, MilRampLut3Band)
MIL.MbufFree(MilRampLut1Band)
MIL.MbufFree(MilRampLut3Band)
MIL.MbufFree(MilColorImage)
End Sub
Private Const GRID_FILENAME As String = MIL.M_IMAGE_PATH & "GridForLaser.mim"
Private Const LASERLINE_FILENAME As String = MIL.M_IMAGE_PATH & "LaserLine.mim"
Private Const OBJECT2_SEQUENCE_FILE As String = MIL.M_IMAGE_PATH & "Cookie.avi"
Private Shared ReadOnly GRID_NB_ROWS As MIL_INT = 13
Private Shared ReadOnly GRID_NB_COLS As MIL_INT = 12
Private Const GRID_ROW_SPACING As Double = 5.0
Private Const GRID_COL_SPACING As Double = 5.0
Private Const CONVEYOR_SPEED As Double = -0.2
Private Shared ReadOnly DEPTH_MAP_SIZE_X As MIL_INT = 480
Private Shared ReadOnly DEPTH_MAP_SIZE_Y As MIL_INT = 480
Private Const GAP_DEPTH As Double = 1.5
Private Shared ReadOnly D3D_DISPLAY_SIZE_X As MIL_INT = 640
Private Shared ReadOnly D3D_DISPLAY_SIZE_Y As MIL_INT = 480
Private Shared ReadOnly PEAK_WIDTH_NOMINAL_2 As MIL_INT = 9
Private Shared ReadOnly PEAK_WIDTH_DELTA_2 As MIL_INT = 7
Private Shared ReadOnly MIN_CONTRAST_2 As MIL_INT = 75
Private Const MIN_HEIGHT_THRESHOLD As Double = 1.0
Private Shared Sub CalibratedCameraExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilCalibration As MIL_ID = MIL.M_NULL
Dim DepthMapId As MIL_ID = MIL.M_NULL
Dim LaserId As MIL_ID = MIL.M_NULL
Dim CalibScanId As MIL_ID = MIL.M_NULL
Dim ScanId As MIL_ID = MIL.M_NULL
Dim CalibrationStatus As MIL_INT = 0
Dim SizeX As MIL_INT = 0
Dim SizeY As MIL_INT = 0
Dim NumberOfImages As MIL_INT = 0
Dim n As MIL_INT = 0
Dim FrameRate As Double = 0.0
Dim StartTime As Double = 0.0
Dim EndTime As Double = 0.0
Dim WaitTime As Double = 0.0
Dim Volume As Double = 0.0
Dim VolumeError As Double = 0.0
Console.WriteLine()
Console.WriteLine("3D PROFILING AND VOLUME ANALYSIS:")
Console.WriteLine("---------------------------------")
Console.WriteLine()
Console.WriteLine("This program generates fully corrected 3d data of a")
Console.WriteLine("scanned cookie and computes its volume.")
Console.WriteLine("The laser (sheet-of-light) profiling system uses a")
Console.WriteLine("3d-calibrated camera.")
Console.WriteLine()
MIL.MbufRestore(GRID_FILENAME, MilSystem, MilImage)
MIL.MdispSelect(MilDisplay, MilImage)
Console.WriteLine("Calibrating the camera...")
Console.WriteLine()
MIL.MbufInquire(MilImage, MIL.M_SIZE_X, SizeX)
MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, SizeY)
MIL.McalAlloc(MilSystem, MIL.M_TSAI_BASED, MIL.M_DEFAULT, MilCalibration)
MIL.McalGrid(MilCalibration, MilImage, 0.0, 0.0, 0.0, GRID_NB_ROWS, GRID_NB_COLS, GRID_ROW_SPACING, GRID_COL_SPACING, MIL.M_DEFAULT, MIL.M_CHESSBOARD_GRID)
MIL.McalInquire(MilCalibration, MIL.M_CALIBRATION_STATUS + MIL.M_TYPE_MIL_INT, CalibrationStatus)
If CalibrationStatus <> MIL.M_CALIBRATED Then
MIL.McalFree(MilCalibration)
MIL.MbufFree(MilImage)
Console.WriteLine("Camera calibration failed.")
Console.WriteLine("Press <Enter> to end.")
Console.WriteLine()
Console.ReadLine()
Return
End If
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, MilOverlayImage)
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_GREEN)
MIL.McalDraw(MIL.M_DEFAULT, MilCalibration, MilOverlayImage, MIL.M_DRAW_IMAGE_POINTS, MIL.M_DEFAULT, MIL.M_DEFAULT)
Console.WriteLine("The camera was calibrated using a chessboard grid.")
Console.WriteLine()
Console.WriteLine("Press <Enter> to continue.")
Console.WriteLine()
Console.ReadKey()
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_DISABLE)
MIL.MbufLoad(LASERLINE_FILENAME, MilImage)
MIL.M3dmapAlloc(MilSystem, MIL.M_LASER, MIL.M_CALIBRATED_CAMERA_LINEAR_MOTION, LaserId)
MIL.M3dmapAllocResult(MilSystem, MIL.M_LASER_CALIBRATION_DATA, MIL.M_DEFAULT, CalibScanId)
Dim MilPeakLocator As MIL_ID = MIL.M_NULL
MIL.M3dmapInquire(LaserId, MIL.M_CONTEXT, MIL.M_LOCATE_PEAK_1D_CONTEXT_ID + MIL.M_TYPE_MIL_ID, MilPeakLocator)
MIL.MimControl(MilPeakLocator, MIL.M_PEAK_WIDTH_NOMINAL, PEAK_WIDTH_NOMINAL_2)
MIL.MimControl(MilPeakLocator, MIL.M_PEAK_WIDTH_DELTA, PEAK_WIDTH_DELTA_2)
MIL.MimControl(MilPeakLocator, MIL.M_MINIMUM_CONTRAST, MIN_CONTRAST_2)
MIL.M3dmapAddScan(LaserId, CalibScanId, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.M3dmapCalibrate(LaserId, CalibScanId, MilCalibration, MIL.M_DEFAULT)
Console.WriteLine("The laser profiling system has been calibrated using the image")
Console.WriteLine("of one laser line.")
Console.WriteLine()
Console.WriteLine("Press <Enter> to continue.")
Console.WriteLine()
Console.ReadKey()
MIL.M3dmapFree(CalibScanId)
CalibScanId = MIL.M_NULL
MIL.M3dmapAllocResult(MilSystem, MIL.M_POINT_CLOUD_CONTAINER, MIL.M_DEFAULT, ScanId)
MIL.M3dmapControl(LaserId, MIL.M_DEFAULT, MIL.M_SCAN_SPEED, CONVEYOR_SPEED)
MIL.MbufDiskInquire(OBJECT2_SEQUENCE_FILE, MIL.M_NUMBER_OF_IMAGES, NumberOfImages)
MIL.MbufDiskInquire(OBJECT2_SEQUENCE_FILE, MIL.M_FRAME_RATE, FrameRate)
MIL.MbufImportSequence(OBJECT2_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, CType(MIL.M_NULL, IntPtr), MIL.M_NULL, MIL.M_NULL, MIL.M_OPEN)
Console.WriteLine("The cookie is being scanned to generate 3d data.")
Console.WriteLine()
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, StartTime)
For n = 0 To NumberOfImages - 1
MIL.MbufImportSequence(OBJECT2_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_LOAD, MIL.M_NULL, MilImage, MIL.M_DEFAULT, 1, MIL.M_READ)
MIL.M3dmapAddScan(LaserId, ScanId, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_POINT_CLOUD_LABEL(1), MIL.M_DEFAULT)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, EndTime)
WaitTime = (1.0 / FrameRate) - (EndTime - StartTime)
If WaitTime > 0 Then
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_WAIT, WaitTime)
End If
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, StartTime)
Next n
MIL.MbufImportSequence(OBJECT2_SEQUENCE_FILE, MIL.M_DEFAULT, MIL.M_NULL, MIL.M_NULL, CType(MIL.M_NULL, IntPtr), MIL.M_NULL, MIL.M_NULL, MIL.M_CLOSE)
MIL.MbufAlloc2d(MilSystem, DEPTH_MAP_SIZE_X, DEPTH_MAP_SIZE_Y, 16 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, DepthMapId)
MIL.M3dmapControl(ScanId, MIL.M_DEFAULT, MIL.M_AUTO_SCALE_XY, MIL.M_CLIP)
MIL.M3dmapControl(ScanId, MIL.M_DEFAULT, MIL.M_FILL_MODE, MIL.M_X_THEN_Y)
MIL.M3dmapControl(ScanId, MIL.M_DEFAULT, MIL.M_FILL_SHARP_ELEVATION, MIL.M_MIN)
MIL.M3dmapControl(ScanId, MIL.M_DEFAULT, MIL.M_FILL_SHARP_ELEVATION_DEPTH, GAP_DEPTH)
MIL.M3dmapSetBox(ScanId, MIL.M_EXTRACTION_BOX, MIL.M_BOUNDING_BOX, MIL.M_ALL, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.M3dmapExtract(ScanId, DepthMapId, MIL.M_NULL, MIL.M_CORRECTED_DEPTH_MAP, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.M3dmapStat(DepthMapId, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_VOLUME, MIL.M_DEFAULT, MIL.M_DEFAULT, Volume)
MIL.M3dmapStat(DepthMapId, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_VOLUME, MIN_HEIGHT_THRESHOLD, MIL.M_DEFAULT, VolumeError)
Volume -= VolumeError
Console.WriteLine("fully corrected 3d data of the cookie is displayed.")
Console.WriteLine()
Dim DispHandle As IntPtr
DispHandle = MdepthD3DAlloc(DepthMapId, MIL.M_NULL, D3D_DISPLAY_SIZE_X, D3D_DISPLAY_SIZE_Y, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, IntPtr.Zero)
If DispHandle <> IntPtr.Zero Then
MIL.MdispControl(MilDisplay, MIL.M_WINDOW_SHOW, MIL.M_DISABLE)
MdispD3DShow(DispHandle)
Console.WriteLine("D3D display controls:")
Console.WriteLine(" .Left click" & Constants.vbTab & "move object")
Console.WriteLine(" .Right click" & Constants.vbTab & "rotate object")
Console.WriteLine(" .Scroll wheel" & Constants.vbTab & "zoom")
Console.WriteLine(" .R" & Constants.vbTab + Constants.vbTab & "start/stop animation")
Console.WriteLine(" .P" & Constants.vbTab + Constants.vbTab & "enable/disable point cloud")
Console.WriteLine()
Else
MIL.MdispControl(MilDisplay, MIL.M_VIEW_MODE, MIL.M_AUTO_SCALE)
MIL.MdispSelect(MilDisplay, DepthMapId)
End If
Console.WriteLine("Volume of the cookie is {0,4:0.0} cm^3.", -Volume / 1000.0)
Console.WriteLine()
Console.WriteLine("Press <Enter> to end.")
Console.WriteLine()
Console.ReadKey()
If DispHandle <> IntPtr.Zero Then
MdispD3DHide(DispHandle)
MdispD3DFree(DispHandle)
End If
MIL.M3dmapFree(ScanId)
MIL.M3dmapFree(LaserId)
MIL.McalFree(MilCalibration)
MIL.MbufFree(DepthMapId)
MIL.MbufFree(MilImage)
End Sub
End Class
End Namespace