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); } } }
private void FindZero(ImageData toCompute) { int minimumGreyValue = 0; for (int indexGrey = 0; indexGrey < 256; indexGrey++) { if (toCompute.histogram[indexGrey] != 0) { minimumGreyValue = indexGrey; break; } } for (int yIndex = 0; yIndex < toCompute.image.Height; yIndex++) { for (int xIndex = 0; xIndex < toCompute.image.Width; xIndex++) { if (HSVColor.HSVtoGreyLevelRGB(HSVColor.RGBtoHSV(toCompute.image.GetPixel(xIndex, yIndex))).B == minimumGreyValue) { int notAround = 0; for (int yWindow = yIndex - 1; yWindow < yIndex + 2; yWindow++) { for (int xWindow = xIndex - 1; xWindow < xIndex + 2; xWindow++) { if ((yIndex < 0) || (yIndex >= toCompute.image.Height) || (xIndex < 0) || (xIndex >= toCompute.image.Width)) { continue; } else if (this.zonesMap[yWindow][xWindow] != 0) { notAround = this.zonesMap[yWindow][xWindow]; } } } if (notAround == 0) { markNumber++; this.zonesMap[yIndex][xIndex] = markNumber; } else { this.zonesMap[yIndex][xIndex] = markNumber; } } } } }
public void GetHistogramFromColoredImage() //Calculate the histogram from a colored image { CreateHistogram(); List <HSVColor> colorList = new List <HSVColor>(); for (int yIndex = 0; yIndex < this.image.Height; yIndex++) { for (int xIndex = 0; xIndex < this.image.Width; xIndex++) { colorList.Add(HSVColor.RGBtoHSV(this.image.GetPixel(xIndex, yIndex))); } } foreach (HSVColor element in colorList) { int greyLevel = HSVColor.HSVtoGreyLevelRGB(element).B; this.histogram[greyLevel]++; } }
public void ConvolutionWindowPixelsSumForColoredFilters(FasterBitmap toCompute, int centerPositionX, int centerPositionY) { for (int yIndex = centerPositionY - (this.windowDiameter - 1); yIndex < centerPositionY + (this.windowDiameter - 1); yIndex++) { for (int xIndex = centerPositionX - (this.windowDiameter - 1); xIndex < centerPositionX + (this.windowDiameter - 1); xIndex++) { if ((yIndex < 0) || (yIndex >= toCompute.Height) || (xIndex < 0) || (xIndex >= toCompute.Width)) { continue; } else { this.sumColoredElements.Add(HSVColor.RGBtoHSV(toCompute.GetPixel(xIndex, yIndex)).value); this.pixelsCounter++; } } } return; }
public static Color HSVtoRGB(HSVColor color) { if (color.saturation == 0) { int greyLevel = Convert.ToInt32(color.value * 255); Console.WriteLine("Stop"); return(Color.FromArgb(greyLevel, greyLevel, greyLevel)); } else { float usableHue = color.hue / 60f; if (usableHue == 6f) { usableHue = 0f; } float coeff1 = color.value * (1f - color.saturation); float coeff2 = color.value * (1f - color.saturation * (usableHue - (float)((int)usableHue))); float coeff3 = color.value * (1f - color.saturation * (1f - (usableHue - (int)usableHue))); switch ((int)usableHue) { case 0: return(Color.FromArgb((int)(color.value * 255f), (int)(coeff3 * 255f), (int)(coeff1 * 255f))); case 1: return(Color.FromArgb((int)(coeff2 * 255f), (int)(color.value * 255f), (int)(coeff1 * 255f))); case 2: return(Color.FromArgb((int)(coeff1 * 255f), (int)(color.value * 255f), (int)(coeff3 * 255f))); case 3: return(Color.FromArgb((int)(coeff1 * 255f), (int)(coeff2 * 255f), (int)(color.value * 255f))); case 4: return(Color.FromArgb((int)(coeff3 * 255f), (int)(coeff1 * 255f), (int)(color.value * 255f))); default: return(Color.FromArgb((int)(color.value * 255f), (int)(coeff1 * 255f), (int)(coeff2 * 255f))); } } }
public static Color HSVtoGreyLevelRGB(HSVColor color) { int greyLevel = Convert.ToInt32(color.value * 255); return(Color.FromArgb(greyLevel, greyLevel, greyLevel)); }
private TailRecursionResult <int[][]> DoDilation(ImageData toCompute, List <Point> seedsList, int[][] map) { int isNotPossible = 0; foreach (Point point in seedsList) { int sum = 0; int pixelCounter = 0; for (int yIndex = point.y - 1; yIndex < point.y + 2; yIndex++) { for (int xIndex = point.x - 1; xIndex < point.x + 2; xIndex++) { if ((yIndex < 0) || (yIndex >= toCompute.image.Height) || (xIndex < 0) || (xIndex >= toCompute.image.Width)) { continue; } else if (zonesMap[yIndex][xIndex] == 0) { sum = sum + HSVColor.HSVtoGreyLevelRGB(HSVColor.RGBtoHSV(toCompute.image.GetPixel(xIndex, yIndex))).R; pixelCounter++; } } } if (pixelCounter == 0) { isNotPossible++; } else { int mean = (int)(sum / pixelCounter); int greyLevel = HSVColor.HSVtoGreyLevelRGB(HSVColor.RGBtoHSV(toCompute.image.GetPixel(point.x, point.y))).R; if ((mean < greyLevel - 8) || (mean > greyLevel + 8)) { isNotPossible++; } else { for (int yIndex = point.y - 1; yIndex < point.y + 2; yIndex++) { for (int xIndex = point.x - 1; xIndex < point.x + 2; xIndex++) { if ((yIndex < 0) || (yIndex >= toCompute.image.Height) || (xIndex < 0) || (xIndex >= toCompute.image.Width)) { continue; } else { zonesMap[yIndex][xIndex] = point.mark; } } } } } } if (isNotPossible == seedsList.Count) { return(End(zonesMap)); } else { seedsList = RefillSeedsFromZonesMap(seedsList, zonesMap); return(Next(() => DoDilation(toCompute, seedsList, zonesMap))); } }