麻烦各位对此有过研究的仁兄帮忙解答一下,小弟对这一块不甚了解。希望各位提供一些资料,最好是源代码?谢谢!!!!
没人回答???????????
如何用vb将图片二值化?
答案:2 悬赏:50 手机版
解决时间 2021-02-18 11:50
- 提问者网友:且恨且铭记
- 2021-02-17 23:02
最佳答案
- 五星知识达人网友:狂恋
- 2021-02-17 23:42
Option Explicit
Dim R As Byte
Dim G As Byte
Dim B As Byte
Dim aaa As Long
Dim bbb As Byte
Dim i As Integer
Dim j As Integer
Private Sub Command1_Click()
For i = 1 To Picture1.ScaleHeight Step 15
For j = 1 To Picture1.ScaleWidth Step 15
aaa = Picture1.Point(j, i)
R = (aaa Mod 256) '取得红色值
G = (aaa Mod 65536) \ 256 '取得绿色值
B = (aaa Mod 16777216) \ 65536 '取得蓝色值
bbb = R / 3 + G / 3 + B / 3 '取得灰度值
If bbb >= 171 Then '这里bbb可作适当改变以获得最佳2值化效果
Picture2.PSet (j, i), RGB(255, 255, 255)
Else
Picture2.PSet (j, i), RGB(0, 0, 0)
End If
Next j
Next i
End Sub
Dim R As Byte
Dim G As Byte
Dim B As Byte
Dim aaa As Long
Dim bbb As Byte
Dim i As Integer
Dim j As Integer
Private Sub Command1_Click()
For i = 1 To Picture1.ScaleHeight Step 15
For j = 1 To Picture1.ScaleWidth Step 15
aaa = Picture1.Point(j, i)
R = (aaa Mod 256) '取得红色值
G = (aaa Mod 65536) \ 256 '取得绿色值
B = (aaa Mod 16777216) \ 65536 '取得蓝色值
bbb = R / 3 + G / 3 + B / 3 '取得灰度值
If bbb >= 171 Then '这里bbb可作适当改变以获得最佳2值化效果
Picture2.PSet (j, i), RGB(255, 255, 255)
Else
Picture2.PSet (j, i), RGB(0, 0, 0)
End If
Next j
Next i
End Sub
全部回答
- 1楼网友:夜风逐马
- 2021-02-18 00:31
'窗体里面加一个按钮command1和图片picture1,单击按钮就会把picture1里面的图片二值化
'我这里没写加载的代码,你自己补上
private declare function getpixel lib "gdi32" (byval hdc as long, byval x as long, byval y as long) as long
private declare function setpixelv lib "gdi32" (byval hdc as long, byval x as long, byval y as long, byval crcolor as long) as long
function getr(byval hrgb as long) as long
getr = hrgb and &hff '分离出红色
end function
function getg(byval hrgb as long) as long
getg = (hrgb and 65280) \ 256 '分离出绿色
end function
function getb(byval hrgb as long) as long
getb = (hrgb and &hff0000) \ 65536 '分离出蓝色 ...展开'窗体里面加一个按钮command1和图片picture1,单击按钮就会把picture1里面的图片二值化
'我这里没写加载的代码,你自己补上
private declare function getpixel lib "gdi32" (byval hdc as long, byval x as long, byval y as long) as long
private declare function setpixelv lib "gdi32" (byval hdc as long, byval x as long, byval y as long, byval crcolor as long) as long
function getr(byval hrgb as long) as long
getr = hrgb and &hff '分离出红色
end function
function getg(byval hrgb as long) as long
getg = (hrgb and 65280) \ 256 '分离出绿色
end function
function getb(byval hrgb as long) as long
getb = (hrgb and &hff0000) \ 65536 '分离出蓝色
end function
private sub command1_click()
dim hhdc as long, i as long, j as long, t as long, hhd as long
const fz = 112 '二值化阀值,不同阀值效果不同
fz = text1.text
dim hhdc as long, i as long, j as long, t as long, hhd as long
hhdc = picture1.hdc
picture1.scalemode = vbpixels
for i = 0 to picture1.scalewidth
for j = 0 to picture1.scaleheight
t = getpixel(hhdc, i, j)
hhd = int((getr(t) + getg(t) + getb(y)) / 3)
if hhd > fz then
setpixelv hhdc, i, j, vbwhite
else
setpixelv hhdc, i, j, vbblack
end if
next
next
msgbox "转换完毕!"
end sub收起
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯