Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied.

Similar presentations


Presentation on theme: "Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied."— Presentation transcript:

1 Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied on an image to fill a particular bounded area with color, it is also known as boundary fill. The flood fill algorithm normally takes three parameters: a start node, a target color, and a replacement color. The algorithm looks for all nodes in the array which are connected to the start node by a path of the target color, and changes them to the replacement color. There are several algorithms doing this.

2 Stack-based recursive implementation (Four-way)
Flood-fill (node, target-color, replacement-color): 1. If target-color is equal to replacement-color, return. 2. If the color of node is not equal to target-color, return. 3. Set the color of node to replacement-color. 4. Perform flood-fill in four ways: 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). Perform Flood-fill (one step to the south of node, target-color, replacement-color). 5. Return.

3 An explicitly queue-based implementation
Flood-fill (node, target-color, replacement-color): 1. If target-color is equal to replacement-color, return. 2. Set Q to the empty queue. 3. Add node to the end of Q. 4. While Q is not empty: 5. Set n equal to the last element of Q. 6. Remove last element from Q. 7. If the color of n is equal to target-color: 8. Set the color of n to replacement-color and mark "n" as processed. 9. Add west node to end of Q if west has not been processed yet. 10. Add east node to end of Q if east has not been processed yet. 11. Add north node to end of Q if north has not been processed yet. 12. Add south node to end of Q if south has not been processed yet. 13. Return.

4 As an optimization to avoid the overhead of stack or queue management.
A practical implementation using a loop for the west and east directions As an optimization to avoid the overhead of stack or queue management. Flood-fill (node, target-color, replacement-color): 1. Set Q to the empty queue. 2. If the color of node is not equal to target-color, return. 3. Add node to Q. 4. For each element N of Q: 5. If the color of N is equal to target-color: 6. Set w and e equal to N. 7. Move w to the west until the color of the node to the west of w no longer matches target-color. 8. Move e to the east until the color of the node to the east of e no longer matches target-color. 9. For each node n between w and e: 10. Set the color of n to replacement-color. 11. If the color of the node to the north of n is target-color, add that node to Q. 12. If the color of the node to the south of n is target-color, add that node to Q. 13. Continue looping until Q is exhausted. 14. Return.

5 Fixed memory method (right-hand fill method)


Download ppt "Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied."

Similar presentations


Ads by Google