C#圖象灰度級拉伸的辦法。本站提示廣大學習愛好者:(C#圖象灰度級拉伸的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#圖象灰度級拉伸的辦法正文
本文實例講述了C#圖象灰度級拉伸的辦法。分享給年夜家供年夜家參考。詳細以下:
//界說圖象灰度拉伸函數
private static Bitmap GrayLP (Bitmap a)
{
Rectangle rect = new Rectangle(0, 0, a.Width, a.Height);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, a.PixelFormat);
IntPtr ptr = srcData.Scan0;
int bytes = 0;
if (a.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
{ bytes = a.Width * a.Height; }
else { bytes = a.Width * a.Height * 3; }
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte n = 255, m = 0;
double p;
//盤算最年夜和最小灰度級
for (int i = 0; i < bytes; i++)
{
//盤算最小灰度級
if (n > grayValues[i])
{
n = grayValues[i];
}
//盤算最年夜灰度級
if (m < grayValues[i])
{
m = grayValues[i];
}
}
//獲得斜率
p = 255.0 / (m - n);
//灰度拉伸
for (int i = 0; i < bytes; i++)
{
grayValues[i] = (byte)(p * (grayValues[i] - n) + 0.5);
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
a.UnlockBits(srcData);
return a;
}
願望本文所述對年夜家的C#法式設計有所贊助。