/// <summary> /// Find all the unassigned neighbors of the given pixel and update stats of the given island /// </summary> /// <param name="x"></param> /// <param name="y"></param> protected void FindNeighbors(Bitmap b, BlobIsland island) { // keep iterating while there are items on the stack while (this.FindNeighborsStack.Count > 0) { // get top of stack Point p = (Point)this.FindNeighborsStack.Pop(); // if out of bounds then ignore if (p.X < 0) break; if (p.Y < 0) break; if (p.X >= this.width) break; if (p.Y >= this.height) break; // continue if this pixel is not assigned to an island if (this.map[p.Y * this.width + p.X] == -1) { // continue only if the pixel is not black Color pixel = b.GetPixel(p.X, p.Y); // if any components not zero consider it a 'set' pixel if ((pixel.R + pixel.G + pixel.B) > 0) { // mark pixel with our index this.map[p.Y * this.width + p.X] = island.index; // bump pixel count island.pixelCount++; // if this is an edge pixel then flag island as belonging to an edge if ((p.X == 0) || (p.Y == 0) || (p.X == this.width - 1) || (p.Y == this.height - 1)) island.edgeConnected = true; // push neighbors onto stack and keep trying this.FindNeighborsStack.Push(new Point(p.X, p.Y - 1)); this.FindNeighborsStack.Push(new Point(p.X - 1, p.Y)); this.FindNeighborsStack.Push(new Point(p.X + 1, p.Y)); this.FindNeighborsStack.Push(new Point(p.X, p.Y + 1)); } } } }
/// <summary> /// Create an island at the given location. Count and flag all 4np pixels /// </summary> /// <param name="x"></param> /// <param name="y"></param> protected BlobIsland CreateIsland(int x, int y, int index, Bitmap b) { // create new island BlobIsland island = new BlobIsland(); // set seed location island.seedLocation = new Point(x, y); // reset pixel count island.pixelCount = 0; // defaults to not edge connected until proven otherwise island.edgeConnected = false; // set index island.index = index; // discover island starting at this location this.FindNeighborsStack.Clear(); this.FindNeighborsStack.Push(island.seedLocation); this.FindNeighbors(b, island); // return island return island; }