static void DoErosion(ConvolutionWindow window, ImageData toCompute) { List <Color> colorList = new List <Color>(); for (int Yindex = 0; Yindex < toCompute.image.Height; Yindex++) { for (int Xindex = 0; Xindex < toCompute.image.Width; Xindex++) { window.sumElements.Clear(); window.ConvolutionWindowPixelsSum(toCompute.image, Xindex, Yindex); int min = window.sumElements.Min(); colorList.Add(Color.FromArgb(min, min, min)); } } int indexColor = 0; for (int yIndex = 0; yIndex < toCompute.image.Height; yIndex++) { for (int xIndex = 0; xIndex < toCompute.image.Width; xIndex++) { toCompute.image.SetPixel(xIndex, yIndex, colorList[indexColor]); indexColor++; } } }
public static void DoFilters(ImageData toCompute, string ConvolutionWindowDiameter, string convolutionWindowFilter, bool saveResult) { ConvolutionWindow window = CreateConvolutionWindow.DoCreate(ConvolutionWindowDiameter, convolutionWindowFilter); ChooseConvolution(window, toCompute); if (saveResult == true) { toCompute.SaveBitmap(savePath); } }
static void DoMedianFilter(ConvolutionWindow window, ImageData toCompute) { for (int yIndex = 0; yIndex < toCompute.image.Height; yIndex++) { for (int xIndex = 0; xIndex < toCompute.image.Width; xIndex++) { window.pixelsCounter = 0; window.sumElements.Clear(); window.ConvolutionWindowPixelsSum(toCompute.image, xIndex, yIndex); int sum = (window.sumElements.Sum()) / window.pixelsCounter; Color pixelColor = Color.FromArgb(sum, sum, sum); toCompute.image.SetPixel(xIndex, yIndex, pixelColor); } } }
static void DoColoredMedianFilter(ConvolutionWindow window, ImageData toCompute) { for (int yIndex = 0; yIndex < toCompute.image.Height; yIndex++) { for (int xIndex = 0; xIndex < toCompute.image.Width; xIndex++) { window.pixelsCounter = 0; window.sumColoredElements.Clear(); window.ConvolutionWindowPixelsSumForColoredFilters(toCompute.image, xIndex, yIndex); float sum = (window.sumColoredElements.Sum()) / window.pixelsCounter; HSVColor pixelHSVColor = HSVColor.RGBtoHSV(toCompute.image.GetPixel(xIndex, yIndex)); Color pixelColor = HSVColor.HSVtoRGB(new HSVColor(pixelHSVColor.hue, pixelHSVColor.saturation, sum)); toCompute.image.SetPixel(xIndex, yIndex, pixelColor); } } }
public static ConvolutionWindow DoCreate(string diameter) //Create a Median Filter Window with the selected diameter { int diameterTemp = 0; if ((!int.TryParse(diameter, out diameterTemp)) || (diameterTemp % 2 == 0)) { Console.WriteLine("Given diameter is invalid!"); return(null); } ConvolutionWindow window = new ConvolutionWindow(diameterTemp); if (window.totalPixel == 0) { Console.WriteLine("The chosen convolution window does not contain any pixels!"); return(null); } return(window); }
static void ChooseConvolution(ConvolutionWindow window, ImageData toCompute) { if (window.processingFilter == "negative") { DoNegativeFilter(toCompute); savePath = "results/" + toCompute.fileName + "NegativeFilterResult.bmp"; } else if (window.processingFilter == "median") { DoMedianFilter(window, toCompute); savePath = "results/" + toCompute.fileName + "MedianFilterResult.bmp"; } else if (window.processingFilter == "colored") { DoColoredMedianFilter(window, toCompute); savePath = "results/" + toCompute.fileName + "ColoredMedianFilterResult.bmp"; } else if (window.processingFilter == "dilation") { DoDilation(window, toCompute); savePath = "results/" + toCompute.fileName + "DilationResult.bmp"; } else if (window.processingFilter == "erosion") { DoErosion(window, toCompute); savePath = "results/" + toCompute.fileName + "ErosionResult.bmp"; } else if (window.processingFilter == "closing") { DoDilation(window, toCompute); DoErosion(window, toCompute); savePath = "results/" + toCompute.fileName + "ClosingResult.bmp"; } else if (window.processingFilter == "opening") { DoErosion(window, toCompute); DoDilation(window, toCompute); savePath = "results/" + toCompute.fileName + "OpeningResult.bmp"; } }
public static ConvolutionWindow DoCreate(string diameter, string filter) //Create a Window with the selected diameter and filter { int diameterTemp = 0; if ((!int.TryParse(diameter, out diameterTemp)) || (diameterTemp % 2 == 0)) { Console.WriteLine("Given diameter is invalid!"); return(null); } if ((filter != "median") && (filter != "dilation") && (filter != "erosion") && (filter != "opening") && (filter != "closing") && (filter != "colored") && (filter != "negative")) { filter = "median"; } ConvolutionWindow window = new ConvolutionWindow(diameterTemp, filter); if (window.totalPixel == 0) { Console.WriteLine("The chosen convolution window does not contain any pixels!"); return(null); } return(window); }