c#數(shù)字圖像處理的3種方法示例分享
本文主要通過彩色圖象灰度化來介紹C#處理數(shù)字圖像的3種方法,Bitmap類、BitmapData類和Graphics類是C#處理圖像的的3個重要的類。
Bitmap只要用于處理由像素數(shù)據(jù)定義的圖像的對象,主要方法和屬性如下:
GetPixel方法和SetPixel方法,獲取和設(shè)置一個圖像的指定像素的顏色。
PixelFormat屬性,返回圖像的像素格式。
Palette屬性,獲取或折紙圖像所使用的顏色調(diào)色板。
Height屬性和Width屬性,返回圖像的高度和寬度。
LockBits方法和UnlockBits方法,分別鎖定和解鎖系統(tǒng)內(nèi)存中的位圖像素。
BitmapData對象指定了位圖的屬性:
Height屬性,被鎖定位圖的高度。
Width屬性,被鎖定位圖的寬度。
PixelFormat屬性,數(shù)據(jù)的實際像素格式。
Scan0屬性,被鎖定數(shù)組的首字節(jié)地址。
Stride屬性,步幅,也稱掃描寬度。
彩色圖象灰度化
24位彩色圖象每個像素用3個字節(jié)表示,每個字節(jié)對應(yīng)著R、G、B分量的亮度(紅、綠、藍)。當3個分量不想同時表現(xiàn)為灰度圖像。下面有三種轉(zhuǎn)換公式:
Gray(I,j)為轉(zhuǎn)換后的灰度圖像在(I,j)點出的灰度值。由于人眼對顏色的感應(yīng)不同,有了下面的轉(zhuǎn)換公式:
觀察發(fā)現(xiàn)綠色所占比重最大,所以轉(zhuǎn)換時直接使用G值作為轉(zhuǎn)換結(jié)果:
圖像處理的3種方法分別是:提取像素法、內(nèi)存法和指針法,它們各自有各自的特點。
提取像素法
使用的是GDI+中的Bitmap.GetPixel和Bitmap.SetPixel方法。
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
Color pixel;
int ret;
for (int x = 0; x < newbitmap.Width; x++)
{
for (int y = 0; y < newbitmap.Height; y++)
{
pixel = newbitmap.GetPixel(x, y);
ret = (int)(pixel.R * 0.299 + pixel.G * 0.587 + pixel.B * 0.114);
newbitmap.SetPixel(x, y, Color.FromArgb(ret, ret, ret));
}
}
pictureBox1.Image = newbitmap.Clone() as Image;
}
內(nèi)存法
內(nèi)存法是把圖像數(shù)據(jù)直接復(fù)制到內(nèi)存中,這樣程序的運行速度就能大大提高了。
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = newbitmap.Width * newbitmap.Height * 3;
byte[] rgbvalues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes);
double colortemp = 0;
for (int i = 0; i < rgbvalues.Length; i += 3)
{
colortemp = rgbvalues[i + 2] * 0.299 + rgbvalues[i + 1] * 0.587 + rgbvalues[i] * 0.114;
rgbvalues[i] = rgbvalues[i + 1] = rgbvalues[i + 2] = (byte)colortemp;
}
System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes);
newbitmap.UnlockBits(bmpdata);
pictureBox1.Image = newbitmap.Clone() as Image;
}
指針法
這個方法和內(nèi)存法相似,開始都是通過LockBits方法來獲取位圖的首地址,這個方法更簡潔,直接用指針進行位圖操作。所以對內(nèi)存的操作需要在unsafe下進行操作。
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
byte temp;
unsafe
{
byte* ptr = (byte*)(bmpdata.Scan0);
for (int x = 0; x < bmpdata.Width; x++)
{
for (int y = 0; y < bmpdata.Height; y++)
{
temp = (byte)(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]);
ptr[0] = ptr[1] = ptr[2] = temp;
ptr += 3;
}
ptr += bmpdata.Stride - bmpdata.Width * 3;
}
}
newbitmap.UnlockBits(bmpdata);
pictureBox1.Image = newbitmap.Clone() as Image;
}
相關(guān)文章
提權(quán)函數(shù)之RtlAdjustPrivilege()使用說明
RtlAdjustPrivilege() 這玩意是在 NTDLL.DLL 里的一個不為人知的函數(shù),MS沒有公開,原因就是這玩意實在是太NB了,以至于不需要任何其他函數(shù)的幫助,僅憑這一個函數(shù)就可以獲得進程ACL的任意權(quán)限!2011-06-06
C#影院售票系統(tǒng)畢業(yè)設(shè)計(2)
這篇文章主要介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計,介紹了電影院座位的動態(tài)繪制、電影信息綁定到窗體中如何展現(xiàn)出來,需要的朋友可以參考下2015-11-11
C# winform 模擬鍵盤輸入自動接入訪問網(wǎng)絡(luò)的實例
本篇文章主要介紹了C# winform 模擬鍵盤輸入自動接入訪問網(wǎng)絡(luò),有興趣的可以了解一下。2016-11-11

