首页 > 编程 > C# > 正文

C#数字图象处理之图像灰度化方法

2020-01-24 01:56:50
字体:
来源:转载
供稿:网友

本文实例讲述了C#数字图象处理之图像灰度化方法。分享给大家供大家参考。具体如下:

//定义图像灰度化函数private static Bitmap PGray(Bitmap src){  int w = src.Width;  int h = src.Height;//构建与原图像大小一样的模版图像  Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);//将原图像存入内存  System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);  System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);  unsafe  { byte* pIn = (byte*)srcData.Scan0.ToPointer(); byte* pOut = (byte*)dstData.Scan0.ToPointer(); byte* p; int stride = srcData.Stride; int r, g, b; for (int y = 0; y < h; y++) {   for (int x = 0; x < w; x++)   { p = pIn; r = p[2]; g = p[1]; b = p[0];//调用图像灰度化公式 pOut[0] = pOut[1] = pOut[2] = (byte)(b * 0.114 + g * 0.587 + r * 0.299);   pIn += 3; pOut += 3;   }   pIn += srcData.Stride - w * 3;   pOut += srcData.Stride - w * 3; } src.UnlockBits(srcData); dstBitmap.UnlockBits(dstData); return dstBitmap;  }}

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表