我正在研究圖像處理工具(C#,使用EmguCV) 我需要在像素上執行單一操作時獲得最佳性能。BitmapData vs Image <,>。訪問像素值時的數據 - 性能
我已經讀了很多關於LockBit()
的線程,並且將指針中的值複製到字節數組中作爲BEST方法(當性能優先時)來獲取/設置像素值。
我發現了一些實施方案是這樣的: http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp
而且我比起來,更簡單的方法:
Image<Bgr, byte> image = new Image<Bgr, byte>(Bitmap);
byte[,,] data = image.Data;
//And then get r channel like:
byte rChannel = data[y,x,0];
//And set r channel like:
data[y,x,0] = color.R;
所以,我從上面的鏈接使用的實現,但我切出一個創建Color
像Color.FromArgb(r, g, b);
,並假設圖像深度= 24,只是爲了優化性能(我返回一個字節的一維數組作爲顏色)
我的結果(圖片2048×2048 - 與Color.White
比較像素的顏色的4194304操作 - 做3次):
Via `LockBit()` and pointer method:
00:00:00.7971876
00:00:00.7569262
00:00:00.7693977
Via `Image<,>.Data` method:
00:00:00.7957318
00:00:00.8136698
00:00:00.8010817
我沒有看到一個不安全LockBits()
和指針的方法和相當清晰,便於Image<,>.Data
一個之間的任何顯著的性能差異。
有人可以解釋爲什麼LockBits()/Pointer
方法被稱爲超快?也許我錯過了一些東西(正如我所說 - 我已經使用了上面鏈接的實現)。
我在問:「使用這兩種方法應該有什麼明顯的區別嗎?也許我錯過了什麼會有所作爲」但最後2段完全清楚了我:)謝謝 – ilovkatie