C#對圖像像素處理的三種方式
Posted on 2009-11-02 22:33 feisky 閱讀(3046) 評論(0) 編輯 收藏在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();
}
}
