C#數字圖像處置之圖象灰度化辦法。本站提示廣大學習愛好者:(C#數字圖像處置之圖象灰度化辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#數字圖像處置之圖象灰度化辦法正文
本文實例講述了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#法式設計有所贊助。