我正在嘗試編寫一些代碼,可以方便地處理視頻幀。我正在接收幀爲System.Windows.Media.Imaging.WriteableBitmap
。出於測試目的,我只是應用一個簡單的閾值過濾器來處理BGRA格式圖像,並根據BGR像素的平均值將每個像素分配爲黑色或白色。爲什麼我的不安全代碼塊比我的安全代碼慢?
這裏是我的「安全」的版本:
public static void ApplyFilter(WriteableBitmap Bitmap, byte Threshold)
{
// Let's just make this work for this format
if (Bitmap.Format != PixelFormats.Bgr24
&& Bitmap.Format != PixelFormats.Bgr32)
{
return;
}
// Calculate the number of bytes per pixel (should be 4 for this format).
var bytesPerPixel = (Bitmap.Format.BitsPerPixel + 7)/8;
// Stride is bytes per pixel times the number of pixels.
// Stride is the byte width of a single rectangle row.
var stride = Bitmap.PixelWidth * bytesPerPixel;
// Create a byte array for a the entire size of bitmap.
var arraySize = stride * Bitmap.PixelHeight;
var pixelArray = new byte[arraySize];
// Copy all pixels into the array
Bitmap.CopyPixels(pixelArray, stride, 0);
// Loop through array and change pixels to black/white based on threshold
for (int i = 0; i < pixelArray.Length; i += bytesPerPixel)
{
// i=B, i+1=G, i+2=R, i+3=A
var brightness =
(byte)((pixelArray[i] + pixelArray[i+1] + pixelArray[i+2])/3);
var toColor = byte.MinValue; // Black
if (brightness >= Threshold)
{
toColor = byte.MaxValue; // White
}
pixelArray[i] = toColor;
pixelArray[i + 1] = toColor;
pixelArray[i + 2] = toColor;
}
Bitmap.WritePixels(
new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight),
pixelArray, stride, 0
);
}
這是我認爲是使用不安全的代碼塊和WriteableBitmap的後臺緩衝區,而不是forebuffer直接翻譯:
public static void ApplyFilterUnsafe(WriteableBitmap Bitmap, byte Threshold)
{
// Let's just make this work for this format
if (Bitmap.Format != PixelFormats.Bgr24
&& Bitmap.Format != PixelFormats.Bgr32)
{
return;
}
var bytesPerPixel = (Bitmap.Format.BitsPerPixel + 7)/8;
Bitmap.Lock();
unsafe
{
// Get a pointer to the back buffer.
byte* pBackBuffer = (byte*)Bitmap.BackBuffer;
for (int i = 0;
i < Bitmap.BackBufferStride*Bitmap.PixelHeight;
i+= bytesPerPixel)
{
var pCopy = pBackBuffer;
var brightness = (byte)((*pBackBuffer
+ *++pBackBuffer
+ *++pBackBuffer)/3);
pBackBuffer++;
var toColor =
brightness >= Threshold ? byte.MaxValue : byte.MinValue;
*pCopy = toColor;
*++pCopy = toColor;
*++pCopy = toColor;
}
}
// Bitmap.AddDirtyRect(
// new Int32Rect(0,0, Bitmap.PixelWidth, Bitmap.PixelHeight));
Bitmap.Unlock();
}
這是我第一次涉及不安全的代碼塊和指針,所以也許邏輯不是最優的。
我已經在使用相同的測試WriteableBitmaps代碼兩個塊:
var threshold = Convert.ToByte(op.Result);
var copy2 = copyFrame.Clone();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
BinaryFilter.ApplyFilterUnsafe(copyFrame, threshold);
stopWatch.Stop();
var unsafesecs = stopWatch.ElapsedMilliseconds;
stopWatch.Reset();
stopWatch.Start();
BinaryFilter.ApplyFilter(copy2, threshold);
stopWatch.Stop();
Debug.WriteLine(string.Format("Unsafe: {1}, Safe: {0}",
stopWatch.ElapsedMilliseconds, unsafesecs));
所以我分析同一圖像。視頻幀的輸入流的試運行:
Unsafe: 110, Safe: 53
Unsafe: 136, Safe: 42
Unsafe: 106, Safe: 36
Unsafe: 95, Safe: 43
Unsafe: 98, Safe: 41
Unsafe: 88, Safe: 36
Unsafe: 129, Safe: 65
Unsafe: 100, Safe: 47
Unsafe: 112, Safe: 50
Unsafe: 91, Safe: 33
Unsafe: 118, Safe: 42
Unsafe: 103, Safe: 80
Unsafe: 104, Safe: 34
Unsafe: 101, Safe: 36
Unsafe: 154, Safe: 83
Unsafe: 134, Safe: 46
Unsafe: 113, Safe: 76
Unsafe: 117, Safe: 57
Unsafe: 90, Safe: 41
Unsafe: 156, Safe: 35
爲什麼我的不安全版本總是會慢?是否由於使用後臺緩衝區?或者我做錯了什麼?
感謝
你是否在Release版本中運行這些測試? – 2010-05-03 19:21:43
現在不在調試器中。 – 2010-05-03 19:23:35
我建議在發佈版本中運行性能測試以獲得有意義的結果。如果你讓優化器與代碼一起使用,你可能會發現性能差距不足以證明使用'不安全'版本的理由。 – 2010-05-03 20:12:04