static void RepeatableTest(int imageSizeStep, int testRepeats, int numberOfTests) { List <string> lines = new List <string>(numberOfTests * 9); for (int i = 1; i <= numberOfTests; i++) { for (int f = 3; f <= 21; f += 2) { StringBuilder builder = new StringBuilder(); builder.Append($"{ i * imageSizeStep };{ f };"); for (int l = 0; l < testRepeats; l++) { Graymap graymap = Generator.HorizontalStripes(i * imageSizeStep, i * imageSizeStep, new int[] { 0, 255 }, imageSizeStep / 5); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Filtering.ApplyFilter(graymap, GraymapLibrary.Filters.Generator.GetGaussianBlur(f)); stopwatch.Stop(); builder.Append($"{ stopwatch.ElapsedMilliseconds };"); Console.WriteLine($"Blur size: { f.ToString("D2") } | Image size: { i * imageSizeStep } | Regular iteration: { stopwatch.ElapsedMilliseconds }ms"); } lines.Add(builder.ToString().Substring(0, builder.ToString().Length - 1)); } } File.WriteAllLines("regular.csv", lines.ToArray()); //======================================================================================================================= lines = new List <string>(numberOfTests * 9); for (int i = 1; i <= numberOfTests; i++) { for (int f = 3; f <= 21; f += 2) { StringBuilder builder = new StringBuilder(); builder.Append($"{ i * imageSizeStep };{ f };"); for (int l = 0; l < testRepeats; l++) { Graymap graymap = Generator.HorizontalStripes(i * imageSizeStep, i * imageSizeStep, new int[] { 0, 255 }, imageSizeStep / 4); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Filtering.ApplyFilterParallel(graymap, GraymapLibrary.Filters.Generator.GetGaussianBlur(f)); stopwatch.Stop(); builder.Append($"{ stopwatch.ElapsedMilliseconds };"); Console.WriteLine($"Blur size: { f.ToString("D2") } | Image size: { i * imageSizeStep } | Parallel iteration: { stopwatch.ElapsedMilliseconds }ms"); } lines.Add(builder.ToString().Substring(0, builder.ToString().Length - 1)); } } File.WriteAllLines("parallel.csv", lines.ToArray()); }
private static int CalculatePixel(int x, int y, Graymap source, Filter filter) { int pixel = 0; int filterX = 0; int filterY = 0; int denominator = 0; for (int j = y - filter.RadiusY; j < y + filter.RadiusY; j++) { for (int i = x - filter.RadiusX; i < x + filter.RadiusX; i++) { if (i >= 0 && i < source.Width && j >= 0 && j < source.Height) { pixel += source.Data[j][i] * filter.Mask[filterY][filterX]; denominator += filter.Mask[filterY][filterX]; } filterX++; } filterY++; filterX = 0; } return(denominator == 0 ? pixel : (pixel / denominator)); }
public static Graymap ApplyFilterParallel(Graymap graymap, Filter filter) { Graymap map = new Graymap(graymap.Width, graymap.Height, graymap.MaxValue); Parallel.For(0, map.Height, y => { for (int x = 0; x < map.Width; x++) { map.Data[y][x] = CalculatePixel(x, y, graymap, filter); } }); return(map); }
public static Graymap ApplyFilter(Graymap graymap, Filter filter) { Graymap map = new Graymap(graymap.Width, graymap.Height, graymap.MaxValue); for (int y = 0; y < map.Height; y++) { for (int x = 0; x < map.Width; x++) { map.Data[y][x] = CalculatePixel(x, y, graymap, filter); } } return(map); }
public static Graymap GetGraymapParallel(System.Drawing.Bitmap bitmap) { Graymap graymap = new Graymap(bitmap.Width, bitmap.Height, 255); Parallel.For(0, graymap.Height, y => { for (int x = 0; x < bitmap.Width; x++) { var color = bitmap.GetPixel(x, y); graymap.Data[y][x] = (int)(0.299f * color.R + 0.587f * color.G + 0.114f * color.B); } }); return(graymap); }
public static Graymap GetGraymap(string file) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(file); Graymap graymap = new Graymap(bitmap.Width, bitmap.Height, 255); for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { var color = bitmap.GetPixel(x, y); graymap.Data[y][x] = (int)(0.299f * color.R + 0.587f * color.G + 0.114f * color.B); } } return(graymap); }