0

我的问题是以合理的速度组合附近的轮廓。

我从一个包含大型闭合形状图像的 CV8U 单通道灰度垫开始。在这个形状的内侧发芽的是一条短曲线。我的任务是仅将曲线变成点向量。

所以我阈值我的垫子并打电话给 FindContours。结果是许多轮廓的向量。有些是噪音,包括我没能完全掩盖的大块形状。但有几个是我曲线的一部分。问题是曲线 - 在原始图像中是连续的 - 被 FindContours 分割成许多轮廓。为了解决这个问题,我找到了最大的轮廓,它始终是我的曲线的一部分,并搜索其他附近的轮廓。如果我找到任何东西,我将它们连接在一起,然后重复。

最后,我有一个包含我的曲线的点向量。有用。但是所有的循环和迭代都让它变得非常慢。

什么替代方法可以保留输出(代表我的曲线的点向量)但运行速度快?

Private vpcurve As VectorOfPoint
...
    GrowCurve(FindContoursOutput, inc, BiggestContourInFindContoursOutput)
...
Private Sub GrowCurve(ContoursToCheck As VectorOfVectorOfPoint, ContoursIncluded() As Boolean, StartContour As Integer)
    vpcurve.Push(ContoursToCheck(StartContour).ToArray)
    ContoursIncluded(StartContour) = True
    For j = ContoursToCheck.Size - 1 To 0 Step -1
        If Not ContoursIncluded(j) Then
            If contoursClose(ContoursToCheck, j, StartContour) Then
                GrowCurve(ContoursToCheck, ContoursIncluded, j)
            End If
        End If
    Next
End Sub

Private Function contoursClose(cnt As VectorOfVectorOfPoint, index1 As Integer, index2 As Integer) As Boolean
    For i As Integer = cnt(index1).Size - 1 To 0 Step -1
        Dim p1 As Point = cnt(index1)(i)
        For j As Integer = cnt(index2).Size - 1 To 0 Step -1
            Dim p2 As Point = cnt(index2)(j)
            If (Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)) < 30 Then 'arbitrary  pixel distance
                contoursClose = True
                Exit Function
            End If
        Next
    Next
    contoursClose = False
End Function
4

0 回答 0