public static void Gauss(BMPImage image) { float[,] kernel = new float[, ] { { 1f / 16f, 1f / 8f, 1f / 16f }, { 1f / 8f, 1f / 4f, 1f / 8f }, { 1f / 16f, 1f / 8f, 1f / 16f } }; UseMatrix(image, kernel); }
public static void SobelX(BMPImage image) { GrayFilter.Gray(image); float[,] kernel = new float[, ] { { -1f, 0f, 1f }, { -2f, 0f, 2f }, { -1f, 0f, 1 } }; UseMatrix(image, kernel); }
public static void Gray(BMPImage image) { for (int x = 0; x < image.Width; x++) { for (int y = 0; y < image.Height; y++) { Pixel pixel = image.GetPixel(x, y); int sum = pixel.Red + pixel.Green + pixel.Blue; pixel.Red = pixel.Green = pixel.Blue = (byte)(sum / 3); image.SetPixel(pixel, x, y); } } }
public static void Main(string[] args) { Console.WriteLine("This program applies one of the filters to the image."); Console.WriteLine("List of available filters: Gray, Median, Gauss, SobelX, SobelY.\n"); if (args.Length != 3) { Console.WriteLine("Incorrect number of arguments."); return; } BMPImage image = new BMPImage(args[0]); switch (args[1].ToLower()) { case "gray": GrayFilter.Gray(image); break; case "median": MedianFilter.Median(image); break; case "gauss": KernelFilters.Gauss(image); break; case "sobelx": KernelFilters.SobelX(image); break; case "sobely": KernelFilters.SobelY(image); break; default: Console.WriteLine("Unknown filter selected. Available names: Gray, Median, Gauss, SobelX, SobelY."); return; } image.WriteToFile(args[2]); Console.WriteLine("The filter has been appled."); }
public static void UseMatrix(BMPImage image, float[,] kernel) { float red; float green; float blue; Pixel[,] copy = new Pixel[image.Width, image.Height]; for (int x = 1; x < image.Width - 1; x++) { for (int y = 1; y < image.Height - 1; y++) { red = 0; green = 0; blue = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { red += image.GetPixel(x + i - 1, y + j - 1).Red *kernel[i, j]; green += image.GetPixel(x + i - 1, y + j - 1).Green *kernel[i, j]; blue += image.GetPixel(x + i - 1, y + j - 1).Blue *kernel[i, j]; } } red = Math.Abs(red); green = Math.Abs(green); blue = Math.Abs(blue); copy[x, y] = new Pixel((byte)red, (byte)green, (byte)blue); } } for (int x = 1; x < image.Width - 1; x++) { for (int y = 1; y < image.Height - 1; y++) { image.SetPixel(copy[x, y], x, y); } } }
public static void Median(BMPImage image) { byte[] red = new byte[9]; byte[] green = new byte[9]; byte[] blue = new byte[9]; Pixel[,] copy = new Pixel[image.Width, image.Height]; for (int x = 1; x < image.Width - 1; x++) { for (int y = 1; y < image.Height - 1; y++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { red[3 * i + j] = image.GetPixel(x + i - 1, y + j - 1).Red; green[3 * i + j] = image.GetPixel(x + i - 1, y + j - 1).Green; blue[3 * i + j] = image.GetPixel(x + i - 1, y + j - 1).Blue; } } Array.Sort(red); Array.Sort(green); Array.Sort(blue); copy[x, y] = new Pixel(red[4], green[4], blue[4]); } } for (int x = 1; x < image.Width - 1; x++) { for (int y = 1; y < image.Height - 1; y++) { image.SetPixel(copy[x, y], x, y); } } }