public void Transform(Bitmap bitmap) { var matrix = new double[, ] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } }; var w = matrix.GetLength(0); var h = matrix.GetLength(1); using (var wr = new ImageWrapper(bitmap) { DefaultColor = Color.Silver }) foreach (var p in wr) { double r = 0d, g = 0d, b = 0d; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { var pixel = wr[p.X + i - 1, p.Y + j - 1]; r += matrix[j, i] * pixel.R; g += matrix[j, i] * pixel.G; b += matrix[j, i] * pixel.B; } } wr.SetPixel(p, r, g, b); } }
//.... private void Convolution(Bitmap img, double[,] matrix) { var w = matrix.GetLength(0); var h = matrix.GetLength(1); using (var wr = new ImageWrapper(img) { DefaultColor = Color.Silver }) foreach (var p in wr) { double r = 0d, g = 0d, b = 0d; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { var pixel = wr[p.X + i - 1, p.Y + j - 1]; r += matrix[j, i] * pixel.R; g += matrix[j, i] * pixel.G; b += matrix[j, i] * pixel.B; } } wr.SetPixel(p, r, g, b); } }
Bitmap AddGaussianNoise(Bitmap bmp, float noisePower = 20) { Bitmap res = (Bitmap)bmp.Clone(); Random rnd = new Random(); using (ImageWrapper wrapper = new ImageWrapper(res)) { foreach (var pixel in wrapper) { Color curColor = wrapper[pixel]; double noise = (rnd.NextDouble() + rnd.NextDouble() + rnd.NextDouble() + rnd.NextDouble() - 2) * noisePower; wrapper.SetPixel(pixel, curColor.R + noise, curColor.G + noise, curColor.B + noise); } } return(res); }