C# 图片处理之:彩色图片转为黑白图 .

首先解释下所谓的黑白图片。其实更准确地应该叫256级灰度图。当一个颜色点的R=G=B时,就是我们所谓的“灰色”。由于RGB的取值范围在[0,255],所以一共只有256种可能。

所以彩色图片转为黑白图片的原理非常简单。只要扫描彩图的每一点,让输出图对应点的R=G=B就成了。现在问题的关键就是如何取值了。

一般有两种,一种是彩图RGB三分量的算数平均值,另一种是加权平均值。加权平均是考虑到人类眼睛对不同分量的敏感程度。 

 具体代码如下:



        /**//// <summary>
        
/// 变成黑白图
        
/// </summary>
        
/// <param name="bmp">原始图</param>
        
/// <param name="mode">模式。0:加权平均  1:算数平均</param>
        
/// <returns></returns>

        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;
            }


        }

posted on 2012-01-17 14:00  chennie  阅读(2477)  评论(0编辑  收藏  举报