【OpenCV_SURF特征识别】VB.NET代码

Public Function SURF特征识别(ByRef 图片X As String, ByRef 图片Y As String,ByRef 命令组 As String(), ByRef 前摇组 As String(), ByRef 后摇组 As String(), ByRef 线程方案 As String, 线程序号 As String,
                             ByRef j As String, x1 As Int32, y1 As Int32, x2 As Int32, y2 As Int32, form1 As Form1, id As Integer) As String
        Dim 立即结束 As Int16 = 0
        Dim minloc As OpenCvSharp.Point, maxloc As OpenCvSharp.Point
        Dim 最不相似 As Double, 最相似值 As Double, 识图时间 = Val(按住时间组(j)) * 1000 - 1
        Dim 图X As Int32 = -1, 图Y As Int32 = -1, 相似度 As Single = 1 - VBS.Eval(拆解键值("精准度", GetINI(线程方案, 线程序号, "", 数据库))) / 100
        Dim 跳转步骤母 As String = 0

        Dim 小图 As New List(Of Mat)
        小图.Add(New Mat(当前图路径(线程序号, 线程方案)))
        For i2 = 1 To 999
            If 步骤图在否(线程序号 & "." & i2, 线程方案) Then
                小图.Add(New Mat(CStr(方案原图路径 & 线程方案 & "\" & 线程序号 & "." & i2 & ".jpg")))
            Else
                Exit For
            End If
        Next

        Dim 开始时间 = Now
        For Each i5 In 小图
            ThreadPool.QueueUserWorkItem(Sub()
                                             Dim 小图mat = i5.Clone, 大图 As New Mat
                                             If 小图mat.Width > x2 - x1 Or 小图mat.Height > y2 - y1 Then '一但超出大图大小,就结束
                                                 Exit Sub
                                             End If

'---------------------以下才是核心代码---------------------


                                             '默认100,关键点检测的阈值,越高监测的点越少质量越高,但是小图太小容易报错,自己权衡
                                             Dim 阈值 As Double = 3000
                                             Dim 金字塔组数 As Int16 = 4 '默认4,金字塔组数,似乎数量只对运算速度有影响?
                                             Dim 金字塔层数 As Int16 = 3 '默认3,每组金子塔的层数,越多过滤的噪点也越多
                                             '参数4:True容易爆内存,但是更准更稳,效率慢点;参数5:True计算速度更快,坐标也更准,False会到处乱飞
                                             Dim SURF = OpenCvSharp.XFeatures2D.SURF.Create(阈值, 金字塔组数, 金字塔层数, True, True)
重新查找:
                                             Dim 截图 = New Bitmap(x2 - x1, y2 - y1)
                                             Dim g = Graphics.FromImage(截图)
                                             g.CopyFromScreen(x1, y1, 0, 0, New Drawing.Size(x2 - x1, y2 - y1))

                                             Dim 大图mat As Mat = 截图.ToMat() : g.Dispose()
                                             Dim matSrcRet As Mat = New Mat()
                                             Dim matToRet As Mat = New Mat()
                                             Dim keyPointsSrc, keyPointsTo As KeyPoint()
                                             SURF.DetectAndCompute(大图mat, Nothing, keyPointsSrc, matSrcRet)
                                             SURF.DetectAndCompute(小图mat, Nothing, keyPointsTo, matToRet)

                                             Dim flnMatcher = New OpenCvSharp.FlannBasedMatcher()
                                             Dim 误差组 = flnMatcher.Match(matSrcRet, matToRet)

                                             Dim 大图特征组 = New List(Of Point2f)()
                                             For i As Integer = 0 To matSrcRet.Rows - 1
                                                 Dim 误差 As Double = 误差组(i).Distance
                                                 If 误差 < 相似度 Then
                                                     大图特征组.Add(keyPointsSrc(误差组(i).QueryIdx).Pt)
                                                 End If
                                             Next
                                             flnMatcher.Dispose() : matToRet.Release() : matSrcRet.Release() : 大图mat.Release()
                                             误差组 = Nothing : 截图.Dispose() : keyPointsSrc = Nothing : keyPointsTo = Nothing

                                             If 大图特征组.Count > 0 Then
                                                 Dim 均X, 均Y As Single
                                                 Dim 元素数 As Int32 = 大图特征组.Count - 1
                                                 For i = 0 To 元素数
                                                     均X += 大图特征组(i).X
                                                     均Y += 大图特征组(i).Y
                                                 Next
                                                 图X = CInt(均X / (元素数 + 1))
                                                 图Y = CInt(均Y / (元素数 + 1))
                                                 立即结束 = 1
                                                 SURF.Dispose()
                                                 Exit Sub
                                             ElseIf 立即结束 = 1 Then
                                                 SURF.Dispose()
                                                 Exit Sub
                                             Else
                                                 '没找到就继续
                                                 If 跳转步骤母 <> 0 Then
                                                     If 开始时间 < Now.AddMilliseconds(-识图时间) And 识图时间 > 0 Then
                                                         当前序号 = 跳转步骤母
                                                         SURF特征识别 = "超时跳转"
                                                         立即结束 = 1
                                                         SURF.Dispose()
                                                         Exit Sub
                                                     End If
                                                 End If
                                                 If 线程管理.方案(id) = Nothing Then
                                                     SURF.Dispose()
                                                     Exit Sub
                                                 End If
                                                 '停止过程
                                                 线程管理.是否关闭(id)
                                                 大图特征组.Clear() ': 最大误差 = Nothing : 最小误差 = Nothing
                                                 GoTo 重新查找
                                             End If
                                             SURF.Dispose()
                                         End Sub, 1)
        Next
        Do While 立即结束 = 0
            Thread.Sleep(1)
        Loop
        '立即结束
        If 图X = -1 Then
            图片X = -1 : 图片Y = -1
        Else
            图片X = 图X : 图片Y = 图Y
        End If
    End Function

你可能感兴趣的:(opencv,.net,计算机视觉,visualstudio)