'
'
'
'
'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MPat
Friend Class Program
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
Console.Write(Constants.vbLf + "GRAYSCALE PATTERN MATCHING:" + Constants.vbLf)
Console.Write("---------------------------" + Constants.vbLf + Constants.vbLf)
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
SearchModelExample(MilSystem, MilDisplay)
SearchModelAngleRangeExample(MilSystem, MilDisplay)
SearchModelAtAngleExample(MilSystem, MilDisplay)
AutoAllocationModelExample(MilSystem, MilDisplay)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Const FIND_IMAGE_FILE As String = MIL.M_IMAGE_PATH & "CircuitsBoard.mim"
Private Const FIND_MODEL_X_POS As Integer = 153
Private Const FIND_MODEL_Y_POS As Integer = 132
Private Const FIND_MODEL_WIDTH As Integer = 128
Private Const FIND_MODEL_HEIGHT As Integer = 128
Private Const FIND_MODEL_X_CENTER As Double = (FIND_MODEL_X_POS + (FIND_MODEL_WIDTH - 1) / 2.0)
Private Const FIND_MODEL_Y_CENTER As Double = (FIND_MODEL_Y_POS + (FIND_MODEL_HEIGHT - 1) / 2.0)
Private Const FIND_SHIFT_X As Double = 4.5
Private Const FIND_SHIFT_Y As Double = 7.5
Private Const FIND_MODEL_MIN_MATCH_SCORE As Double = 70.0
Private Const FIND_MODEL_MIN_ACCURACY As Double = 0.1
Private Shared Sub SearchModelExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilImage As MIL_ID = MIL.M_NULL
Dim GraphicList As MIL_ID = MIL.M_NULL
Dim ContextId As MIL_ID = MIL.M_NULL
Dim Result As MIL_ID = MIL.M_NULL
Dim NumResults As MIL_INT = 0
Dim XOrg As Double = 0.0
Dim YOrg As Double = 0.0
Dim x As Double = 0.0
Dim y As Double = 0.0
Dim ErrX As Double = 0.0
Dim ErrY As Double = 0.0
Dim Score As Double = 0.0
Dim Time As Double = 0.0
Dim AnnotationColor As Double = MIL.M_COLOR_GREEN
MIL.MbufRestore(FIND_IMAGE_FILE, MilSystem, MilImage)
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, GraphicList)
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList)
MIL.MpatAlloc(MilSystem, MIL.M_NORMALIZED, MIL.M_DEFAULT, ContextId)
MIL.MpatDefine(ContextId, MIL.M_REGULAR_MODEL, MilImage, FIND_MODEL_X_POS, _
FIND_MODEL_Y_POS, FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT, MIL.M_DEFAULT)
MIL.MpatControl(ContextId, MIL.M_DEFAULT, MIL.M_ACCURACY, MIL.M_HIGH)
MIL.MpatControl(ContextId, MIL.M_DEFAULT, MIL.M_SPEED, MIL.M_HIGH)
MIL.MpatPreprocess(ContextId, MIL.M_DEFAULT, MilImage)
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor)
MIL.MpatDraw(MIL.M_DEFAULT, ContextId, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, _
MIL.M_DEFAULT, MIL.M_ORIGINAL)
Console.Write(Constants.vbLf + "A {0}x{1} model was defined in the source image." + Constants.vbLf, FIND_MODEL_WIDTH, FIND_MODEL_HEIGHT)
Console.Write("It will be found in an image shifted by {0:0.00} in X and {1:0.00} in Y." + Constants.vbLf, FIND_SHIFT_X, FIND_SHIFT_Y)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraClear(MIL.M_DEFAULT, GraphicList)
MIL.MimTranslate(MilImage, MilImage, FIND_SHIFT_X, FIND_SHIFT_Y, MIL.M_DEFAULT)
MIL.MpatAllocResult(MilSystem, MIL.M_DEFAULT, Result)
MIL.MpatFind(ContextId, MilImage, Result)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, CType(MIL.M_NULL, IntPtr))
MIL.MpatFind(ContextId, MilImage, Result)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, Time)
MIL.MpatGetResult(Result, MIL.M_GENERAL, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, NumResults)
If NumResults = 1 Then
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_X, x)
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_Y, y)
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_SCORE, Score)
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor)
MIL.MpatDraw(MIL.M_DEFAULT, Result, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT)
ErrX = Math.Abs((FIND_MODEL_X_CENTER + FIND_SHIFT_X) - x)
ErrY = Math.Abs((FIND_MODEL_Y_CENTER + FIND_SHIFT_Y) - y)
MIL.MpatInquire(ContextId, MIL.M_DEFAULT, MIL.M_ORIGINAL_X, XOrg)
MIL.MpatInquire(ContextId, MIL.M_DEFAULT, MIL.M_ORIGINAL_Y, YOrg)
Console.Write("Search results:" + Constants.vbLf)
Console.Write("---------------------------------------------------" + Constants.vbLf)
Console.Write("The model is found to be shifted by " & Constants.vbTab & "X:{0:0.00}, Y:{1:0.00}." + Constants.vbLf, x - XOrg, y - YOrg)
Console.Write("The model position error is " & Constants.vbTab + Constants.vbTab & "X:{0:0.00}, Y:{1:0.00}" + Constants.vbLf, ErrX, ErrY)
Console.Write("The model match score is " & Constants.vbTab + Constants.vbTab & "{0:0.0}" + Constants.vbLf, Score)
Console.Write("The search time is " & Constants.vbTab + Constants.vbTab + Constants.vbTab & "{0:0.000} ms" + Constants.vbLf + Constants.vbLf, Time * 1000.0)
If (Math.Abs((x - XOrg) - FIND_SHIFT_X) > FIND_MODEL_MIN_ACCURACY) OrElse (Math.Abs((y - YOrg) - FIND_SHIFT_Y) > FIND_MODEL_MIN_ACCURACY) OrElse (Score < FIND_MODEL_MIN_MATCH_SCORE) Then
Console.Write("Results verification error !" + Constants.vbLf)
End If
Else
Console.Write("Model not found !" + Constants.vbLf)
End If
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraClear(MIL.M_DEFAULT, GraphicList)
MIL.MgraFree(GraphicList)
MIL.MpatFree(Result)
MIL.MpatFree(ContextId)
MIL.MbufFree(MilImage)
End Sub
Private Const ROTATED_FIND_IMAGE_FILE As String = MIL.M_IMAGE_PATH & "CircuitsBoard.mim"
Private Const ROTATED_FIND_ROTATION_DELTA_ANGLE As Integer = 10
Private Const ROTATED_FIND_ROTATION_ANGLE_STEP As Integer = 1
Private Const ROTATED_FIND_RAD_PER_DEG As Double = 0.01745329251
Private Const ROTATED_FIND_MODEL_X_POS As Integer = 153
Private Const ROTATED_FIND_MODEL_Y_POS As Integer = 132
Private Const ROTATED_FIND_MODEL_WIDTH As Integer = 128
Private Const ROTATED_FIND_MODEL_HEIGHT As Integer = 128
Private Const ROTATED_FIND_MODEL_X_CENTER As Double = ROTATED_FIND_MODEL_X_POS + (ROTATED_FIND_MODEL_WIDTH - 1) / 2.0
Private Const ROTATED_FIND_MODEL_Y_CENTER As Double = ROTATED_FIND_MODEL_Y_POS + (ROTATED_FIND_MODEL_HEIGHT - 1) / 2.0
Private Const ROTATED_FIND_MIN_POSITION_ACCURACY As Double = 0.1
Private Const ROTATED_FIND_MIN_ANGLE_ACCURACY As Double = 0.25
Private Const ROTATED_FIND_ANGLE_DELTA_POS As Integer = ROTATED_FIND_ROTATION_DELTA_ANGLE
Private Const ROTATED_FIND_ANGLE_DELTA_NEG As Integer = ROTATED_FIND_ROTATION_DELTA_ANGLE
Private Shared Sub SearchModelAngleRangeExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilSourceImage As MIL_ID = MIL.M_NULL
Dim MilTargetImage As MIL_ID = MIL.M_NULL
Dim MilDisplayImage As MIL_ID = MIL.M_NULL
Dim GraphicList As MIL_ID = MIL.M_NULL
Dim MilContextId As MIL_ID = MIL.M_NULL
Dim MilResult As MIL_ID = MIL.M_NULL
Dim RealX As Double = 0.0
Dim RealY As Double = 0.0
Dim RealAngle As Double = 0.0
Dim X As Double = 0.0
Dim Y As Double = 0.0
Dim Angle As Double = 0.0
Dim Score As Double = 0.0
Dim Time As Double = 0.0
Dim ErrX As Double = 0.0
Dim ErrY As Double = 0.0
Dim ErrAngle As Double = 0.0
Dim SumErrX As Double = 0.0
Dim SumErrY As Double = 0.0
Dim SumErrAngle As Double = 0.0
Dim SumTime As Double = 0.0
Dim NumResults As Integer = 0
Dim NbFound As Integer = 0
Dim AnnotationColor As Double = MIL.M_COLOR_GREEN
MIL.MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, MilSourceImage)
MIL.MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, MilTargetImage)
MIL.MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, MilDisplayImage)
MIL.MdispSelect(MilDisplay, MilDisplayImage)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, GraphicList)
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList)
MIL.MpatAlloc(MilSystem, MIL.M_NORMALIZED, MIL.M_DEFAULT, MilContextId)
MIL.MpatDefine(MilContextId, MIL.M_REGULAR_MODEL + MIL.M_CIRCULAR_OVERSCAN, MilSourceImage, _
ROTATED_FIND_MODEL_X_POS, ROTATED_FIND_MODEL_Y_POS, _
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT, MIL.M_DEFAULT)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_SPEED, MIL.M_MEDIUM)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_ACCURACY, MIL.M_HIGH)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_MODE, MIL.M_ENABLE)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_DELTA_NEG, ROTATED_FIND_ANGLE_DELTA_NEG)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_DELTA_POS, ROTATED_FIND_ANGLE_DELTA_POS)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_ACCURACY, ROTATED_FIND_MIN_ANGLE_ACCURACY)
MIL.MpatControl(MilContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_INTERPOLATION_MODE, MIL.M_BILINEAR)
MIL.MpatPreprocess(MilContextId, MIL.M_DEFAULT, MilSourceImage)
MIL.MpatAllocResult(MilSystem, MIL.M_DEFAULT, MilResult)
MIL.MpatDraw(MIL.M_DEFAULT, MilContextId, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, _
MIL.M_DEFAULT, MIL.M_ORIGINAL)
Console.Write(Constants.vbLf + "A {0}x{1} model was defined in the source image." + Constants.vbLf, ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT)
Console.Write("It will be searched in images rotated from {0} degree to {1} degree." + Constants.vbLf, -ROTATED_FIND_ROTATION_DELTA_ANGLE, ROTATED_FIND_ROTATION_DELTA_ANGLE)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MpatFind(MilContextId, MilSourceImage, MilResult)
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, NumResults)
If NumResults = 1 Then
RealAngle = ROTATED_FIND_ROTATION_DELTA_ANGLE
Do While RealAngle >= -ROTATED_FIND_ROTATION_DELTA_ANGLE
MIL.MimRotate(MilSourceImage, MilTargetImage, RealAngle, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_BILINEAR + MIL.M_OVERSCAN_CLEAR)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, CType(MIL.M_NULL, IntPtr))
MIL.MpatFind(MilContextId, MilTargetImage, MilResult)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, Time)
MIL.MgraClear(MIL.M_DEFAULT, GraphicList)
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, NumResults)
If NumResults = 1 Then
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_POSITION_X, X)
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_POSITION_Y, Y)
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_ANGLE, Angle)
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_SCORE, Score)
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor)
MIL.MpatDraw(MIL.M_DEFAULT, MilResult, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MbufCopy(MilTargetImage, MilDisplayImage)
ErrAngle = CalculateAngleDist(Angle, RealAngle)
RotateModelCenter(MilSourceImage, RealX, RealY, RealAngle)
ErrX = Math.Abs(X - RealX)
ErrY = Math.Abs(Y - RealY)
SumErrAngle += ErrAngle
SumErrX += ErrX
SumErrY += ErrY
SumTime += Time
NbFound += 1
If (ErrX > ROTATED_FIND_MIN_POSITION_ACCURACY) OrElse (ErrY > ROTATED_FIND_MIN_POSITION_ACCURACY) OrElse (ErrAngle > ROTATED_FIND_MIN_ANGLE_ACCURACY) Then
Console.Write("Model accuracy error at angle {0:0.0} !" + Constants.vbLf + Constants.vbLf, RealAngle)
Console.Write("Errors are X:{0:0.000}, Y:{1:0.000} and Angle:{2:0.00}" + Constants.vbLf + Constants.vbLf, ErrX, ErrY, ErrAngle)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
End If
Else
Console.Write("Model was not found at angle {0:0.0} !" + Constants.vbLf + Constants.vbLf, RealAngle)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
End If
RealAngle -= ROTATED_FIND_ROTATION_ANGLE_STEP
Loop
Console.Write(Constants.vbLf + "Search statistics for the model found in the rotated images." + Constants.vbLf)
Console.Write("------------------------------------------------------------" + Constants.vbLf)
Console.Write("The average position error is " & Constants.vbTab + Constants.vbTab & "X:{0:0.000}, Y:{1:0.000}" + Constants.vbLf, SumErrX / NbFound, SumErrY / NbFound)
Console.Write("The average angle error is " & Constants.vbTab + Constants.vbTab & "{0:0.000}" + Constants.vbLf, SumErrAngle / NbFound)
Console.Write("The average search time is " & Constants.vbTab + Constants.vbTab & "{0:0.000} ms" + Constants.vbLf + Constants.vbLf, SumTime * 1000.0 / NbFound)
Else
Console.Write("Model was not found!" + Constants.vbLf + Constants.vbLf)
End If
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraFree(GraphicList)
MIL.MpatFree(MilResult)
MIL.MpatFree(MilContextId)
MIL.MbufFree(MilTargetImage)
MIL.MbufFree(MilSourceImage)
MIL.MbufFree(MilDisplayImage)
End Sub
Private Shared Sub RotateModelCenter(ByVal Buffer As MIL_ID, ByRef X As Double, ByRef Y As Double, ByVal Angle As Double)
Dim BufSizeX As MIL_INT = MIL.MbufInquire(Buffer, MIL.M_SIZE_X, MIL.M_NULL)
Dim BufSizeY As MIL_INT = MIL.MbufInquire(Buffer, MIL.M_SIZE_Y, MIL.M_NULL)
Dim RadAngle As Double = Angle * ROTATED_FIND_RAD_PER_DEG
Dim CosAngle As Double = Math.Cos(RadAngle)
Dim SinAngle As Double = Math.Sin(RadAngle)
Dim OffSetX As Double = CDbl(BufSizeX - 1) / 2.0
Dim OffSetY As Double = CDbl(BufSizeY - 1) / 2.0
X = (ROTATED_FIND_MODEL_X_CENTER - OffSetX) * CosAngle + (ROTATED_FIND_MODEL_Y_CENTER - OffSetY) * SinAngle + OffSetX
Y = (ROTATED_FIND_MODEL_Y_CENTER - OffSetY) * CosAngle - (ROTATED_FIND_MODEL_X_CENTER - OffSetX) * SinAngle + OffSetY
End Sub
Private Shared Function CalculateAngleDist(ByVal Angle1 As Double, ByVal Angle2 As Double) As Double
Dim dist As Double = Math.Abs(Angle1 - Angle2)
Do While dist >= 360.0
dist -= 360.0
Loop
If dist > 180.0 Then
dist = 360.0 - dist
End If
Return dist
End Function
Shared Sub SearchModelAtAngleExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilSourceImage As MIL_ID = MIL.M_NULL
Dim MilTargetImage As MIL_ID = MIL.M_NULL
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim ContextId As MIL_ID = MIL.M_NULL
Dim GraphicList As MIL_ID = MIL.M_NULL
Dim MilResult As MIL_ID = MIL.M_NULL
Dim Time As Double = 0.0
Dim NbFound As MIL_INT = 0
Dim AnnotationColor As Double = MIL.M_COLOR_GREEN
MIL.MbufRestore(ROTATED_FIND_IMAGE_FILE, MilSystem, MilSourceImage)
MIL.MdispSelect(MilDisplay, MilSourceImage)
MIL.MbufAlloc2d(MilSystem, MIL.MbufInquire(MilSourceImage, MIL.M_SIZE_X, MIL.M_NULL), _
MIL.MbufInquire(MilSourceImage, MIL.M_SIZE_Y, MIL.M_NULL), _
8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, MilTargetImage)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, GraphicList)
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList)
MIL.MpatAlloc(MilSystem, MIL.M_NORMALIZED, MIL.M_DEFAULT, ContextId)
MIL.MpatDefine(ContextId, MIL.M_REGULAR_MODEL, MilSourceImage, _
ROTATED_FIND_MODEL_X_POS, ROTATED_FIND_MODEL_Y_POS, _
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT, MIL.M_DEFAULT)
MIL.MpatControl(ContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_MODE, MIL.M_ENABLE)
MIL.MpatControl(ContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_DELTA_NEG, 0)
MIL.MpatControl(ContextId, MIL.M_DEFAULT, MIL.M_SEARCH_ANGLE_DELTA_POS, 0)
MIL.MpatControl(ContextId, 0, MIL.M_SEARCH_ANGLE, ROTATED_FIND_ROTATION_DELTA_ANGLE)
MIL.MpatPreprocess(ContextId, MIL.M_DEFAULT, MilSourceImage)
MIL.MpatAllocResult(MilSystem, MIL.M_DEFAULT, MilResult)
MIL.MpatDraw(MIL.M_DEFAULT, ContextId, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, _
MIL.M_DEFAULT, MIL.M_ORIGINAL)
Console.Write(Constants.vbLf + "A {0}x{1} model was defined in the source image." + Constants.vbLf, _
ROTATED_FIND_MODEL_WIDTH, ROTATED_FIND_MODEL_HEIGHT)
Console.Write("It will be searched in an image rotated at {0} degrees." + Constants.vbLf, _
-ROTATED_FIND_ROTATION_DELTA_ANGLE)
Console.Write("Press <Enter> to continue." & Constants.vbLf)
Console.ReadKey()
MIL.MimRotate(MilSourceImage, MilTargetImage, ROTATED_FIND_ROTATION_DELTA_ANGLE, MIL.M_DEFAULT, _
MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_BILINEAR + MIL.M_OVERSCAN_CLEAR)
MIL.MdispSelect(MilDisplay, MilTargetImage)
MIL.MpatFind(ContextId, MilTargetImage, MilResult)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, CType(MIL.M_NULL, IntPtr))
MIL.MpatFind(ContextId, MilTargetImage, MilResult)
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, Time)
MIL.MgraClear(MIL.M_DEFAULT, GraphicList)
MIL.MpatGetResult(MilResult, MIL.M_DEFAULT, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, NbFound)
If NbFound = 1 Then
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor)
MIL.MpatDraw(MIL.M_DEFAULT, MilResult, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT)
Console.Write("A search model at a specific angle has been found in the rotated image." & Constants.vbLf)
Console.Write("The search time is {0:F3} ms." & Constants.vbLf + Constants.vbLf, Time * 1000.0)
Else
Console.Write("Model was not found!" & Constants.vbLf + Constants.vbLf)
End If
Console.Write("Press <Enter> to continue." & Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_SHOW, MIL.M_DISABLE)
MIL.MpatFree(MilResult)
MIL.MpatFree(ContextId)
MIL.MgraFree(GraphicList)
MIL.MbufFree(MilTargetImage)
MIL.MbufFree(MilSourceImage)
End Sub
Private Const AUTO_MODEL_IMAGE_FILE As String = MIL.M_IMAGE_PATH & "Wafer.mim"
Private Const AUTO_MODEL_TARGET_IMAGE_FILE As String = MIL.M_IMAGE_PATH & "WaferShifted.mim"
Private Const AUTO_MODEL_WIDTH As Integer = 64
Private Const AUTO_MODEL_HEIGHT As Integer = 64
Private Shared Sub AutoAllocationModelExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilSubImage As MIL_ID = MIL.M_NULL
Dim GraphicList As MIL_ID = MIL.M_NULL
Dim ContextId As MIL_ID = MIL.M_NULL
Dim Result As MIL_ID = MIL.M_NULL
Dim AllocError As MIL_INT = 0
Dim NumResults As MIL_INT = 0
Dim ImageWidth As MIL_INT = 0
Dim ImageHeight As MIL_INT = 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 AnnotationColor As Double = MIL.M_COLOR_GREEN
MIL.MbufRestore(AUTO_MODEL_IMAGE_FILE, MilSystem, MilImage)
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, GraphicList)
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList)
MIL.MbufInquire(MilImage, MIL.M_SIZE_X, ImageWidth)
MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, ImageHeight)
MIL.MbufChild2d(MilImage, ImageWidth / 2, ImageHeight / 2, ImageWidth / 2, ImageHeight / 2, MilSubImage)
MIL.MgraControl(MIL.M_DEFAULT, MIL.M_DRAW_OFFSET_X, CType(-(ImageWidth / 2), Double))
MIL.MgraControl(MIL.M_DEFAULT, MIL.M_DRAW_OFFSET_Y, CType(-(ImageHeight / 2), Double))
MIL.MpatAlloc(MilSystem, MIL.M_NORMALIZED, MIL.M_DEFAULT, ContextId)
MIL.MpatDefine(ContextId, MIL.M_AUTO_MODEL, MilSubImage, MIL.M_DEFAULT, MIL.M_DEFAULT, _
AUTO_MODEL_WIDTH, AUTO_MODEL_HEIGHT, MIL.M_DEFAULT)
MIL.MpatControl(ContextId, MIL.M_DEFAULT, MIL.M_ACCURACY, MIL.M_HIGH)
MIL.MappGetError(MIL.M_DEFAULT, MIL.M_CURRENT, AllocError)
If AllocError = 0 Then
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor)
MIL.MpatDraw(MIL.M_DEFAULT, ContextId, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, _
MIL.M_DEFAULT, MIL.M_ORIGINAL)
Console.Write("A model was automatically defined in the image." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraClear(MIL.M_DEFAULT, GraphicList)
MIL.MbufLoad(AUTO_MODEL_TARGET_IMAGE_FILE, MilImage)
MIL.MpatAllocResult(MilSystem, MIL.M_DEFAULT, Result)
MIL.MpatPreprocess(ContextId, MIL.M_DEFAULT, MilSubImage)
MIL.MpatFind(ContextId, MilSubImage, Result)
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, NumResults)
If NumResults = 1 Then
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_X, x)
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_POSITION_Y, y)
MIL.MpatGetResult(Result, MIL.M_DEFAULT, MIL.M_SCORE, Score)
MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor)
MIL.MpatDraw(MIL.M_DEFAULT, Result, GraphicList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MpatInquire(ContextId, MIL.M_DEFAULT, MIL.M_ORIGINAL_X, OrgX)
MIL.MpatInquire(ContextId, MIL.M_DEFAULT, MIL.M_ORIGINAL_Y, OrgY)
Console.Write("An image misaligned by 50 pixels in X and 20 pixels in Y was loaded." + Constants.vbLf + Constants.vbLf)
Console.Write("The image is found to be shifted by {0:0.00} in X, and {1:0.00} in Y." + Constants.vbLf, x - OrgX, y - OrgY)
Console.Write("Model match score is {0:0.0} percent." + Constants.vbLf, Score)
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
Else
Console.Write("Error: Pattern not found properly." + Constants.vbLf)
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
End If
MIL.MpatFree(Result)
MIL.MpatFree(ContextId)
Else
Console.Write("Error: Automatic model definition failed." + Constants.vbLf)
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
End If
MIL.MgraControl(MIL.M_DEFAULT, MIL.M_DRAW_OFFSET_X, 0.0)
MIL.MgraControl(MIL.M_DEFAULT, MIL.M_DRAW_OFFSET_Y, 0.0)
MIL.MgraFree(GraphicList)
MIL.MbufFree(MilSubImage)
MIL.MbufFree(MilImage)
End Sub
End Class
End Namespace