彩色图像转换为黑白图像时需要计算图像中每像素有效的亮度值,通过匹配像素
亮度值可以轻松转换为黑白图像。
计算像素有效的亮度值可以使用下面的公式:
Y=0.3RED+0.59GREEN+0.11Blue
然后使用 Color.FromArgb(Y,Y,Y) 来把计算后的值转换
转换代码可以使用下面的方法来实现:
C#
1
public 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
1
Public Function ConvertToGrayscale()Function ConvertToGrayscale()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
25
End Function
posted on 2008-10-20 13:10
幽幽 阅读(995)
评论(0) 编辑 收藏 引用 所属分类:
杂集