# Region Filling Region Filling is the process of

• Slides: 9

Region Filling • Region Filling is the process of “coloring in” a definite image area or region. • Seed Fill Approaches – 2 algorithms: Boundary Fill and Flood Fill – works at the pixel level – suitable for interactive painting applications 168 471 Computer Graphics, KKU. Lecture 7 1

Seed Fill Algorithms: Connectedness • 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) 4 -connected 8 -connected 168 471 Computer Graphics, KKU. Lecture 7 2

Boundary Fill Algorithm • • • Start at a point inside a region Paint the interior outward to the edge The edge must be specified in a single color Fill the 4 -connected or 8 -connected region : 4 -connected fill is faster, but can have problems 168 471 Computer Graphics, KKU. Lecture 7 3

Boundary Fill Algorithm (cont(. void Boundary. Fill 4(int x, int y , int fill, int Boundary) { int current; current = Read. Pixel(x, y); if(current != Boundary && current != fill) { Boundary. Fill 4(x+1, y, fill, Boundary); Boundary. Fill 4(x-1, y, fill, Boundary); Boundary. Fill 4(x, y+1, fill , Boundary); Boundary. Fill 4(x, y-1, fill, Boundary); } } 168 471 Computer Graphics, KKU. Lecture 7 4

• Recursive Boundary fill algorithms may not fill regions correctly if some interior pixels are already displayed in the fill color. • This occurs because the algorithms checks next pixels both for boundary color and fill color. • Encountering a pixel with the fill color can cause a recursive branch to terminate, leaving other interior pixels unfilled. • To avoid this we can first change the color of any interior pixels that are initially set to fill color before applying boundary fill procedure. 168 471 Computer Graphics, KKU. Lecture 7 5

Flood Fill Algorithm • 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 168 471 Computer Graphics, KKU. Lecture 7 6

Flood Fill Algorithm (cont(. void Flood. Fill 4(int x, int y, int fillcolor, int old. Color) { if(Read. Pixel(x, y) == old. Color) { Flood. Fill 4(x+1, y, fillcolor, old. Color); Flood. Fill 4(x-1, y, fillcolor, old. Color); Flood. Fill 4(x, y+1, fillcolor, old. Color); Flood. Fill 4(x, y-1, fillcolor, old. Color); } } 168 471 Computer Graphics, KKU. Lecture 7 7

{ setcolor(RED); rectangle(100, 200, 200); circle(300, 50); setfillstyle(1, YELLOW); floodfill(101, RED); floodfill(301, RED); } 168 471 Computer Graphics, KKU. Lecture 7 8

• Setfillstyle Purpose: setfillstyle is used to set the color and style to be filled in the object using the flood fill method. Syntax: stefillstyle(STYLE, COLOR); Example: setfillstyle(1, RED) • Floodfill Purpose: floodfill function is used to fill the color in the object, object may be circle, rectangle or any other closed image. Syntax: floodfill(x, y, boundary color); Example: floodfill(100, BLUE); 168 471 Computer Graphics, KKU. Lecture 7 9