发个刚写的mean shift跟踪算法
vb.net吧
全部回复
仅看楼主
level 6
93° 楼主
Public Class MeanShift
     Const HISTOGRAM_LENGTH As Integer = 6000
     Private imgWidth As Integer
     Private imgHeight As Integer
     Private trackWinWidth As Integer
     Private trackWinHeight As Integer
     Private currentX As Integer
     Private currentY As Integer
     Private currentHistogram(HISTOGRAM_LENGTH) As Double
     Private tempHistogram(HISTOGRAM_LENGTH) As Double
     ' ---------------------------------------------------
     Public Sub InitMeanShiftTracker(ByVal firstFrame() As Byte, ByVal frameWidth As Integer, ByVal frameHeight As Integer, _
                 ByVal targetPosX As Integer, ByVal targetPosY As Integer, ByVal targetWidth As Integer, ByVal targetHeight As Integer)
         imgWidth = frameWidth
         imgHeight = frameHeight
         currentX = targetPosX
         currentY = targetPosY
         trackWinHeight = targetHeight
         trackWinWidth = targetWidth
     End Sub
     Public Function CalcHistogramSp(ByVal frame() As Byte, ByVal histogram() As Double) As Integer
         Dim pxValue As Integer = 0
         For i As Integer = 0 To HISTOGRAM_LENGTH
             histogram(i) = 0
         Next
         For j As Long = Math.Max(0, currentY - trackWinHeight / 2) To Math.Min(currentY + trackWinHeight / 2, imgHeight - 1)
             For i As Integer = Math.Max(0, currentX - trackWinWidth / 2) To Math.Min(currentX + trackWinWidth / 2, imgWidth - 1)
                 Dim r As Integer = frame(j * imgWidth * 3 + i * 3) / 16
                 Dim g As Integer = frame(j * imgWidth * 3 + i * 3 + 1) / 16
                 Dim b As Integer = frame(j * imgWidth * 3 + i * 3 + 2) / 16
                 histogram(Int(256 * r + 16 * g + b)) += 1
                 pxValue += 1

2010年07月18日 03点07分 1
level 6
93° 楼主

             Next
         Next
         For i As Integer = 0 To HISTOGRAM_LENGTH
             histogram(i) /= pxValue
         Next
         Return pxValue
     End Function
     Public Function MeanShiftProcessSp(ByVal frame() As Byte)
         Dim weights(HISTOGRAM_LENGTH) As Double
         Dim newX As Double = 0.0
         Dim newY As Double = 0.0
         For i As Integer = 0 To HISTOGRAM_LENGTH
             If currentHistogram(i) > 0.0 Then
                 weights(i) = tempHistogram(i) / currentHistogram(i)
             Else
                 weights(i) = 0
             End If
         Next
         Dim sumOfWeights As Double = 0.0
         For j As Long = Math.Max(0, currentY - trackWinHeight / 2) To Math.Min(currentY + trackWinHeight / 2, imgHeight - 1)
             For i As Integer = Math.Max(0, currentX - trackWinWidth / 2) To Math.Min(currentX + trackWinWidth / 2, imgWidth - 1)
                 Dim r As Integer = frame(j * imgWidth * 3 + i * 3) / 16
                 Dim g As Integer = frame(j * imgWidth * 3 + i * 3 + 1) / 16
                 Dim b As Integer = frame(j * imgWidth * 3 + i * 3 + 2) / 16
                 Dim ptr As Integer = 256 * r + 16 * g + b
                 newX += weights(ptr) * i
                 newY += weights(ptr) * j
                 sumOfWeights += weights(ptr)
             Next

2010年07月18日 03点07分 2
level 6
93° 楼主
         Next
         If sumOfWeights <> 0 Then
             currentX = Int(newX / sumOfWeights) + 0.5
             currentY = Int(newY / sumOfWeights) + 0.5
         End If
         Return Nothing
     End Function
     Public Function MeanShiftTrackProcess(ByVal frame() As Byte, ByVal frameNumber As Integer)
         If frameNumber = 0 Then
             Me.CalcHistogramSp(frame, tempHistogram)
         Else
             Dim stopThreshold As Integer = 10
             Dim counter As Integer = 0
             While counter < stopThreshold
                 Me.CalcHistogramSp(frame, currentHistogram)
                 Me.MeanShiftProcessSp(frame)
                 counter += 1
             End While
             Me.DrawTrackBox(frame)
         End If
         Return Nothing
     End Function
     Public Sub DrawTrackBox(ByVal frame() As Byte)
         For i As Integer = currentX To Math.Min(imgWidth, currentX + trackWinWidth)
             frame(currentY * imgWidth * 3 + i * 3 + 0) = 0
             frame(currentY * imgWidth * 3 + i * 3 + 1) = 0
             frame(currentY * imgWidth * 3 + i * 3 + 2) = 255
             frame(Math.Min(imgHeight - 1, currentY + trackWinHeight) * imgWidth * 3 + i * 3 + 0) = 0
             frame(Math.Min(imgHeight - 1, currentY + trackWinHeight) * imgWidth * 3 + i * 3 + 1) = 0
             frame(Math.Min(imgHeight - 1, currentY + trackWinHeight) * imgWidth * 3 + i * 3 + 2) = 255
         Next
         For j As Integer = currentY To Math.Min(imgHeight - 1, currentY + trackWinHeight)
             frame(j * imgWidth * 3 + currentX * 3 + 0) = 0
             frame(j * imgWidth * 3 + currentX * 3 + 1) = 0
             frame(j * imgWidth * 3 + currentX * 3 + 2) = 255
             frame(j * imgWidth * 3 + Math.Min(imgWidth - 1, currentX + trackWinWidth) * 3 + 0) = 0
             frame(j * imgWidth * 3 + Math.Min(imgWidth - 1, currentX + trackWinWidth) * 3 + 1) = 0
             frame(j * imgWidth * 3 + Math.Min(imgWidth - 1, currentX + trackWinWidth) * 3 + 2) = 255
         Next
     End Sub
End Class
2010年07月18日 03点07分 3
level 6
93° 楼主
嘛 玩玩就好
2010年07月18日 03点07分 4
level 10
虽然不知道楼主说的是什么,但是感觉楼主好厉害!!!
2011年05月16日 01点05分 5
level 11
囧三同学的强大程度您是没领略过。。。
2011年05月16日 06点05分 6
level 1
干什么用的 求讲解
2011年05月16日 09点05分 7
level 1
[打酱油]
虽然看不懂,但是直觉这玩意很NB!
话说,模糊感.......
看过老外的把小图片放大为矢量图后的结果.....
2011年10月13日 12点10分 10
level 4
请问这道题怎么做?
编写一个Windows应用程序,程序刚运行时显示出所图所示的【欢迎】对话框;若用户单击【确定】按钮将出现主界面;若用户单击【改变标题】按钮,将把窗体的标题改变为"这里是VB .NET实习基地";若用户单击【随机移动】按钮,将把窗体在屏幕上随机移动10次;若用户单击【退出】按钮将会显示如图的【再见】对话框,用户单击【确定】按钮将退出应用程序。
多谢
2011年10月16日 05点10分 11
level 1
[哈哈]
路过!
2011年11月26日 18点11分 12
level 1
Dim r As Integer = frame(j * imgWidth * 3 + i * 3) / 16
Dim g As Integer = frame(j * imgWidth * 3 + i * 3 + 1) / 16
Dim b As Integer = frame(j * imgWidth * 3 + i * 3 + 2) / 16
请问一下楼主,为什么rgb三个变量是这样存取的呢?不应该是按BGR的顺序读取的吗?
2013年03月26日 05点03分 13
level 1
mark一下[拜]
2013年08月08日 09点08分 15
1