Polygon Area filling algorithm-recursive Seed Filling Algorithm

Source: Internet
Author: User

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

The area filling algorithm is a very important algorithm in the computer graphics field. Area filling gives the boundary of a region (or it can have no boundary but only a specified color ), all pixel units within the boundary range must be modified to the specified color (or pattern filling ). Polygon fill is the most commonly used in Area filling. In this article, we will discuss several Polygon Area filling algorithms.

I. Seed Filling)

If the area to be filled is given in the form of image metadata, seed filling is usually used for area filling. The seed filling algorithm needs to provide the area of the image data and a point in the area. This algorithm is suitable for image filling in interactive ways, and is not suitable for automatic processing and judgment by computers. Based on the border definition method of the image area and the color modification method of the point, seed filling can be subdivided into several categories, such as the Injection Filling Algorithm (flood fill algorithm) boundary fill algorithm and scanning seed filling algorithm improved to reduce the number of recursion and pressure stacks.

The core of all seed filling algorithms is actually a recursive algorithm, which starts from a specified seed point, searches for each direction, and processes each pixel until it encounters a boundary, various Seed Filling algorithms differ only in the way of processing colors and boundaries. Before introducing the seed filling algorithm, we first introduce two concepts: 4-Unicom algorithm and 8-Unicom algorithm ". Since the search involves the search direction problem, starting from any point in the area, if you only search for any pixel in the area through the top, bottom, left and right directions, the area filled with this method is called the four-connected domain, and this filling method is called the "4-Unicom algorithm ". If you start from any point in the area and reach any pixel in the area in the upper, lower, left, right, upper right, upper left, lower left, and lower right directions, the filling area of this method is called the eight connected domain, and this filling method is called the "8-connected algorithm ". 1 (a). Assume that the blue point in the center is the current processing point. If it is a "4-Unicom algorithm", only search for the four points that process the surrounding blue logo, for the "8-China Unicom algorithm", in addition to processing the four blue mark points on the top, bottom, left, and right, it also searches for the points that process the four red marks. The filling effects of the two search algorithms are shown in 1 (B) and 1 (c) respectively. If the filling starts from the yellow point, the "4-Unicom algorithm" 1 (B) as shown in the following figure, only the area in the lower-left corner is searched, and the area in the lower-left corner and upper-right corner is filled as shown in 1 (c.

Figure (1) filling effect of "4-China Unicom" and "8-China Unicom"

 

Because of the filling effect of Figure 1, the "8-Unicom algorithm" must be better than the "4-Unicom algorithm". You should select the UNICOM Search Method Based on the application environment and actual needs, in many cases, only the "4-Unicom algorithm" can get the correct results.

1.1 injection and Filling Algorithm (flood fill algorithm)

The Injection Filling Algorithm does not particularly emphasize the border of a region. It only replaces the Specified Color points in all connected areas with another color from the specified position to achieve the filling effect. The Injection Filling Algorithm can realize color replacement and other functions, which is widely used in image processing software. The implementation of the Injection Filling Algorithm is very simple. The core is recursion and search. The following is an implementation of the Injection Filling 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 recursive search for eight Unicom directions. The secret is defined in direction_8:

15 typedef struct tagdirection

16 {

17 int x_offset;

18 int y_offset;

19} direction;

79 direction direction_8 [] = {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0 }, {1,-1}, {0,-1}, {-1,-1 }};

This is a commonly used technique in search algorithms. You do not need to describe it too much. In fact, you only need to replace it with the following dire_4 _4 definition, you can change the algorithm to four Unicom direction filling algorithms:

80 direction dire_4 _4 [] = {-1, 0}, {0, 1}, {1, 0}, {0,-1 }};

Figure 2 shows the filling effect of "4-Unicom" and "8-Unicom" implemented by this algorithm:

Figure (2) injection and Filling Algorithm Implementation

1.2 boundary Fill Algorithm)

The essence of the boundary filling algorithm is the same as that of the Injection Filling Algorithm. They are both recursive and search. The difference lies only in the confirmation of the boundary, that is, the ending condition of recursion is different. The Injection Filling Algorithm does not have a boundary concept, but replaces the specified color in the China Unicom region. The boundary Filling Algorithm emphasizes the existence of the boundary, as long as it is a vertex in the boundary, no matter what color it is, are replaced with the specified color. The border filling algorithm is also widely used. The "paint bucket" function in the drawing software is an example of the border filling algorithm. The following is an implementation of the border Filling Algorithm:

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

178 {

179 int curcolor = getpixelcolor (x, y );

180 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 more information about dire8 _8, see the previous section, figure 3 shows the filling effect of "4-Unicom" and "8-Unicom" implemented by this algorithm (the point with the color value of 1 is the specified boundary ):

Figure (3) Border Filling Algorithm Implementation

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.