halcon中match匹配的例子(ide版和vb6版)

By admin at 2018-05-05 • 0人收藏 • 1707人看过


blob.png

ide(11版):

不能复制代码,位置在

blob.png

1 个回复 | 最后更新于 2018-05-05
2018-05-05   #1

相同功能的VB6版本代码如下:

VERSION 5.00
Object = "{6EBD9146-D219-11D2-ADE5-0000C00F4EF9}#1.9#0"; "halconx.dll"
Begin VB.Form MatchingForm 
   Caption         =   "Matching and Measurement Demo"
   ClientHeight    =   8970
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   11790
   LinkTopic       =   "Form1"
   ScaleHeight     =   598
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   786
   StartUpPosition =   3  'Windows Default
   Begin HALCONXLibCtl.HWindowXCtrl HXCtrl 
      Height          =   7380 
      Left            =   240
      OleObjectBlob   =   "matching.frx":0000
      TabIndex        =   0
      Top             =   240
      Width           =   9690
   End
   Begin VB.CommandButton CreateBtn 
      Caption         =   "Create Model"
      Height          =   495
      Left            =   10200
      TabIndex        =   8
      Top             =   240
      Width           =   1215
   End
   Begin VB.Timer Timer 
      Interval        =   1
      Left            =   10320
      Top             =   4200
   End
   Begin VB.CommandButton StopBtn 
      Caption         =   "Stop"
      Height          =   495
      Left            =   10200
      TabIndex        =   2
      Top             =   1680
      Width           =   1215
   End
   Begin VB.CommandButton StartBtn 
      Caption         =   "Start"
      Height          =   495
      Left            =   10200
      TabIndex        =   1
      Top             =   960
      Width           =   1215
   End
   Begin VB.Label MeasureDIstLabel 
      Caption         =   "Minimum lead distance: "
      Height          =   255
      Left            =   3720
      TabIndex        =   11
      Top             =   8160
      Width           =   2415
   End
   Begin VB.Label MeasureTimeLabel 
      Caption         =   "Time: "
      Height          =   255
      Left            =   1080
      TabIndex        =   10
      Top             =   8160
      Width           =   855
   End
   Begin VB.Label MeasureNumLabel 
      Caption         =   "Number of leads: "
      Height          =   255
      Left            =   2040
      TabIndex        =   9
      Top             =   8160
      Width           =   1575
   End
   Begin VB.Label MatchingScoreLabel 
      Caption         =   "Score: "
      Height          =   255
      Left            =   2040
      TabIndex        =   7
      Top             =   7800
      Width           =   1215
   End
   Begin VB.Label MatchingTimeLabel 
      Caption         =   "Time: "
      Height          =   255
      Left            =   1080
      TabIndex        =   6
      Top             =   7800
      Width           =   855
   End
   Begin VB.Label MeasureLabel 
      Caption         =   "Measure:"
      Height          =   255
      Left            =   240
      TabIndex        =   5
      Top             =   8160
      Width           =   735
   End
   Begin VB.Label MatchingLabel 
      Caption         =   "Matching:"
      Height          =   255
      Left            =   240
      TabIndex        =   4
      Top             =   7800
      Width           =   735
   End
   Begin VB.Label Copyright 
      Alignment       =   2  'Center
      Caption         =   "?2000-2015 MVTec Software GmbH"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   240
      TabIndex        =   3
      Top             =   8640
      Width           =   10935
   End
End
Attribute VB_Name = "MatchingForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' HALCON/VB pattern matching and measure example
'
' ?2000-2015 MVTec Software GmbH
'
' Purpose:
' This example program shows the use of pattern matching with shape models
' to locate an object.  Furthermore, it shows how to use the detected position
' and rotation of the object to construct search spaces for inspection tasks.
' In this particular example, the print on an IC is used to find the IC.  From
' the found position and rotation, two measurement rectangles are constructed
' to measure the spacing between the leads of the IC.  Because of the lighting
' used in this example, the leads have the saturated gray value of 255 at
' several positions and rotations, which enlarges the apparent width of the
' leads, and hence seems to reduce the spacing between the leads, although the
' same board is used in all images.

Option Explicit

Dim Window As HWindowX
Dim Framegrabber As New HFramegrabberX
Dim Img As HImageX, ImgReduced As HImageX, ModelImage As HImageX
Dim Rectangle As New HRegionX, ModelRegion As HRegionX, ModelRegionTrans As HRegionX
Dim Rectangle1 As New HRegionX, Rectangle2 As New HRegionX
Dim ShapeModel As New HShapeModelX
Dim Row As Variant, Column As Variant, Area As Variant
Dim Rect1Row As Double, Rect1Col As Double
Dim Rect2Row As Double, Rect2Col As Double
Dim RectPhi As Double, RectLength1 As Double, RectLength2 As Double
Dim Sys As New HSystemX
Dim Tuple As New HTupleX
Dim Matrix As New HHomMat2DX
Dim Measure1 As New HMeasureX, Measure2 As New HMeasureX
Dim ImgWidth As Variant, ImgHeight As Variant

Private Sub Form_Load()
    Set Window = HXCtrl.HalconWindow
    Call Window.SetPart(0, 0, 491, 645)
    Call Framegrabber.OpenFramegrabber("File", 1, 1, 0, 0, 0, 0, "default", -1, "default", _
                                       -1, "default", "board/board.seq", "default", 1, -1)
    Set Img = Framegrabber.GrabImage()
    Call Window.DispImage(Img)
    ImgWidth = Img.GetImageSize(ImgHeight)
    Call Rectangle.GenRectangle1(188, 182, 298, 412)
    Area = Rectangle.AreaCenter(Row, Column)
    Rect1Row = Row - 102
    Rect1Col = Column + 5
    Rect2Row = Row + 107
    Rect2Col = Column + 5
    RectPhi = 0
    RectLength1 = 170
    RectLength2 = 5
    CreateBtn.Enabled = True
    StartBtn.Enabled = False
    StopBtn.Enabled = False
    Timer.Enabled = False
End Sub

Private Sub CreateBtn_Click()
    CreateBtn.Enabled = False
    Call Window.SetColor("red")
    Call Window.SetDraw("margin")
    Call Window.SetLineWidth(3)
    Call Window.DispRegion(Rectangle)
    Set ImgReduced = Img.ReduceDomain(Rectangle)
    Set ModelImage = ImgReduced.InspectShapeModel(ModelRegion, 1, 30)
    Call Rectangle1.GenRectangle2(Rect1Row, Rect1Col, RectPhi, RectLength1, RectLength2)
    Call Rectangle2.GenRectangle2(Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2)
    Call ShapeModel.CreateShapeModel(ImgReduced, 4, 0, Tuple.TupleRad(360), Tuple.TupleRad(1), _
                                     "none", "use_polarity", 30, 10)
    Call Window.SetColor("green")
    Call Window.SetDraw("fill")
    Call Window.DispRegion(ModelRegion)
    Call Window.SetColor("blue")
    Call Window.SetDraw("margin")
    Call Window.DispRegion(Rectangle1)
    Call Window.DispRegion(Rectangle2)
    StopBtn.Enabled = False
    StartBtn.Enabled = True
End Sub

Private Sub StartBtn_Click()
    Timer.Enabled = True
    CreateBtn.Enabled = False
    StopBtn.Enabled = True
    StartBtn.Enabled = False
End Sub

Private Sub StopBtn_Click()
    Timer.Enabled = False
    CreateBtn.Enabled = False
    StopBtn.Enabled = False
    StartBtn.Enabled = True
End Sub

Private Sub Timer_Timer()
    Action
End Sub

Private Sub Action()
    Dim S1 As Double, S2 As Double
    Dim RowCheck As Variant, ColumnCheck As Variant
    Dim AngleCheck As Variant, Score As Variant
    Dim Rect1RowCheck As Variant, Rect1ColCheck As Variant
    Dim Rect2RowCheck As Variant, Rect2ColCheck As Variant
    Dim RowEdgeFirst1 As Variant, ColumnEdgeFirst1 As Variant
    Dim AmplitudeFirst1 As Variant, RowEdgeSecond1 As Variant
    Dim ColumnEdgeSecond1 As Variant, AmplitudeSecond1 As Variant
    Dim IntraDistance1 As Variant, InterDistance1 As Variant
    Dim RowEdgeFirst2 As Variant, ColumnEdgeFirst2 As Variant
    Dim AmplitudeFirst2 As Variant, RowEdgeSecond2 As Variant
    Dim ColumnEdgeSecond2 As Variant, AmplitudeSecond2 As Variant
    Dim IntraDistance2 As Variant, InterDistance2 As Variant
    Dim NumLeads As Long, MinDistance As Variant

    Set Img = Framegrabber.GrabImage()
    Call Window.DispImage(Img)
    ' Find the IC in the current image.
    S1 = Sys.CountSeconds()
    RowCheck = ShapeModel.FindShapeModel(Img, 0, Tuple.TupleRad(360), 0.7, 1, 0.5, "least_squares", _
                                         4, 0.7, ColumnCheck, AngleCheck, Score)
    S2 = Sys.CountSeconds()
    MatchingTimeLabel = "Time: " & Format(((S2 - S1) * 1000), "0.00")
    MatchingScoreLabel = "Score: "
    If Not IsEmpty(RowCheck) And Not IsArray(RowCheck) Then
        MatchingScoreLabel = "Score: " & Format(Score, "0.00000")
        ' Rotate the model for visualization purposes.
        Call Matrix.VectorAngleToRigid(Row, Column, 0, RowCheck, ColumnCheck, AngleCheck)
        Set ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false")
        Call Window.SetColor("green")
        Call Window.SetDraw("fill")
        Call Window.DispRegion(ModelRegionTrans)
        ' Compute the parameters of the measurement rectangles.
        Rect1RowCheck = Matrix.AffineTransPixel(Rect1Row, Rect1Col, Rect1ColCheck)
        Rect2RowCheck = Matrix.AffineTransPixel(Rect2Row, Rect2Col, Rect2ColCheck)
        ' For visualization purposes, generate the two rectangles as regions and display them.
        Call Rectangle1.GenRectangle2(Rect1RowCheck, Rect1ColCheck, RectPhi + AngleCheck, _
                                      RectLength1, RectLength2)
        Call Rectangle2.GenRectangle2(Rect2RowCheck, Rect2ColCheck, RectPhi + AngleCheck, _
                                      RectLength1, RectLength2)
        Call Window.SetColor("blue")
        Call Window.SetDraw("margin")
        Call Window.DispRegion(Rectangle1)
        Call Window.DispRegion(Rectangle2)
        ' Do the actual masurements.
        S1 = Sys.CountSeconds()
        Call Measure1.GenMeasureRectangle2(Rect1RowCheck, Rect1ColCheck, RectPhi + AngleCheck, _
                                           RectLength1, RectLength2, ImgWidth, ImgHeight, _
                                           "bilinear")
        Call Measure2.GenMeasureRectangle2(Rect2RowCheck, Rect2ColCheck, RectPhi + AngleCheck, _
                                           RectLength1, RectLength2, ImgWidth, ImgHeight, _
                                           "bilinear")
        RowEdgeFirst1 = Measure1.MeasurePairs(Img, 2, 90, "positive", "all", ColumnEdgeFirst1, _
                                              AmplitudeFirst1, RowEdgeSecond1, ColumnEdgeSecond1, _
                                              AmplitudeSecond1, IntraDistance1, InterDistance1)
        RowEdgeFirst2 = Measure2.MeasurePairs(Img, 2, 90, "positive", "all", ColumnEdgeFirst2, _
                                              AmplitudeFirst2, RowEdgeSecond2, ColumnEdgeSecond2, _
                                              AmplitudeSecond2, IntraDistance2, InterDistance2)
        S2 = Sys.CountSeconds()
        MeasureTimeLabel = "Time: " & Format(((S2 - S1) * 1000), "0.00")
        Call Window.SetColor("red")
        Call Window.DispLine(Tuple.TupleSub(RowEdgeFirst1, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleSub(ColumnEdgeFirst1, Tuple.TupleMult(RectLength2, Sin(AngleCheck))), _
                             Tuple.TupleAdd(RowEdgeFirst1, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleAdd(ColumnEdgeFirst1, Tuple.TupleMult(RectLength2, Sin(AngleCheck))))
        Call Window.DispLine(Tuple.TupleSub(RowEdgeSecond1, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleSub(ColumnEdgeSecond1, Tuple.TupleMult(RectLength2, Sin(AngleCheck))), _
                             Tuple.TupleAdd(RowEdgeSecond1, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleAdd(ColumnEdgeSecond1, Tuple.TupleMult(RectLength2, Sin(AngleCheck))))
        Call Window.DispLine(Tuple.TupleSub(RowEdgeFirst2, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleSub(ColumnEdgeFirst2, Tuple.TupleMult(RectLength2, Sin(AngleCheck))), _
                             Tuple.TupleAdd(RowEdgeFirst2, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleAdd(ColumnEdgeFirst2, Tuple.TupleMult(RectLength2, Sin(AngleCheck))))
        Call Window.DispLine(Tuple.TupleSub(RowEdgeSecond2, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleSub(ColumnEdgeSecond2, Tuple.TupleMult(RectLength2, Sin(AngleCheck))), _
                             Tuple.TupleAdd(RowEdgeSecond2, Tuple.TupleMult(RectLength2, Cos(AngleCheck))), _
                             Tuple.TupleAdd(ColumnEdgeSecond2, Tuple.TupleMult(RectLength2, Sin(AngleCheck))))
        NumLeads = Tuple.TupleLength(IntraDistance1) + Tuple.TupleLength(IntraDistance2)
        MeasureNumLabel = "Number of leads: " & Format(NumLeads, "00")
        MinDistance = Tuple.TupleMin(Tuple.TupleConcat(InterDistance1, InterDistance2))
        MeasureDIstLabel = "Minimum lead distance: " & Format(MinDistance, "#0.000")
    End If
End Sub


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...