14
Pattern Recognition 38 (2005) 2564 – 2577 www.elsevier.com/locate/patcog A new and fast contour-filling algorithm Mingwu Ren , WankouYang, JingyuYang Department of Computer Science, Nanjing University of Science and Technology, Nanjing 210094, China Received 14 September 2004; accepted 27 April 2005 Abstract Contour filling is one of the most common problems in image and graphics processing. The executing speed of the contour filling is vital, especially for a real-time system. This paper studies the advantages and disadvantages of the conventional filling algorithms, analyzes their original ideas, and proposes a new filling algorithm. The new algorithm presents a very simple idea for finding seeds automatically, and limits the scan within the filling regions. Repeated experiments have proven that with the new algorithm, any complex inner and outer contours can be filled with higher speed. 2005 Pattern Recognition Society. Published by Elsevier Ltd. All rights reserved. Keywords: Contour filling; Chain code; Filling algorithm; Region filling; Contour tracing 1. Introduction Region filling is a basic and important problem in the field of image processing and computer graphics [1] that has been tackled for many years. Image processing often uses this technique to complete the image analysis, such as for obtaining common regions of two objects, obtaining the rela- tion between two regions, or restoring a binary image. Com- puter graphics often uses region filling to paint an area with a certain color. At present, with the fast-evolving computer hardware, it is important to come out with a more efficient filling algorithm. In this paper we shall study the conven- tional algorithms and present a more credible and sim- pler one, to rectify the shortcomings of the conventional algorithms and fit the demand of the increasingly time- consuming applications of real-time image processing sys- tem. This paper analyzes the principles of conventional filling algorithms, and points out their advantages and Corresponding author. Tel.: +86 25 84315751816; fax: +86 25 843 155 10. E-mail address: [email protected] (M. Ren). 0031-3203/$30.00 2005 Pattern Recognition Society. Published by Elsevier Ltd. All rights reserved. doi:10.1016/j.patcog.2005.04.017 disadvantages in Section 2. A new filling algorithm with a higher filling speed is proposed in Section 3, and is test-run in Section 4. Some conclusions are given in Section 5. 2. Problems review The conventional filling algorithms can be classified into two categories: one is based on parity check [2] and the other is based on seeds growing [3,4]. Filling by parity check assumes that a straight line intersects a closed con- tour at even number of times and obtains line segments. If the number of the segments is odd, these segments are then interior and should be filled. The shortcoming of this kind of filling algorithm surfaces when the number of the line segments cannot be obtained correctly. In particular, when the resolution of sampling is not high enough, two different points could be mapped onto the same pixel. Therefore, a wrong number of the segments leads to a wrong filling result. On the other hand, filling by seeds assumes that the interior pixels are the ones that can reach the contour lines from the seeds without crossing them. To finish the filling, one simply has to paint these interior pixels. However, it can be very difficult sometimes to find the seeds automatically,

A new and fast contour-filling algorithm

Embed Size (px)

Citation preview

Page 1: A new and fast contour-filling algorithm

Pattern Recognition 38 (2005) 2564–2577www.elsevier.com/locate/patcog

A newand fast contour-filling algorithm

Mingwu Ren∗, WankouYang, JingyuYangDepartment of Computer Science, Nanjing University of Science and Technology, Nanjing 210094, China

Received 14 September 2004; accepted 27 April 2005

Abstract

Contour filling is one of the most common problems in image and graphics processing. The executing speed of the contourfilling is vital, especially for a real-time system. This paper studies the advantages and disadvantages of the conventional fillingalgorithms, analyzes their original ideas, and proposes a new filling algorithm. The new algorithm presents a very simple ideafor finding seeds automatically, and limits the scan within the filling regions. Repeated experiments have proven that with thenew algorithm, any complex inner and outer contours can be filled with higher speed.� 2005 Pattern Recognition Society. Published by Elsevier Ltd. All rights reserved.

Keywords:Contour filling; Chain code; Filling algorithm; Region filling; Contour tracing

1. Introduction

Region filling is a basic and important problem in thefield of image processing and computer graphics[1] that hasbeen tackled for many years. Image processing often usesthis technique to complete the image analysis, such as forobtaining common regions of two objects, obtaining the rela-tion between two regions, or restoring a binary image. Com-puter graphics often uses region filling to paint an area witha certain color. At present, with the fast-evolving computerhardware, it is important to come out with a more efficientfilling algorithm. In this paper we shall study the conven-tional algorithms and present a more credible and sim-pler one, to rectify the shortcomings of the conventionalalgorithms and fit the demand of the increasingly time-consuming applications of real-time image processing sys-tem.This paper analyzes the principles of conventional

filling algorithms, and points out their advantages and

∗ Corresponding author. Tel.: +862584315751816;fax: +862584315510.

E-mail address:[email protected](M. Ren).

0031-3203/$30.00� 2005 Pattern Recognition Society. Published by Elsevier Ltd. All rights reserved.doi:10.1016/j.patcog.2005.04.017

disadvantages in Section 2. A new filling algorithm with ahigher filling speed is proposed in Section 3, and is test-runin Section 4. Some conclusions are given in Section 5.

2. Problems review

The conventional filling algorithms can be classified intotwo categories: one is based on parity check[2] and theother is based on seeds growing[3,4]. Filling by paritycheck assumes that a straight line intersects a closed con-tour at even number of times and obtains line segments. Ifthe number of the segments is odd, these segments are theninterior and should be filled. The shortcoming of this kindof filling algorithm surfaces when the number of the linesegments cannot be obtained correctly. In particular, whenthe resolution of sampling is not high enough, two differentpoints could be mapped onto the same pixel. Therefore,a wrong number of the segments leads to a wrong fillingresult. On the other hand, filling by seeds assumes that theinterior pixels are the ones that can reach the contour linesfrom the seeds without crossing them. To finish the filling,one simply has to paint these interior pixels. However, it canbe very difficult sometimes to find the seeds automatically,

Page 2: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2565

Fig. 1. Definitions of chain codes.

and a search for all interior pixels may be time and memoryconsuming.In the field of computer graphics, contour is usually de-

scribed as a list of coordinated points. Thus, its filling al-gorithms are not much different from the principles of theparity check or the seeds growing as mentioned above. Butin the field of digital image processing, contour is usuallydescribed by chain codes[5,6] and thus, its filling algo-rithm [7–11] is very different from that in computer graphicsfield. Algorithms in[7–9] comply with the principle of par-ity check by analyzing the attributes of the contour pixels’chain code, classifying contour points into isolated points,marking points, and skipping points. The isolated points andskipping points are temporarily neglected during the fillingin order to present the occurrence of the wrong number ofline segments. Only the marking points are used for fillingby the parity check. Algorithms in[10,11] comply with theprinciple of the seeds growing, by analyzing the attributesof the contour pixels’ chain code to find seeds automati-cally. The shortcomings of[7–11] will be analyzed later inthe paper.To make this paper more comprehensive, some of the

terms used in this paper are explained here. A contour isdescribed by(x0, y0)c0c1 . . . cn−1, (x0, y0) being the trac-ing beginning point,n the quantity of chain codes,ci the ithchain code, 0�ci�7 as defined inFig. 1 by Freeman[5],W andH the width and the height of the circum-rectangleof the region enclosed by a contour line, respectively,Areabeing the area of the region enclosed by a contour, andArea�W ×H . By convention, both outer and inner contourlines are 8-connected, and the outer contour is clockwise,and the inner contour is counterclockwise.Tables 1–5depicta combination of two consecutive chain codes; “x” standsfor the inexistent combination in them. In all the figures,filled regions are displayed in white and the background isin black.

Table 1CAI_LUT

Ci Ci+1

0 1 2 3 4 5 6 7

0 0 1 1 1 1 x x 01 0 1 1 1 1 0 x 02 x 1 1 1 1 0 0 x3 x 1 1 1 1 0 0 04 1 x x 0 0 1 1 15 1 0 x 0 0 1 1 16 1 0 0 x x 1 1 17 1 0 0 0 x 1 1 1

Table 2CHANG_LUT

Ci Ci+1

0 1 2 3 4 5 6 7

0 2 1 1 1 1 x x 21 2 1 1 1 1 0 x 22 x 1 1 1 1 0 0 x3 x 1 1 1 1 0 0 04 1 x x 2 2 1 1 15 1 0 x 2 2 1 1 16 1 0 0 x x 1 1 17 1 0 0 0 x 1 1 1

Table 3REN_LUT

Ci Ci+1

0 1 2 3 4 5 6 7

0 0 0 0 0 0 x x 11 0 0 0 0 0 0 x 12 x 0 0 0 0 0 0 x3 x 0 0 0 0 0 0 04 0 x x 0 0 0 0 05 0 0 x 1 1 1 1 16 0 0 0 x x 1 1 17 0 0 0 0 x 1 1 1

2.1. CAI

One filling algorithm by parity check[7] (CAI) based onchain code was proposed earlier. In this algorithm, whethera contour point(xi , yi) is a marking point for filling or anisolated point can be decided by its reaching codeci andleaving codeci+1, according to the fact that there are twochain codes connecting a certain point in the contour. Theoriginal idea of the algorithm is to give up the isolated pointstemporarily and retain only the marking points for filling

Page 3: A new and fast contour-filling algorithm

2566 M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577

Table 4TANG_LUT

Ci Ci+1

0 1 2 3 4 5 6 7

0 0 1 1 1 1 x x 01 0 1 1 1 1 0 x 02 x 1 1 1 1 0 0 x3 x 1 1 1 1 0 0 04 −1 x x 0 0 −1 −1 −15 −1 0 x 0 0 −1 −1 −16 −1 0 0 x x −1 −1 −17 −1 0 0 0 x −1 −1 −1

Table 5OFFSETY_LUT

Ci 0 1 2 3 4 5 6 7

dy 0 −1 −1 −1 0 1 1 1dx 1 1 0 −1 −1 −1 0 1

by parity check. After the filling of the marking points isfinished, the isolated points are added to obtain the finalfilling result. The key process in this method is to determinewhether a contour pointpi is an isolated point, which is doneby analyzing the combination ofci andci+1 as demonstratedin Table 1. “0” representspi being an isolated point whereas“1” representspi being a marking point, and “x” representsthe inexistent combination ofci andci+1.Since a point may be on a contour more than once,

that is, a contour point may be classified as a markingpoint or an isolated point multiple times, the algorithm’simplementation uses two equal-sized imagesA and B.Both A and B are initialized to 0.A is used to recordthe marking points andB is used to record the isolatedpoints. Assume a contour point(x, y), its values inAand B being a(x, y) and b(x, y), respectively, and itsreaching code and leaving code beingci and ci+1, re-spectively; then, whether this point is a marking point oran isolated point can be determined by the rule below.

If CAI_LUT[ci ][ci+1] equal to1 then{ If a(x, y) equal to1 then{ a(x, y)= 0; b(x, y)= 1; }

elsea(x, y)= 1;}elseb(x, y)= 1;b(x, y)= 1 means that(x, y) is an isolated point anda(x, y)= 1 means that(x, y) is a marking point.

It may seem thatB is memory-consuming. In fact,B ismerelyW ×H bits and can be mapped onto one of the bitplanes ofA. The major advantage of this algorithm is itshigh speed when restoring a whole binary image.Figs. 2–7demonstrate the algorithm. Before filling the region enclosed

Fig. 2. Original image.

Fig. 3. Two contours.

by the contour, it fills the whole image to the backgroundcolor first. Fig. 2 shows the original binary image.Fig. 3demonstrates its two contours, one outer and the other in-ner.� represents the tracing beginning point of the contourand thus, the outer contour is(1,1)6666666000010222222445434 and the inner contour is(1,3)1007666654432222.Fig. 4 shows the marking points, andFig. 5 shows the iso-lated points.Fig. 6 shows the filling result ofFig. 4, andFig. 7 shows the final filling result after addingFigs. 5and6.The disadvantage of this algorithm is that it visits un-

wanted pixels out of the contour during the filling, and thuswastes time. To fill in a singular contour, it must scan ev-ery pixel in the circum-rectangle of the region in order to

Page 4: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2567

Fig. 4. ImageA.

Fig. 5. ImageB.

find the marking points in imageA. Sometimes, the circum-rectangle of sizeW × H is larger than the region of sizeArea. This problem seems to be solved by analyzing thechain codes to obtain all the marking points. However, as itcomplies with the parity check filling, even it knows whichpoint is a marking point; it cannot determine whether themarking point is a parity number or an odd number. Thescan of the circum-rectangle is therefore unavoidable. Thisis the greatest shortcoming of CAI.

2.2. CHANG

Another filling algorithm by parity check[8,9] (CHANG)was proposed later than CAI. It extends the classification

Fig. 6. Filling A.

Fig. 7. AddingB.

of an isolated point defined in CAI. It classifies an isolatedpoint in CAI into singular points or skipping points, andkeeps the marking points in CAI, as illustrated inTable 2;“0” represents a singular point, “1” a marking point, and“2” a skipping point. An ordered array is used to recordcoordinates of the marking points and the singular points.These coordinates are ranked by rows (coordinatey) firstand columns (coordinatex) second in this array. Choos-ing the odd-numbered line segments from this array to fillthen completes the filling. In order for this array to sat-isfy the rule of parity check, a singular point (“0”) must berecorded in this array twice whereas a marking point (“1”)must be recorded only once, and skipping points (“2”) arenot recorded. Once a contour point is classified as a singular

Page 5: A new and fast contour-filling algorithm

2568 M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577

Fig. 8. Original image.

Fig. 9. An outer contour.

point, it will never be classified as a marking point when itis encountered again.The major advantage of this algorithm over CAI is that

CHANG does not need to scan pixels out of the region sinceit uses an ordered array to record the marking points andthe singular points whereas CAI uses an image to record themarking points. But to obtain an ordered array, the algorithmneeds a sorting operation and thus, spends more time. This isthe greatest shortcoming of CHANG. Another shortcomingconcerns the memory wasted by the array.Fig. 8 shows the original image. There is one outer con-

tour, as illustrated inFig. 9. � stands for the tracing ofbeginning point of the contour. Therefore, the contour is(1,1)756610755511076217232221445434.Fig. 10demon-

Fig. 10. Times recorded in array.

strates the number of times the contour points are recordedin the array according toTable 2.Although CHANG can be modified not to use this ordered

array and thus, not to use the sorting operation, but an imageto record points, as illustrated inFig. 10, this modificationwill not result in any difference between CHANG and CAI.

2.3. REN

Actually, the trouble in filling by parity check is that thecontour cannot be ensured to be smooth and continuous.That is, some contour points will be passed more than onceby the chain codes. Differences between CAI and CHANGalgorithms thus stem from the differences in the solutions tothis problem. For example, CAI removes the isolated pointstemporarily and CHANG records the singular points twice.But for filling by seeds, this problem will not exist anymore. The problem for filling by seeds is how to find seedsautomatically and quickly. A filling algorithm by seeds isproposed by[10] (REN). It firstly splits the whole contourinto many simpler ones, and then finds seeds at each rowautomatically in the simple contour, and finally executesthe filling from left to right in each row to obtain highspeed. The simple contour is defined as a contour line whosecontour points are passed by chain codes only once. Ona simple contour, according toTable 3, one contour pointcan be determined as to whether it is a seed. InTable 3,“1” represents a seed, and “0” is not a seed, and “x” is theinexistent combination ofci andci+1.If one contour point(xk, yk) has been passed when the

contour reaches it through reaching codecj , the contourmust be split at this point. Assuming that its leaving codeis ci , a new simple contour(xk, yk)ci . . . cj can be takendown from the whole contour. According toTable 3, seedson this new simple contour are obtained. This process is

Page 6: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2569

Fig. 11. Original image.

Fig. 12. An outer contour.

executed repetitively until all simple contours and all seedsare found. Seeds are marked by one color and other con-tour points are marked by another color (nonc) in the fillingimage. After all seeds are found, filling is executed start-ing at each seed from left to right until a pixel withnonccolor is encountered. Notably, once a contour point is clas-sified as seed in one simple contour, it remains a seed for-ever even if it is not a seed in another. Colornoncis a colorthat never appears in the filling image.Figs. 11–13demon-strate the algorithm.Fig. 11shows the original image. Thereis an outer contour, asFig. 12 illustrates.� represents thetracing of the beginning point of the contour, and the con-tour is (2,1)57177621033453. “+” represents the contourpoints passed by multiple times. Three simple contours arefound. They are(5,4) 62, and(3,2) 77103345, and(2,1)5713, denoted as circles inFig. 13 illustrate. According toTable 3, three seeds can be found as “�” marked inFig. 13.This algorithm, REN, is fast in finding seeds, and has

the same fast filling speed as the parity check. REN neitheruses the sorting operation nor wastes memory space or scanspixels out of the region and thus, saves time. Its only disad-vantage is that it cannot be used to fill the inner contour.

Fig. 13. Seeds.

Fig. 14. For an outer contour.

2.4. TANG

Another filling algorithm by seeds is proposed in[11](TANG). According to its reaching code and leaving code,one contour point can be classified as a left point or a rightpoint or anx-tip point. Contour points passed by multipletimes are classified multiple times. For example, pointF isboth a left point and a right point inFig. 14. In Table 4onecan look up how to determine the type of a contour point onan outer contour. Accordingly, “−1” stands for a left pointand “+1” stands for a right point. The type of a contourpoint on an inner contour is then inversed toTable 4. ButTable 4does not lead to any difference between the total sumof the left point and the right points on the outer contour oron the inner contour.Assuming that a point is not passed by the contour, by

noting the number of the left points (NUM_LEFT) and thatof the right points (NUM_RIGHT) on its right side, thefollowing conclusions can be reached: (1) if the point is outof the region, then NUM_LEFT equals to NUM_RIGHT;(2) if it is in the region, then NUM_LETT does not equalto NUM_RIGHT. For example inFig. 14, there are one left

Page 7: A new and fast contour-filling algorithm

2570 M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577

Fig. 15. For an inner contour.

point and one right point on the right side of both pointsAandD, respectively. Thus,A andD are out of the region.There is only one right point on the right side of pointsB,and the same asC. Thus,B andC are in the region. InFig. 15, pointA has two left points and two right points onits right side and therefore, it is out of the region. PointChas one left point and one right point on its right side. Thus,it is out of the region. By the same token, pointB has onlyone left point on its right side and it is in the region. Just bycounting its NUM_LEFT and NUM_RIGHT, a point can bedetermined as to whether it is out of the region. All thesepoints in the region can be used as the seeds for filling inTANG.Since determining whether a point is in the interior, that

is, to find a seed, is time-consuming, TANG employs thespatial connectivity to decrease the number of times of find-ing seeds. TANG splits each image row into many runs. Arun is a line segment starting at a non-boundary point andending at a boundary point. TANG uses an imageZ initial-ized with colorcolorE, marks all contour points in imageZto colorcolorB, and will fill points in the region to colorcol-orI, and points out of the region to colorcolorO. In imageZ, assumingL is a run starting at the non-boundary point(x1, y) and ending at the point(x2, y), (x2, y) is the firstboundary point on the right of(x1, y), or equals (width-1,y) when there is no boundary point on the right of(x1, y).Three cases occurred: (1) if all points in its previous row,that is, (x, y − 1) with x1�x�x2 are withcolorB, thenit must be determined whether(x1, y) is in the interior. If(x1, y) is in the interior then, set all pixels as belonging toL, that is(x, y) with x1�x�x2, to colorI, or otherwise tocolorO. (2) If there is a point(x, y − 1), x1�x�x2 withcolorI, then, set all pixels as belonging toL to colorI. (3) Ifthere is a point(x, y − 1), x1�x�x2 with colorO, then,set all pixels as belonging toL to colorO.Figs. 16–18demonstrate the algorithm, whereO,B,Eand

I stand forcolorO, colorB, colorE, andcolorI, respectively.Fig. 16 shows the original image and the contour points.Fig. 17shows the initial status of imageA. When executingfrom top to bottom, the filling result is shown inFig. 18; the

Fig. 16. Original image.

Fig. 17. Initial status.

point with “+” is the point at which one process determineswhether it is in the interior and as analyzed in the followingprocess spends more time. In row 2 ofFig. 18, the first runis (1,2) to (2,2), and since there is a point(1,1) in itsprevious row outside, the run is also outside; the second runis (3,2) to (4,2), and since all points in its previous row,only the point(3,1) in this case, are “B”, then (3,2) it mustbe determined whether it is interior. According to the TANGalgorithm, the total number of times to determine whether apoint is interior is six (although TANG can be improved, thenumber of times cannot be less than three) for this example.When comparing this algorithm with CAI, CHANG, and

REN, it has no competitive advantage. The greatest disad-vantage is that TANG scans pixels out of the region. Thesecond disadvantage is that to know whether a point is inthe region it needs to scan all chain codes at one timeand needs much more comparison operations. The third

Page 8: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2571

Fig. 18. Filling result.

disadvantage is that the number of times required to deter-mine whether a point is the interior is uncertain. Thus, itsmuch more time consuming and difficult, as expected.

3. The new method

As mentioned above, REN is a simple method to findseeds and TANG is time-consuming when it finds seeds.But REN is not fit for the filling of the inner contour. Inthis section, a new filling method by seeds is proposed.In general, the outer contour comes down first on the leftside and then goes up on the right side, whereas the innercontour comes down first on the right side and goes up onthe left side. Therefore, one can be determined whether onecontour point is a seed by analyzing its turning trend. Thenew method classifies each contour point into filling startingpoint (FSP), filling ending point (FEP) and filling skippingpoint (FSP). For an outer contour, the FSP is a pixel withits right side being in the region, the FEP is one with itsleft side in the region, and if FSP is temporarily neglectedduring the filling, filling starts at the FSP and finishes at theFEP, that is, from left to right. For an inner contour, the FSPis a point with its left side in the region, and the FEP is onewith its right side in the region; filling also starts at the FSPand finishes at the FEP, but this time from right to left.

Fig. 21. Passed by twice, one pair of reaching code and leaving code with opposite directions.

Fig. 19. Type of simple outer contour.

Fig. 20. Type of simple inner contour.

Assume that(xi , yi) is a contour point with some reachingand leaving codes. Letsum_delta_y be the sum of offsetsof these codes in they direction. According toFig. 1, theoffsets of the coordinates in they direction(dy) and in thex direction(dx) corresponding to the value ofci are definedin Table 5.

Page 9: A new and fast contour-filling algorithm

2572 M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577

Fig. 22. Passed by twice, no pair of reaching and leaving code with opposite directions: (a) sum=0; (b) sum=0; (c) sum=0; (d) sum=0;(e) sum= 0; (f) sum= 0; (g) sum= 0; (h) sum= 0; (i) sum= 0; (j) sum= 1; (k) sum= 1; (l) sum= 0; (m) sum= 0; (n) sum= −1; (o)sum= −1; (p) sum= 0; (r) sum= 0; and (s) sum= 0.

Theorem 1. On a simple contour, if sum_delta_y >o, then(xi , yi)must be a filling starting point; if sum_delta_y <o,then (xi , yi) must be a filling ending point; and ifsum_delta_y = o, then (xi , yi) must be a skippingpoint.

Proof. On a simple contour, each contour point is passedby just once, and has only one reaching code and one leav-ing code. Ifsum_delta_y >o for a contour point(xi , yi),then this point is coming down. Because the contour isclosed, there is another contour point(xk, yi) going up

Page 10: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2573

which has sum_delta_y <o. Therefore, all pixels from(xi , yi) to (xk, yi) must be in the region(xi , yi) must bethe filling starting point for the filling and(xk, yi) must bethe filling ending point. For example, inFig. 19, there is asimple outer contour and inFig. 20, there is a simple innercontour. In bothB stands for a filling starting point andEstands for a filling ending point.�

Theorem 2. Theorem1 can be extended to any complexcontours.

Proof. A contour point may be passed by one, two, three,or at most four times. The following deduction will dependon the number of passing times to verify Theorem 2. Thededuction assumes that the contours are outer contours, andcan be split into many simple contours as with REN.

(1) If a contour point is passed by only once, then it mustbe on a simple contour and thus, Theorem 2 is proven.

(2) If a contour point is passed by twice, then it must be ontwo simple contours and it must have four chain codes,two reaching and two leaving codes. These four chaincodes can be divided into two pairs of reaching andleaving codes. Each pair appears in one simple contour.All combinations are shown below (2.1). If one pair’sreaching code is opposite to the leaving code, then thecontour point must be a skipping point in this simplecontour and thus,sum_delta_y of this point depends onthe pair of reaching and leaving codes in another simplecontour. Therefore, Theorem 2 is proven, as illustratedin Fig. 21(2.2). If there is no such pair of reaching andleaving codes with opposite directions, then there mustbe at least four contour points neighboring this contourpoint, and at most six such contour points. Otherwise,if there are less than four such points, then there mustbe one pair of reaching and leaving codes with oppo-site directions, and if there are more than six, then thiscontour point would not be passed by twice. All com-binations are listed inFig. 22, from which, Theorem 2can be proven.

(3) If a contour point is passed by three times, which meansthat there are three simple contours passing by it, thenthere are at least two pairs of reaching and leaving codeswith opposite directions as inFig. 23. By ignoring thesetwo pairs, Theorem 2 is proven.

(4) If a contour point is passed by four times, there is onlyone combination asFig. 23(d) illustrates. It is a skippingpoint with sum_delta_y = o. It verifies the validity ofTheorem 2. �

For inner contours, Theorem 2 is also correct.

Theorem 3. −2�sum_delta_y� + 2.

Proof. FromFigs. 21–23, it is proven.

Theorem 4. In the same row, if there is a filling startingpoint, then there must be a filling ending point.

Proof. Assume that (xi , y) is a filling starting pointon an outer contour, and according to Theorem 2, itssum_delta_y >0, that is, it must be next to another fillingstarting point(xj , y−1), or the contour comes down at thisposition, as point(1,2) or point (2,1) in Fig. 19. Becausethe contour is closed, it must go up one point to counteractits coming down. In other words, in the same row, a fillingending point(xk, y) must exist, as point(4,2) or (7,1) inFig. 19.Particularly, the number of filling staring points and that

of filling ending points may not be equal. For example inFig. 19, there are one filling starting point(1,2) and twofilling ending points(4,2) and (7,2) in the third row. Ac-tually, the total sum ofsum_delta_y of all contour pointsin the same row must equal zero. If there is a filling start-ing point with sum_delta_y = 2, then there must be fillingending point withsum_delta_y=−2, or two filling endingpoints withsum_delta_y = −1 each. �

4. The new algorithm

According to Theorem 2, whether a contour point is thefilling starting point depends on itssum_delta_y, no matterhow many times it is passed by. A filling starting point canbe considered as a seed for filling in the same row. It is easierand faster to find seeds by this newmethod thanTANG.Afterfinding out a filling starting point, the algorithm executes thefilling from left to right for an outer contour, or from rightto left for an inner contour, until it reaches a filling endingpoint.The first problem of the new algorithm concerns how

to mark the filling starting point, the filling ending point,the filling skipping point, andsum_delta_y by differentcolors in the filling image. Letfillc stand for the filling color,and nonc the color which does not appear in the fillingimage. Thus, the filling ending point can be marked bynonc.According to Theorem 3,−2�sum_delta_y� + 2. Sincethe value of a color cannot be less than zero, the coloroffcmust be found to ensure thatsum_delta_y+off c�0. offcmust then satisfy:

offc∈ [2,2m − 3] and fillc /∈ [offtc− 2,offc+ 2]and nonc/∈ [offc− 2,offc+ 2]. (1)

Here,m is the number of bits of a pixel.When the color of a contour point equalsoffc, its

sum_delta_y must be 0. The filling skipping point is thusmarked byoffc. Let a filling starting point be marked asstartcas below:

startc �= fillc and startc�= nonc and

startc/∈ [offc− 2,offc+ 2]. (2)

Page 11: A new and fast contour-filling algorithm

2574 M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577

Fig. 23. Passed by three times, at least two pairs with opposite directions: (a) sum= 0; (b) sum= 1; (c) sum= 0; and (d) sum= 0.

Table 6Angle LUT

Ci Ci+1

0 1 2 3 4 5 6 7

0 0 45 90 135 180 x x −451 −45 0 45 90 135 180 x −902 x −45 0 45 90 135 180 x3 x −90 −45 0 45 90 135 1804 180 x x −45 0 45 90 1355 135 180 x −90 −45 0 45 906 90 135 180 x x −45 0 457 45 90 135 180 x −90 −45 0

Formulas (1) and (2) can be satisfied easily, becausefillc and nonc are just two kinds of colors and whenm�4, offc must exist. Finallystartc can be found. Inpresent day computers, a byte is the minimum unit, andin the field of image processing, one pixel is usuallystored in more than one byte, and as a result,m�8.These different colors cannot only mark the filling startpoints, the filling ending points and the filling skippingpoints but also indicate whether a contour point has beenclassified.Another problem of the new algorithm lies in automati-

cally determining whether a contour is an outer contour oran inner contour. A method based on a common observationis adapted to solve this problem. If a person runs around aclosed loop starting at one point, when he reaches the pointagain, the sum of all angles he has turned equals to 2�. Theturning angle from the codeci to the next codecj is de-fined as positive when it is counter-clockwise and negativewhen it is clockwise. All combinations ofci and cj andtheir turning angles are defined inTable 6. If the sum of allturning angles equals to +360, the contour is then counter-clockwise; otherwise, the contour is clockwise. Since theouter contours are counter-clockwise whereas the inner con-tours are clockwise, +360 means outer contours, and−360means inner contours. Therefore, the rule of determining the

type of a contour can be shown as follows:

sum_angle = angle[cn−1][c0]

+n−1∑i=0

angle[ci ][ci+1], (3)

type={outer contour if sum_angle >0,inner contour otherwise.

(4)

So the following steps can complete the new filling algo-rithm. Firstly, compute each contour point’ssum_delta_y.Secondly, according to Theorem 2, obtain all the fillingstarting points, filling ending points and filling skippingpoints. Mark them asstartc, nonc, andoffc color, respec-tively. Thirdly, fill the image beginning at every filling start-ing point from left to right or from right to left, until onereaches a filling ending point. The algorithm can be pro-grammed in C language as follows:[Algorithm] RmwContourFilling(pImg,width,height,

x0,y0,C,n,fillc,nonc)Step 1: According to formula (1) to obtain offc, and

according to formula (2) to obtain startc, let sum_angleequal 0.Step2: For (x= x0, y= y0, i= 0; i < n; x+ =dx[C[i]],

y + =dy[C[i]], i + +) pImg[x][y] = offc.Step 3: For (x = x0, y = y0, i = 0, preCode =

C[n − 1]; i < n; x + =dx[C[i]], y + =dy[C[i]], i + +){ pImg[x][y] + =dy[preCode] + dy[C[i]];sum_angle+ =angle[preCode][C[i]];// according to formula(3)preCode= C[i];

}direction= sum_angle/360 //according to formula (4),direction= +1 or direction= −1.Step4: For (x= x0, y= y0, i= 0; i < n; x+ =dx[C[i]],

y + =dy[C[i]], i + +){if pImg[x][y] equal offc+1 or offc+2,then let pImg[x][y] = startc;else if Img[x][y] equal offc-1 or offc-2,then let pImg[x][y] = nonc;

}

Page 12: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2575

Step5: For (x= x0, y= y0, i= 0; i < n; x+ =dx[C[i]],y + =dy[C[i]], i + +){ if pImg[x][y] equal startc{ j = x;while (pImg[j ][y] not equal nonc){ pImg[j ][y] = fillc ;j + =direction; }pImg[x][y] = nonc; // to avoid repeated filling

}Step6: For (x= x0, y= y0, i= 0; i < n; x+ =dx[C[i]],

y + =dy[C[i]], i + +) pImg[x][y] = fillc.

Here, pImg stands for the image required in the filling.The width stands for the image width and the height standsfor the image height.(x0, y0) is the contour beginning pointandn is the quantity of chain codes as Section 2 assumed. Cis the array of the chain codes. dx and dy are the offsets ofthe coordinates corresponding toci in thex andy directions,respectively, as defined inTable 5. Step 2 sets all contourpoints to coloroffc. Step 3 calculates each contour point’ssum_delta_y, and the sum of all angles, and sets the di-rection to +1 when the contour is an outer contour, and−1when the contour is an inner contour. Step 4 classifies con-tour points to filling starting point and filling ending point,and marks them as colorstartcandnonc, respectively. Step5 executes the filling from the filling starting point until thefilling ending point; direction of value +1 ensures that thefilling is done from left to right for an outer contour, andthat with−1 ensures the filling from right to left for an innercontour. Step 6 paints the contour points to colorfillc.Particularly, if there are two consecutive filling starting

points in a given scan line, the second filling starting pointwill be painted to the filling color during the filling beginningat the first filling starting point and thus, the second fillingstarting point will not result in any repeated filling. Forexample, inFig. 19, there are two consecutive filling startingpoints (1,6) and (3,6) at line 6. After the filling beginsat (1,6) and finishes at the first filling ending point(5,6),point (3,6) has been painted tofillc and thus, will not resultin repeated filling from it to point(5,6). Therefore, the newalgorithm is as fast as the filling by parity check.This new algorithm scans the region once. Its advantages

are:

(1) None of the pixels out of the region requires to bescanned.

(2) Seeds are found very fast.(3) Its filling by seeds is as fast as the filling by parity check.

In practice, another technique can be adopted to speed upthe filling. Because pixels are stored in continuous memoryspace one by one, if the address of the current contour pointis pcur, and its leaving code isci , then the address of thecontour points (pnxt) following pcur is

pnxt= pcur+ offsetp[ci ], (5)

offsetp[ci ] = dy[ci ] × width+ dx[ci ]. (6)

offsetp[ci ] is the offset of the address in memory spacefrom pcur to pnxtwhen the leaving code isci . In the fillingalgorithm,offsetpis initialized once. By usingoffsetp, thealgorithm can visit pixels faster. For example, step 2 can bemodified to:Step2: For(pcur=pImg+y0∗width+x0, i=0; i < n; i+

+) { ∗pcur= offc;pcur+ =offsetp[C[i]]; }.

5. Experiments

The greatest shortcoming of CAI is that it cannot avoidscanning pixels out of the filling region; thus, CAI wastestime. The greatest shortcoming of CHANG is that it requiressorting operations to obtain an orderly array, thus CHANGwastes time considerably sometimes and it is difficult toestimate the time consumed by it. The greatest shortcomingof TANG is that it is very slow to find seeds, and the numberof times required to find seeds is uncertain; thus TANGwastes time and it is also difficult to estimate how muchtime it consumes. The new algorithm proposed in this paperovercomes all those shortcomings, has a fast speed in findingseeds, does not scan pixels out of the filling region or needsort operations, and can be easily implemented.Theoretically speaking, these algorithm performances

were compared inTable 7. Because CAI must scan everypixel in the circum-rectangle of the region,W ×H +O(n);CHANG does not need to scan pixels out of the regionthus area, but it needs quick sorting operations to orderthe marking points and thusO(n logn) added. TANG mustscan every pixel in the circum-rectangle of the region, andsometimes more than once, thus(1+ �)W ×H, �>0, andthe filling operation needsarea, and to determine seedsmore times and thusO(k×n), k is the time required to findseeds and is uncertain; the new algorithm just scans pixelsin the region one time and thusarea, and to find fillingstarting points and filling ending points it needsO(n).Filling Figs. 24–29demonstrates performance compari-

son among CAI, CHANG, TANG, and the new algorithm.All algorithms are implemented and tested through soft-ware written in C programming language in windows 2000on a PC with PIII 800 CPU, and the sorting operationused to implement CHANG is the quick sorting algorithm,which has the fastest speed among all sorting algorithms.The sum of 1000 times filling operation in millisecond

Table 7Theoretical algorithm performance

Compared times

CAI W ×H +O(n)CHANG Area +O(n logn)TANG (1+ �)W ×H + area +O(k × n), �>0NEW Area +O(n)

Page 13: A new and fast contour-filling algorithm

2576 M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577

Fig. 24. Filling a Chinese font.

Fig. 25. Filling the rotated Chinese font.

Fig. 26. Filling connected lines whose width is single pixel.

is time consuming. Both the width and the height of all ofFigs. 24–29are 512. InFigs. 24–26, there is only one con-tour. In Figs. 27–29, there are many contours.Fig. 25 is generated by rotatingFig. 24 and nearly has

the same number of filled pixels asFig. 24. The new algo-rithm spends less time in filling than fillingFig. 24, mainlybecause the number of chain codes inFig. 25 is less thanthat in Fig. 24. CAI spends more time because the sizeof the circum-rectangle inFig. 25 is larger than that inFig. 24 and CAI must scan every pixel in the circum-rectangle. CHANG spends more time in fillingFig. 25thanfilling Fig. 24 since the time consumed by the sorting al-gorithm varies with the shape of the contour, although thenumber of chain codes inFig. 25is less than that inFig. 24.

Fig. 27. Filling natural scenes.

Fig. 28. Filling man made object.

Fig. 29. Filling document image.

TANG also spends more time in fillingFig. 25 than fillingFig. 24 since TANG must scan every pixel in the circum-rectangle and the number of times to find seeds also varieswith the shape of the contour. InFig. 26, there are fewerpixels belonging to the region that need to be filled, andalmost of its contours points havesum_delta_y = 0; thus,the new algorithm has a higher speed. Both CAI and TANGspend more time inFig. 26 than the new algorithm sincethey must scan every pixel in the circum-rectangle. CHANGalso spends more time because it needs sorting operationsto order so many contour points (Table 8).In Figs. 27–29, only those regions with black color and

regions with white color are required to be filled; otherregions with gray color must be preserved. The time con-

Page 14: A new and fast contour-filling algorithm

M. Ren et al. / Pattern Recognition 38 (2005) 2564–2577 2577

Table 8The performance comparison using a single contour

N W ×H Area CAI CHANG TANG NEW

Fig. 24 3112 308× 324 37640 3925 511 2774 340Fig. 25 2681 406× 358 37527 5748 621 5938 321Fig. 26 3322 404× 362 1735 5668 611 3275 200

Table 9The performance comparison using many contours

CAI CHANG TANG NEW

Fig. 27 12309 1783 263479 2212Fig. 28 7930 1572 44632 1434Fig. 29 1632 1593 7040 1282

sumed is listed inTable 9. FromFigs. 27–29, the circum-rectangles of the regions are more and more near to theirarea, and thus CAI spends lesser and lesser time, and regionsare more and more regular, and thus TANG spends lesserand lesser time. CHANG and the new algorithm are notsensitive to the circum-rectangles of the regions, and thusthe time they consume is very stable. ForFig. 27, CHANGspends lesser time than the new algorithm, because inFig. 27, there are many contours with fewer chain codes;thus,O(n logn) is not outstanding, but one case must benoted: that CHANG uses much more memory to finish itsquick sort operation during filling.

6. Conclusions

This paper studies the advantages and disadvantages ofthe conventional filling algorithms, analyzes their originalideas, and proposes a new filling algorithm. The new algo-rithm presents a very simple idea for finding seeds automati-cally, and limits the scan within the filling regions. Repeatedexperiments have proven that with the new algorithm,

any complex inner and outer contours can be filled withhigher speed.More specifically, it presents a new method to determine

a seed point automatically and efficiently, a new strategyto deal with a contour point that had been visited severaltimes during a contour tracing, and its algorithm perfor-mance reachesarea+O(n), which means that it has nearlya constant amount of time consumed that is proportional tothe quantity of pixels that needs to be filled and less variedwith the shape of contours. Thus, the new algorithm is moreeffective and more practical, and more convenient to apply.

References

[1] T. Pavlids, Algorithm for Graphics and Image Processing,Computation Science, Washington, DC, 1982.

[2] B.D. Ackland, N. Weste, The edge flag algorithm—a fillmethod for raster scan display, IEEE Trans. Comput. 30 (1981)41–47.

[3] U. Sahni, Filling regions in binary restore images, inSIGGRAPH’ 80, pp. 321–327.

[4] R.D. Merrill, Representation of contours and regions forefficient computer search, Commun. ACM 16 (2) (1973)69–82.

[5] H. Freeman, Computer processing of line drawing images,Comput. Surv. 6 (1974) 57–97.

[6] M. Ren, J. Yang, H. Sun, Tracing boundary contours in abinary image, Image Vision Comput. 20 (2) (2002) 125–131.

[7] Z. Cai, Restoration of binary images using contour directionchain codes description, CVGIP 41 (1988) 101–106.

[8] L.-W. Chang, K.-L. Leu, A fast algorithm for the restoration ofimages based on chain codes description and its application,CVGIP 50 (1990) 296–307.

[9] F.Y. Shih, W.-T. Wong, An improved fast algorithm forthe restoration of images based on chain codes description,CVGIP 1994, pp. 348–351.

[10] M. Ren, J. Yang, H. Sun, A new contour filling algorithmbased on chain codes description, Chin. J. Image Graphics 6(4) (2001) 348–352.

[11] G.Y. Tang, Region Filling with the use of the discrete Greentheorem, CVGIP 42, 1988, pp. 297–305.

About the Author—MINGWU REN, born in 1969 in PR China, is an assistant professor at the Computer Science Department, NanjingUniversity of Science and Technology, PR China. He received M.S. and Ph.D. degrees in Pattern Recognition and Computer Image Processingfrom Nanjing University of Science and Technology, PR China. His research interests include computer vision, digital image processing,pattern recognition and artificial control.

About the Author—WANKOU YANG, born in 1979 in PR China, is a graduate student at the Computer Science Department, NanjingUniversity of Science and Technology, PR China.

About the Author—JINGYU YANG, born in 1941 in PR China, is a professor at the Computer Science Department, Nanjing Universityof Science and Technology, PR China. His research interests include artificial intelligence and robotics.