当前位置:编程学习 > VB >>

求图像边界的算法/边界分析模块

求图像边界的算法/边界分析模块(送Q币)
Q币, 模块, 算法, 图像
请看我头像,这是一个两个工件的加工在一起后和图像.

现在要求测量中间这个工件的位置偏移量等,精度要求不高,主要的边界判断要准确.
 
请各位帮忙指点指点! --------------------编程问答-------------------- 图像在这:

/upload/2013122117/66_avatar_middle.jpg --------------------编程问答-------------------- --------------------编程问答-------------------- UPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUP --------------------编程问答-------------------- 种子填充算法+颜色差别算法
请参考这个
Dim stackX(0 To STACK_SIZE) As Integer
Dim stackY(0 To STACK_SIZE) As Integer
Dim stackI As Long
Dim x1, y1, x2, y2 As Integer '临时存放seedfill覆盖的矩形区域
Private Function Minimum(ParamArray Vals())
Dim n As Integer, MinVal
On Error Resume Next
    MinVal = Vals(0)
    For n = 1 To UBound(Vals)
        If Vals(n) < MinVal Then MinVal = Vals(n)
    Next n
    Minimum = MinVal
End Function
Private Function Maximum(ParamArray Vals())
Dim n As Integer, MaxVal
On Error Resume Next
    MaxVal = Vals(0)
    For n = 1 To UBound(Vals)
        If Vals(n) > MaxVal Then MaxVal = Vals(n)
    Next n
    Maximum = MaxVal
End Function
Private Sub c2hsb(ByVal clr As Long)
Dim MyR As Single, MyG As Single, MyB As Single
Dim Max As Single, Min As Single
Dim MyS As Single
Dim Delta As Single, MyVal As Single
Dim cc As String * 6
Dim r1, g1, b1 As Byte
On Error Resume Next
    cc = Right("000000" + Hex$(clr), 6)
    b1 = Val("&H" + Left(cc, 2))
    g1 = Val("&H" + Mid(cc, 3, 2))
    r1 = Val("&H" + Right(cc, 2))
    MyR = r1 / 255: MyG = g1 / 255: MyB = b1 / 255
    Max = Maximum(MyR, MyG, MyB)
    Min = Minimum(MyR, MyG, MyB)
    hsbB = Int(Max * 100)
    If Max <> 0 Then
        MyS = (Max - Min) / Max * 100
    Else
        MyS = 0
    End If
    hsbS = MyS
    If hsbS = 0 Then
        hsbH = 0
    Else
        Delta = Max - Min
        Select Case Max
        Case MyR
            MyVal = (MyG - MyB) / Delta
        Case MyG
            MyVal = 2 + (MyB - MyR) / Delta
        Case MyB
            MyVal = 4 + (MyR - MyG) / Delta
        End Select
        MyVal = MyVal * 60
        If MyVal < 0 Then MyVal = MyVal + 360
        hsbH = MyVal
    End If
'   Debug.Print "hsb="; hsbH; " "; hsbS; " "; hsbB
End Sub
Private Function ColorDistance(ByVal c1 As Long, ByVal c2 As Long) As Long
Dim cd As Long
Dim h1, s1, b1, h2, s2, b2 As Single
On Error Resume Next
    If c1 = -1 Or c2 = -1 Then
        ColorDistance = 1000000
        Exit Function
    End If
    c2hsb (c1)
    h1 = hsbH / 360
    s1 = hsbS
    b1 = hsbB
    c2hsb (c2)
    h2 = hsbH / 360
    s2 = hsbS
    b2 = hsbB
    cd = Abs(h1 - h2)
    cd = cd + Abs(s1 - s2)
    cd = cd + Abs(b1 - b2)
    ColorDistance = cd
End Function
Private Sub push(ByVal px As Integer, ByVal py As Integer)
On Error Resume Next
    stackX(stackI) = px
    stackY(stackI) = py
    If stackI < STACK_SIZE Then
        stackI = stackI + 1
    Else
        MsgBox "堆栈溢出!"
        stackI = 0
    End If
End Sub
Private Sub pop()
On Error Resume Next
    If stackI <= 0 Then
        XX = -1
        YY = -1
        Exit Sub
    End If
    stackI = stackI - 1
    XX = stackX(stackI)
    YY = stackY(stackI)
End Sub
Private Function seedfill(ByVal sx As Integer, ByVal sy As Integer, ByVal cc As Long, ByVal fc As Long, ByVal mc As Long, ByVal bc As Long) As Boolean
Dim min_cd2 As Long
Dim cd As Long
Dim cd1 As Long
On Error Resume Next
    min_cd2 = ColorDistance(fc, bc) '与背景色差
    stackI = 0
    x1 = 32767: x2 = -1: y1 = 32767: y2 = -1
    push sx, sy
    Do
        pop
        If XX = -1 And YY = -1 Then Exit Do
        c = Pic.Point(XX, YY)
        cd = ColorDistance(c, fc)
        If c <> cc And cd < min_cd2 Then
            Pic.PSet (XX, YY), cc
            If XX < x1 Then x1 = XX
            If XX > x2 Then x2 = XX
            If YY < y1 Then y1 = YY
            If YY > y2 Then y2 = YY
            If YY < BVIH - 1 Then
                push XX, YY + 1
                If stackI = 0 Then
                    Pic.Cls
                    seedfill = False
                    Exit Function
                End If
            End If
            If YY > 0 Then
                push XX, YY - 1
                If stackI = 0 Then
                    Pic.Cls
                    seedfill = False
                    Exit Function
                End If
            End If
            If XX > 0 And sx - MAX_XYD <= XX - 1 And XX - 1 < sx + MAX_XYD Then '限制XX最多左右扩展MAX_XYD个像素
                push XX - 1, YY
                If stackI = 0 Then
                    Pic.Cls
                    seedfill = False
                    Exit Function
                End If
            End If
            If XX < BVIW - 1 And sx - MAX_XYD <= XX + 1 And XX + 1 < sx + MAX_XYD Then '限制XX最多左右扩展MAX_XYD个像素
                push XX + 1, YY
                If stackI = 0 Then
                    Pic.Cls
                    seedfill = False
                    Exit Function
                End If
            End If
        End If
    Loop
'   Pic.Line (x1, y1)-(x2, y2), &HFFFF&, B
    If x2 = -1 Or y2 = -1 Then
        seedfill = False
    Else
        seedfill = True
    End If
End Function
--------------------编程问答--------------------
引用 4 楼 zhao4zhong1 的回复:
种子填充算法+颜色差别算法
请参考这个

VB code
Dim stackX(0 To STACK_SIZE) As Integer
Dim stackY(0 To STACK_SIZE) As Integer
Dim stackI As Long
Dim x1, y1, x2, y2 As Integer '临时存放seedfill覆盖的矩形区域
Private Functio……


这个算法对源图象要求太高,只能分析一些类似验证码之类的、相对纯净的图象,对于从相机捕获的“杂乱”的基本不适用。
--------------------编程问答--------------------
引用 5 楼 zwxu999 的回复:
引用 4 楼 zhao4zhong1 的回复:
种子填充算法+颜色差别算法
请参考这个

VB code
Dim stackX(0 To STACK_SIZE) As Integer
Dim stackY(0 To STACK_SIZE) As Integer
Dim stackI As Long
Dim x1, y1, x2, y2 As Integer '临时存放seedfil……
……
这个算法对源图象要求太高,只能分析一些类似验证码之类的、相对纯净的图象,对于从相机捕获的“杂乱”的基本不适用。

你不妨计算一下你头像图片上中间工件上和背景上每个所谓杂乱像素之间的ColorDistance是多少。
--------------------编程问答-------------------- [img=http://static3.photo.sina.com.cn/small/3f91fdd0x73a041fa07d2&690][/img]

看看这个结果是否符合要求 --------------------编程问答--------------------
引用 7 楼 chewinggum 的回复:
看看这个结果是否符合要求


这种效果似乎也还可以,不过现在的要求是测量,要量化关键的边界数据.

我说一下我的方向,首先切数据,怎么切,象切蛋糕一样,从边切到中心,切多少刀,这就要看算法需要了,这样就得到平面数据,然后就来分析这些数据是否能串联起来,比如,第一条在XX位置有比较大的上升或下降,第二条这个位置的附近有吗?有就再看第三条,没有就当这是个噪点放弃,再从第一条的下一个起伏再纵向搜索.

--------------------编程问答-------------------- 图形是否只有一个边缘且边缘是封闭的?如果是,则从图形内任意点开始水平方向查找一个边缘点坐标,然后以此边缘点坐标为起点,对该点八个方向的像素进行判断,如果与边缘点颜色一致且离图形内任意点距离最近,则取该为新的起点,然后重复上一步,在重复时,需要排除已取得的点,循环一周即可。 --------------------编程问答-------------------- 上述算法应该叫做八连通。 --------------------编程问答-------------------- 修改了一下代码,现在是识别成这样。不知道上传到QQ空间里面的图片能否在这里显示

[img=http://group.store.qq.com/http_imgload.cgi?/rurl4_b=70e43016c302ca98093d008c7d741f6dd447f4fb0acbeece20a89afda38760291c42d7108a08d32a0b42252cdfccbbad631650491d0599788d6ca04027600aca8fe73d65017f46b1b8e681a2d3a417cc48e7ceef52cdcb38c8cc7f7fccca86606cac388e49efe73a34f3934e27ba4998a9799687][/img] --------------------编程问答-------------------- 挺好!!!!!!!!
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,