C#圖像灰度級拉伸的方法
更新時間:2015年04月24日 09:58:40 作者:滄海一粟……
這篇文章主要介紹了C#圖像灰度級拉伸的方法,涉及C#灰度操作的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了C#圖像灰度級拉伸的方法。分享給大家供大家參考。具體如下:
//定義圖像灰度拉伸函數(shù)
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#程序設(shè)計有所幫助。
相關(guān)文章
C#實現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法
這篇文章主要介紹了C#實現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法,結(jié)合實例形式詳細分析了C#讀取Excel表數(shù)據(jù)及導(dǎo)入Sql Server數(shù)據(jù)庫的具體操作步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
Unity3D網(wǎng)格功能生成球體網(wǎng)格模型
這篇文章主要為大家詳細介紹了Unity3D網(wǎng)格功能生成球體網(wǎng)格模型,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù)
這篇文章主要介紹了C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02

