0% found this document useful (0 votes)
133 views

Flood Fill Algorithm

The flood fill algorithm is used to fill in areas bounded by multiple color regions. It works by replacing all pixels of a specified interior color with a fill color, rather than searching for a boundary color. The algorithm recursively fills 4-connected or 8-connected neighboring pixels of the initial seed point that match the interior color, until the entire area is repainted.

Uploaded by

sam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Flood Fill Algorithm

The flood fill algorithm is used to fill in areas bounded by multiple color regions. It works by replacing all pixels of a specified interior color with a fill color, rather than searching for a boundary color. The algorithm recursively fills 4-connected or 8-connected neighboring pixels of the initial seed point that match the interior color, until the entire area is repainted.

Uploaded by

sam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Flood fill algorithm

Flood Fill Algorithm


 Sometimes we want to fill in (or recolor) an area that is not defined within a single color
boundary. Figure 3-46 shows an area bordered by several different color regions.
 We can paint such areas by replacing a specified interior color instead of searching for a
boundary color value. This approach is called a flood-fill algorithm.
 We start from a specified interior point (x, y) and reassign all pixel values that are
currently set to a given interior color with the desired fill color.
 If the area we want to paint has more than one interior color, we can first reassign pixel
values so that all interior points have the same color.
 Using either a 4-connected or 8-connected approach, we then step through pixel positions
until all interior points have been repainted.
 The following procedure flood fills a 4-connected region recursively, starting from the
input position. An area defined
Figure 3-46
within multiple color
boundaries
Flood Fill Algorithm – 4 Connected pixels
Void floodFill4 ( int x, int y, int fill color , int oldcolor)
{
if (getpixel (x,y) == oldcolor)
x,y+1
{
setcolor ( f i l l c o l o r ) ;
X-1,y x+1,y An area defined
Figure 3-46
setpixel (x, y ) :
within multiple color
floodFill4 ( x + l , y, fillColor, oldColor); x,y-1
boundaries
floodfill4 (x-1, y, fillcolor, oldcolor);
floodPill4 (x, y + l , fillcolor, oldcolor);
floodFill4 ( x , y-1, fillColor, oldcolor);
}
}
Flood Fill Algorithm – 8 Connected pixels
Void floodFill8 ( int x, int y, int fill color , int oldcolor)
{
if (getpixel (x,y) == oldcolor)
{
setcolor ( f i l l c o l o r ) ;
setpixel (x, y ) :
floodFill8 ( x + l , y, fillColor, oldColor);
An area defined
Figure 3-46
floodfill8 (x-1, y, fillcolor, oldcolor);
within multiple color
floodPill8 (x, y + l , fillcolor, oldcolor);
boundaries
floodFill8 ( x , y-1, fillColor, oldcolor);
floodFill8 (x+1, y+1, fillcolor, oldColor);
X-1, y+1 x,y+1 x+1, y+1
floodFill8 (x+1, y-1, fillcolor, oldColor);
floodFill8 (x-1, y+1, fillcolor, oldColor);
floodFill8 (x-1, y-1, fillcolor, oldColor); X-1,y x+1,y

}
} X-1, y-1 x,y-1 x+1, y-1
Flood Fill Algorithm
 Basic concept:
 It is used in case where there is no single color boundary for polygon, i.e the boundary has
multiple colors.
 In flood fill algorithm, instead of filling color till you encountered/reached a specific boundary
color, you just fill the pixel with default color.
 It is used in the “bucket” fill tool of paint programs to fill connected, similarly-
colored areas with a different color.

Figure 3-42 Example color boundaries for a boundary-fill procedure

You might also like