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

2012-07-03 09:09:19  阅读 4030 次 评论 0 条

  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

 

本文地址:https://jinesc.net/?id=109
版权声明:本文为原创文章,版权归 jinesc 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?