'
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Matrox.MatroxImagingLibrary
Namespace MBead
Friend Class Program
Private Shared ReadOnly USER_POSITION_COLOR As MIL_INT = MIL.M_COLOR_RED
Private Shared ReadOnly USER_TEMPLATE_COLOR As MIL_INT = MIL.M_COLOR_CYAN
Private Shared ReadOnly TRAINED_BEAD_WIDTH_COLOR As MIL_INT = MIL.M_RGB888(255, 128, 0)
Private Shared ReadOnly MODEL_FINDER_COLOR As MIL_INT = MIL.M_COLOR_GREEN
Private Shared ReadOnly COORDINATE_SYSTEM_COLOR As MIL_INT = MIL.M_RGB888(164, 164, 0)
Private Shared ReadOnly RESULT_SEARCH_BOX_COLOR As MIL_INT = MIL.M_COLOR_CYAN
Private Shared ReadOnly PASS_BEAD_WIDTH_COLOR As MIL_INT = MIL.M_COLOR_GREEN
Private Shared ReadOnly PASS_BEAD_POSITION_COLOR As MIL_INT = MIL.M_COLOR_GREEN
Private Shared ReadOnly FAIL_NOT_FOUND_COLOR As MIL_INT = MIL.M_COLOR_RED
Private Shared ReadOnly FAIL_SMALL_WIDTH_COLOR As MIL_INT = MIL.M_RGB888(255, 128, 0)
Private Shared ReadOnly FAIL_EDGE_OFFSET_COLOR As MIL_INT = MIL.M_COLOR_RED
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))
FixturedBeadExample(MilSystem, MilDisplay)
PredefinedBeadExample(MilSystem, MilDisplay)
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL)
End Sub
Private Shared ReadOnly IMAGE_FILE_TRAINING As String = MIL.M_IMAGE_PATH & "BeadTraining.mim"
Private Shared ReadOnly IMAGE_FILE_TARGET As String = MIL.M_IMAGE_PATH & "BeadTarget.mim"
Private Const NUMBER_OF_TRAINING_POINTS As Integer = 13
Private Shared ReadOnly TrainingPointsX() As Double = {180, 280, 400, 430, 455, 415, 370, 275, 185, 125, 105, 130, 180}
Private Shared ReadOnly TrainingPointsY() As Double = {190, 215, 190, 200, 260, 330, 345, 310, 340, 305, 265, 200, 190}
Private Const MAX_ANGLE_CORRECTION_VALUE As Double = 20.0
Private Const MAX_DEVIATION_OFFSET_VALUE As Double = 25.0
Private Const WIDTH_DELTA_NEG_VALUE As Double = 2.0
Private Const MODEL_ORIGIN_X As Integer = 145
Private Const MODEL_ORIGIN_Y As Integer = 115
Private Const MODEL_SIZE_X As Integer = 275
Private Const MODEL_SIZE_Y As Integer = 60
Private Shared Sub FixturedBeadExample(ByVal MilSystem As MIL_ID, ByVal MIlDisplay As MIL_ID)
Dim MilGraList As MIL_ID = MIL.M_NULL
Dim MilImageTraining As MIL_ID = MIL.M_NULL
Dim MilImageTarget As MIL_ID = MIL.M_NULL
Dim MilBeadContext As MIL_ID = MIL.M_NULL
Dim MilBeadResult As MIL_ID = MIL.M_NULL
Dim MilModelFinderContext As MIL_ID = MIL.M_NULL
Dim MilModelFinderResult As MIL_ID = MIL.M_NULL
Dim MilFixturingOffset As MIL_ID = MIL.M_NULL
Dim NominalWidth As Double = 0.0
Dim AvWidth As Double = 0.0
Dim MaxGap As Double = 0.0
Dim GapCov As Double = 0.0
Dim Score As Double = 0.0
MIL.MbufRestore(IMAGE_FILE_TRAINING, MilSystem, MilImageTraining)
MIL.MbufRestore(IMAGE_FILE_TARGET, MilSystem, MilImageTarget)
MIL.MdispSelect(MIlDisplay, MilImageTraining)
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, MilGraList)
MIL.MdispControl(MIlDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraList)
Console.Write(Constants.vbLf + "FIXTURED BEAD INSPECTION:" + Constants.vbLf)
Console.Write("-------------------------" + Constants.vbLf + Constants.vbLf)
Console.Write("This program performs a bead inspection on a mechanical part." + Constants.vbLf)
Console.Write("In the first step, a bead template context is trained using an ")
Console.Write("image." + Constants.vbLf + "In the second step, a mechanical part, at an arbitrary ")
Console.Write("angle and with" + Constants.vbLf + "a defective bead, is inspected." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbeadAlloc(MilSystem, MIL.M_DEFAULT, MIL.M_DEFAULT, MilBeadContext)
MIL.MbeadAllocResult(MilSystem, MIL.M_DEFAULT, MilBeadResult)
MIL.MbeadTemplate(MilBeadContext, MIL.M_ADD, MIL.M_DEFAULT, MIL.M_TEMPLATE_LABEL(1), NUMBER_OF_TRAINING_POINTS, TrainingPointsX, TrainingPointsY, CType(MIL.M_NULL, IntPtr), MIL.M_DEFAULT)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_TEMPLATE_INPUT_UNITS, MIL.M_WORLD)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_ANGLE_ACCURACY_MAX_DEVIATION, MAX_ANGLE_CORRECTION_VALUE)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_OFFSET_MAX, MAX_DEVIATION_OFFSET_VALUE)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_WIDTH_DELTA_NEG, WIDTH_DELTA_NEG_VALUE)
MIL.MgraColor(MIL.M_DEFAULT, USER_TEMPLATE_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadContext, MilGraList, MIL.M_DRAW_POSITION_POLYLINE, MIL.M_USER, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, USER_POSITION_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadContext, MilGraList, MIL.M_DRAW_POSITION, MIL.M_USER, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
Console.Write("The initial points specified by the user (in red) are" + Constants.vbLf)
Console.Write("used to train the bead information from an image." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.McalUniform(MilImageTraining, 0, 0, 1, 1, 0, MIL.M_DEFAULT)
MIL.MbeadTrain(MilBeadContext, MilImageTraining, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, TRAINED_BEAD_WIDTH_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadContext, MilGraList, MIL.M_DRAW_WIDTH, MIL.M_TRAINED, MIL.M_TEMPLATE_LABEL(1), MIL.M_ALL, MIL.M_DEFAULT)
MIL.MbeadInquire(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_TRAINED_WIDTH_NOMINAL, NominalWidth)
Console.Write("The template has been trained and is displayed in orange." + Constants.vbLf)
Console.Write("Its nominal trained width is {0:#.##} pixels." + Constants.vbLf + Constants.vbLf, NominalWidth)
MIL.MmodAlloc(MilSystem, MIL.M_GEOMETRIC, MIL.M_DEFAULT, MilModelFinderContext)
MIL.MmodAllocResult(MilSystem, MIL.M_DEFAULT, MilModelFinderResult)
MIL.MmodDefine(MilModelFinderContext, MIL.M_IMAGE, MilImageTraining, MODEL_ORIGIN_X, MODEL_ORIGIN_Y, MODEL_SIZE_X, MODEL_SIZE_Y)
MIL.MmodPreprocess(MilModelFinderContext, MIL.M_DEFAULT)
MIL.McalAlloc(MilSystem, MIL.M_FIXTURING_OFFSET, MIL.M_DEFAULT, MilFixturingOffset)
MIL.McalFixture(MIL.M_NULL, MilFixturingOffset, MIL.M_LEARN_OFFSET, MIL.M_MODEL_MOD, MilModelFinderContext, 0, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, MODEL_FINDER_COLOR)
MIL.MmodDraw(MIL.M_DEFAULT, MilModelFinderContext, MilGraList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_ORIGINAL)
Console.Write("A Model Finder model (in green) is also defined to" + Constants.vbLf)
Console.Write("further fixture the bead verification operation." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraClear(MIL.M_DEFAULT, MilGraList)
MIL.MdispSelect(MIlDisplay, MilImageTarget)
MIL.MmodFind(MilModelFinderContext, MilImageTarget, MilModelFinderResult)
MIL.MgraColor(MIL.M_DEFAULT, MODEL_FINDER_COLOR)
MIL.MmodDraw(MIL.M_DEFAULT, MilModelFinderResult, MilGraList, MIL.M_DRAW_BOX + MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.McalFixture(MilImageTarget, MilFixturingOffset, MIL.M_MOVE_RELATIVE, MIL.M_RESULT_MOD, MilModelFinderResult, 0, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, COORDINATE_SYSTEM_COLOR)
MIL.McalDraw(MIL.M_DEFAULT, MIL.M_NULL, MilGraList, MIL.M_DRAW_RELATIVE_COORDINATE_SYSTEM, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MbeadVerify(MilBeadContext, MilImageTarget, MilBeadResult, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, RESULT_SEARCH_BOX_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadResult, MilGraList, MIL.M_DRAW_SEARCH_BOX, MIL.M_ALL, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
Console.Write("The mechanical part
Console.Write("located" + Constants.vbLf + "using Model Finder, and the bead
Console.Write("(in cyan) were" + Constants.vbLf + "positioned accordingly." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MgraClear(MIL.M_DEFAULT, MilGraList)
MIL.MgraColor(MIL.M_DEFAULT, COORDINATE_SYSTEM_COLOR)
MIL.McalDraw(MIL.M_DEFAULT, MIL.M_NULL, MilGraList, MIL.M_DRAW_RELATIVE_COORDINATE_SYSTEM, MIL.M_DEFAULT, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, PASS_BEAD_WIDTH_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadResult, MilGraList, MIL.M_DRAW_WIDTH, MIL.M_PASS, MIL.M_TEMPLATE_LABEL(1), MIL.M_ALL, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, FAIL_NOT_FOUND_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadResult, MilGraList, MIL.M_DRAW_SEARCH_BOX, MIL.M_FAIL_NOT_FOUND, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, FAIL_SMALL_WIDTH_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadResult, MilGraList, MIL.M_DRAW_SEARCH_BOX, MIL.M_FAIL_WIDTH_MIN, MIL.M_TEMPLATE_LABEL(1), MIL.M_ALL, MIL.M_DEFAULT)
MIL.MbeadGetResult(MilBeadResult, MIL.M_TEMPLATE_LABEL(1), MIL.M_GENERAL, MIL.M_SCORE, Score)
MIL.MbeadGetResult(MilBeadResult, MIL.M_TEMPLATE_LABEL(1), MIL.M_GENERAL, MIL.M_GAP_COVERAGE, GapCov)
MIL.MbeadGetResult(MilBeadResult, MIL.M_TEMPLATE_LABEL(1), MIL.M_GENERAL, MIL.M_WIDTH_AVERAGE, AvWidth)
MIL.MbeadGetResult(MilBeadResult, MIL.M_TEMPLATE_LABEL(1), MIL.M_GENERAL, MIL.M_GAP_MAX_LENGTH, MaxGap)
Console.Write("The bead has been inspected:" + Constants.vbLf)
Console.Write(" -Passing bead sections (green) cover {0:0.00}% of the bead" + Constants.vbLf, Score)
Console.Write(" -Missing bead sections (red) cover {0:0.00}% of the bead" + Constants.vbLf, GapCov)
Console.Write(" -Sections outside the specified width tolerances are drawn in orange" + Constants.vbLf)
Console.Write(" -The bead
Console.Write(" -The bead
Console.Write("Press <Enter> to continue." + Constants.vbLf)
Console.ReadKey()
MIL.MmodFree(MilModelFinderContext)
MIL.MmodFree(MilModelFinderResult)
MIL.MbeadFree(MilBeadContext)
MIL.MbeadFree(MilBeadResult)
MIL.McalFree(MilFixturingOffset)
MIL.MbufFree(MilImageTraining)
MIL.MbufFree(MilImageTarget)
MIL.MgraFree(MilGraList)
End Sub
Private Shared ReadOnly CAP_FILE_TARGET As String = MIL.M_IMAGE_PATH + "Cap.mim"
Private Const CIRCLE_CENTER_X As Double = 330.0
Private Const CIRCLE_CENTER_Y As Double = 230.0
Private Const CIRCLE_RADIUS As Double = 120.0
Private Const EDGE_THRESHOLD_VALUE As Double = 25.0
Private Const MAX_CONTOUR_DEVIATION_OFFSET As Double = 5.0
Private Const MAX_CONTOUR_FOUND_OFFSET As Double = 25.0
Private Shared Sub PredefinedBeadExample(ByVal MilSystem As MIL_ID, ByVal MIlDisplay As MIL_ID)
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim MilImageTarget As MIL_ID = MIL.M_NULL
Dim MilBeadContext As MIL_ID = MIL.M_NULL
Dim MilBeadResult As MIL_ID = MIL.M_NULL
Dim MaximumOffset As Double = 0.0
MIL.MbufRestore(CAP_FILE_TARGET, MilSystem, MilImageTarget)
MIL.MdispSelect(MIlDisplay, MilImageTarget)
MIL.MdispControl(MIlDisplay, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispInquire(MIlDisplay, MIL.M_OVERLAY_ID, MilOverlayImage)
MIL.MdispControl(MIlDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_TRANSPARENT_COLOR)
Console.Write(Constants.vbLf + "PREDEFINED BEAD INSPECTION:" + Constants.vbLf)
Console.Write("---------------------------" + Constants.vbLf + Constants.vbLf)
Console.Write("This program performs a bead inspection of a bottle" + Constants.vbLf)
Console.Write("cap
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbeadAlloc(MilSystem, MIL.M_DEFAULT, MIL.M_DEFAULT, MilBeadContext)
MIL.MbeadAllocResult(MilSystem, MIL.M_DEFAULT, MilBeadResult)
MIL.MbeadTemplate(MilBeadContext, MIL.M_ADD, MIL.M_BEAD_EDGE, MIL.M_TEMPLATE_LABEL(1), 0, CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr), CType(MIL.M_NULL, IntPtr), MIL.M_DEFAULT)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_TRAINING_PATH, MIL.M_CIRCLE)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_TEMPLATE_CIRCLE_CENTER_X, CIRCLE_CENTER_X)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_TEMPLATE_CIRCLE_CENTER_Y, CIRCLE_CENTER_Y)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_TEMPLATE_CIRCLE_RADIUS, CIRCLE_RADIUS)
MIL.MbeadControl(MilBeadContext, MIL.M_TEMPLATE_LABEL(1), MIL.M_THRESHOLD_VALUE, EDGE_THRESHOLD_VALUE)
MIL.MbeadControl(MilBeadContext, MIL.M_ALL, MIL.M_WIDTH_NOMINAL_MODE, MIL.M_USER_DEFINED)
MIL.MbeadControl(MilBeadContext, MIL.M_ALL, MIL.M_FAIL_WARNING_OFFSET, MAX_CONTOUR_FOUND_OFFSET)
MIL.MbeadControl(MilBeadContext, MIL.M_ALL, MIL.M_OFFSET_MAX, MAX_CONTOUR_DEVIATION_OFFSET)
MIL.MgraColor(MIL.M_DEFAULT, USER_TEMPLATE_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadContext, MilOverlayImage, MIL.M_DRAW_POSITION, MIL.M_USER, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
MIL.MbeadTrain(MilBeadContext, MIL.M_NULL, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, TRAINED_BEAD_WIDTH_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadContext, MilOverlayImage, MIL.M_DRAW_SEARCH_BOX, MIL.M_TRAINED, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
Console.Write("A circular template that was parametrically defined by the user" + Constants.vbLf)
Console.Write("is displayed (in cyan). The template has been trained and the resulting" + Constants.vbLf)
Console.Write("search is displayed (in orange)." + Constants.vbLf)
Console.Write("Press <Enter> to continue." + Constants.vbLf + Constants.vbLf)
Console.ReadKey()
MIL.MbeadVerify(MilBeadContext, MilImageTarget, MilBeadResult, MIL.M_DEFAULT)
MIL.MdispControl(MIlDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_TRANSPARENT_COLOR)
MIL.MgraColor(MIL.M_DEFAULT, PASS_BEAD_POSITION_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadResult, MilOverlayImage, MIL.M_DRAW_POSITION, MIL.M_PASS, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
MIL.MgraColor(MIL.M_DEFAULT, FAIL_EDGE_OFFSET_COLOR)
MIL.MbeadDraw(MIL.M_DEFAULT, MilBeadResult, MilOverlayImage, MIL.M_DRAW_POSITION, MIL.M_FAIL_OFFSET, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT)
MIL.MbeadGetResult(MilBeadResult, MIL.M_TEMPLATE_LABEL(1), MIL.M_GENERAL, MIL.M_OFFSET_MAX, MaximumOffset)
Console.Write("The bottle cap shape has been inspected:" + Constants.vbLf)
Console.Write(" -Sections outside the specified offset tolerance are drawn in red" + Constants.vbLf)
Console.Write(" -The maximum offset value is {0:0.00} pixels." + Constants.vbLf + Constants.vbLf, MaximumOffset)
Console.Write("Press <Enter> to terminate." + Constants.vbLf)
Console.ReadKey()
MIL.MbeadFree(MilBeadContext)
MIL.MbeadFree(MilBeadResult)
MIL.MbufFree(MilImageTarget)
End Sub
End Class
End Namespace