C#数字图象处理之图像灰度化方法
程序员文章站
2023-01-22 12:34:51
本文实例讲述了c#数字图象处理之图像灰度化方法。分享给大家供大家参考。具体如下:
//定义图像灰度化函数
private static bitmap pgray...
本文实例讲述了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#程序设计有所帮助。