public WatershedStructure(Bitmap image) { Structure = new WatershedPixel[image.Width, image.Height]; //Remplissage du tableau for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { int gsv = (image.GetPixel(x, y).R + image.GetPixel(x, y).G + image.GetPixel(x, y).B) / 3; Structure[x, y] = new WatershedPixel(x, y, gsv); } } //Références des voisins for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { WatershedPixel wp = Structure[x, y]; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (i != 0 || j != 0) { try { wp.addNeighbour(Structure[x + i, y + j]); } catch (IndexOutOfRangeException e) { } } } } wp.initDepth(); } } }
public MeyerStruct(Bitmap image) { Structure = new List <WatershedPixel>(); int width = image.Width; int height = image.Height; //Remplissage du tableau for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { int gsv = (image.GetPixel(x, y).R + image.GetPixel(x, y).G + image.GetPixel(x, y).B) / 3; Structure.Add(new WatershedPixel(x, y, gsv)); } } for (int y = 0; y < height; y++) { int offset = y * width; int topOffset = offset + width; int bottomOffset = offset - width; for (int x = 0; x < width; x++) { WatershedPixel currentPixel = (WatershedPixel)Structure.ElementAt(x + offset); if (x + 1 < width) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x + 1 + offset)); if (y - 1 >= 0) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x + 1 + bottomOffset)); } if (y + 1 < height) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x + 1 + topOffset)); } } if (x - 1 >= 0) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x - 1 + offset)); if (y - 1 >= 0) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x - 1 + bottomOffset)); } if (y + 1 < height) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x - 1 + topOffset)); } } if (y - 1 >= 0) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x + bottomOffset)); } if (y + 1 < height) { currentPixel.addNeighbour((WatershedPixel)Structure.ElementAt(x + topOffset)); } } } Structure.Sort(); }
public int watershed(MeyerStruct image) { Queue <WatershedPixel> queue = new Queue <WatershedPixel>(); int curlab = 0; int heightIndex1 = 0; int heightIndex2 = 0; for (int h = HMIN; h < HMAX; h++) { for (int pixelIndex = heightIndex1; pixelIndex < image.Structure.Count; pixelIndex++) { WatershedPixel p = image.Structure.ElementAt(pixelIndex); if (p.Height != h) { heightIndex1 = pixelIndex; break; } p.setLabelToMASK(); List <WatershedPixel> neighbours = p.Neightbours; for (int i = 0; i < neighbours.Count(); i++) { WatershedPixel q = (WatershedPixel)neighbours.ElementAt(i); if (q.Label >= 0) {/*Initialise queue with neighbours at level h of current basins or watersheds*/ p.Depth = 1; queue.Enqueue(p); break; } // end if } } int curdist = 1; queue.Enqueue(new WatershedPixel(WatershedPixel.FICTIONOUS)); while (true) { WatershedPixel p = queue.Dequeue(); if (p.Label == WatershedPixel.FICTIONOUS) { if (queue.Count == 0) { break; } else { queue.Enqueue(new WatershedPixel(WatershedPixel.FICTIONOUS)); curdist++; p = queue.Dequeue(); } } List <WatershedPixel> neighbours = p.Neightbours; for (int i = 0; i < neighbours.Count; i++) { WatershedPixel q = (WatershedPixel)neighbours.ElementAt(i); if ((q.Depth <= curdist) && (q.Label >= 0)) { if (q.Label > 0) { if (p.Label == WatershedPixel.MASK) { p.Label = q.Label; } else if (p.Label != q.Label) { p.Label = WatershedPixel.WSHED; } } else if (p.Label == WatershedPixel.MASK) { p.Label = WatershedPixel.WSHED; } } else if (q.Label == WatershedPixel.MASK && q.Depth == 0) { q.Depth = curdist + 1; queue.Enqueue(q); } } } for (int pixelIndex = heightIndex2; pixelIndex < image.Structure.Count; pixelIndex++) { WatershedPixel p = image.Structure.ElementAt(pixelIndex); if (p.Height != h) { heightIndex2 = pixelIndex; break; } p.Depth = 0; if (p.Label == WatershedPixel.MASK) { curlab++; p.Label = curlab; queue.Enqueue(p); while (queue.Count > 0) { WatershedPixel q = queue.Dequeue(); List <WatershedPixel> neighbours = p.Neightbours; for (int i = 0; i < neighbours.Count; i++) { WatershedPixel r = neighbours.ElementAt(i); if (r.Label == WatershedPixel.MASK) { r.Label = curlab; queue.Enqueue(r); } } } } } } return(curlab); }