'
'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MModelTracking
Friend Class Program
Private Const MODEL_WIDTH As Integer = 128
Private Const MODEL_HEIGHT As Integer = 128
Private Shared Function MODEL_POS_X_INIT(ByVal TargetImage As MIL_ID) As MIL_INT
Return CType((MIL.MbufInquire(TargetImage, MIL.M_SIZE_X, MIL.M_NULL) / 2), MIL_INT)
End Function
Private Shared Function MODEL_POS_Y_INIT(ByVal TargetImage As MIL_ID) As MIL_INT
Return CType((MIL.MbufInquire(TargetImage, MIL.M_SIZE_Y, MIL.M_NULL) / 2), MIL_INT)
End Function
Private Const MODEL_MIN_MATCH_SCORE As Double = 50.0
Private Const DRAW_COLOR As Integer = &HFF
Private Const RUN_PAT_TRACKING_EXAMPLE As Integer = MIL.M_YES
Private Const RUN_MOD_TRACKING_EXAMPLE As Integer = MIL.M_YES
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
Dim MilDigitizer As MIL_ID = MIL.M_NULL
Dim MilDisplayImage As MIL_ID = MIL.M_NULL
Dim MilModelImage As MIL_ID = MIL.M_NULL
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, MilDigitizer, MilDisplayImage)
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilDisplayImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilDisplayImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_PROC, MilModelImage)
Console.Write(Constants.vbLf + "MODEL TRACKING:" + Constants.vbLf)
Console.Write("---------------" + Constants.vbLf + Constants.vbLf)
GetModelImage(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage)
If RUN_PAT_TRACKING_EXAMPLE = MIL.M_YES Then
MpatTrackingExample(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage)
End If
If RUN_MOD_TRACKING_EXAMPLE = MIL.M_YES Then
MmodTrackingExample(MilSystem, MilDisplay, MilDigitizer, MilDisplayImage, MilModelImage)
End If
MIL.MbufFree(MilModelImage)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilDisplayImage)
End Sub
Private Shared Sub GetModelImage(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID, ByVal MilDigitizer As MIL_ID, ByVal MilDisplayImage As MIL_ID, ByVal MilModelImage As MIL_ID)
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim DrawColor As Double = DRAW_COLOR
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, MilOverlayImage)
MIL.MgraColor(MIL.M_DEFAULT, DrawColor)
MIL.MgraRect(MIL.M_DEFAULT, MilOverlayImage, MODEL_POS_X_INIT(MilOverlayImage) - CType((MODEL_WIDTH / 2), MIL_INT), MODEL_POS_Y_INIT(MilOverlayImage) - CType((MODEL_HEIGHT / 2), MIL_INT), MODEL_POS_X_INIT(MilOverlayImage) + CType((MODEL_WIDTH / 2), MIL_INT), MODEL_POS_Y_INIT(MilOverlayImage) + CType((MODEL_HEIGHT / 2), MIL_INT))
Console.Write("Model definition:" + Constants.vbLf + Constants.vbLf)
Console.Write("Place a unique model to find in the marked rectangle." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
MIL.MdigGrabContinuous(MilDigitizer, MilDisplayImage)
Console.ReadKey()
MIL.MdigHalt(MilDigitizer)
MIL.MbufCopy(MilDisplayImage, MilModelImage)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_DISABLE)
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
End Sub
Private Shared Sub MpatTrackingExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID, ByVal MilDigitizer As MIL_ID, ByVal MilDisplayImage As MIL_ID, ByVal MilModelImage As MIL_ID)
Dim MilImage() As MIL_ID = {MIL.M_NULL, MIL.M_NULL}
Dim Model As MIL_ID = MIL.M_NULL
Dim Result As MIL_ID = MIL.M_NULL
Dim DrawColor As Double = DRAW_COLOR
Dim Found As MIL_INT = 0
Dim NbFindDone As Integer = 0
Dim OrgX As Double = 0.0
Dim OrgY As Double = 0.0
Dim x As Double = 0.0
Dim y As Double = 0.0
Dim Score As Double = 0.0
Dim Time As Double = 0.0
Console.Write(Constants.vbLf + "GRAYSCALE PATTERN MATCHING:" + Constants.vbLf)
Console.Write("---------------------------" + Constants.vbLf + Constants.vbLf)
MIL.MbufCopy(MilModelImage, MilDisplayImage)
MIL.MpatAllocModel(MilSystem, MilModelImage, MODEL_POS_X_INIT(MilModelImage) - CType(MODEL_WIDTH / 2, MIL_INT), MODEL_POS_Y_INIT(MilModelImage) - CType(MODEL_HEIGHT / 2, MIL_INT), MODEL_WIDTH, MODEL_HEIGHT, MIL.M_NORMALIZED, Model)
MIL.MpatAllocResult(MilSystem, 1, Result)
MIL.MgraColor(MIL.M_DEFAULT, DrawColor)
MIL.MpatDraw(MIL.M_DEFAULT, Model, MilDisplayImage, MIL.M_DRAW_BOX, MIL.M_DEFAULT, MIL.M_ORIGINAL)
MIL.MpatSetAcceptance(Model, MODEL_MIN_MATCH_SCORE)
MIL.MpatSetSpeed(Model, MIL.M_HIGH)
MIL.MpatSetAccuracy(Model, MIL.M_LOW)
MIL.MpatPreprocModel(MilModelImage, Model, MIL.M_DEFAULT)
MIL.MpatInquire(Model, MIL.M_ORIGINAL_X, OrgX)
MIL.MpatInquire(Model, MIL.M_ORIGINAL_Y, OrgY)
Console.Write("A Grayscale Model was defined." + Constants.vbLf)
Console.Write("Model dimensions: {0} x {1}." + Constants.vbLf, MODEL_WIDTH, MODEL_HEIGHT)
Console.Write("Model center: X={0:0.00}, Y={0:0.00}." + Constants.vbLf, OrgX, OrgY)
Console.Write("Model is scale and rotation dependant." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, MilImage(0))
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, MilImage(1))
Console.Write(Constants.vbLf + "Continuously finding the Grayscale model." + Constants.vbLf)
Console.Write("Press <Enter> to stop." + Constants.vbLf + Constants.vbLf)
MIL.MdigControl(MilDigitizer, MIL.M_GRAB_MODE, MIL.M_ASYNCHRONOUS)
MIL.MdigGrab(MilDigitizer, MilImage(NbFindDone Mod 2))
MIL.MdigGrab(MilDigitizer, MilImage(NbFindDone Mod 2))
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET, Time)
Do
MIL.MdigGrab(MilDigitizer, MilImage((NbFindDone + 1) Mod 2))
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, Time)
MIL.MpatFindModel(MilImage(NbFindDone Mod 2), Model, Result)
MIL.MpatGetNumber(Result, Found)
MIL.MpatGetResult(Result, MIL.M_POSITION_X, x)
MIL.MpatGetResult(Result, MIL.M_POSITION_Y, y)
MIL.MpatGetResult(Result, MIL.M_SCORE, Score)
If Found > 0 Then
MIL.MpatDraw(MIL.M_DEFAULT, Result, MilImage(NbFindDone Mod 2), MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT)
Console.Write("Found: X={0,7:0.00}, Y={1,7:0.00}, Score={2,5:0.0}% ({3:0.0} fps). " & Constants.vbCr, x, y, Score, (NbFindDone + 1) / Time)
Else
Console.Write("Not found ! (score<{0,5:0.0}%) ({1:0.0} fps). " & Constants.vbCr, MODEL_MIN_MATCH_SCORE, (NbFindDone + 1) / Time)
End If
MIL.MbufCopy(MilImage(NbFindDone Mod 2), MilDisplayImage)
NbFindDone += 1
Loop While Not Console.KeyAvailable
Console.ReadKey()
Console.Write(Constants.vbLf + Constants.vbLf)
MIL.MdigGrabWait(MilDigitizer, MIL.M_GRAB_END)
MIL.MpatFree(Result)
MIL.MpatFree(Model)
MIL.MbufFree(MilImage(1))
MIL.MbufFree(MilImage(0))
End Sub
Private Const MODEL_MAX_OCCURRENCES As Integer = 16
Private Shared Sub MmodTrackingExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID, ByVal MilDigitizer As MIL_ID, ByVal MilDisplayImage As MIL_ID, ByVal MilModelImage As MIL_ID)
Dim MilImage() As MIL_ID = {MIL.M_NULL, MIL.M_NULL}
Dim SearchContext As MIL_ID = MIL.M_NULL
Dim Result As MIL_ID = MIL.M_NULL
Dim DrawColor As Double = DRAW_COLOR
Dim Found As MIL_INT = 0
Dim NbFindDone As Integer = 0
Dim OrgX As Double = 0.0
Dim OrgY As Double = 0.0
Dim Score(MODEL_MAX_OCCURRENCES - 1) As Double
Dim x(MODEL_MAX_OCCURRENCES - 1) As Double
Dim y(MODEL_MAX_OCCURRENCES - 1) As Double
Dim Angle(MODEL_MAX_OCCURRENCES - 1) As Double
Dim Scale(MODEL_MAX_OCCURRENCES - 1) As Double
Dim Time As Double = 0.0
Console.Write(Constants.vbLf + "GEOMETRIC MODEL FINDER (scale and rotation independent):" + Constants.vbLf)
Console.Write("--------------------------------------------------------" + Constants.vbLf + Constants.vbLf)
MIL.MbufCopy(MilModelImage, MilDisplayImage)
MIL.MmodAlloc(MilSystem, MIL.M_GEOMETRIC, MIL.M_DEFAULT, SearchContext)
MIL.MmodDefine(SearchContext, MIL.M_IMAGE, MilModelImage, CDbl(MODEL_POS_X_INIT(MilModelImage)) - (MODEL_WIDTH / 2), CDbl(MODEL_POS_Y_INIT(MilModelImage)) - (MODEL_HEIGHT / 2), MODEL_WIDTH, MODEL_HEIGHT)
MIL.MmodAllocResult(MilSystem, MIL.M_DEFAULT, Result)
MIL.MgraColor(MIL.M_DEFAULT, DrawColor)
MIL.MmodDraw(MIL.M_DEFAULT, SearchContext, MilDisplayImage, MIL.M_DRAW_BOX, MIL.M_DEFAULT, MIL.M_ORIGINAL)
MIL.MmodControl(SearchContext, MIL.M_CONTEXT, MIL.M_SPEED, MIL.M_VERY_HIGH)
MIL.MmodControl(SearchContext, MIL.M_DEFAULT, MIL.M_ACCEPTANCE, MODEL_MIN_MATCH_SCORE)
MIL.MmodPreprocess(SearchContext, MIL.M_DEFAULT)
MIL.MmodInquire(SearchContext, MIL.M_DEFAULT, MIL.M_ORIGINAL_X, OrgX)
MIL.MmodInquire(SearchContext, MIL.M_DEFAULT, MIL.M_ORIGINAL_Y, OrgY)
Console.Write("The Geometric target model was defined." + Constants.vbLf)
Console.Write("Model dimensions: {0} x {1}." + Constants.vbLf, MODEL_WIDTH, MODEL_HEIGHT)
Console.Write("Model center: X={0:0.00}, Y={0:0.00}." + Constants.vbLf, OrgX, OrgY)
Console.Write("Model is scale and rotation independent." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, MilImage(0))
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilModelImage, MIL.M_SIZE_X, MIL.M_NULL), MIL.MbufInquire(MilModelImage, MIL.M_SIZE_Y, MIL.M_NULL), 8, MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC, MilImage(1))
Console.Write(Constants.vbLf + "Continuously finding the Geometric Model." + Constants.vbLf)
Console.Write("Press a <Enter> to stop." + Constants.vbLf + Constants.vbLf)
MIL.MdigControl(MilDigitizer, MIL.M_GRAB_MODE, MIL.M_ASYNCHRONOUS)
MIL.MdigGrab(MilDigitizer, MilImage(NbFindDone Mod 2))
MIL.MdigGrab(MilDigitizer, MilImage(NbFindDone Mod 2))
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET, Time)
Do
MIL.MdigGrab(MilDigitizer, MilImage((NbFindDone + 1) Mod 2))
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, Time)
MIL.MmodFind(SearchContext, MilImage(NbFindDone Mod 2), Result)
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, Found)
If (Found >= 1) AndAlso (Found < MODEL_MAX_OCCURRENCES) Then
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_X, x)
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_Y, y)
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_SCALE, Scale)
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_ANGLE, Angle)
MIL.MmodGetResult(Result, MIL.M_DEFAULT, MIL.M_SCORE, Score)
MIL.MmodDraw(MIL.M_DEFAULT, Result, MilImage(NbFindDone Mod 2), MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION + MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT)
Console.Write("Found: X={0,6:0.0}, Y={1,6:0.0}, Angle={2,6:0.0}, Scale={3,5:0.00}, Score={4,5:0.0}% ({5,5:0.0} fps)." & Constants.vbCr, x(0), y(0), Angle(0), Scale(0), Score(0), (NbFindDone + 1) / Time)
Else
Console.Write("Not found! (score<{0,5:0.0}%) ({1,5:0.0} fps)." & Constants.vbCr, MODEL_MIN_MATCH_SCORE, (NbFindDone + 1) / Time)
End If
MIL.MbufCopy(MilImage(NbFindDone Mod 2), MilDisplayImage)
NbFindDone += 1
Loop While Not Console.KeyAvailable
Console.ReadKey()
Console.Write(Constants.vbLf + Constants.vbLf)
MIL.MdigGrabWait(MilDigitizer, MIL.M_GRAB_END)
MIL.MmodFree(Result)
MIL.MmodFree(SearchContext)
MIL.MbufFree(MilImage(1))
MIL.MbufFree(MilImage(0))
End Sub
End Class
End Namespace