在C#中,可以採用直接擷取像素法(GetPixel)、記憶體拷貝法和指標法(unsafe)來擷取映像像素並進行處理。
下面以映像的灰階化為例說明具體的處理方法和速度的比較(1G記憶體,P4處理器測試)。
1.GetPixel方法
GetPixel(i,j)和SetPixel(i, j,Color)可以直接得到映像的一個像素的Color結構,但是處理速度比較慢,處理一副180*180的映像大約需要100.48ms。
private void pixel_Click(object sender, EventArgs e){ if(curBitmap != null) { myTimer.ClearTimer(); myTimer.Start(); Color curColor; int ret; for (int i = 0; i < curBitmap.Width; i++) { for (int j = 0; j < curBitmap.Height ; j++) { curColor = curBitmap.GetPixel(i,j); ret = (int)(curColor.R * 0.299 + curColor.G * 0.587 + curColor.B * 0.114); curBitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret)); } } myTimer.Stop(); timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; Invalidate(); }}
2.記憶體拷貝法
記憶體拷貝法就是採用System.Runtime.InteropServices.Marshal.Copy將映像資料拷貝到數組中,然後進行處理,這不需要直接對指標進行操作,不需採用unsafe,處理速度和指標處理相差不大,
處理一副180*180的映像大約需要1.32ms。
private void memory_Click(object sender, EventArgs e){ if (curBitmap != null) { myTimer.ClearTimer(); myTimer.Start(); Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = curBitmap.Width * curBitmap.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); curBitmap.UnlockBits(bmpData); myTimer.Stop(); timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; Invalidate(); }}
3.指標法
指標在c#中屬於unsafe操作,需要用unsafe括起來進行處理,速度最快,處理一副180*180的映像大約需要1.16ms。
採用byte* ptr = (byte*)(bmpData.Scan0); 擷取映像資料根位置的指標,然後用bmpData.Scan0擷取映像的掃描寬度,就可以進行指標操作了。
private void pointer_Click(object sender, EventArgs e){ if (curBitmap != null) { myTimer.ClearTimer(); myTimer.Start(); Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat); byte temp = 0; unsafe { byte* ptr = (byte*)(bmpData.Scan0); for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { 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; } } curBitmap.UnlockBits(bmpData); myTimer.Stop(); timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; Invalidate(); }}