c#數(shù)字圖像處理的3種方法示例分享
本文主要通過(guò)彩色圖象灰度化來(lái)介紹C#處理數(shù)字圖像的3種方法,Bitmap類(lèi)、BitmapData類(lèi)和Graphics類(lèi)是C#處理圖像的的3個(gè)重要的類(lèi)。
Bitmap只要用于處理由像素?cái)?shù)據(jù)定義的圖像的對(duì)象,主要方法和屬性如下:
GetPixel方法和SetPixel方法,獲取和設(shè)置一個(gè)圖像的指定像素的顏色。
PixelFormat屬性,返回圖像的像素格式。
Palette屬性,獲取或折紙圖像所使用的顏色調(diào)色板。
Height屬性和Width屬性,返回圖像的高度和寬度。
LockBits方法和UnlockBits方法,分別鎖定和解鎖系統(tǒng)內(nèi)存中的位圖像素。
BitmapData對(duì)象指定了位圖的屬性:
Height屬性,被鎖定位圖的高度。
Width屬性,被鎖定位圖的寬度。
PixelFormat屬性,數(shù)據(jù)的實(shí)際像素格式。
Scan0屬性,被鎖定數(shù)組的首字節(jié)地址。
Stride屬性,步幅,也稱(chēng)掃描寬度。
彩色圖象灰度化
24位彩色圖象每個(gè)像素用3個(gè)字節(jié)表示,每個(gè)字節(jié)對(duì)應(yīng)著R、G、B分量的亮度(紅、綠、藍(lán))。當(dāng)3個(gè)分量不想同時(shí)表現(xiàn)為灰度圖像。下面有三種轉(zhuǎn)換公式:
Gray(I,j)為轉(zhuǎn)換后的灰度圖像在(I,j)點(diǎn)出的灰度值。由于人眼對(duì)顏色的感應(yīng)不同,有了下面的轉(zhuǎn)換公式:
觀察發(fā)現(xiàn)綠色所占比重最大,所以轉(zhuǎn)換時(shí)直接使用G值作為轉(zhuǎn)換結(jié)果:
圖像處理的3種方法分別是:提取像素法、內(nèi)存法和指針?lè)?,它們各自有各自的特點(diǎn)。
提取像素法
使用的是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)存中,這樣程序的運(yùn)行速度就能大大提高了。
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;
}
指針?lè)?/P>
這個(gè)方法和內(nèi)存法相似,開(kāi)始都是通過(guò)LockBits方法來(lái)獲取位圖的首地址,這個(gè)方法更簡(jiǎn)潔,直接用指針進(jìn)行位圖操作。所以對(duì)內(nèi)存的操作需要在unsafe下進(jìn)行操作。
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)文章
C# Environment.CurrentDirectory 靜態(tài)屬性的實(shí)現(xiàn)
本文主要介紹了C# Environment.CurrentDirectory 靜態(tài)屬性的實(shí)現(xiàn),它返回當(dāng)前應(yīng)用程序的工作目錄路徑,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02C#獲取硬盤(pán)序列號(hào)的問(wèn)題小結(jié)
本文給大家分享C#獲取硬盤(pán)序列號(hào)的問(wèn)題及解決方法,非常不錯(cuò),需要的朋友參考下2016-12-12C#使用FluentScheduler實(shí)現(xiàn)觸發(fā)定時(shí)任務(wù)
FluentScheduler是.Net平臺(tái)下的一個(gè)自動(dòng)任務(wù)調(diào)度組件,這篇文章主要為大家詳細(xì)介紹了C#如何使用FluentScheduler實(shí)現(xiàn)觸發(fā)定時(shí)任務(wù),感興趣的小伙伴可以了解下2023-12-12C#實(shí)現(xiàn)Base64處理的加密解密,編碼解碼示例
這篇文章主要介紹了C#實(shí)現(xiàn)Base64處理的加密解密,編碼解碼,結(jié)合實(shí)例形式分析了基于C#實(shí)現(xiàn)的base64編碼解碼操作相關(guān)技巧,需要的朋友可以參考下2017-01-01