private void ProcessQueue(int pivotX, int pivotY) { do { var found = queue.Dequeue(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { var neighbor = new Point(found.X + x, found.Y + y); if ((neighbor.X >= 0 && neighbor.Y >= 0) && (neighbor.X < Image.Width && neighbor.Y < Image.Height)) { if (Image.MeetsThreshold(Image.GetPixel(neighbor.X, neighbor.Y)) && labels[neighbor.X, neighbor.Y] == 0) { var connectedNeighbor = new LabeledPixel(neighbor.X, neighbor.Y, currentLabel); if (!labeledPixels.Contains(connectedNeighbor)) { queue.Enqueue(connectedNeighbor); } labels[neighbor.X, neighbor.Y] = currentLabel; } } } } }while (queue.Any()); currentLabel++; }
public IEnumerable <Rectangle> From(IAddressableImage image) { labels = new int[image.Width, image.Height]; var rectangles = new List <Rectangle>(); Image = image; for (int x = 0; x < image.Width; x++) { for (int y = 0; y < image.Height; y++) { if (Image.MeetsThreshold(Image.GetPixel(x, y)) && labels[x, y] == 0) { var found = new LabeledPixel(x, y, currentLabel); labels[x, y] = currentLabel; if (!queue.Contains(found)) { queue.Enqueue(found); } ProcessQueue(x, y); } } } return(CreateRectangle()); }