private static Point CheckPoint(ImageData toCompute, int element) { List <Point> possiblePoints = new List <Point>(); Random random = new Random(); for (int yIndex = 0; yIndex < toCompute.image.Height; yIndex++) { for (int xIndex = 0; xIndex < toCompute.image.Width; xIndex++) { Color pixelColor = toCompute.image.GetPixel(xIndex, yIndex); if (element == HSVColor.HSVtoGreyLevelRGB(HSVColor.RGBtoHSV(pixelColor)).B) { possiblePoints.Add(new Point(xIndex, yIndex)); } } } if (possiblePoints.Count == 0) { return(null); } else { return(possiblePoints[random.Next(possiblePoints.Count)]); } }
public void DoWatershed(ImageData toCompute, int isOkAltitude) { this.zonesMap = Watershed.AllocZoneMap(toCompute.image.Width, toCompute.image.Height); this.FindZero(toCompute); for (int greyLevelIndex = 0; greyLevelIndex < 256; greyLevelIndex++) { for (int yIndex = 0; yIndex < toCompute.image.Height; yIndex++) { for (int xIndex = 0; xIndex < toCompute.image.Width; xIndex++) { int pixelGreyScale = HSVColor.HSVtoGreyLevelRGB(HSVColor.RGBtoHSV(toCompute.image.GetPixel(xIndex, yIndex))).B; if ((this.zonesMap[yIndex][xIndex] != 0) && (pixelGreyScale >= greyLevelIndex) && (pixelGreyScale <= greyLevelIndex + isOkAltitude)) { 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) { this.zonesMap[yWindow][xWindow] = this.zonesMap[yIndex][xIndex]; } } } } } } } }
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; }
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))); } }