Flood Fill Algorithm: Finding and Modifying Connected Pixels of the Same Color
Flood Fill Algorithm: Finding and Modifying Connected Pixels of the Same Color
When working with images, one common task is to find and modify all connected pixels of the same color starting from a given pixel. This process is known as the flood fill algorithm, a computationally efficient technique used in image processing, painting applications, and more. Below, we will explore how to implement this algorithm using both Depth-First Search (DFS) and Breadth-First Search (BFS) methodologies.
Understanding the Flood Fill Algorithm
The goal of the flood fill algorithm is to identify and modify all contiguous pixels of a certain color, known as the ldquo;seedrdquo; color, starting from a specific pixel. This process is akin to using a paint bucket in a graphic editor to fill a region with a new color.
Steps to Implement the Flood Fill Algorithm
1. Input the Image
Represent the image as a 2D array (list of lists) in Python, where each element represents a pixel's color. Here is an example of how to structure the image:
image [ [1, 1, 0, 0], [1, 1, 1, 0], [0, 1, 1, 1], [0, 0, 0, 1]]
2. Define the Target Pixel
Identify the starting pixel's coordinates (x, y) and its color. In our example, the coordinates (1, 1) have a color value of 1.
3. Check Bounds
Ensure that you do not go out of the image boundaries when exploring the neighboring pixels.
4. Check Color Match
Only proceed if the current pixel's color matches the target color.
5. Explore Neighbors
Recursively explore the neighboring pixels (up, down, left, right) and repeat the process.
Implementation Using DFS
def flood_fill(image, x, y, target_color): rows, cols len(image), len(image[0]) original_color image[x][y] # Directions for moving up, down, left, right directions [(-1, 0), (1, 0), (0, -1), (0, 1)] def dfs(x, y): # Check if the current pixel is out of bounds or not the target color if x 0 or x rows or y 0 or y cols or image[x][y] ! original_color: return # Change the pixel color to avoid re-visiting image[x][y] target_color # Explore the neighbors for dx, dy in directions: dfs(x dx, y dy) # Start the flood fill dfs(x, y)
Implementation Using BFS
from collections import deque def flood_fill_bfs(image, x, y, target_color): rows, cols len(image), len(image[0]) original_color image[x][y] queue deque([(x, y)]) # Directions for moving up, down, left, right directions [(-1, 0), (1, 0), (0, -1), (0, 1)] while queue: x, y queue.popleft() # Check if the current pixel is out of bounds or not the target color if x 0 or x rows or y 0 or y cols or image[x][y] ! original_color: continue # Change the pixel color to avoid re-visiting image[x][y] target_color # Explore the neighbors for dx, dy in directions: ((x dx, y dy))
Example Usage
Let's modify the connected pixels starting from the pixel at (1, 1) with color 2:
image [ [1, 1, 0, 0], [1, 1, 1, 0], [0, 1, 1, 1], [0, 0, 0, 1]] # Flood fill starting from (1, 1) with color 2flood_fill(image, 1, 1, 2)
The modified image will now have the connected pixels changed to the new color:
print(image)
Explanation of the Code
Function Definitions
The flood_fill function takes the image, starting coordinates, and the target color as arguments.
DFS Function
The dfs function is defined to explore the image recursively, ensuring that we do not revisit pixels and stay within the boundaries of the image.
Boundary and Color Check
Before proceeding, the code checks if the pixel is within bounds and matches the original color.
Color Change
The pixels' color is changed to the target color to avoid processing it again.
Direction Exploration
The function recursively calls itself for each of the four directions (up, down, left, right).
Conclusion
This method effectively finds and modifies all connected pixels of the same color starting from a given pixel. You can adapt the algorithm to return the coordinates of the connected pixels instead of changing their color by maintaining a list of visited pixels. Whether you choose to use DFS or BFS, both methods are efficient and provide powerful tools for image processing tasks.