'
'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports Matrox.MatroxImagingLibrary
Namespace MDigAutoFocus
Class Program
Private Const IMAGE_FILE As String = MIL.M_IMAGE_PATH & "BaboonMono.mim"
Private Const FOCUS_MAX_NB_POSITIONS As Integer = 100
Private Const FOCUS_MIN_POSITION As Integer = 0
Private Const FOCUS_MAX_POSITION As Integer = (FOCUS_MAX_NB_POSITIONS - 1)
Private Const FOCUS_START_POSITION As Integer = 10
Private Const FOCUS_MAX_POSITION_VARIATION As Integer = MIL.M_DEFAULT
Private Const FOCUS_MODE As Integer = MIL.M_SMART_SCAN
Private Const FOCUS_SENSITIVITY As Integer = 1
Public Class DigHookUserData
Public SourceImage As MIL_ID
Public FocusImage As MIL_ID
Public Display As MIL_ID
Public Iteration As Integer
End Class
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 MilSource As MIL_ID = MIL.M_NULL
Dim MilCameraFocus As MIL_ID = MIL.M_NULL
Dim FocusPos As MIL_INT = 0
Dim UserData As DigHookUserData = New DigHookUserData()
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilSource)
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilCameraFocus)
MIL.MbufClear(MilCameraFocus, 0)
MIL.MdispSelect(MilDisplay, MilCameraFocus)
SimulateGrabFromCamera(MilSource, MilCameraFocus, FOCUS_START_POSITION, MilDisplay)
UserData.SourceImage = MilSource
UserData.FocusImage = MilCameraFocus
UserData.Iteration = 0
UserData.Display = MilDisplay
Console.Write(Constants.vbLf + "AUTOFOCUS:" + Constants.vbLf)
Console.Write("----------" + Constants.vbLf + Constants.vbLf)
Console.Write("Automatic focusing operation will be done on this image." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
Console.Write("Autofocusing..." + Constants.vbLf + Constants.vbLf)
Dim hUserData As GCHandle = GCHandle.Alloc(UserData)
MIL.MdigFocus(MIL.M_NULL, MilCameraFocus, MIL.M_DEFAULT, AddressOf MoveLensHookFunction, CType(hUserData, IntPtr), FOCUS_MIN_POSITION, FOCUS_START_POSITION, FOCUS_MAX_POSITION, FOCUS_MAX_POSITION_VARIATION, FOCUS_MODE + FOCUS_SENSITIVITY, FocusPos)
hUserData.Free()
Console.Write("The best focus position is {0}." + Constants.vbLf, FocusPos)
Console.Write("The best focus position found in {0} iterations." + Constants.vbLf + Constants.vbLf, UserData.Iteration)
Console.Write("Press <Enter> to end." + Constants.vbLf)
Console.ReadKey()
MIL.MbufFree(MilSource)
MIL.MbufFree(MilCameraFocus)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Shared Function MoveLensHookFunction(ByVal HookType As MIL_INT, ByVal Position As MIL_INT, ByVal UserDataHookPtr As IntPtr) As MIL_INT
If (Not IntPtr.Zero.Equals(UserDataHookPtr)) Then
Dim hUserData As GCHandle = CType(UserDataHookPtr, GCHandle)
Dim UserData As DigHookUserData = CType(hUserData.Target, DigHookUserData)
If HookType = MIL.M_CHANGE OrElse HookType = MIL.M_ON_FOCUS Then
SimulateGrabFromCamera(UserData.SourceImage, UserData.FocusImage, CInt(Fix(Position)), UserData.Display)
UserData.Iteration += 1
End If
End If
Return 0
End Function
'
Private Const FOCUS_BEST_POSITION As Double = (FOCUS_MAX_NB_POSITIONS / 2)
Private Shared Sub SimulateGrabFromCamera(ByVal SourceImage As MIL_ID, ByVal FocusImage As MIL_ID, ByVal Iteration As Integer, ByVal AnnotationDisplay As MIL_ID)
Dim NbSmoothNeeded As Integer = 0
Dim BufType As MIL_INT = 0
Dim BufSizeX As MIL_INT = 0
Dim BufSizeY As MIL_INT = 0
Dim Smooth As Integer = 0
Dim TempBuffer As MIL_ID = MIL.M_NULL
Dim SourceOwnerSystem As MIL_ID = MIL.M_NULL
NbSmoothNeeded = CInt(Fix(Math.Abs(Iteration - FOCUS_BEST_POSITION)))
BufType = MIL.MbufInquire(FocusImage, MIL.M_TYPE, MIL.M_NULL)
BufSizeX = MIL.MbufInquire(FocusImage, MIL.M_SIZE_X, MIL.M_NULL)
BufSizeY = MIL.MbufInquire(FocusImage, MIL.M_SIZE_Y, MIL.M_NULL)
If NbSmoothNeeded = 0 Then
MIL.MbufCopy(SourceImage, FocusImage)
ElseIf NbSmoothNeeded = 1 Then
MIL.MimConvolve(SourceImage, FocusImage, MIL.M_SMOOTH)
Else
SourceOwnerSystem = CType(MIL.MbufInquire(SourceImage, MIL.M_OWNER_SYSTEM, MIL.M_NULL), MIL_ID)
MIL.MbufAlloc2d(SourceOwnerSystem, BufSizeX, BufSizeY, BufType, MIL.M_IMAGE + MIL.M_PROC, TempBuffer)
MIL.MimConvolve(SourceImage, TempBuffer, MIL.M_SMOOTH)
For Smooth = 1 To NbSmoothNeeded - 2
MIL.MimConvolve(TempBuffer, TempBuffer, MIL.M_SMOOTH)
Next Smooth
MIL.MimConvolve(TempBuffer, FocusImage, MIL.M_SMOOTH)
MIL.MbufFree(TempBuffer)
End If
DrawCursor(AnnotationDisplay, Iteration)
End Sub
Private Shared Function CURSOR_POSITION(ByVal BufSizeY As MIL_INT) As Double
Return ((BufSizeY * 7) / 8)
End Function
Private Const CURSOR_SIZE As Integer = 14
Private Shared ReadOnly CURSOR_COLOR As Integer = MIL.M_COLOR_GREEN
Private Shared Sub DrawCursor(ByVal AnnotationDisplay As MIL_ID, ByVal Position As Integer)
Dim AnnotationImage As MIL_ID = MIL.M_NULL
Dim BufSizeX As MIL_INT = 0
Dim BufSizeY As MIL_INT = 0
Dim n As MIL_INT = 0
MIL.MdispControl(AnnotationDisplay, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispControl(AnnotationDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
MIL.MdispInquire(AnnotationDisplay, MIL.M_OVERLAY_ID, AnnotationImage)
MIL.MbufInquire(AnnotationImage, MIL.M_SIZE_X, BufSizeX)
MIL.MbufInquire(AnnotationImage, MIL.M_SIZE_Y, BufSizeY)
MIL.MgraColor(MIL.M_DEFAULT, CURSOR_COLOR)
n = (BufSizeX / FOCUS_MAX_NB_POSITIONS)
MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, 0, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE, BufSizeX - 1, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE)
MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, Position * n, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE, Position * n - CURSOR_SIZE, CURSOR_POSITION(BufSizeY))
MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, Position * n, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE, Position * n + CURSOR_SIZE, CURSOR_POSITION(BufSizeY))
MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, Position * n - CURSOR_SIZE, CURSOR_POSITION(BufSizeY), Position * n + CURSOR_SIZE, CURSOR_POSITION(BufSizeY))
End Sub
End Class
End Namespace