Flood Fill - Fixed Memory Method (right-hand Fill Method)

Fixed Memory Method (right-hand Fill Method)

A method exists that uses essentially no memory for four-connected regions by pretending to be a painter trying to paint the region without painting himself into a corner. This is also a method for solving mazes. The four pixels making the primary boundary are examined to see what action should be taken. The painter could find themselves in one of several conditions:

  1. All four boundary pixels are filled.
  2. Three of the boundary pixels are filled.
  3. Two of the boundary pixels are filled.
  4. One boundary pixel is filled.
  5. Zero boundary pixels are filled.

Where a path or boundary is to be followed, the right-hand rule is used. The painter follows the region by placing their right-hand on the wall (the boundary of the region) and progressing around the edge of the region without removing their hand.

For case #1, the painter paints (fills) the pixel the painter is standing upon and stops the algorithm.

For case #2, a path leading out of the area exists. Paint the pixel the painter is standing upon and move in the direction of the open path.

For case #3, the two boundary pixels define a path which, if we painted the current pixel, may block us from ever getting back to the other side of the path. We need a "mark" to define where we are and which direction we are heading to see if we ever get back to exactly the same pixel. If we already created such a "mark", then we preserve our previous mark and move to the next pixel following the right-hand rule.

A mark is used for the first 2-pixel boundary that is encountered to remember where the passage started and in what direction the painter was moving. If the mark is encountered again and the painter is traveling in the same direction, then the painter knows that it is safe to paint the square with the mark and to continue in the same direction. This is because (through some unknown path) the pixels on the other side of the mark can be reached and painted in the future. The mark is removed for future use.

If the painter encounters the mark but is going in a different direction, then some sort of loop has occurred which caused the painter to return to the mark. This loop must be eliminated. The mark is picked up and the painter then proceeds in the direction indicated previously by the mark using a left-hand rule for the boundary (similar to the right-hand rule but using the painter's left hand). This continues until an intersection is found (with three or more open boundary pixels). Still using the left-hand rule the painter now searches for a simple passage (made by two boundary pixels). Upon finding this two-pixel boundary path, that pixel is painted. This breaks the loop and allows the algorithm to continue.

For case #4, we need to check the opposite 8-connected corners to see if they are filled or not. If either or both are filled, then this creates a many-path intersection and cannot be filled. If both are empty, then the current pixel can be painted and the painter can move following the right-hand rule.

The algorithm trades time for memory. For simple shapes it is very efficient. However, if the shape is complex with many features, the algorithm spends a large amount of time tracing the edges of the region trying to ensure that all can be painted.

This algorithm was first available commercially in 1981 on a Vicom Image Processing system manufactured by Vicom Systems, Inc. The classic recursive flood fill algorithm was available on this system as well.

Read more about this topic:  Flood Fill

Famous quotes containing the words fixed, memory, method and/or fill:

    To find the length of an object, we have to perform certain
    physical operations. The concept of length is therefore fixed when the operations by which length is measured are fixed: that is, the concept of length involves as much as and nothing more than the set of operations by which length is determined.
    Percy W. Bridgman (1882–1961)

    I hope there will be no effort to put up a shaft or any monument of that sort in memory of me or of the other women who have given themselves to our work. The best kind of a memorial would be a school where girls could be taught everything useful that would help them to earn an honorable livelihood; where they could learn to do anything they were capable of, just as boys can. I would like to have lived to see such a school as that in every great city of the United States.
    Susan B. Anthony (1820–1906)

    The country needs and, unless I mistake its temper, the country demands bold, persistent experimentation. It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something. The millions who are in want will not stand idly by silently forever while the things to satisfy their needs are within easy reach.
    Franklin D. Roosevelt (1882–1945)

    “The room’s very hot, with all this crowd,” the Professor said to Sylvie. “I wonder why they don’t put some lumps of ice in the grate? You fill it with lumps of coal in the winter, you know, and you sit round it and enjoy the warmth. How jolly it would be to fill it now with lumps of ice, and sit round it and enjoy the coolth!”
    Lewis Carroll [Charles Lutwidge Dodgson] (1832–1898)