0% found this document useful (0 votes)
113 views10 pages

Polygon Filling

Polygon filling involves coloring in a defined image area or region. There are two main approaches: seed fill and scanline fill. Seed fill works at the pixel level using algorithms like boundary fill and flood fill, while scanline fill works at the polygon level and has better performance. Boundary fill starts at an interior point and paints outward to the boundary, while flood fill replaces a specified interior color. Scanline fill intersects the scanline with polygon edges and fills between intersection pairs on each scanline from bottom to top. The scanline seed fill algorithm minimizes duplicate pixels by pushing only extreme pixels of unfilled spans onto the stack per scanline.

Uploaded by

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

Polygon Filling

Polygon filling involves coloring in a defined image area or region. There are two main approaches: seed fill and scanline fill. Seed fill works at the pixel level using algorithms like boundary fill and flood fill, while scanline fill works at the polygon level and has better performance. Boundary fill starts at an interior point and paints outward to the boundary, while flood fill replaces a specified interior color. Scanline fill intersects the scanline with polygon edges and fills between intersection pairs on each scanline from bottom to top. The scanline seed fill algorithm minimizes duplicate pixels by pushing only extreme pixels of unfilled spans onto the stack per scanline.

Uploaded by

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

POLYGON Filling

Polygon filling is the process of "coloring in" a definite image area or region. Polygon may be
defined at the pixel or geometric level.
Seed Fill Approaches
– 2 algorithms: Boundary Fill and Flood Fill

– works at the pixel level

– suitable for interactive painting apllications

• Scanline Fill Approaches

– works at the polygon level

– better performance

• Start at a point inside a region

• Paint the interior outward to the edge

• The boundary must be specified in a single color

• Fill the 4-connected or 8-connected region

• 4-connected fill is faster, but can have problems:


• 4-connected region: From a given pixel, the region that you can get to by a series of 4 way
moves (N, S, E and W)

• 8-connected region: From a given pixel, the region that you can get to by a series of 8 way
moves (N, S, E, W, NE, NW, SE, and SW)

void BoundaryFill4(int x, int y,

color newcolor, color boundarycolor)

int current;

current = ReadPixel(x, y);

if(current != boundarycolor && current != newcolor)

BoundaryFill4(x+1, y, newcolor, boundarycolor);

BoundaryFill4(x-1, y, newcolor, boundarycolor);

BoundaryFill4(x, y+1, newcolor, boundarycolor);

BoundaryFill4(x, y-1, newcolor, boundarycolor);

Boundary fill using 8-connected fill method

Input seed_pixel(x, y), fill, boundary


Procedure boundary_fill(x, y, fill, boundary)

If (Get pixel x, y) < > fill and Get Pixel (x, y) < > boundary) then

Set pixel (x, y fill)

boundary_fill (x + 1, y, fill, boundary)

boundary_fill (x, - y, y, fill, boundary)

boundary_fill(x, y + 1, fill boundary)

boundary_fill(x, y- 1, fill boundary)

boundary_fill(x + 1, y + 1, fill boundary)

boundary-fill(x – 1, y + 1, fill boundary)

boundary_fill(x-1, y-1, fill boundary)

boundary_fill(x + 1, y -1, fill boundary)

End of Procedure

• Used when an area defined with multiple color boundaries

• Start at a point inside a region

• Replace a specified interior color (old color) with fill color

• Fill the 4-connected or 8-connected region until all interior points being replaced

void FloodFill4(int x, int y, color newcolor, color oldColor)

{
if(ReadPixel(x, y) == oldColor)

FloodFill4(x+1, y, newcolor, oldColor);

FloodFill4(x-1, y, newcolor, oldColor);

FloodFill4(x, y+1, newcolor, oldColor);

FloodFill4(x, y-1, newcolor, oldColor);

Stack Based Fill Algorithm !


. A stack is a simply an array in which insertion or push of elements take place on the top of previously
inserted elements. If we want to delete or pop an element from the stack then last element which was
pushed onto stack will be popped first. Operations are performed as LIFO(Last In First Out) form.

Stack-based approach

Steps for performed this operations are:

1).Push the starting or seed pixel onto stack.

2).Repeat while stack is not empty.


(a).Delete the starting pixel from the stack and fill the pixel with a fixed color or value.
(b).Check the surrounding pixels around seed pixel, if it is not previously filled with color and not a
boundary pixel, then insert it onto stack otherwise ignored it.
3).End of step 2.

4).Now stack is empty and polygon is filled.

Following is the boundary fill algorithm using 4-connected fill method. It is a stacked-based approach with
fill color specified by ‘fill’ and boundary color specified by ‘boundary’.

or
Boundary fill using 4-connected fill method

Input seed pixel(x, y), fill, boundary

PUSH pixel(x, y)

While (stack not empty)

POP pixel (x, y)

Set pixel (x, y, fill)

if (Get pixel (x +1, y) < > fill and Get pixel (x +,y) < > boundary) then

PUSH pixel (x + 1,y)

If Get pixel(x - 1, y) < >fill and get pixel (x - 1, y) < > boundary) then

PUSH pixel (x- 1,y)

If Get pixel(x, y + 1) < >fill and get pixel (x, y + 1) < > boundary) then

PUSH pixel (x ,y+1)

If Get pixel(x, y -1) < >fill and get pixel (x ,y -1) < > boundary) then

PUSH pixel (x,y-1)

end of while

SCAN Line algorithm:

• Intersect scanline with polygon edges

• Fill between pairs of intersections


• Basic algorithm:
For y = ymin to ymax
1) intersect scanline y with each edge
2) sort intersections by increasing x [p0,p1,p2,p3]
3) fill pairwise (p0->p1, p2->p3, …)

• Intersection is an edge end point

Intersection points: (p0, p1, p2) ???

->(p0,p1,p1,p2) so we can still fill pairwise

->In fact, if we compute the intersection of the scanline with edge e1 and e2 separately, we will get the
intersection point p1 twice. Keep both of the p1.
However, in this case we don’t want to count p1 twice (p0,p1,p1,p2,p3), otherwise we will fill pixels
between p1 and p2, which is wrong

Summary: If the intersection is the ymin of the edge’s endpoint, count it. Otherwise, don’t.

Scan line seed fill algorithm-


Scan Line Seed Fill Algorithm:
In seed filling algorithm, the stack size is very large as in each loop the algorithm
pushes 4 or 8 pixels onto the stack. There are duplicate pixels too which are
recursively propagated making the process slow(more in case of 8 interconnected
pixels). But, the scan line seed filling algorithm minimizes the duplicate pixels, by
pushing on to the stack only one pixel in any uninterrupted unfilled span of pixels
in a single scan line, or a row of pixels in a defined boundary region, that is, is a
closed polygon. Instead of proceeding along 4 or 8 connected pixels, this
algorithm processes in raster pattern, that s, along left to right along each scan
line in the region.

The algorithm flows in following manner:

1). A seed pixel located on the scan line within the area popped from a stack
containing the seed pixel is selected.
2). The line or span containing the seed pixel is filled to its right and left including
the seed pixel itself until the boundary is found.

3). The extreme left and extreme right unprocessed pixel in the span are saved as
x-left & x-right, respectively.

4). The scan line above and below the current scan line are examined in the range
x-left and x-right for any simple crossover. The extreme right pixel in all the
unfilled spans on these scan lines within the same range is marked as a seed pixel
and pushed onto the stack.

The Seed Fill Algorithms assume that at least one pixel is interior to a polygon or region is known. The
algorithm then attempts to find and color or fill all other pixels interior to the region.

Or

Input : Pixel (x,y) as the seed pixel, fill as fill color and boundary as boundary colour

Push pixel (x,y) : Initialse stack with seed pixel

while (stack not empty)

Pop pixel (x,y) : Get the seed pixel

Setpixel (x,y,fill) : Set it to fill color

save x = x : Save the x & y coordinates of seed pixel

save y = y

x = x +1

while (Get pixel (x,y) <> boundary

setpixel (x,y,fill) : Fill the span to the right of the seed pixel

x = x+1

end while

xright = x – 1 : Save the extreme right pixel of the span in the current scan line

x = save x : Again come back to the seed pixel position


x=x–1

while Get pixel (x,y) <> boundary)

Set pixel (x,y, fill) : fill the span to the left of the seed pixel

x=x–1

end while

xleft = x +1 : Save the extreme left pixel of the span in the current xcan line

x = xleft, y = save y + 1

while (x <= x right)

if Get (pixel (x,y) = boundary or Get pixel (x,y) = fill

: check the scan line immediately above the current can


line for filled pixel span or boundary filled span

: Simply cross over such span, if found

x = x+1

else

while (Get pixel (x,y) <> boundary and Get pixel (x,y) <> fill and x <= x right)

x=x+1 : find the extreme right pixel of each unfilled span and
push it onto the stack

end while

Push pixel (x – 1, y)

x = x+1

end if

end while

x = xleft y = save y – 1 : Move to the scanline immediately below the


scanline containing the seed pixel.

while ( <= x right)

check the scan line below and stack pixels accordingly

end while
end while

You might also like