Polygon Region filling algorithm--recursive seed filling algorithm

Source: Internet
Author: User

http://blog.csdn.net/orbit/article/details/7323090

The plane area filling algorithm is an important algorithm in the field of computer graphics, which gives the boundary of an area (or the absence of a boundary, but only the specified color), which requires that all the pixel elements within the boundary range be modified to the specified color (or it may be a hatch). Polygon fills are most commonly used in area fills, and we discuss several polygon area filling algorithms in this paper.

Seed filling algorithm (seed Filling)

If the area you want to populate is given as image metadata, you typically use the seed fill algorithm (seed Filling) for area fills. The seed filling algorithm needs to give the area of image data, and a point within the region, which is suitable for the image filling operation of human-computer interaction, and is not suitable for automatic processing and judging of fill color. The seed fill can be subdivided into several categories, such as the Injection fill algorithm (Flood fill algorithm) and the boundary Fill algorithm (boundary fill), depending on how the image area boundary is defined and how the color of the point is modified. algorithm) and improved scanning line seed filling algorithms to reduce recursion and stack counts, and more.

The core of all the seed filling algorithms is actually a recursive algorithm, which starts from the specified seed point, searches in all directions, processes pixels by pixel, until a boundary is encountered, and the various seed filling algorithms differ only in how colors and boundaries are handled. Before we begin to introduce the seed filling algorithm, we first introduce two concepts, namely "4-Unicom algorithm" and "8-unicom algorithm". Since the search is related to the direction of the search, from any point in the region, if only through the top, bottom, left, and right four directions to search for any pixel in the region, then the area filled with this method is called the four connected domain, this fill method is called "4-unicom algorithm." If you start from any point in the region, through the upper, lower, left, right, top left, bottom left, right, upper and lower right all eight directions to reach any pixel within the area, then this method fills the area is called the eight connected domain, this fill method is called "8-unicom algorithm." 1 (a), assuming that the center's Blue point is the current processing point, if it is a "4-unicom algorithm", then only the four points to deal with the surrounding blue identification, if the "8-Unicom algorithm" In addition to processing the top, bottom, left, and right four blue identified points, but also the processing of four red identified points. The fill effects of the two search algorithms are shown in 1 (b) and Figure 1 (c), respectively. If all are filled from the yellow point, then "4-Unicom algorithm" 1 (b) is only searched for the area that fills the lower left corner, while the "8-Unicom algorithm" is 1 (c), the lower-left and upper-right corner of the area are populated.

Figure (1) "4-Unicom" and "8-unicom" fill effect

Not just because the fill effect of Figure 1 is that "8-unicom algorithm" is better than the "4-unicom algorithm", should be based on the application environment and the actual needs of the choice of Unicom search mode, in many cases, only the "4-unicom algorithm" to get the correct results.

1.1 Injection Fill algorithm (Flood fill algorithm)

The injection-fill algorithm does not specifically emphasize the boundary of a region, it only starts at the specified position, and replaces the point of a specified color in all the unicom areas with another color to achieve the fill effect. The injection filling algorithm can realize the function of color substitution, which has been widely used in image processing software. The implementation of the injection fill algorithm is very simple, the core is recursion and search, the following is an implementation of the injection fill algorithm:

164 void Floodseedfill (int x, int y, int old_color, int new_color)

165 {

166 if (Getpixelcolor (x, y) = = Old_color)

167 {

168 Setpixelcolor (x, y, New_color);

169 for (int i = 0; i < count_of (direction_8); i++)

170 {

171 Floodseedfill (x + Direction_8[i].x_offset,

172 y + direction_8[i].y_offset, old_color, New_color);

173}

174}

175}

The For loop implements a recursive search to 8 unicom directions, the secret is defined in Direction_8:

typedef struct TAGDIRECTION

16 {

X_offset int;

Y_offset int;

}direction;

DIRECTION direction_8[] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, -1},{0, 0}, {-1,-1}};

This is the search algorithm commonly used techniques, do not need to do too much to explain, in fact, as long as it is replaced with the following direction_4 definition, you can change the algorithm to 4 of the unicom direction filling algorithm:

DIRECTION direction_4[] = {{-1, 0}, {0, 1}, {1, 0}, {0,-1}};

Figure 2 is the application of this algorithm to achieve the "4-unicom" and "8-unicom" filling effect:

Figure (2) Injection fill algorithm implementation

1.2 Boundary fill algorithm (Boundary fill algorithm)

The boundary filling algorithm is the same as the essence of the injected filling algorithm, which is recursive and search, the difference is only the confirmation of the boundary, that is, the end condition of recursion is not the same. The injection-fill algorithm has no boundary concept, but replaces the specified color in the Unicom region, while the boundary filling algorithm emphasizes the existence of the boundary, as long as the point within the boundary, regardless of the color, is replaced by the specified color. The boundary filling algorithm is also widely used in the application, and the "Paint bucket" function in drawing software is an example of the boundary filling algorithm. The following is an implementation of the Boundary fill algorithm:

177 void Boundaryseedfill (int x, int y, int new_color, int boundary_color)

178 {

179 int curcolor = Getpixelcolor (x, y);

if (curcolor! = Boundary_color)

181 && (Curcolor! = New_color))

182 {

183 Setpixelcolor (x, y, New_color);

184 for (int i = 0; i < count_of (direction_8); i++)

185 {

186 Boundaryseedfill (x + Direction_8[i].x_offset,

187 y + direction_8[i].y_offset, new_color, Boundary_color);

188}

189}

190}

For a description of the Direction_8, refer to the previous section, figure 3 is the implementation of the algorithm using the "4-unicom" and "8-unicom" fill effect (where the color value is 1 of the point is the specified boundary):

Figure (3) Implementation of boundary filling algorithm

Polygon Region filling algorithm--recursive seed filling algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.