which flood-fill algorithm is better for performance?

flood fill algorithm c#
flood fill algorithm javascript
flood fill pathfinding
flood fill algorithm c++
seed fill algorithm
flood fill - leetcode
flood fill scanline algorithm
3d flood fill algorithm

i'm trying to implement an algorithm which is flood-fill alike. the problem is that i'm not sure in what way i should implement it e.g recursive - non-recursive. i know that each has its defects but one of them must be faster than the other one. the recursive opens new function on the stack when the non-recursive allocates 4 new points each time. example for the non-iterative :

Stack<Point> stack = new Stack<Point>();
    stack.Push(q);
    while (stack.Count > 0)
    {
        Point p = stack.Pop();
        int x = p.X;
        int y = p.Y;
        if (y < 0 || y > h - 1 || x < 0 || x > w - 1)
                continue;
        byte val = vals[y, x];
        if (val == SEED_COLOR)
        {
                vals[y, x] = COLOR;
                stack.Push(new Point(x + 1, y));
                stack.Push(new Point(x - 1, y));
                stack.Push(new Point(x, y + 1));
                stack.Push(new Point(x, y - 1));
        }
    }

edit : im going to apply the following algorithm on a 600X600 pixels map. though the floodfill isn't going to be applied on the entire map, it should cover about 30% - 80% of the map each iteration. my point is to discover edges in a height map and mark those edges for a further use.

Make a mask - a parallel 2-dim array of bytes. Unchecked areas bytes has 0, for the fresh border of flooded area it will have value 1. For the inside of the flooded area - value 2. And keep the list of current border points, too.

At any end of the outer cycle you have the mask with marked current border, inside and outside area, and the array of the border points. So you will check for the new points only on the border. And while checking the first arraylist of border points, you are creating the second border arraylist and second mask. At the next step you recreate the first border array and mask. Going this way, we can use simple while cycle instead of recursion, for the data structure you check at any step is very simple.

BTW, you have forgotten to check coordinates of the new points for being on the drawn border or on the border of the whole rectangle.

As for cycling through all neighbouring points, look at my algorithm here

which flood-fill algorithm is better for performance?, Make a mask - a parallel 2-dim array of bytes. Unchecked areas bytes has 0, for the fresh border of flooded area it will have value 1. For the inside of the flooded  Set the color of node to replacement-color. Perform Flood-fill (one step to the west of node, target-color, replacement-color). Perform Flood-fill (one step to the east of node, target-color, replacement-color). Perform Flood-fill (one step to the north of node, target-color, replacement-color).

this seems to be the most efficient implementation of a flood fill i've found...

I mentions the border fill technique mentioned in this answer as a good one, but not as good as quickfill.

graphics - which flood-fill algorithm is better for performance?, The QuickFill Algorithm. Finally, we get to the QuickFill method of flood filling, which is a type of 4 way method, but is more efficient than the simple non-recursive method (believe it or not). I'm having performance issues with my flood-fill algorithm. Basically, I open an image with the different modules that are part of a frame of an animation. When I double-click a module I run a depth-first search algorithm that checks all the pixels of that part of the image that are surrounded by transparency.

A recursive flood fill can overflow the stack, if the image is complex. Use the non-recursive flood fill.

If you care about the allocations, you can represent a point as a packed value of type long, and code up your own LongStack that internally stores the long values in an array.

QuickFill: An Efficient Flood Fill Algorithm, However, in order to achieve better performance on neural networks, es- pecially filling algorithm called Scan-flood Fill(SCAFF) algorithm. To show that the  The Flood-fill and boundary-fill algorithm come under the category of area filling algorithm. The crucial differing point in these algorithms is that the flood-fill first checks whether a random pixel is having the region’s original colour or not. Conversely, the boundary-fill examines for boundary pixel and it has already been filled or not.

Computers process XY loops and 2D arrays very efficiently.

If you can use XY loops and 2D arrays to sweep through a floodfill, rather than all kinds of stepwise logic, chances are it will be very fast.

The simplest stepwise logic is the floodfill recursive function which only checks 4 neighbors. That's what I have used to achieve 9 million pixels per second on a 2017 i7 processor. here is a video to explain the simplicity of the logic. it uses 2D arrays rather than memory (very efficient) and loops: https://www.youtube.com/watch?v=Xddws-50Igs

Here is a page with a video to prove that i get 4 million voxels per second for this algorythm on a 2012 FX processor.(33 million voxels filled in 7 seconds) https://unity3dmc.blogspot.com/2017/02/ultimate-3d-floodfill-scanline.html?showComment=1537614917045#c9221615743847221048

Flood fill, It can process the image containing more than one boundary colours. It can only process the image containing single boundary colour. Flood-fill algorithm is  The flood fill algorithm has many characters similar to boundary fill. But this method is more suitable for filling multiple colors boundary. When boundary is of many colors and interior is to be filled with one color we use this algorithm. In fill algorithm, we start from a specified interior point (x,

[PDF] Scan-Flood Fill(SCAFF): An Efficient Automatic , However, in order to achieve better performance on neural networks, es- filling algorithm called Scan-flood Fill(SCAFF) algorithm. To show  Performance. The plot below shows the timings of each method relative to each other, lower values on the y axis Time (s) is better. The y axis is logarithmic for clarity.

Difference Between Flood-fill and Boundary-fill Algorithm , Flood fill is an algorithm mainly used to determine a bounded area a (or more) problem/s using a newly learned algorithm is the best way to  The following is the code for the algorithm. The FloodFill() method fills an area starting from a given point. The LinearFill() method is used by the FloodFill() method to get the furthest extent of the color area on a given horizontal scanline, filling as it goes, and then add the horizontal range to the queue.

[PDF] Download PDF, guage with little performance penalty. For ease of coding, debugging and maintenance, C++ is a good choice. Hence, we divide the flood fill algorithm into the  Flood fill is an algorithm mainly used to determine a bounded area connected to a given node in a multi-dimensional array. It is a close resemblance to the bucket tool in paint programs. The most approached implementation of the algorithm is a stack-based recursive function, and that’s what we’re gonna talk about next.

Comments
  • This is really implementation-dependent.
  • you should say what language you are using. in c or java, you would want the iterative solution.
  • C++ mainly.. if there is a quicker way to use CUDA / OMP ..
  • I know for a fact that is wrong advice. Recursion is the fastest. ALOT. and simplest(50 lines) if you write your own memory array into the recursion loop. Its the lightest. Recursions can be stored in grids not trees. Grids are 1x picture memory size, trees are 100 gygabytes. Its also bitwise fast. I wrote a post about box. I tried to document my research here unity3dmc.blogspot.com/2017/02/… because it thrashes scanline codes and its ze smaller zimpler programming. Je te le guarantie emphatiquement.
  • I have realized this algorithm on the computer with the speed 340 operations/second. And it worked. Please, explain, how do you want to use border/colored/free areas marking and recursion? I can't imagine this. If you mean totally another way, that you mentioned in your answer, try to explain it to people that read it. Existing test is unreadable. I was not one of downvoters of your post. Now I am, too. Effectiveness of an unreadable post is 0.
  • I achieved 2million pixels per second. The recursive loop is about 40 ASM instructions, and a PC can run 80million flops. Where do you get 340 flops from? Please start a post "how do I order memory arrays within a recursion for flood fill" and I promise I will explain you its simple logic. The recursion is very easy: compare neighbor colors with "If-val instruction", mark results in arrays, recurse again, reading from arrays. What dont you understand? Start a query please.
  • @com.prehensible 1. Talking about the practical speed of algorithm without mentioning the speed of comp is senseless. 2. Explain your method in your answer, please. 3. Recursion is not an Assembler command. It is always based on more primitive operations. And it can be more simple only in the sense of the volume of written code and/or readability. In other aspects it always lower the effectiveness of the algorithm. 4. 340 op/sec? There were times when the speed of comps was lower. Probably, I am a bit longer than you in programming :-)
  • @com.prehensible Trying to explain better my algorithm, I have noticed that in one aspect you are right. Here we can use the recursion without noticeable losses. And the code will be more simple in understanding. But we will have to create more complex structures of data. And, of course, recursion will never be more effective, even a little bit. It simply can't.
  • That's a cool page, I read many similar pages before I wrote a very small code that can fill 9 million pixels per second on an i7 with perfect accuracy, that's about 10 - 100 times faster than the page your provided. answer and demonstration video on this page. youtube.com/watch?v=4hQ1wA4Sl4c&feature=youtu.be
  • The non-recursive is slower because it has many if conditions. Use 2-3 array duplicates of your canvas to store the steps of your recursion. It turns memory adressing into raw processing ASM speed.