请问这里的高手,下面这个代码是取得整个图片的象素并进行二值化处理,请问怎么把他改成获取坐标为(3,3)到右下角(8,8)这个矩形范围里的象素值呢,我对VB比较了解,这个代码是VB的,请朋友们帮忙看下该如何改呢
Option Explicit
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 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 Sub Form_Load()
picShow.BorderStyle = vbBSNone
picShow.Move 0, 0
End Sub Private Sub cmdGray_Click()
Dim a
a = Time
Dim PicBits() As Byte, PicInfo As BITMAP, BytesPerPixel As Long
Dim R, g, b, Gray As Byte
Dim 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
Debug.Print DateDiff("n", a, Time)
End SubPrivate Sub Command1_Click()
dlg.ShowOpen
picShow.Picture = LoadPicture(dlg.FileName)
End Sub
Option Explicit
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 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 Sub Form_Load()
picShow.BorderStyle = vbBSNone
picShow.Move 0, 0
End Sub Private Sub cmdGray_Click()
Dim a
a = Time
Dim PicBits() As Byte, PicInfo As BITMAP, BytesPerPixel As Long
Dim R, g, b, Gray As Byte
Dim 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
Debug.Print DateDiff("n", a, Time)
End SubPrivate Sub Command1_Click()
dlg.ShowOpen
picShow.Picture = LoadPicture(dlg.FileName)
End Sub
这个不应该放在这一段代码内处理,
而是应该先对原图像进行处理:
1:窗体上增加一个PICTURE1对象,缩放到6X6大小 (就是从3,3到8,8的大小)
2:使用PAINTPICTURE方法或BITLT将3,3到8,8的图像贴到新增的PICTURE1的0,0处
3:使用上面的代码获得PICTURE1中的图像数值。注意:新增加的PICTURE1的AUTOREDRAW必须为TURE,贴完图之后必须REFRESH,VISIBLE可以为FALSE