幽幽
 
posts - 51,  comments - 28,  trackbacks - 0

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

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

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

Y=0.3RED+0.59GREEN+0.11Blue

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

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

 1public Bitmap ConvertToGrayscale(Bitmap source)
 2
 3{
 4
 5  Bitmap bm = new Bitmap(source.Width,source.Height);
 6
 7  for(int y=0;y<bm.Height;y++)
 8
 9  {
10
11    for(int x=0;x<bm.Width;x++)
12
13    {
14
15      Color c=source.GetPixel(x,y);
16
17      int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
18
19      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));
20
21    }

22
23  }

24
25  return bm;
26
27}

VB
 1Public Function ConvertToGrayscale()Function ConvertToGrayscale(ByVal source As Bitmap) as Bitmap
 2
 3  Dim bm as new Bitmap(source.Width,source.Height)
 4
 5  Dim x
 6
 7  Dim y
 8
 9  For y=0 To bm.Height
10
11    For x=0 To bm.Width
12
13      Dim c as Color = source.GetPixel(x,y)
14
15      Dim luma as Integer = CInt(c.R*0.3 + c.G*0.59 + c.B*0.11)
16
17      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma)
18
19    Next
20
21  Next
22
23  Return bm
24
25End Function
posted on 2008-10-20 13:10 幽幽 阅读(962) 评论(0)  编辑 收藏 引用 所属分类: 杂集

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(5)

随笔分类(35)

随笔档案(51)

文章分类(3)

文章档案(3)

相册

我的链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜