'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MBlob
Friend Class Program
Private Const IMAGE_FILE As String = MIL.M_IMAGE_PATH & "BoltsNutsWashers.mim"
Private Const IMAGE_THRESHOLD_VALUE As Integer = 26
Private Const MIN_BLOB_AREA As Integer = 50
Private Const MAX_BLOB_AREA As Integer = 50000
Private Const MIN_BLOB_RADIUS As Integer = 3
Private Const MIN_COMPACTNESS As Double = 1.5
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 MilImage As MIL_ID = MIL.M_NULL
Dim MilGraphicList As MIL_ID = MIL.M_NULL
Dim MilBinImage As MIL_ID = MIL.M_NULL
Dim MilBlobResult As MIL_ID = MIL.M_NULL
Dim MilBlobFeatureList As MIL_ID = MIL.M_NULL
Dim TotalBlobs As MIL_INT = 0
Dim BlobsWithHoles As MIL_INT = 0
Dim BlobsWithRoughHoles As MIL_INT = 0
Dim n As Integer = 0
Dim SizeX As MIL_INT = 0
Dim SizeY As MIL_INT = 0
Dim CogX() As Double = Nothing
Dim CogY() As Double = Nothing
MIL.MappAllocDefault(MIL.M_DEFAULT, MilApplication, MilSystem, MilDisplay, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr))
MIL.MbufRestore(IMAGE_FILE, MilSystem, MilImage)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, MilGraphicList)
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList)
MIL.MdispSelect(MilDisplay, MilImage)
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)
Console.Write(Constants.vbLf + "BLOB ANALYSIS:" + Constants.vbLf)
Console.Write("--------------" + Constants.vbLf + Constants.vbLf)
Console.Write("This program determines the number of bolts, nuts and washers" + Constants.vbLf)
Console.Write("in the image and finds their center of gravity." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MimBinarize(MilImage, MilBinImage, MIL.M_FIXED + MIL.M_GREATER_OR_EQUAL, IMAGE_THRESHOLD_VALUE, MIL.M_NULL)
MIL.MimOpen(MilBinImage, MilBinImage, MIN_BLOB_RADIUS, MIL.M_BINARY)
MIL.MimClose(MilBinImage, MilBinImage, MIN_BLOB_RADIUS, MIL.M_BINARY)
MIL.MblobAllocFeatureList(MilSystem, MilBlobFeatureList)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_AREA)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_CENTER_OF_GRAVITY)
MIL.MblobAllocResult(MilSystem, MilBlobResult)
MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult)
MIL.MblobSelect(MilBlobResult, MIL.M_EXCLUDE, MIL.M_AREA, MIL.M_LESS_OR_EQUAL, MIN_BLOB_AREA, MIL.M_NULL)
MIL.MblobGetNumber(MilBlobResult, TotalBlobs)
Console.Write("There are {0} objects ", TotalBlobs)
CogX = New Double(CType((TotalBlobs - 1), Integer)) {}
CogY = New Double(CType((TotalBlobs - 1), Integer)) {}
If CogX IsNot Nothing AndAlso CogY IsNot Nothing Then
MIL.MblobGetResult(MilBlobResult, MIL.M_CENTER_OF_GRAVITY_X, CogX)
MIL.MblobGetResult(MilBlobResult, MIL.M_CENTER_OF_GRAVITY_Y, CogY)
Console.Write("and their centers of gravity are:" + Constants.vbLf)
For n = 0 To CType((TotalBlobs - 1), Integer)
Console.Write("Blob #{0}: X={1,5:0.0}, Y={2,5:0.0}" + Constants.vbLf, n, CogX(n), CogY(n))
Next n
Else
Console.Write(Constants.vbLf + "Error: Not enough memory." + Constants.vbLf)
End If
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_RED)
MIL.MblobDraw(MIL.M_DEFAULT, MilBlobResult, MilGraphicList, MIL.M_DRAW_CENTER_OF_GRAVITY, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT)
MIL.MblobControl(MilBlobResult, MIL.M_FOREGROUND_VALUE, MIL.M_ZERO)
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_COMPACTNESS)
MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult)
MIL.MblobSelect(MilBlobResult, MIL.M_EXCLUDE, MIL.M_AREA, MIL.M_OUT_RANGE, MIN_BLOB_AREA, MAX_BLOB_AREA)
MIL.MblobGetNumber(MilBlobResult, BlobsWithHoles)
MIL.MblobSelect(MilBlobResult, MIL.M_EXCLUDE, MIL.M_COMPACTNESS, MIL.M_LESS_OR_EQUAL, MIN_COMPACTNESS, MIL.M_NULL)
MIL.MblobGetNumber(MilBlobResult, BlobsWithRoughHoles)
Console.Write(Constants.vbLf + "Identified objects:" + Constants.vbLf)
Console.Write("{0} bolts" + Constants.vbLf, TotalBlobs - BlobsWithHoles)
Console.Write("{0} nuts" + Constants.vbLf, BlobsWithHoles - BlobsWithRoughHoles)
Console.Write("{0} washers" + Constants.vbLf + Constants.vbLf, BlobsWithRoughHoles)
Console.Write("Press <Enter> to end." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraFree(MilGraphicList)
MIL.MblobFree(MilBlobResult)
MIL.MblobFree(MilBlobFeatureList)
MIL.MbufFree(MilBinImage)
MIL.MbufFree(MilImage)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
End Class
End Namespace