/// <summary> /// /// </summary> public void evolution(imageCallback callback) { int iterations = 500; int i = 0; int countOfChanges = 1; int ws = (windowSize - 1) / 2; while (i < iterations && countOfChanges != 0) { i++; CellDescription[,] newStates = (CellDescription[, ])states.Clone(); countOfChanges = 0; // Проходимся по всем клеткам Parallel.For(0, width, x => { for (int y = 0; y < height; y++) { int[] C_p = colorToVector(sourceImage.GetPixel(x, y)); CellDescription S_p = states[x, y]; // Соседи атакуют! int startXX = Math.Max(0, x - ws); int startYY = Math.Max(0, y - ws); int endXX = Math.Min(x + ws + 1, width); int endYY = Math.Min(y + ws + 1, height); for (int xx = startXX; xx < endXX; xx++) { for (int yy = startYY; yy < endYY; yy++) { int[] C_q = colorToVector(sourceImage.GetPixel(xx, yy)); CellDescription S_q = states[xx, yy]; if (g(C_p, C_q) * S_q.strength > S_p.strength) { newStates[x, y].label = S_q.label; newStates[x, y].strength = g(C_p, C_q); countOfChanges++; } } } } }); Console.Out.WriteLine("Iteration : " + i); Console.Out.WriteLine("Count of changes : " + countOfChanges); states = newStates; } callback(states); }
public static CellDescription[,] getCellsMap(Bitmap originalBitmap, Bitmap cellSeeds) { if (originalBitmap.Size != originalBitmap.Size) { throw new RankException("Image with label seed has different size"); } CellDescription emptyCell = new CellDescription(strength: 0, label: 0); CellDescription[,] result = new CellDescription[originalBitmap.Width, originalBitmap.Height]; Dictionary <Color, int> labels = new Dictionary <Color, int>(); Color emptyPixel = Color.FromArgb(0, 0, 0, 0); int labelNumber = 0; for (int x = 0; x < originalBitmap.Width; x++) { for (int y = 0; y < originalBitmap.Height; y++) { Color currentPixel = cellSeeds.GetPixel(x, y); if (currentPixel == emptyPixel) { result[x, y] = emptyCell; } else if (labels.ContainsKey(cellSeeds.GetPixel(x, y))) { result[x, y] = new CellDescription(strength: 1.0, label: labels[currentPixel]); } else { labelNumber++; labels.Add(currentPixel, labelNumber); result[x, y] = new CellDescription(strength: 1.0, label: labelNumber); } } } return(result); }