//http://www.gutgames.com/post/Matrix-Convolution-Filters-in-C.aspx public static MatrixData ApplyConvolutionFilter(this MatrixData Input, int[,] filter) { MatrixData NewBitmap = new MatrixData(Input.Width(), Input.Height()); MatrixData OldData = Input; double MeanPixel = Input.ModePixel(); int Width = filter.GetLength(1); int Height = filter.GetLength(0); for (int x = 0; x < Input.Width(); ++x) { for (int y = 0; y < Input.Height(); ++y) { double Value = 0; double Weight = 0; int XCurrent = -Width / 2; for (int x2 = 0; x2 < Width; ++x2) { if (XCurrent + x < Input.Width() && XCurrent + x >= 0) { int YCurrent = -Height / 2; for (int y2 = 0; y2 < Height; ++y2) { if (YCurrent + y < Input.Height() && YCurrent + y >= 0) { double Pixel; try { Pixel = Input[YCurrent + y, YCurrent + x]; } catch { Pixel = MeanPixel; } Value += filter[x2, y2] * Pixel; Weight += filter[x2, y2]; } ++YCurrent; } } ++XCurrent; } double PixelVal = Input[y, x]; if (Weight == 0) { Weight = 1; } if (Weight > 0) { Value = System.Math.Abs(Value); Value = (Value / Weight); Value = (Value > 255)? 255 : Value; PixelVal = Value; } NewBitmap[y, x] = PixelVal; } } return(NewBitmap); }