【求助】关于VB二值化程序问题
下面是一个VB.NET程序,怎样把它改成VB程序。这是一个图像二值化程序。Public Shared Function BestThreshold(ByVal Bmp As Bitmap, ByRef TimeElapse As Integer) As Byte
TimeElapse = Environment.TickCount
Dim i, j, k, Stride, Temp As Integer
Dim Threshold ,NewThreshold,MaxGrayValue ,MinGrayValue,MeanGrayValue1,MeanGrayValue2 As Byte
Dim IP1 As Integer, IP2 As Integer, IS1 As Integer, IS2 As Integer
Dim Iteration As Integer, Histgram(255) As Integer
MaxGrayValue = 0 : MinGrayValue = 255
Stride = (((Bmp.Width * 24) + 31) \ 32) * 4
Dim BmpData() As Byte
ReadBitmap(Bmp, BmpData)
Dim Number As Integer = BmpData.Length - 1
'求出图像中的最小和最大灰度值,并计算阈值初值为
For i = 0 To Bmp.Height - 1
For j = 0 To Bmp.Width - 1
Temp = i * Stride + j * 3
Histgram(BmpData(Temp)) += 1 '统计图像的直方图
If MinGrayValue > BmpData(Temp) Then MinGrayValue = BmpData(Temp)
If MaxGrayValue < BmpData(Temp) Then MaxGrayValue = BmpData(Temp)
Next
Next
NewThreshold = (MinGrayValue + MaxGrayValue) / 2
While Threshold <> NewThreshold And Iteration < 100
Threshold = NewThreshold
'根据阈值将图像分割成目标和背景两部分,求出两部分的平均灰度值
For i = MinGrayValue To Threshold
IP1 += Histgram(i) * i
IS1 += Histgram(i)
Next
MeanGrayValue1 = CByte(IP1 / IS1)
For i = Threshold + 1 To MaxGrayValue
IP2 += Histgram(i) * i
IS2 += Histgram(i)
Next
MeanGrayValue2 = CByte(IP2 / IS2)
'求出新的阈值:
NewThreshold = (MinGrayValue + MaxGrayValue) / 2
Iteration += 1
End While
For i = 0 To Bmp.Height - 1
For j = 0 To Bmp.Width - 1
Temp = i * Stride + j * 3 '二值显示
If BmpData(Temp) <= Threshold Then
BmpData(Temp) = 0 : BmpData(Temp + 1) = 0 : BmpData(Temp + 2) = 0
Else
BmpData(Temp) = 255 : BmpData(Temp + 1) = 255 : BmpData(Temp + 2) = 255
End If
Next
Next
WriteBitmap(Bmp, BmpData)
TimeElapse = Environment.TickCount - TimeElapse
Return True
End Function