当前位置:   article > 正文

VB6二值化图像的方法_vb6 as image

vb6 as image

方法:用windows API

下面是快速灰度化源代码,网上找的,亲测,管用!

 

  1. Option Explicit
  2. Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  3. Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  4. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long
  5. Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long
  6. Private Type BitMapInfoHeader ''文件信息头——BITMAPINFOHEADER
  7. biSize As Long
  8. biWidth As Long
  9. biHeight As Long
  10. biPlanes As Integer
  11. biBitCount As Integer
  12. biCompression As Long
  13. biSizeImage As Long
  14. biXPelsPerMeter As Long
  15. biYPelsPerMeter As Long
  16. biClrUsed As Long
  17. biClrImportant As Long
  18. End Type
  19. Private Type RGBQuad
  20. rgbBlue As Byte
  21. rgbGreen As Byte
  22. rgbRed As Byte
  23. ''rgbReserved As Byte
  24. End Type
  25. Private Type BitMapInfo
  26. bmiHeader As BitMapInfoHeader
  27. bmiColors As RGBQuad
  28. End Type
  29. Private Sub Command1_Click()
  30. Dim ix As Integer
  31. Dim iy As Integer
  32. Dim iWidth As Integer '以像素为单位的图形宽度
  33. Dim iHeight As Integer '以像素为单位的图形高度
  34. Dim bytGray As Byte
  35. Dim bytThreshold As Byte
  36. Dim bits() As Byte '三维数组,用于获取原彩色图像中各像素的RGB数值以及存放转化后的灰度值
  37. Dim bitsBW() As Byte '三维数组,用于存放转化为黑白图后各像素的值
  38. '获取图形的宽度和高度
  39. iWidth = Picture1.ScaleWidth / Screen.TwipsPerPixelX
  40. iHeight = Picture1.ScaleHeight / Screen.TwipsPerPixelY
  41. Picture1.Picture = Picture1.Image
  42. '创建并初始化一个bitMapInfo自定义类型
  43. Dim bi24BitInfo As BitMapInfo
  44. With bi24BitInfo.bmiHeader
  45. .biBitCount = 32
  46. .biCompression = 0&
  47. .biPlanes = 1
  48. .biSize = Len(bi24BitInfo.bmiHeader)
  49. .biWidth = iWidth
  50. .biHeight = Picture1.ScaleHeight / Screen.TwipsPerPixelY
  51. End With
  52. '重新定义数组大小
  53. ReDim bits(3, 0 To iWidth, 0 To iHeight) As Byte
  54. ReDim bitsBW(3, 0 To iWidth, 0 To iHeight) As Byte
  55. '使用GetDIBits方法一次性获取picture1中各点的rgb值,比point方法或getPixel函数逐像素获取像素rgb要快出一个数量级
  56. Dim lrtn As Long
  57. lrtn = GetDIBits(Picture1.hdc, Picture1.Picture.Handle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&)
  58. '数组的三个维度分别代表像素的RGB分量、以图形左下角为原点的X和Y坐标。
  59. '具体说来,这时bits(0,2,3)代表从图形左下角数起横向第2个纵向第3个像素的Blue值,而bits(1,2,3)和bits(2,2,3)分别的Green值和Red值.
  60. bytThreshold = 128 '这里定义转换为黑白图像时的阈值为128,即灰色亮度大于128的像素转为白色,小于128的像素转为黑的,此值可根据需要修改为0-255之前任意数值
  61. For ix = 0 To iWidth
  62. For iy = 0 To iHeight
  63. '***********RGB转为灰度的算法有多种,这里给出常见的两种*******
  64. 'bytGray = bits(0, ix, iy) * 0.11 + bits(1, ix, iy) * 0.59 + bits(2, ix, iy) * 0.3 '这是传统的根据三原色亮度加权得到灰阶的算法
  65. bytGray = (bits(0, ix, iy) ^ 2.2 * 0.0722 + bits(1, ix, iy) ^ 2.2 * 0.7152 + bits(2, ix, iy) ^ 2.2 * 0.2126) ^ (1 / 2.2) '这是简化 sRGB IEC61966-2.1 [gamma=2.20],有点类似于photoshop中所用的算法
  66. bits(0, ix, iy) = bytGray
  67. bits(1, ix, iy) = bytGray
  68. bits(2, ix, iy) = bytGray
  69. '*********转为黑白图像********
  70. If bits(0, ix, iy) < bytThreshold Then
  71. bitsBW(0, ix, iy) = 0
  72. bitsBW(1, ix, iy) = 0
  73. bitsBW(2, ix, iy) = 0
  74. Else
  75. bitsBW(0, ix, iy) = 255
  76. bitsBW(1, ix, iy) = 255
  77. bitsBW(2, ix, iy) = 255
  78. End If
  79. Next
  80. Next
  81. '将黑白图显示到picture3中
  82. Picture2.Picture = Picture2.Image '如果picture2的picture属性为空,需要在setDIBits之前将其picture属性设置一下,否则无法显示出图形
  83. SetDIBits Picture2.hdc, Picture2.Picture.Handle, 0&, iHeight, bitsBW(0, 0, 0), bi24BitInfo, 0&
  84. Picture2.Picture = Picture2.Image
  85. End Sub
  86. Private Sub Form_Load()
  87. Picture1.Picture = LoadPicture(App.Path & "\66668.JPG")
  88. Picture2.Width = Picture1.Width
  89. Picture2.Height = Picture1.Height
  90. End Sub

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/125188
推荐阅读
相关标签
  

闽ICP备14008679号