0
3.9kviews
Flood fill algorithm.

Mumbai university > Comp > SEM 4 > Computer Graphics

Marks: 5M

Year: May 2015 , Dec 2015

1 Answer
0
40views
  1. Sometimes we want to fill in (or recolor) an area that is not defined within a single color boundary.

  2. Figure below shows an area bordered by several different color regions.

enter image description here

  1. We can paint such areas by replacing a specified interior color instead of searching for a boundary color value.

  2. This approach is called a flood-fill algorithm.

  3. 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.

  4. 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.

  5. Using either a 4-connected or 8-connected approach, we then step through pixel positions until all interior points have been repainted.

  6. The following procedure flood fills a 4-connected region recursively, starting from the input position.

  7. Flood Fill Function:

    void floodFill4 ( int x, int y, intfillColor , intoldColor)

    {

    i f (getpixel (x, y) == oldcolor)

    {

    setcolor ( fillColor ) ;

    setpixel (x, y ) :

    floodFill4 (x + l, y, fillColor, oldColor):

    floodfill4 (x-1, y, fillcolor, oldcolor);

    floodPill4 (x, y + l, fillcolor, oldcolor);

    floodFill4 (x, y-1, fillColor, oldcolor);

    }

    }

  8. We can modify procedure f loodFill4 to reduce the storage requirements of the stack by filling horizontal pixel spans..

  9. In this approach, we stack only the beginning positions for those pixel spans having the value oldColor.

  10. Starting at the first position of each span, the pixel values are replaced until a value other than oldColor is encountered.

Please log in to add an answer.