Flood fill recursive algorithm

后端 未结 3 1265
一向
一向 2021-01-05 08:14

I\'m trying to make an algorithm that could fill an int array in c#. Basically, as the fill tool in MS Paint, I have a color and if I choose (x,y) coordinates in the array,

相关标签:
3条回答
  • 2021-01-05 08:32

    How about using a stack/queue to manage the remaining work?

    public void Fill(int[,] array, int x, int y, int newInt)
    {
        int initial = array[x,y];
    
        Queue<Tuple<int,int>> queue = new Queue<Tuple<int,int>>();
        queue.Push(new Tuple<int, int>(x, y));
    
        while (queue.Any())
        {
            Tuple<int, int> point = queue.Dequeue();
    
            if (array[point.Value1, point.Value2] != initial)
                continue;
    
            array[point.Value1, point.Value2] = newInt;
    
            EnqueueIfMatches(array, queue, point.Value1 - 1, point.Value2, initial);
            EnqueueIfMatches(array, queue, point.Value1 + 1, point.Value2, initial);
            EnqueueIfMatches(array, queue, point.Value1, point.Value2 - 1, initial);
            EnqueueIfMatches(array, queue, point.Value1, point.Value2 + 1, initial);        
        }
    }
    
    private void EnqueueIfMatches(int[,] array, Queue<Tuple<int, int>> queue, int x, int y, int initial)
    {
        if (x < 0 || x >= array.GetLength(0) || y < 0 || y >= array.GetLength(1))
            return;
    
        if (array[x, y] == initial)
           queue.Enqueue(new Tuple<int, int>(x, y));
    }
    
    0 讨论(0)
  • 2021-01-05 08:47

    This is a textbook example of why using recursion is not always appropriate. Recursion is great for going through data structures that are inherently recursive (e.g. trees), but your array of pixel data is not.

    I suggest to add a counter to your code to print how often the fill() function is called. On every function call, your machine has to create a new frame on the stack in memory (so it knows to which memory position it has to return after the function is over). The recursive image fill algorithm calls the fill() function a huge number of times, hence the stack will grow very quickly. It has a limited size, so it will overflow for larger pictures.

    The solution is to implement an iterative fill algorithm, using loops instead of recursion to iterate over the pixels.

    See Wikipedia on recursion and stack overflows, or “Computer Graphics, Principles and Practice” by Foley et al. for a more detailed introduction to basic computer graphics algorithms.

    0 讨论(0)
  • 2021-01-05 08:52

    As it was suggested already, the issue is with the number of recursive calls. On a 32bit machine you have 4bytes for the pointers, so if you have a 512*512 image and you want to fill the whole thing, the function pointers alone will occupy 512*512*4bytes = 1MB memory on your stack. And that is the default size of the stack. Regardless of the function pointers, you have another 5 references you pass (array, x, y, initialInt, newInt) which all are copied with every call as temporary objects, and are on the stack until the function call terminates. On the same sized image, those are another 512*512*5*4bytes = 5MB of memory.

    To solve your issue you can modify (same link as above) the size of the stack.

    Also, to save some stack space, you might consider wrapping the parameters inside a single object, in this case you will have only 4bits of temporary memory per call, instead of 20.

    Still, as it was as well pointed out, the best solution is to rewrite your algorithm iteratively.

    0 讨论(0)
提交回复
热议问题