• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

彩色BMP档转成灰度

发布: 2007-6-21 21:57 | 作者:   | 来源:   | 查看: 96次 | 进入软件测试论坛讨论

领测软件测试网

    彩色BMP档转成灰度

作者: cww 

将RGB想成3D之X,Y,Z轴,则BMP的RGB为(r,g,b)与座标(Y,Y,Y)距离最小时的Y即为灰度值
Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
    整数化

Y = ( 9798*R + 19235*G +  3735*B) / 32768
RGB(Y, Y, Y)就可以了

需一个内有彩色图的PictureBox, CommandBox
Option Explicit
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
Private tmpPic As Picture

Private Sub Form_Load()
Picture1.ScaleMode = 3  注释:设为Pixel
Picture1.AutoRedraw = True  注释:设定所有Pixel的改变不立即在pictureBox上显示
Set tmpPic = Picture1.Picture
End Sub

Private Sub Command1_click()
Dim width5  As Long, heigh5 As Long, rgb5 As Long
Dim hdc5 As Long, i As Long, j As Long
Dim bBlue As Long, bRed As Long, bGreen As Long
Dim Y As Long
width5 = Picture1.ScaleWidth
heigh5 = Picture1.ScaleHeight
hdc5 = Picture1.hdc
For i = 1 To width5
   For j = 1 To heigh5
       rgb5 = GetPixel(hdc5, i, j)
       bBlue = Blue(rgb5)
       bRed = Red(rgb5)
       bGreen = Green(rgb5)
       Y = (9798 * bRed + 19235 * bGreen + 3735 * bBlue) 32768
       rgb5 = RGB(Y, Y, Y)
       SetPixelV hdc5, i, j, rgb5
   Next j
Next i
Set Picture1.Picture = Picture1.Image  注释:此时才真正显示Picture
End Sub

Private Function Red(ByVal mlColor As Long) As Long
Red = mlColor And &HFF
End Function
Private Function Green(ByVal mlColor As Long) As Long
Green = (mlColor &H100) And &HFF
End Function
Private Function Blue(ByVal mlColor As Long) As Long
Blue = (mlColor &H10000) And &HFF
End Function

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网