| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 750 人关注过本帖
标题:【求助】关于VB二值化程序问题
只看楼主 加入收藏
风雨巛
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-4-30
收藏
 问题点数:0 回复次数:0 
【求助】关于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
搜索更多相关主题的帖子: Integer Dim Byte Iteration 
2007-05-11 10:50
快速回复:【求助】关于VB二值化程序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025740 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved