written 7.9 years ago by |
Seed Filling:
In this method a particular seed point is picked and we start filling upwards and downwards pixels until boundary is reached. Seed fill method is of two types: Boundary fill and Flood fill.
Boundary fill algorithm:
In Boundary filling a seed point is fixed, and then neighboring pixels are checked to match with the boundary color. Then, color filling is done until boundary is reached. A region may be 4 connected or 8 connected:
Procedure for filling a 4- connected region:
Color is specified by parameter fill color (f-color) and boundary color is specified by boundary color (b-color). getpixel() function gives the color of specified pixel and putpixel() fills the pixel with particular color.
boundary_ fill (x,y, f_color, b_color)
{
If ( getpixel (x,y) != b_color && getpixel (x,y) != f_color)
putpixel(x,y,f_color)
boundary_fill( x+1, y, f_color, b_color);
boundary_fill( x, y+1, f_color, b_color);
boundary_fill( x-1, y, f_color, b_color);
boundary_fill( x, y-1, f_color, b_color);
}
Flood fill algorithm:
There are some cases where the boundary color is different than the fill color. For situations like these Flood fill algorithm is used. Here the process is started in a similar way by examining the colors of neighboring pixels. But instead of matching it with a boundary color a specified color is matched.
Procedure for filling a 8- connected region:
flood_ fill (x,y, old_color, new_color)
{
putpixel(x,y,new_color)
flood_ fill (x+1, y, old_color, new_color)
flood_ fill (x-1, y, old_color, new_color)
flood_ fill (x, y+1, old_color, new_color)
flood_ fill (x, y-1, old_color, new_color)
flood_ fill (x+1, y+1, old_color, new_color)
flood_ fill (x-1, y-1, old_color, new_color)
flood_ fill (x+1, y-1, old_color, new_color)
flood_ fill (x-1, y+1, old_color, new_color)
}
Flood Fill Algorithm | Boundary Fill Algorithm |
---|---|
Flood fill colors an entire area in an enclosed figure through interconnected pixels using a single color | Here area gets colored with pixels of a chosen color as boundary this giving the technique its name |
So, Flood Fill is one in which all connected pixels of a selected color get replaced by a fill color. | Boundary Fill is very similar with the difference being the program stopping when a given color boundary is found. |
A flood fill may use an unpredictable amount of memory to finish because it isn't known how many sub-fills will be spawned | Boundary fill is usually more complicated but it is a linear algorithm and doesn't require recursion |
Time Consuming | It is less time Consuming |
written 6.9 years ago by |
Food Fill Algorithm:-
Sometimes it is required to fill in an area that is not defined within a single colour boundary. In such cases we can fill areas by replacing a specified interior colour instead of searching for a boundary colour. This approach is called a flood-fill algorithm,here we start with some seed and examine the neighbouring pixels. However, here pixels are checked for a specified interior colour instead of boundary colour and they are replaced by new colour. Using either a 4-connected or 8-connected approach we can step through pixel positions until all interior point has been filled.
The following procedure illustrates the recursive method for filling 8-connected region using flood-fill algorithm.
Procedure:-
8-connected flood fill algorithm:-
flood fill (x, y, old_colour, new_colour)
{
if (getpixel (x, y) = old_colour)
{
put-pixel (x, y, new_colour);
flood_fill (x + 1, y, old_colour, new_colour);
flood fill (x - 1, y, old_colour, new_colour);
flood_fill (x, y + 1, old_colour, new_colour);
flood_fill (x, y - 1, old_colour, new_colour);
flood_fill (x + 1. y + 1, old_colour, new_colour);
flood_fill (x — 1, y 1, old_colour, new_colour);
flood_fill (x + 1, y 1, old_colour, new_colour);
flood_fill (x — 1, y + 1, old_colour. new_colour);
}
}
Boundary Fill Algorithm :-
In this method. edges of the polygons are drawn. Then starting with some seed, any point inside the Polygon we examine the neighbouring pixels to check whether the boundary pixel is reached. If boundary pixels are not reached, pixels are highlighted and the process is continued until boundary pixels are reached.
Boundary defined regions may be either 4-connected or 8-connected as shown in the figure (a) and (b). If a region is 4-connected, then every pixel in the region may be reached by a combination of moves in only four directions: left, right, up and down. For an 8-connected region every pixel in the region may be reached by a combination of moves in the two horizontal, two vertical, and four diagonal directions.
In some cases, an 8-connected algorithm is more accurate than the 4-connected algorithm. This is illustrated in figure (c). Here, a 4-connected algorithm produces the partial fill.
The following procedure illustrates the recursive method for filling a 4-connected region with colour specified in parameter fill colour (f_colour) up to a boundary colour specified with parameter boundary colour (b_colour).
Procedure:-
4-connected boundary / edge fill:-
boundary_fill (x, y, f_colour, b_colour)
{
if (getpixel (x, y)! =b_colour && getpixel (x, y)! = f_colour)
{
putpixel (x, y, f_colour);
boundary_fill (x + 1, y, f_colour, b_colour);
boundary_fill (x, y + 1, f_colour, b_colour);
boundary_fill (x -1, y, f_colour, b_colour);
boundary_fill (x, y – 1, f_colour, b_colour);
}
}