[英]Flood fill algorithm is causing OutOfMemory Exception
好吧,我正在獲取建築物的輪廓(路徑)。 而且有問題。 當我迭代它的像素時,我檢查當前像素是否為白色以開始檢測它的牆壁。
這會發生什么? 當檢測完成時,它會一次又一次地開始,因為下一個迭代像素也是白色的。 這就是為什么我需要使用洪水填充算法(以標記當前構建已完成)。
嗯,我已經實現了,但是我有問題。
public static void FloodFill(ref Color[] source, Point pt, int width, int height, Color targetColor, Color replacementColor)
{
Stack<Point> pixels = new Stack<Point>();
targetColor = source[P(pt.x, pt.y, width, height)];
pixels.Push(pt);
while (pixels.Count > 0)
{
Point a = pixels.Pop();
if (source[P(a.x, a.y, width, height)] == targetColor)
{
source[P(a.x, a.y, width, height)] = replacementColor;
if (a.x > 0)
pixels.Push(new Point(a.x - 1, a.y));
if (a.x < width)
pixels.Push(new Point(a.x + 1, a.y));
if (a.y > 0)
pixels.Push(new Point(a.x, a.y - 1));
if (a.y < height)
pixels.Push(new Point(a.x, a.y + 1));
}
}
}
摘自: https : //simpledevcode.wordpress.com/2015/12/29/flood-fill-algorithm-using-c-net/
這里的問題是由於某種原因, ref 關鍵字沒有生效。
我的實現:
private IEnumerator MainGenerationDebug(Color[] source, Color32[] target, int width, int height)
{
// (1)
for (int i = 0; i < source.Length; Interlocked.Increment(ref i))
{
// (2)
Color c = source[i];
// (3)
int x = i % width,
y = i / width;
// (4) & (5)
yield return IntMapIteration(source, target, width, c, i, exceptions, (s, t) =>
{
source = s;
target = t;
});
// (6)
if (c == Color.white)
{
F.FloodFill(ref source, new Point(x, y), width, height, Color.white, Color.red);
}
}
}
過程如下:
我認為一切都是正確的。 也許我錯過了一些東西。
對此你有什么想說的?
我創建了一個自己的實現來展示正在發生的事情以及為什么最好使用 HashSet。
為什么使用HashSet? HashSet.Contains更快。
這是實際的實現:
using System.Collections.Generic;
using System.Linq;
public static class F
{
/// <summary>
/// Floods the fill.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="target">The target to replace.</param>
/// <param name="replacement">The replacement.</param>
public static void FloodFill<T>(this T[] source, int x, int y, int width, int height, T target, T replacement)
{
int i = 0;
FloodFill(source, x, y, width, height, target, replacement, out i);
}
/// <summary>
/// Floods the array following Flood Fill algorithm
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="target">The target to replace.</param>
/// <param name="replacement">The replacement.</param>
/// <param name="i">The iterations made (if you want to debug).</param>
public static void FloodFill<T>(this T[] source, int x, int y, int width, int height, T target, T replacement, out int i)
{
i = 0;
// Queue of pixels to process. :silbar:
HashSet<int> queue = new HashSet<int>();
queue.Add(Pn(x, y, width));
while (queue.Count > 0)
{
int _i = queue.First(),
_x = _i % width,
_y = _i / width;
queue.Remove(_i);
if (source[_i].Equals(target))
source[_i] = replacement;
for (int offsetX = -1; offsetX < 2; offsetX++)
for (int offsetY = -1; offsetY < 2; offsetY++)
{
// do not check origin or diagonal neighbours
if (offsetX == 0 && offsetY == 0 || offsetX == offsetY || offsetX == -offsetY || -offsetX == offsetY)
continue;
int targetIndex = Pn(_x + offsetX, _y + offsetY, width);
int _tx = targetIndex % width,
_ty = targetIndex / width;
// skip out of bounds point
if (_tx < 0 || _ty < 0 || _tx >= width || _ty >= height)
continue;
if (!queue.Contains(targetIndex) && source[targetIndex].Equals(target))
{
queue.Add(targetIndex);
++i;
}
}
}
}
public static int Pn(int x, int y, int w)
{
return x + (y * w);
}
}
你可以在這里看到它的工作: https : //dotnetfiddle.net/ZacRiB (使用 Stack 是 O(4n)(我想出了如何解決這個問題,但我沒有找到實際的代碼)和 HashSet 是 O(n - 1 ))。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.