您当前的位置: 首页 > 

如何将一个彩色图像转换成黑白图像

发布时间:2004-10-26 18:51:00 ,浏览量:0

作者:未知 彩色图像转换为黑白图像时需要计算图像中每像素有效的亮度值,通过匹配像素

亮度值可以轻松转换为黑白图像。

计算像素有效的亮度值可以使用下面的公式:

Y=0.3RED+0.59GREEN+0.11Blue

然后使用 Color.FromArgb(Y,Y,Y) 来把计算后的值转换

转换代码可以使用下面的方法来实现:

[C#] public  Bitmap ConvertToGrayscale(Bitmap source) {   Bitmap bm = new Bitmap(source.Width,source.Height);   for(int y=0;y<bm.Height;y++)   {     for(int x=0;x<bm.Width;x++)     {       Color c=source.GetPixel(x,y);       int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);       bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));     }   }   return bm; }

[VB] Public   Function ConvertToGrayscale() Function ConvertToGrayscale(ByVal source As Bitmap) as Bitmap   Dim bm as new Bitmap(source.Width,source.Height)   Dim x   Dim y   For y=0 To bm.Height     For x=0 To bm.Width       Dim c as Color = source.GetPixel(x,y)       Dim luma as Integer = CInt(c.R*0.3 + c.G*0.59 + c.B*0.11)       bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma)     Next   Next   Return bm End Function

当然了这是一个好的方法,如果需要更简单的做到图像的色彩转换还可以使用ColorMatrix类

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1363s