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

Region Filling (RF & FF)

The document discusses two types of image regions - boundary-defined and interior-defined. It then describes boundary-fill and flood-fill algorithms used to color these different region types. Boundary-fill is used for boundary-defined regions, tracing the outline and filling the interior. Flood-fill is for interior-defined regions, filling any connected pixels of the original color. Both algorithms can use 4-connected or 8-connected pixel tracing to handle differently shaped regions.

Uploaded by

Ansuman Mahanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
322 views

Region Filling (RF & FF)

The document discusses two types of image regions - boundary-defined and interior-defined. It then describes boundary-fill and flood-fill algorithms used to color these different region types. Boundary-fill is used for boundary-defined regions, tracing the outline and filling the interior. Flood-fill is for interior-defined regions, filling any connected pixels of the original color. Both algorithms can use 4-connected or 8-connected pixel tracing to handle differently shaped regions.

Uploaded by

Ansuman Mahanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Region Filling

There are two basic types of areas or region


1. Boundary-defined
2. Interior-defined

A Boundary-defined region pixels that mark the boundary or outline of the region have a unique a
color that is not same as the color of the interior pixels.
An Interior-defined region is a collection or patch of same color contigous pixels. Pixels exterior to
the region have different colors.

Boundary Defined Region Interior Defined Region

Boundary-fill Algorithm
Algorithm used for filling boundary-defined regions are generally known as Boundary-fill
algorithm. This is a recursive algorithm that picks with a starting pixel, called seed, inside the
region. This algorithm checks to see whether this pixel is a boundary pixel or already has been
filled. If the answer is no, it fills the pixel and makes a recursive call to itself using each and every
neighbouring pixel as a new seed. If the answer is yes, the algorithm simply returns to its caller.
The procedure accepts as input the coordinates of a seed pixel from the inside the region, a fill color
value and a boundary color value.
This algorithm works elegently on an arbitrarily shaped regionby chasing and filling all non-
boundary pixels that are connected to the seed, either directly or indirectly through a chain of
neighbouring relations. However, a straightforward implementation can consume time and memory
to execute due to potentialy high number of recursive calls, especially when the region is relatively
large.
When we consider neighbours to the left, right, top and bottom only this method is called 4-
connected method whereas when the four diagonal pixels are also included as neighbours, this
method is called 8-connected method.

Algorithm
void boundary_fill4(int x, int y, int fill_color, int boundary_color)
{
int c;
c=getpixel (x, y);
if ((c!=fill_color) && (c!=boundary_color))
{
setpixel(x, y, fill_color);
boundary_fill4(x+1, y, fill_color, boundary_color);
boundary_fill4(x-1, y, fill_color, boundary_color);
boundary_fill4(x, y+1, fill_color, boundary_color);
boundary_fill4(x, y-1, fill_color, boundary_color);
}
There is a problem with this technique. Consider the case as shown below where we tried to fill the
entire region. Here, the image is filled only partially. In such cases, 4-connected pixels technique
cannot be used.

8-Connected Polygon

In this technique 8-connected pixels are used as shown in the figure. We are putting pixels above,
below, right and left side of the current pixels as we were doing in 4-connected technique.
In addition to this, we are also putting pixels in diagonals so that entire area of the current pixel is
covered. This process will continue until we find a boundary with different color.
void boundary_fill8(int x, int y, int fill_color, int boundary_color)
{
int c;
c=getpixel (x, y);
if ((c!=fill_color) && (c!=boundary_color))
{
setpixel(x, y, fill_color);
boundary_fill8(x+1, y, fill_color, boundary_color);
boundary_fill8(x-1, y, fill_color, boundary_color);
boundary_fill8(x, y+1, fill_color, boundary_color);
boundary_fill8(x, y-1, fill_color, boundary_color);
boundary_fill8(x+1, y+1, fill_color, boundary_color);
boundary_fill8(x-1, y+1, fill_color, boundary_color);
boundary_fill8(x-1, y-1, fill_color, boundary_color);
boundary_fill8(x+1, y-1, fill_color, boundary_color);
}

Flood-fill Algorithm
Algorithm used for filling interior-defined regions are generally known as Flood fill algorithm.
This is a recursive algorithm that picks with a starting pixel, called seed, inside the region. This
algorithm checks to see whether this pixel has the region’s original color. If the answer is yes, it fills
the pixel with the fill color and makes a recursive call to itself using each and every neighbouring
pixel as a new seed. If the answer is no, it simply returns to its caller.
This procedure accepts as input the coordinates of a seed pixel from inside the region, a fill color
value and a original color value.
It is particularly useful when the region has no uniformly colored boundary.
When we consider neighbours to the left, right, top and bottom only this method is called 4-
connected method whereas when the four diagonal pixels are also included as neighbours, this
method is called 8-connected method.

void floodfill4(int x, int y, int fill_Color, int original_Color)


{
if(getpixel(x,y)== original_Color)
{
putpixel(x,y, fill_Color);
floodfill4(x+1,y, fill_Color, original_Color);
floodfill(x-1,y, fill_Color, original_Color);
floodfill(x,y+1, fill_Color, original_Color);
floodfill(x,y-1, fill_Color, original_Color);
}
}
There is a problem with this technique. Consider the case as shown below where we tried to fill the
entire region. Here, the image is filled only partially. In such cases, 4-connected pixels technique
cannot be used.

8 Connected Polygon

void floodfill8(int x,int y,int fill_Color, int original_Color)


{
if(getpixel(x,y)== original_Color)
{
putpixel(x,y, fill_Color);
floodfill8(x+1,y, fill_Color, original_Color);
floodfill8(x-1,y, fill_Color, original_Color);
floodfill8(x,y+1, fill_Color, original_Color);
floodfill8(x,y-1, fill_Color, original_Color);
floodfill8(x+1,y+1, fill_Color, original_Color);
floodfill8(x-1,y+1, fill_Color, original_Color);
floodfill8(x-1,y-1, fill_Color, original_Color);
floodfill8(x+1,y-1, fill_Color, original_Color);
}
}

Flood-fill algorithm is particularly useful when the region has no uniformly colored boundary. On
the other hand, a region that has a well defined boundary but itself multiple colored would be better
handled by the boundary-fill algorithm.

You might also like