GDI+里将一个彩色图像转换成黑白图像的方法
彩色图像转换为黑白图像时需要计算图像中每像素有效的亮度值,通过匹配像素亮度值可以轻松转换为黑白图像。下面分别给出C#和VB里的 程序.
计算像素有效的亮度值可以使用下面的公式:
Y=0.3RED+0.59GREEN+0.11Blue
然后使用 Color.FromArgb(Y,Y,Y) 来把计算后的值转换,转换代码可以使用下面的方法来实现:
[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
[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;
}
当然了这是一个好的方法,如果需要更简单的做到图像的色彩转换还可以使用ColorMatrix类
Tags:彩色图像转换为黑白图像 匹配像素亮度值
最新文章
- VC++实现GPS定位信息的接收及对各定 [08-03]
- GDI+里将一个彩色图像转换成黑白图 [08-03]
- ArcMap连接微软SQL Server数据库三 [12-08]
- 如何把鼠标坐标转换为大地经纬度坐 [12-08]
- Avenue语言下实现经纬度转换大地坐 [11-22]
- 等高线加密基本原理以及算法代码实 [11-22]
- 在VB中如何将Access表中点和线转换 [10-17]
- 用C++实现矩阵基本运算的实例代码 [10-17]
- 地图着色算法原理及C语言实现实例 [10-09]
- 高斯投影正算与反算的理论方法与实 [09-25]
推荐文章


