FLOOD FILL
ALGORITHM
Presentation By Siddarth
CONTENT
1. INTRODUCTION
2. 4 –CONNECTED PIXELS
3. 8-CONNECTED PIXELS
4. EXAMPLE
INTRODUCTION
The Flood Fill Algorithm is used for filling a connected area with a specific colour or pattern,
starting from a particular point. It identifies all neighboring pixels of a given start pixel with the same
color, replacing their colour with a new one.
Flood fill algorithms are essential in various applications, including:
o Image editing software: Filling a closed region (like the paint bucket tool in MS Paint).
o Game development: For terrain filling or object colouring.
o Image processing: Detecting and highlighting regions of interest.
4-CONNECTED PIXEL
Floodfill (x, y,fill_ color, old_color: integer)
If (getpixel (x, y)=old_color)
{
setpixel (x, y, fill_color);
Floodfill (x+1, y, fill_color, old_color);
Floodfill (x-1, y, fill_color, old_color);
Floodfill (x, y+1, fill_color, old_color);
Floodfill (x, y-1, fill_color, old_color);
}
In a 4-connected pixel neighborhood, each pixel is considered connected to its horizontal
and vertical neighbors, but not diagonally. This means that for a pixel, its neighbors
are the pixels directly adjacent to it in the up, down, left, and right directions.
ALGORITHM:
8-CONNECTED PIXELS
In an 8-connected pixel neighborhood, each pixel is considered connected to all of its immediate
neighbors, including the diagonals. This means that for a pixel, it is connected to its neighbors in all 8
directions (up, down, left, right, and diagonals).
ALGORITHM:
Floodfill (x, y,fill_ color, old_color: integer)
If (getpixel (x, y)=old_color)
{
setpixel (x, y, fill_color);
FloodFill(x + 1, y, targetColor,
fillColor)
FloodFill(x - 1, y, targetColor, fillColor)
FloodFill(x, y + 1, targetColor, fillColor)
FloodFill(x, y - 1, targetColor,
fillColor)
FloodFill(x + 1, y + 1, targetColor,
fillColor)
EXAMPLE
Let’s assume we have an image represented as a 2D grid (matrix), where each cell contains a color value.
The matrix could look like this:
Before Flood Fill (image):
1 1 1 2 2
1 1 2 2 2
1 2 2 1 1
2 2 1 1 1
We want to fill the region connected to the pixel at position (0,0) with the new color 3. The pixel at (0,0)
currently has color 1.
After applying the 4-connected Flood Fill algorithm:
After Flood Fill:
3 3 3 2 2
3 3 2 2 2
3 2 2 1 1
2 2 1 1 1
Explanation:
•We start from pixel (0,0) with color 1.
•The algorithm changes the color to 3 and recursively fills its 4-connected neighbors that also have color 1.
•The algorithm stops when there are no more connected pixels with the original color.
THANK YOU

FLOOD FILL algorithm in computer graphics.pptx

  • 1.
  • 2.
    CONTENT 1. INTRODUCTION 2. 4–CONNECTED PIXELS 3. 8-CONNECTED PIXELS 4. EXAMPLE
  • 3.
    INTRODUCTION The Flood FillAlgorithm is used for filling a connected area with a specific colour or pattern, starting from a particular point. It identifies all neighboring pixels of a given start pixel with the same color, replacing their colour with a new one. Flood fill algorithms are essential in various applications, including: o Image editing software: Filling a closed region (like the paint bucket tool in MS Paint). o Game development: For terrain filling or object colouring. o Image processing: Detecting and highlighting regions of interest.
  • 4.
    4-CONNECTED PIXEL Floodfill (x,y,fill_ color, old_color: integer) If (getpixel (x, y)=old_color) { setpixel (x, y, fill_color); Floodfill (x+1, y, fill_color, old_color); Floodfill (x-1, y, fill_color, old_color); Floodfill (x, y+1, fill_color, old_color); Floodfill (x, y-1, fill_color, old_color); } In a 4-connected pixel neighborhood, each pixel is considered connected to its horizontal and vertical neighbors, but not diagonally. This means that for a pixel, its neighbors are the pixels directly adjacent to it in the up, down, left, and right directions. ALGORITHM:
  • 5.
    8-CONNECTED PIXELS In an8-connected pixel neighborhood, each pixel is considered connected to all of its immediate neighbors, including the diagonals. This means that for a pixel, it is connected to its neighbors in all 8 directions (up, down, left, right, and diagonals). ALGORITHM: Floodfill (x, y,fill_ color, old_color: integer) If (getpixel (x, y)=old_color) { setpixel (x, y, fill_color); FloodFill(x + 1, y, targetColor, fillColor) FloodFill(x - 1, y, targetColor, fillColor) FloodFill(x, y + 1, targetColor, fillColor) FloodFill(x, y - 1, targetColor, fillColor) FloodFill(x + 1, y + 1, targetColor, fillColor)
  • 6.
    EXAMPLE Let’s assume wehave an image represented as a 2D grid (matrix), where each cell contains a color value. The matrix could look like this: Before Flood Fill (image): 1 1 1 2 2 1 1 2 2 2 1 2 2 1 1 2 2 1 1 1 We want to fill the region connected to the pixel at position (0,0) with the new color 3. The pixel at (0,0) currently has color 1. After applying the 4-connected Flood Fill algorithm: After Flood Fill: 3 3 3 2 2 3 3 2 2 2 3 2 2 1 1 2 2 1 1 1 Explanation: •We start from pixel (0,0) with color 1. •The algorithm changes the color to 3 and recursively fills its 4-connected neighbors that also have color 1. •The algorithm stops when there are no more connected pixels with the original color.
  • 7.