12
2012
09

用vb编程将彩色图像转换为黑白

  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 Command1_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 Picture1
                  .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
                          'Gray = CInt(Gray / 2) * 2
                          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

 

Private Sub 比较图片(PP1 As PictureBox, PP2 As PictureBox)
        Dim PicBits1()     As Byte, PicInfo1       As BITMAP, BytesPerPixel1       As Long
        Dim PicBits2()     As Byte, PicInfo2       As BITMAP, BytesPerPixel2       As Long
        Dim PicBits3()     As Byte
          Dim R     As Byte, G       As Byte, B       As Byte, Gray       As Byte, Gray1      As Byte, Gray2      As Byte, i       As Long
          Dim z As Long
          With PP1
                  .AutoRedraw = True
                  GetObject .Image, Len(PicInfo1), PicInfo1
                  BytesPerPixel1 = PicInfo1.bmBitsPixel \ 8
                  ReDim PicBits1(1 To PicInfo1.bmWidth * PicInfo1.bmHeight * BytesPerPixel1)
                  GetBitmapBits .Image, UBound(PicBits1), PicBits1(1)
          End With
          With PP2
                  .AutoRedraw = True
                  GetObject .Image, Len(PicInfo2), PicInfo2
                  BytesPerPixel2 = PicInfo2.bmBitsPixel \ 8
                  ReDim PicBits2(1 To PicInfo1.bmWidth * PicInfo2.bmHeight * BytesPerPixel2)
                  ReDim PicBits3(1 To PicInfo1.bmWidth * PicInfo1.bmHeight * BytesPerPixel1)
                  GetBitmapBits .Image, UBound(PicBits2), PicBits2(1)
          End With
          Picture3.Height = Picture1.Height
          Picture3.Width = Picture1.Width
          Picture3.Cls
                    z = 0
                  For i = 0 To UBound(PicBits1) \ BytesPerPixel1 - 1
                          B = PicBits1(i * BytesPerPixel1 + 1)
                          G = PicBits1(i * BytesPerPixel1 + 2)
                          R = PicBits1(i * BytesPerPixel1 + 3)
                          Gray1 = R * 0.39 + G * 0.5 + B * 0.11
  '下面这一句是将灰度值换算成二值
                          If Gray1 > 127 Then Gray1 = 255 Else Gray1 = 0
                         
                         
                          B = PicBits2(i * BytesPerPixel2 + 1)
                          G = PicBits2(i * BytesPerPixel2 + 2)
                          R = PicBits2(i * BytesPerPixel2 + 3)
                          Gray1 = R * 0.39 + G * 0.5 + B * 0.11
  '下面这一句是将灰度值换算成二值
                          If Gray2 > 127 Then Gray2 = 255 Else Gray2 = 0
                          If Gray2 <> Gray1 Then
                            Gray = 255
                           
                          Else
                            Gray = 0
                            z = z + 1
                          End If
                         
                          PicBits3(i * BytesPerPixel1 + 1) = Gray
                          PicBits3(i * BytesPerPixel1 + 2) = Gray
                          PicBits3(i * BytesPerPixel1 + 3) = Gray
                  Next i
                  SetBitmapBits Picture3.Image, UBound(PicBits3), PicBits3(1)
                  Picture3.Refresh
                  Text2 = Format(z / i, "0.000")

End Sub

 

 收钱码.jpg

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。