C# 图片处理之:彩色图片转为黑白图[转]
首先解释下所谓的黑白图片。其实更准确地应该叫256级灰度图。当一个颜色点的R=G=B时,就是我们所谓的“灰色”。由于RGB的取值范围在[0,255],所以一共只有256种可能。
所以彩色图片转为黑白图片的原理非常简单。只要扫描彩图的每一点,让输出图对应点的R=G=B就成了。现在问题的关键就是如何取值了。
一般有两种,一种是彩图RGB三分量的算数平均值,另一种是加权平均值。加权平均是考虑到人类眼睛对不同分量的敏感程度。
具体代码如下:
////// 变成黑白图 /// /// 原始图 /// 模式。0:加权平均 1:算数平均 ///private Bitmap ToGray(Bitmap bmp,int mode) { if (bmp == null) { return null; } int w = bmp.Width; int h = bmp.Height; try { byte newColor = 0; BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { byte* p = (byte*)srcData.Scan0.ToPointer(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (mode == 0) // 加权平均 { newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f); } else // 算数平均 { newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f); } p[0] = newColor; p[1] = newColor; p[2] = newColor; p += 3; } p += srcData.Stride - w * 3; } bmp.UnlockBits(srcData); return bmp; } } catch { return null; } }
原文链接:http://www.cnblogs.com/zhaoming/archive/2009/07/08/1519274.html
本文出自 逆时针's Place,转载时请注明出处及相应链接。
本文永久链接: http://www.nishizhen.cn/c-%e5%9b%be%e7%89%87%e5%a4%84%e7%90%86%e4%b9%8b%ef%bc%9a%e5%bd%a9%e8%89%b2%e5%9b%be%e7%89%87%e8%bd%ac%e4%b8%ba%e9%bb%91%e7%99%bd%e5%9b%be%e8%bd%ac/