private Dictionary<int, List<Pixel>> Find() { int labelCount = 1; var allLabels = new Dictionary<int, Label>(); for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { Pixel currentPixel = new Pixel(new Point(j, i), _input.GetPixel(j, i)); if (CheckIsBackGround(currentPixel)) { continue; } IEnumerable<int> neighboringLabels = GetNeighboringLabels(currentPixel); int currentLabel; if (!neighboringLabels.Any()) { currentLabel = labelCount; allLabels.Add(currentLabel, new Label(currentLabel)); labelCount++; } else { currentLabel = neighboringLabels.Min(n => allLabels[n].GetRoot().Name); Label root = allLabels[currentLabel].GetRoot(); foreach (var neighbor in neighboringLabels) { if (root.Name != allLabels[neighbor].GetRoot().Name) { allLabels[neighbor].Join(allLabels[currentLabel]); } } } _board[j, i] = currentLabel; } } Dictionary<int, List<Pixel>> patterns = AggregatePatterns(allLabels); return patterns; }
protected virtual bool CheckIsBackGround(Pixel currentPixel) { return currentPixel.color.A == 255 && currentPixel.color.R == 255 && currentPixel.color.G == 255 && currentPixel.color.B == 255; }
private IEnumerable<int> GetNeighboringLabels(Pixel pix) { var neighboringLabels = new List<int>(); for (int i = pix.Position.Y - 1; i <= pix.Position.Y + 2 && i < _height - 1; i++) { for (int j = pix.Position.X - 1; j <= pix.Position.X + 2 && j < _width - 1; j++) { if (i > -1 && j > -1 && _board[j, i] != 0) { neighboringLabels.Add(_board[j, i]); } } } return neighboringLabels; }
private Dictionary<int, List<Pixel>> Find(int width, int height) { int labelCount = 1; var allLabels = new Dictionary<int, Label>(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Pixel currentPixel = new Pixel(new Point(j, i), _input.GetPixel(j, i)); if (CheckIsForeGround(currentPixel)) { HashSet<int> neighboringLabels = GetNeighboringLabels(currentPixel, width, height); int currentLabel; if (neighboringLabels.Count == 0) { currentLabel = labelCount; allLabels.Add(currentLabel, new Label(currentLabel)); labelCount++; } else { currentLabel = neighboringLabels.Min(); var root = allLabels[currentLabel].GetRoot(); foreach (var item in neighboringLabels) { var root2 = allLabels[item].GetRoot(); if (root.Name != root2.Name) { allLabels[item].Join(allLabels[currentLabel]); } } } _board[j, i] = currentLabel; } } } Dictionary<int, List<Pixel>> patterns = AggregatePatterns(allLabels, width, height); return patterns; }
private int CheckDimensionType(Pixel shape, bool isWidth) { return isWidth ? shape.Position.X : shape.Position.Y; }
private HashSet<int> GetNeighboringLabels(Pixel pix, int width, int height) { var neighboringLabels = new HashSet<int>(); int x = pix.Position.Y; int y = pix.Position.X; if (x > 0)//North { CheckCorner(neighboringLabels, x - 1, y); if (y > 0)//North West { CheckCorner(neighboringLabels, x - 1, y - 1); } if (y < width - 1)//North East { CheckCorner(neighboringLabels, x - 1, y + 1); } } if (y > 0)//West { CheckCorner(neighboringLabels, x, y - 1); if (x < height - 1)//South West { CheckCorner(neighboringLabels, x + 1, y - 1); } } if (y < width - 1)//East { CheckCorner(neighboringLabels, x, y + 1); if (x < height - 1)//South East { CheckCorner(neighboringLabels, x + 1, y + 1); } } return neighboringLabels; }