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

Lecture4 Polygon Fill Algorithms - معدل

The document discusses algorithms for filling polygons, including boundary fill and flood fill. It defines different types of polygons and describes seed filling methods. It provides details on the basic boundary fill algorithm, including pseudocode, and provides examples of applying boundary fill with 4-connected and 8-connected pixels.

Uploaded by

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

Lecture4 Polygon Fill Algorithms - معدل

The document discusses algorithms for filling polygons, including boundary fill and flood fill. It defines different types of polygons and describes seed filling methods. It provides details on the basic boundary fill algorithm, including pseudocode, and provides examples of applying boundary fill with 4-connected and 8-connected pixels.

Uploaded by

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

Polygon Fill

Algorithms
Definition
• Polygon is a closed figure with many vertices and edges (line segments),
and at each vertex exactly two edges meet and no edge crosses the other.

Types of Polygon:
• Regular polygons
» No. of edges equal to no. of angles.
• Convex polygons
» Line generated by taking two points in the polygon must lie
within the polygon.

• Concave polygons
» Line generated by taking two points in the polygon may lie
outside the polygon.
Polygon Filling
• Polygon filling: Highlights all pixels inside the polygon.
Two methods:
Boundary Fill
• Seed Fill
Flood Fill
• Scan Line

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.
Basic Filling Algorithm
The basic filling algorithm is commonly used in
interactive graphics packages, where the user
specifies an interior point of the region to be filled.

4-connected pixels

4
Types of Basic Filling Algorithms
 Boundary Fill Algorithm
◦ For filling a region with a single boundary color.
◦ Condition for setting pixels:
 Color is not the same as border color
 Color is not the same as fill color
 Flood Fill Algorithm
◦ For filling a region with multiple boundary colors.
◦ Here we don’t have to deal with boundary color
◦ Condition for setting pixels:
 Coloring of the pixels of the polygon is done with
the fill color until we keep getting the old interior
color.
5
Boundary Fill Algorithm
• An approach to area filling is to start at a point
inside a region and paint the interior outward toward
the boundary.
• If the boundary is specified in a single color, the fill
algorithm processed outward pixel by pixel until the
boundary color is encountered.
• A boundary-fill procedure accepts as input the
coordinate of the interior point (x, y), a fill color,
and a boundary color.

6
Boundary Fill Algorithm
The following steps illustrate the idea of the
recursive boundary-fill algorithm:

1. Start from an interior point.


2. If the current pixel is not already filled and if it is
not an edge point, then set the pixel with the fill
color, and store its neighboring pixels (4 or 8-
connected) in the stack for processing. Store only
neighboring pixel that is not already filled and is not
an edge point.
3. Select the next pixel from the stack, and continue
with step 2.

7
Boundary Fill Algorithm

The order of pixels that should be added to stack


using 4-connected is above, below, left, and
right. For 8-connected is above, below, left, right,
above-left, above-right, below-left, and below-
right.

8
Area Fill Algorithm
• There are two methods for processing
neighboring pixels from a current point.
1. Four neighboring points.
– These are the pixel positions that are right, left,
above, and below the current pixel.
– Areas filled by this method are called 4-
connected.
Area Fill Algorithm
2. Eight neighboring points.
– This method is used to fill more complex figures.
– Here the set of neighboring points to be set
includes the four diagonal pixels, in addition to the
four points in the first method.
– Fill methods using this approach are called 8-
connected.
Boundary Fill Algorithm (Code)
void boundaryFill(int x, int y,
int fillColor, int borderColor)
{
getPixel(x, y, color);# fetch color
if ((color != borderColor)
&& (color != fillColor)) {
setPixel(x,y);# to give color on particular
pixel
boundaryFill(x+1,y,fillColor,borderColor);
boundaryFill(x-1,y,fillColor,borderColor);
boundaryFill(x,y+1,fillColor,borderColor);
boundaryFill(x,y-1,fillColor,borderColor);
}
}

11
Boundary Fill Algorithm
4-connected (Example) ‫امل‬+‫ك‬

Start Position

12
Boundary Fill Algorithm
4-connected (Example)

3
2
1
1
2 3

13
Boundary Fill Algorithm
4-connected (Example)

4
2
1
1 4
2

14
Boundary Fill Algorithm
4-connected (Example)

2
1

1
2

15
Boundary Fill Algorithm
4-connected (Example)

5
1

5 1

16
Boundary Fill Algorithm
4-connected (Example)

17
Boundary Fill Algorithm
4-connected (Example)

18
Boundary Fill Algorithm
8-connected (Example)

Start Position

19
Boundary Fill Algorithm
8-connected (Example)

5
4
4 1 5 3
2 3 2
1

20
Boundary Fill Algorithm
8-connected (Example)

6
6 4
4 1 3
2 3 2
1

21
Boundary Fill Algorithm
8-connected (Example)

8
7 8
7
4
4 1
3
2 3
2
1

22
Boundary Fill Algorithm
8-connected (Example)

12
11 9 12 11
7 10 10
9
4 1 7
2 3 4
3
2
1

23
Boundary Fill Algorithm
8-connected (Example)

11
11 9
10
7 10
9
7
4 1
4
2 3
3
2
1

24
Boundary Fill Algorithm
8-connected (Example)

9 10
7 10 9
7
4 1 4
2 3 3
2
1

25
Boundary Fill Algorithm
8-connected (Example)

9
9
7
7
4
4 1
3
2 3
2
1

26
Boundary Fill Algorithm
8-connected (Example)

7 7
4
4 1 3
2 3 2
1

27
Boundary Fill Algorithm
8-connected (Example)

4
4 1 3
2 3 2
1

28
Boundary Fill Algorithm
8-connected (Example)

3
1
2
2 3
1

29
Boundary Fill Algorithm
8-connected (Example)

1 2
2 1

30
Boundary Fill Algorithm
8-connected (Example)

1
1

31
Using the Boundary Fill Algorithm to fill
the region in below figure using 4-
connected
8-connected (Example)

32
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.
Flood Fill Algorithm
Sometimes we want to fill in (recolor) an area that is
not defined within a single color boundary.
We 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.

34
Flood Fill Algorithm
We start from a specified interior pixel (x, y) and
reassign all pixel values that are currently set to a
given interior color with the desired fill color.
If the area has more than one interior color, we
can first reassign pixel values so that all interior
pixels have the same color.
Using either 4-connected or 8-connected
approach, we then step through pixel positions
until all interior pixels have been repainted.

35
Area 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.
Area Fill Algorithm
• 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.
Flood Fill Algorithm
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

1/2 - 39
Flood Fill
• 1. Region is a patch of like-colored pixels
• 2. A seed pixel is set and a range of colors is
defined
• 3. Check if the pixel is in the color range
• 4. If yes, fill it and make the neighbors news
seeds

1/2 - 40
1/2 - 41
1/2 - 43
1/2 - 44
Improving seed fill
Shortcomings of boundary fill and flood fill:
• Time
• Large number of recursive calls
• Pixels might be considered more than once
(test if set, test if inside)
• Memory
• Don’t know how big the fill list should be
• Could be all of the image pixels
• More if our algorithm allows us to consider a pixel more than once

1/2 - 45
Boundary-Fill Flood-Fill Basis for
Algorithm Algorithm comparison
Basic

Painting process

Memory
consumption

Speed

Algorithm
Complexity

Thank You
46

You might also like