进医院的心情感受说说:图像二值化的VB代码

来源:百度文库 编辑:偶看新闻 时间:2024/04/20 20:34:51

输入您的搜索字词 提交搜索表单
Web 80diy.com

本文来源:CSDN社区 - 原文地址:http://topic.csdn.net/t/20041225/14/3675559.html
',1)">
',2)">
谁有图像二值化的代码?
lxcy(始经天月照,终若流星驰!)2004-12-25 14:43:06 在 VB / 基础类 提问
如题 问题点数:0、回复次数:7Top
aohan(aohan)回复于 2004-12-26 00:22:21 得分 0
http://www.kehui.net/html/article/26/26073.htmlTop
bu_wen(不文)回复于 2004-12-26 01:09:21 得分 0
对图象没什么研究。大致是这样的吧。速度有点慢。
Private   Sub   erzhi
Dim   Picwidth     As   Long,   Picheigh   As   Long,   rgb5   As   Long
Dim   hdc   As   Long,   i   As   Long,   j   As   Long
Dim   bBlue   As   Long,   bRed   As   Long,   bGreen   As   Long
Dim   T   As   Long
Picwidth     =   Picture1.ScaleWidth
Picheigh   =   Picture1.ScaleHeight
hdc   =   Picture1.hdc
For   i   =   1   To   Picwidth
For   j   =   1   To   Picheigh
rgb5   =   GetPixel(hdc,   i,   j)
bBlue   =   Blue(rgb5)             '获得兰色值
bRed   =   Red(rgb5)                 '获得红色值
bGreen   =   Green(rgb5)         '获得绿色值
'将三原色转换为灰度
T   =   (9798   *   bRed   +   19235   *   bGreen   +   3735   *   bBlue)   \   32768
If   T   >   127   Then     '127为定义的阀值,阀值具体如何确定,看自己的情况吧,有直方图方法,剃度方法等。。
T   =   0
Else
T   =   255
End   If
'将灰度转换为RGB
rgb5   =   RGB(T,   T,   T)
SetPixelV   hdc,   i,   j,   rgb5
Next   j
Next   i
Set   Picture1.Picture   =   Picture1.Image
End   sub
Private   Function   Red(ByVal   mlColor   As   Long)   As   Long
'从RGB值中获得红色值
Red   =   mlColor   And   &HFF
End   Function
Private   Function   Green(ByVal   mlColor   As   Long)   As   Long
'从RGB值中获得绿色值
Green   =   (mlColor   \   &H100)   And   &HFF
End   Function
Private   Function   Blue(ByVal   mlColor   As   Long)   As   Long
''从RGB值中获得蓝色值
Blue   =   (mlColor   \   &H10000)   And   &HFF
End   Function
Top
WallesCai(沧海明月一度,西风残阳无悔.)回复于 2004-12-26 01:54:53 得分 0
给你个思路:
1:取得像素的颜色值
2:分解出红绿蓝的分量
3:换算成灰度值
4:按照给定的阀值判断改点灰度是否大于它,如果比阀值大,取255,反之取0
5:将该点的新颜色(255或0)输出到图像上
Top
WallesCai(沧海明月一度,西风残阳无悔.)回复于 2004-12-26 01:59:40 得分 0
阀值的选取一般用127,
也可以按照颜色统计来进行。
就是将整幅图片的所有点的灰度累加起来再除以总的点数。
除下来的商作为一个阀值。
缺点是必须对整张图片扫描两次,速度比较慢。
特别是用PIONT方法(GETPIXEL也一样)处理一张小点的图片还能忍受,当处理的图片面积一大就象死机一样了,呵呵Top
laviewpbt(人一定要靠自己)回复于 2004-12-26 11:12:40 得分 0
iDATA为图象数据矩阵,可由   GetDIBits得到
灰度级切片法二值化
Private   Sub   qiepianerzhi(ByRef   pprogress   As   Long)
Dim   x   As   Long,   y   As   Long
Dim   R   As   Long,   G   As   Long,   B   As   Long
Dim   mf   As   Long,   dF   As   Long
Static   lut(255)   As   Integer
R   =   30
G   =   100
For   x   =   0   To   R
lut(x)   =   255
Next   x
For   x   =   R   To   R   +   G   -   1
lut(x)   =   0
Next   x
For   y   =   1   To   PicInfo.bmHeight
For   x   =   1   To   PicInfo.bmWidth
a   =   iDATA(1,   x,   y)
iDATA(1,   x,   y)   =   lut(a):     iDATA(2,   x,   y)   =   lut(a):     iDATA(3,   x,   y)   =   lut(a)
Next   x
DoEvents
Next   y
End   Sub
等灰度片法二值化
Private   Sub   denghuidu(ByRef   pprogress   As   Long)
Dim   x   As   Long,   y   As   Long
Dim   R   As   Integer,   G   As   Integer
Dim   mf   As   Long,   dF   As   Long
Static   lut(255)   As   Integer
R   =   50
G   =   100
look8   R,   G,   lut()
mProgress   =   0
For   y   =   1   To   PicInfo.bmHeight
For   x   =   1   To   PicInfo.bmWidth
a   =   iDATA(1,   x,   y)
iDATA(1,   x,   y)   =   lut(a):     iDATA(2,   x,   y)   =   lut(a):     iDATA(3,   x,   y)   =   lut(a)
Next   x
Next   y
pprogress   =   100
DoEvents
End   Sub
线性二值化
Private   Sub   linear(ByRef   pprogress   As   Long)
Dim   x   As   Long,   y   As   Long
Dim   R   As   Integer,   G   As   Integer
Dim   mf   As   Long,   dF   As   Long
Static   lut(255)   As   Integer
R   =   50
G   =   100
look7   R,   G,   lut()
mProgress   =   0
For   y   =   1   To   PicInfo.bmHeight
For   x   =   1   To   PicInfo.bmWidth
a   =   iDATA(1,   x,   y)
iDATA(1,   x,   y)   =   lut(a):     iDATA(2,   x,   y)   =   lut(a):     iDATA(3,   x,   y)   =   lut(a)
Next   x
Next   y
pprogress   =   100
DoEvents
End   SubTop
zmrok(朱朱)回复于 2005-02-24 13:11:58 得分 0
学习ingTop
jadeluo(秀峰)回复于 2005-02-24 13:19:31 得分 0
一个用API来完成的例子,   速度比GetPixel快n倍
Option   Explicit
'图像处理的一个例子
'Powered   by   Jadeluo   ,   2004/02/21
'EMail:   Jadeluo@sina.com
Private   Type   BITMAP
bmType   As   Long
bmWidth   As   Long
bmHeight   As   Long
bmWidthBytes   As   Long
bmPlanes   As   Integer
bmBitsPixel   As   Integer
bmBits   As   Long
End   Type
Private   Declare   Function   GetObject   Lib   "gdi32"   Alias   "GetObjectA"   (ByVal   hObject   As   Long,   ByVal   nCount   As   Long,   lpObject   As   Any)   As   Long
Private   Declare   Function   GetBitmapBits   Lib   "gdi32"   (ByVal   hBitmap   As   Long,   ByVal   dwCount   As   Long,   lpBits   As   Any)   As   Long
Private   Declare   Function   SetBitmapBits   Lib   "gdi32"   (ByVal   hBitmap   As   Long,   ByVal   dwCount   As   Long,   lpBits   As   Any)   As   Long
Private   Sub   Form_Load()
picShow.BorderStyle   =   vbBSNone
picShow.Move   0,   0
End   Sub
Private   Sub   cmdLoad_Click()
On   Error   Resume   Next
With   CommonDialog1
.Filter   =   "Picture(*.BMP;*.JPG;*.GIF;*.ICO)|*.BMP;*.JPG;*.GIF;*.ICO|All   Files(*.*)|*.*"
.CancelError   =   True
.ShowOpen
If   Err.Number   =   0   Then
picShow.AutoSize   =   True
picShow.Picture   =   LoadPicture(.FileName)
End   If
End   With
On   Error   GoTo   0
End   Sub
Private   Sub   cmdGray_Click()
Dim   PicBits()   As   Byte,   PicInfo   As   BITMAP,   BytesPerPixel   As   Long
Dim   R   As   Byte,   G   As   Byte,   B   As   Byte,   Gray   As   Byte,   i   As   Long
With   picShow
.AutoRedraw   =   True
GetObject   .Image,   Len(PicInfo),   PicInfo
BytesPerPixel   =   PicInfo.bmBitsPixel   \   8
ReDim   PicBits(1   To   PicInfo.bmWidth   *   PicInfo.bmHeight   *   BytesPerPixel)
GetBitmapBits   .Image,   UBound(PicBits),   PicBits(1)
For   i   =   0   To   UBound(PicBits)   \   BytesPerPixel   -   1
B   =   PicBits(i   *   BytesPerPixel   +   1)
G   =   PicBits(i   *   BytesPerPixel   +   2)
R   =   PicBits(i   *   BytesPerPixel   +   3)
Gray   =   R   *   0.39   +   G   *   0.5   +   B   *   0.11
'下面这一句是将灰度值换算成二值
'                         If   Gray   >   127   Then   Gray   =   255   Else   Gray   =   0
PicBits(i   *   BytesPerPixel   +   1)   =   Gray
PicBits(i   *   BytesPerPixel   +   2)   =   Gray
PicBits(i   *   BytesPerPixel   +   3)   =   Gray
Next   i
SetBitmapBits   .Image,   UBound(PicBits),   PicBits(1)
.Refresh
End   With
End   Sub
Top
你可能对以下的问题也比较感兴趣....
图像二值化求treeview背景图像的代码?求图像浏览程序源代码谁能讲讲图像的二值化?图像二值化得相关算法图像赋值???代码简化代码优化============我想找《精通VC图像编程》的随书代码==============
发表者个人观点不代表本站赞成其内容或立场,本站不承担任何法律责
以上内容均转载自:CSDN技术论

站长统计