'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Imports System.Runtime.InteropServices
Namespace MGraInteractive
Public Class STestParameters
Public MilDisplay As MIL_ID
Public MilGraphicsList As MIL_ID
Public MilGraphicsContext As MIL_ID
Public MilBinImage As MIL_ID
Public MilBlobFeatureList As MIL_ID
Public MilBlobResult As MIL_ID
Public RegionLabel As MIL_INT
End Class
Public 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
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
Console.WriteLine()
Console.WriteLine("INTERACTIVE REGIONS AND SUBPIXEL ANNOTATIONS:")
Console.WriteLine("---------------------------------------------")
Console.WriteLine()
Console.WriteLine("This program determines the number of blobs in a region")
Console.WriteLine("defined interactively by the user. The extracted blob
Console.WriteLine("features are drawn with subpixel accuracy in a zoomable")
Console.WriteLine("display.")
Console.WriteLine()
InteractivityExample(MilSystem, MilDisplay)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Shared ReadOnly IMAGE_FILE As String = MIL.M_IMAGE_PATH & "Seals.mim"
Private Const IMAGE_THRESHOLD_VALUE As Integer = 110
Private Const RECTANGLE_POSITION_X As Integer = 160
Private Const RECTANGLE_POSITION_Y As Integer = 310
Private Const RECTANGLE_WIDTH As Integer = 200
Private Const RECTANGLE_HEIGHT As Integer = 175
Private Const RECTANGLE_ANGLE As Integer = 0
Private Shared Sub InteractivityExample(ByVal MilSystem As MIL_ID, ByVal MilDisplay As MIL_ID)
Dim MilImage As MIL_ID = MIL.M_NULL
Dim MilGraphicsList As MIL_ID = MIL.M_NULL
Dim MilGraphicsContext As MIL_ID = MIL.M_NULL
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 RegionLabel As MIL_INT
Dim DataStructure As New STestParameters()
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilImage)
MIL.MdispSelect(MilDisplay, MilImage)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, MilGraphicsList)
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicsList)
MIL.MgraAlloc(MilSystem, MilGraphicsContext)
MIL.MdispControl(MilDisplay, MIL.M_GRAPHIC_LIST_INTERACTIVE, MIL.M_ENABLE)
MIL.MgraRectAngle(MilGraphicsContext, MilGraphicsList, RECTANGLE_POSITION_X, RECTANGLE_POSITION_Y, RECTANGLE_WIDTH, RECTANGLE_HEIGHT, RECTANGLE_ANGLE, MIL.M_CENTER_AND_DIMENSION)
MIL.MgraInquireList(MilGraphicsList, MIL.M_LIST, MIL.M_DEFAULT, MIL.M_LAST_LABEL, RegionLabel)
MIL.MgraControl(MilGraphicsContext, MIL.M_SELECTABLE, MIL.M_DISABLE)
MIL.MbufInquire(MilImage, MIL.M_SIZE_X, SizeX)
MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, SizeY)
MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, MilBinImage)
MIL.MimBinarize(MilImage, MilBinImage, MIL.M_FIXED + MIL.M_LESS, IMAGE_THRESHOLD_VALUE, MIL.M_NULL)
MIL.MblobAllocFeatureList(MilSystem, MilBlobFeatureList)
MIL.MblobAllocResult(MilSystem, MilBlobResult)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_CENTER_OF_GRAVITY)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_BOX)
MIL.MgraControlList(MilGraphicsList, MIL.M_GRAPHIC_LABEL(RegionLabel), MIL.M_DEFAULT, MIL.M_GRAPHIC_SELECTED, MIL.M_TRUE)
CountObjects(MilDisplay, MilGraphicsList, MilGraphicsContext, MilBinImage, MilBlobFeatureList, MilBlobResult)
DataStructure.MilDisplay = MilDisplay
DataStructure.MilGraphicsList = MilGraphicsList
DataStructure.MilGraphicsContext = MilGraphicsContext
DataStructure.MilBinImage = MilBinImage
DataStructure.RegionLabel = RegionLabel
DataStructure.MilBlobFeatureList = MilBlobFeatureList
DataStructure.MilBlobResult = MilBlobResult
Dim DataStructureHandle As GCHandle = GCHandle.Alloc(DataStructure)
Dim HookHandlerDelegate As MIL_GRA_HOOK_FUNCTION_PTR = New MIL_GRA_HOOK_FUNCTION_PTR(AddressOf HookHandler)
MIL.MgraHookFunction(MilGraphicsList, MIL.M_GRAPHIC_MODIFIED, HookHandlerDelegate, GCHandle.ToIntPtr(DataStructureHandle))
If (MIL.MdispInquire(MilDisplay, MIL.M_DISPLAY_TYPE, CType(MIL.M_NULL, IntPtr)) <> MIL.M_AUXILIARY) Then
Console.WriteLine("You can try using your mouse to interactively modify the")
Console.WriteLine("displayed region, such as moving, resizing, or rotating the")
Console.WriteLine("region. If you do so, the results and annotations will be")
Console.WriteLine("immediately updated")
Console.WriteLine()
Else
Console.WriteLine("***NOTE: This example does not implement the interactivity")
Console.WriteLine(" with an auxiliary display.")
Console.WriteLine()
End If
Console.WriteLine("Press <Enter> to exit.")
Console.ReadKey()
MIL.MgraHookFunction(MilGraphicsList, MIL.M_GRAPHIC_MODIFIED + MIL.M_UNHOOK, HookHandlerDelegate, GCHandle.ToIntPtr(DataStructureHandle))
DataStructureHandle.Free()
MIL.MblobFree(MilBlobResult)
MIL.MblobFree(MilBlobFeatureList)
MIL.MbufFree(MilBinImage)
MIL.MgraFree(MilGraphicsContext)
MIL.MgraFree(MilGraphicsList)
MIL.MbufFree(MilImage)
End Sub
Private Shared Function HookHandler(ByVal HookType As MIL_INT, ByVal EventId As MIL_ID, ByVal UserDataPtr As IntPtr) As MIL_INT
If (Not IntPtr.Zero.Equals(UserDataPtr)) Then
Dim hUserData As GCHandle = GCHandle.FromIntPtr(UserDataPtr)
Dim DataStructure As STestParameters = TryCast(hUserData.Target, STestParameters)
Dim ModifiedGraphicLabel As MIL_INT = 0
MIL.MgraGetHookInfo(EventId, MIL.M_GRAPHIC_LABEL_VALUE, ModifiedGraphicLabel)
If ModifiedGraphicLabel = DataStructure.RegionLabel Then
CountObjects(DataStructure.MilDisplay, DataStructure.MilGraphicsList, DataStructure.MilGraphicsContext, DataStructure.MilBinImage, DataStructure.MilBlobFeatureList, DataStructure.MilBlobResult)
End If
End If
Return MIL.M_NULL
End Function
Private Const MAX_TEXT_SIZE As Integer = 100
Private Shared Sub CountObjects(ByVal MilDisplay As MIL_ID, ByVal MilGraphicList As MIL_ID, ByVal MilGraphicContext As MIL_ID, ByVal MilBinImage As MIL_ID, ByVal MilBlobFeatureList As MIL_ID, ByVal MilBlobResult As MIL_ID)
Dim NumberOfBlobs As MIL_INT = 0
Dim NumberOfPrimitives As MIL_INT = 0
Dim Index As MIL_INT
Dim TextLabel As String
MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_DISABLE)
MIL.MgraInquireList(MilGraphicList, MIL.M_LIST, MIL.M_DEFAULT, MIL.M_NUMBER_OF_GRAPHICS, NumberOfPrimitives)
For Index = NumberOfPrimitives - 1 To 1 Step -1
MIL.MgraControlList(MilGraphicList, MIL.M_GRAPHIC_INDEX(1), MIL.M_DEFAULT, MIL.M_DELETE, MIL.M_DEFAULT)
Next
MIL.MbufSetRegion(MilBinImage, MilGraphicList, MIL.M_DEFAULT, MIL.M_RASTERIZE + MIL.M_FILL_REGION, MIL.M_DEFAULT)
MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult)
MIL.MblobGetNumber(MilBlobResult, NumberOfBlobs)
MIL.MgraControl(MilGraphicContext, MIL.M_INPUT_UNITS, MIL.M_DISPLAY)
TextLabel = String.Format(" Number of blobs found: {0:00} ", NumberOfBlobs)
MIL.MgraColor(MilGraphicContext, MIL.M_COLOR_WHITE)
MIL.MgraText(MilGraphicContext, MilGraphicList, 10, 10, TextLabel)
MIL.MgraControl(MilGraphicContext, MIL.M_INPUT_UNITS, MIL.M_PIXEL)
MIL.MgraColor(MilGraphicContext, MIL.M_COLOR_RED)
MIL.MblobDraw(MilGraphicContext, MilBlobResult, MilGraphicList, MIL.M_DRAW_CENTER_OF_GRAVITY, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT)
MIL.MgraColor(MilGraphicContext, MIL.M_COLOR_GREEN)
MIL.MblobDraw(MilGraphicContext, MilBlobResult, MilGraphicList, MIL.M_DRAW_BOX, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT)
MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_ENABLE)
End Sub
End Class
End Namespace