Gray = 0.299R + 0.587G + 0.114B(依人眼敏感度加權)。利用抖動矩陣將誤差分散,使黑白圖像產生灰度視覺錯覺。效果對比:
Bitmap temp = AForge.Imaging.Image.Clone(src, PixelFormat.Format24bppRgb); temp = Grayscale.CommonAlgorithms.RMY.Apply(temp); // 灰度化 pictureBox.Image = new OrderedDithering().Apply(temp); // 抖動二值化
庫支援數十種濾鏡,代碼極簡潔,適合快速原型。
private static Bitmap WeightGrayScaleImple(Bitmap bmp, Funcfoo) { BitmapData data = bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); unsafe { byte* ptr = (byte*)data.Scan0; for (int y=0; y (byte)(0.299*r + 0.587*g + 0.114*b));
注意: 項目屬性 → Build → 勾選 "Allow unsafe code" 才能編譯通過。
AForge.NET 適合快速開發;特殊需求(如硬體僅支援黑白)仍需手寫優化算法。掌握灰度化、閾值、抖動核心原理才能靈活應對。