Computers & Graphics 24 (2000) 529}537
Technical Section
Region-"lling algorithm on bincode-based contour and its implementation Yao-Hong Tsai, Kuo-Liang Chung* Department of Information Management and Institute of Information Engineering, National Taiwan University of Science and Technology, No. 43, Section 4, Keelung Road, Taipei 10672, Taiwan, ROC
Abstract Using bincodes to represent binary images is a storage-saving encoding scheme. Region "lling is a classical and important operation in the "elds of computer graphics. Given a contour, i.e., region boundary, represented by p bincodes with their corresponding neighbor information, this paper presents an O(p;d)-time algorithm for region "lling, where d denotes the depth of the corresponding bintree of the given bincodes. Experimental results are illustrated to justify the performance of the proposed algorithm. 2000 Elsevier Science Ltd. All rights reserved. Keywords: Bincodes; Contour; Computer graphics; Hole; Region "lling
1. Introduction Region "lling is a classical and important operation in the "eld of computer graphics. Previously, Pavlidis [1,2] presented region-"lling algorithms on raster graphics. Shani [3] presented an algorithm for region "lling on binary raster images. Brassel and Fegeas [4] presented an e$cient algorithm on vector display graphics. The input used in the articles [1}4] is the set of the coordinates or chain codes of the region boundary. The output of the "lled region is represented by the coordinates of those pixels in the "lled region. Spatial data structures are very useful because of their ability to focus on the homogeneous subsets of the image. There are many e$cient image representations and the related fast image manipulations on these representations have been developed [5,6]. Given a region boundary, Samet [7] presented an e$cient algorithm for "lling the region represented by a quadtree. Later, given a vector-based
* Corresponding author. Tel.: #886-2-27376771; fax: #8862-27376777. E-mail address:
[email protected] (K.-L. Chung). This research was supported in part by the National Science Council of R.O.C. under contracts NSC88-2213-E011-005 and NSC88-2213-E011-006.
region boundary, Mark and Abel [8] presented an algorithm to "ll the region represented by a linear quadtree. The region-"lling algorithms mentioned above are only concerned with the simple region-"lling problem without including the holes case. For solving the case containing holes, Gargantini and Atkinson [9] presented a clever blocking technique to perform region "lling on a linear quadtree. In the article [10], Atkinson, Gargantini, and Walsh showed that given a region boundary represented by linear quadtree with p codes, the region"lling problem can be solved in O(p;d) time, where d denotes the depth of the corresponding quadtree. Using bincodes [11] to represent binary images is a storage-saving encoding scheme. According to the studies [11,12], bincodes are shown to be some spacesaving over the linear quadtrees. The key concept of the above results is counting the total nodes needed for a bintree and a quadtree to represent images in average case. Thus, using bincodes is shown to have about 25% space utilization improvement over using linear quadtree encoding in empirical comparisons. A fast neighbor-"nding algorithm on bincodes was developed in the article [13]. The motivation of this research is to present an e$cient region-"lling algorithm on bincodes since the previously published region-"lling algorithms [7}10] cannot be applied to bincodes directly.
0097-8493/00/$ - see front matter 2000 Elsevier Science Ltd. All rights reserved. PII: S 0 0 9 7 - 8 4 9 3 ( 0 0 ) 0 0 0 5 6 - X
530
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
Given a bincode-based contour with p bincodes with their corresponding neighbor information, in this paper, "lling the region takes O(p;d) time, where d denotes the depth of the corresponding bintree. The "lled region is also represented by bincodes. The proposed region-"lling algorithm is based on a divide-and-conquer approach, in which the dividing process is based on some properties of the bincodes and the merging process involves a method to test the neighbor's color of the bincode. Some experimentations are carried out to justify the performance of the proposed algorithm. To the best of our knowledge, this is the "rst time that such a region-"lling algorithm is proposed on bincodes in the literature. The remainder of this paper is organized as follows. In Section 2, a brief review of bincodes is given. Then we present the proposed region-"lling algorithm in Section 3. Section 4 illustrates some experimental results. Finally, some conclusions are addressed in Section 5.
2. Bincodes A bintree [14] is a hierarchical data structure for representing binary images. If the entire image is totally black or white, the image is represented by one node, the root. Otherwise, the root is gray and the image is split into two equal-sized subimages and its two sons are added to represent the subimages. This subdividing process is then repeated recursively for each of the two subimages until the subimage is totally black or white. If a subdivision is either black or white, then its corresponding node is an external node; otherwise it is an internal node (gray node). According to the structure of bintree, a 2,;2, image have a bintree with height 2N at most and the root node is at level 0. The block corresponding to a node at level h is of size 2,\ WFX ;2,\ UFV , i.e., when h is even (odd), this block is square (rectangular). Considering the boundary of a polygon represented by a 2;2 binary image as shown in Fig. 1(a), the corresponding blocks and the bintree are shown in Figs. 1(b) and (c), respectively. Based on the bintree structure, the image can be represented by an ordered collection of external black nodes in a preorder way. Each external black node is represented by an integer, namely, bincodes [11], converted by its location and level. Without loss of generality, the location of a block is de"ned at the lower-left pixel in the block. Given a 2,;2, binary image, if the node at level l of a bintree corresponds to a black block at location (i, j) in the binary image, the bincode b can be obtained by computing ,\(i ;2I>)# ,\( j ;2I>)# I I I I ,\(s ;2I), where i" ,\(i ;2I), j" ,\( j ;2I) I I I I I I and s"2,!2,\J" ,\(s ;2I). That is, b" I I (i s j s i s j s ) . ,\ ,\ ,\ ,\ 2 Return to Fig. 1. Considering the block B(3) at location (1, 2) and level 3, we have that i"1"(01) ,
Fig. 1. A 2;2 binary image.
j"2"(10) , and s"14"(1110) . Therefore, the bi ncode of B(3) is (01111100) "124. Then, the bincodes of Fig. 1(a) can be represented by the ordered sequence 187, 92, 124, 208, 244, 2532.
3. The proposed algorithm Given a contour represented by bincodes, an e$cient algorithm for region "lling on the bincodes is presented in this section. For convenience, `blocka and `bincodea are used exchangeably to represent the bincode of a given block. Let D be the direction in 1E, S, =, N2, which maybe is the east, south, west, or north direction of a given bincode Q. The D-neighbor of Q, say Q, shares a common side with Q in the direction D and the size of Q is larger than or equal to that of Q. Considering the
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
531
Fig. 2. A region containing a hole.
bincode b(5) in Fig. 2, its N-neighbor is the bincode of b(6). Four bits are needed to store the information about the 4-neighbor of a given bincode Q, which is called the neighbor information (NI) of Q. NI is represented by 1NI , NI , NI , NI 2, where NI , NI , NI , # 1 5 , # 1 5 and NI 3+1, 0,. If the D-neighbor is in the interior or , at the boundary of the region, then NI is 1; otherwise, " NI is 0. For example, the bincode b(18) (b(24)) in " Fig. 2; the NI of b(18) (b(24)) is 11, 1, 0, 12 (11, 1, 1, 02). Using the NI of the boundary, we can identify the white block as the interior of the region, the exterior of the region or the hole. Thus, the proposed algorithm can solve the region-"lling problem that contains holes by using the NI. Suppose a set of bincodes B with their corresponding NI, which represents the contour in a 2,;2, binary image, is given as the input of our algorithm. The input is stored in a two-"eld array, where one "eld holds the bincode and the other holds the corresponding NI. Then, the output is the region enclosed by the given contour expressed as a set of bincodes. Our proposed region"lling algorithm is a divide-and-conquer approach. The dividing process is "rst presented, then the merging process is presented. For convenience, an example without holes is adopted to illustrate how the proposed algorithm works although the proposed algorithm can solve the region-"lling problem that contains holes. At last, the formal algorithm is presented in Section 3.3. 3.1. Dividing process The dividing process is used to determine each pair of two sons with the same parent at each level in the corresponding bintree. After performing this process, each bincode is at the right position in the spatial sense with respect to the bottom level of the bintree. The dividing process on the input set B is based on the following two lemmas, which can be performed in O(1)
time by the hardware implementation of Wu and Chung [15]. Lemma 1 (Ouksel and Yaagoub [11]). Given a bincode q of an internal node at level l in the bintree of a 2,;2, binary image, the bincodes, b and c, of the left son and the right son of that internal node are q#2,\J\ and q#3;2,\J\, respectively. Lemma 2 (Huang and Chung [13]). Given a bincode q at level l, if some bincodes fall in the range [q, q#(4,\J!1)] then these bincodes are covered by q in a spatial sense and q#(4,\J!1) is said to be the rightmost coverage (RC) of q. In the example as shown in Fig. 3, the bincodes of the contour are represented by 1b(1), b(2), b(3), b(4), b(5), b(6), b(7), b(8), b(9)2"187, 93, 213, 221, 223, 253, 247, 127, 1252. We "rst divide the set B with p bincodes into two subsets which are covered by the left son and the right son of the root, respectively, in the spatial sense. By Lemma 1, let q"0 to be the root, then we have the two sons, b and c, of the root. By Lemma 2, the RC of b can be obtained and the RC is used as a pivot to divide B. Then the left subset (right subset) is those bincodes which are less than or equal to (larger than) the pivot. Since this partition on those p bincodes needs p comparison operations, this initial dividing step takes O(p) time using O(p) working memory. Return to Fig. 3. By Lemma 1, the root has the bincode q"0 and we know that b"64 ("0#2) and c"192 ("0#3;2). By Lemma 2, the RC of b is 127 ("64#4!1). Using 127 as the pivot, the set of the given bincodes is divided into two subsets 187, 93, 125, 1272 and 1223, 247, 253, 221, 2132. Then, the dividing process is repeated recursively on the two subsets covered by b and c, respectively, until the bottom of the corresponding bintree is arrived or an
532
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
3.2. Merging process The merging process is responsible for deciding whether two sons with the same parent can be merged or not. Let S denote the son's state and be de"ned by
0, the son is a white block,
S" 1, the son is a black block, 2, the son is a gray block.
Fig. 3. The contour of Fig. 1.
empty subset (i.e., a subset containing no bincode) is processed. The empty subset corresponds to a white block. Since the total number of bincodes in those divided subsets at each level of the corresponding bintree is always p, the total number of comparison operations needed is O(p) for each level. Thus, each dividing step takes O(p) time and uses O(p) working memory. For the subset covered by 64 (192), the left son is 80 (208) and the new RC of 80 (208) is 95 (223). Thus, using 95 (223) as a pivot, the related subset is subdivided into two smaller subsets, 187, 932 and 1125, 1272 (1223, 213, 2212 and 1253, 2472). The remaining results are shown in Fig. 3(b). Suppose that the depth of the bintree is d. Since the execution time of the dividing step at each level is O(p), we have the following result. Theorem 3. Given a contour represented by p bincodes, the dividing process on these bincodes takes O(p;d) time, where d is the depth of the corresponding bintree, using O(p) working memory.
Clearly, the value of S for any given boundary bincode is 1. From the combinatorial view point, there are sixteen cases to be considered according to the states of the two sons with the same parent. The 16 cases are shown in Fig. 4. Each case in (1)}(8) consists of two square blocks, each block with size 2I;2I; each case in (9)}(16) consists of two rectangular blocks, each block with size 2I>;2I for k"1, 2,2, N!1. In the merging process, region "lling occurs at each level from the bottom to the root of the corresponding bintree. At each level, the states, S and S , of the left and J P right sons, respectively, are used to determine their father's state S . The two sons are merged according to the D following "ve rules. The NI of the father, NID, is obtained from NIJ and NIP of the left son and the right son, respectively. Rule}1: If S "1 and S "1 for cases (8) and (16), the J P two corresponding black sons are merged and the father's bincode can be obtained by Lemma 1. The father's NI can be obtained by computing NID"NIJNIP"1NIJ NIP , NIJ NIP , NIJ NIP , # # 1 1 5 5 NIJ NIP 2, where `a denotes the ANDing operation. , , The state of S is 1. D Rule}2: If S "0 and S "1, the southern (western) J P block for case (3) (case (11)) is a white block. If the NIP 1 (NIP ) is 1, the southern (western) block is in the interior 5 of the region and Rule}1 can be applied; otherwise, the northern (eastern) black block is outputted since the southern (western) white block is not in the interior of the region and the S is set to 2; NID"NIP. For the dual D situation, if S "1 and S "0 for case (4) (case (12)), NIJ J P , (NIJ ) is checked to determine the northern (eastern) # block and NID"NIJ. Rule}3: If S "2 and S "1 for case (5) (case (13)) or J P S "1 and S "2 for case (6) (case (14)), the black block J P is outputted and NID"1NIJ NIP , NIJ , NIJ NIP , # # 1 5 5 NIP 2 (1NIP , NIJ NIP , NIJ , NIJ NIP 2). S is set , # 1 1 5 , , D to 2. Rule}4: If S "0 and S "2, the southern (western) J P block for case (1) (case (9)) is a white block. If the NIP 1 (NIP ) is 1, the southern (western) block is in the interior 5 of the region and Rule}3 can be applied; otherwise, S "2 since the southern (western) white block is not in D the interior of the region and NID"NIP. For the dual situation, if S "2 and S "0 for case (2) (case (10)), NIJ J P ,
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
533
Fig. 4. The 16 cases.
Table 1 The summary of cases (1)}(16) and the "ve rules
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Case
S,S J P
8, 16 3
1, 1
11
0, 1
4 12
1, 0
5 (13) 6 (14) 1
2, 1 1, 2
9
0, 2
2 10
2, 0
7, 15
2, 2
NI
NIP "1 1 NIP "0 1 NIP "1 5 NIP "0 5 NIJ "1 , NIJ "0 , NIJ "1 # NIJ "0 # NIP "1 1 NIP "0 1 NIP "1 5 NIP "0 5 NIJ "1 , NIJ "0 , NIJ "1 # NIJ "0 #
(NIJ ) is checked to determine the northern (eastern) # block and NID"NIJ. Rule}5: If S "2 and S "2 for case (7) (case (15)), J P S "2 and NID"1NIJ NIP , NIJ , NIJ NIP , NIP 2 D # # 1 5 5 , (1NIP , NIJ NIP , NIJ , NIJ NIP 2). # 1 1 5 , , Since the input of the algorithm is the boundary of the contour, the case for S "0 and S "0 will not happen. J P When two blocks cannot be merged, the father of them is gray and its NI keeps only the NI of the interior block
Merge Yes Yes No Yes No Yes No Yes No No No No No No No No No No No No
Output black block
Northern block Eastern block Southern block Western block Northern (eastern) block Southern (western) block Southern block Western block Northern block Eastern block
S D 1 1 2 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2
because the neighboring block in the direction of the exterior block is still exterior. For clarity, Table 1 summarizes all the cases in Fig. 4 and the above "ve rules. Initially, according to Table 1, two sons with the same parent at the bottom level of the bintree are "rst determined to be merged or not. Since there are O(p) subsets at the bottom level, this step takes O(p) time and uses O(p) working memory. The detailed simulation of the merging step on the divided subsets in Fig. 3 is illustrated in Table 2. The empty bucket at level 4 corresponds to a white
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
Rule}3
0110 2
0110 2
Rule}2
Rule}3
0010 2
Rule}2 244 0110 1 Rule}1 220 0011 1 Rule}1 208 0011 1 Rule}2 212 1011 1
block. For bincodes 125 and 127, since S "1 and S "1, J P they are merged into 124 and NID"11, 1, 0, 02 ("111, 11, 00, 102) by Rule}1 of Table 1. Considering the bincode 87 and its southern white block, since S "0 and S "1, Rule}2 is applied; we have J P NIP "0 and the bincode 87 is outputted; 1 NID"11, 0, 0, 02("NIP); S "2. By Rule}2 again and D NIJ "1, the bincode 93 and its northern white block are , merged to be 92 since S "1 and S "0; S is set to 1 and J P D NID"11, 0, 0, 12("NIJ). After performing the merging step at the bottom level, we go to the upper level of the bintree until the root is arrived. Since the number of subsets at one level needed to be processed is decreasing from the bottom level to the root, in the worst case, each merging step takes O(p) time using O(p) working memory. Return to Table 2. By Rule}3, since S "2 and S "1, the bincode 92 and its J P western gray block at level 3 cannot be merged and the bincode 92 is outputted; S "2 and NID"11, 0, 0, 02 D (11, 00, 0, 012). At level 1, since the state of each node is 2, by Rule}5, we have that S "2 and D NID"10, 0, 0, 02 (10, 00, 0, 002). Finally, we have the bincodes of the "lled region represented by the order set 187, 92, 124, 208, 244, 2532, which is the same as that of Fig. 1. Since the depth of the bintree is d and the execution time of the merging step at each level is O(p), we have the following result.
1100 2
Theorem 4. Given a contour represented by p bincodes, the merging process on those subsets generated by the dividing process takes O(p;d) time, where d is the depth of the corresponding bintree, and uses O(p) working memory.
0
Rule}1 124 1100 1 Rule}2
125 127 1101 1100 1 1
213 1011 1
0
221 0011 1
223 0111 1
0
247 0110 1
253 0110 1
0
534
1000 2
Rule}5
1000 2
1000 2
Rule}3
Rule}2 92 1001 1
Level 1
Level 2
Level 3
Rule Bincode NI Status Rule Bincode NI Status Rule Bincode NI Status
0
Rule}2
93 1001 1 87 1000 1 Bincode NI Status Level 4
Table 2 The simulation of the merging process for Fig. 3
0
3.3. The formal algorithm Based on the dividing process described in Section 3.1 and the merging process described in Section 3.2, the formal algorithm for region "lling on bincode-based boundary is listed in Fig. 5. NIP and NIJ are merged into NID by the procedure Merge}NI( ). The procedure Output( ) is used to output the black blocks. Initially, Father}code is the root of the corresponding bintree and level is set to 0. The variables, Left}ptr and Right}ptr, are used to point to the left and the right end of the input, respectively. By Theorems 3 and 4, the total execution time of the proposed algorithm is O(p;d). Accordingly, we have the following result. Theorem 5. Given a contour represented by p bincodes with their corresponding NI, the algorithm for region xlling takes O(p;d) time, where d is the depth of the corresponding bintree, using O(p) working memory.
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
535
Fig. 5. The formal algorithm for region-"lling.
4. Experimental results In this section, some experimentations are carried out to justify the performance of the proposed algorithm. Five 2;2 binary images, "ve 2;2 binary images, "ve 2;2 binary images, and "ve 2;2 binary images are used as the input (i.e., boundary of the regions) of the proposed algorithm. Among these images, four images of size 2;2 are shown in Fig. 6. The proposed algorithm
is coded in turbo C programming language and is executed on the PC-586. Recall that the time complexity of the proposed algorithm is O(p;d), where p is the number of the given bincodes and d is the depth of the corresponding bintree. Thus, the execution time needed can be rewritten by cH;p;d for some constant cH. Each row in Table 3 shows the average experimental result of those "ve equal-sized binary images, where p and tM denote the average number of bincodes and the average execution
536
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537
Fig. 6. (a) The Taiwan map; (b) the #ood map; (c) the world map; (d) the kid picture.
the proposed algorithm. In fact, the proposed region"lling algorithm on bincode-based boundary can be extended to 3-D case.
Table 3 Experimental results Image size
Depth d
p
tM (10\ s)
cH (10\)
2;2 2;2 2;2 2;2
5 6 7 8
126 421 758 1614
0.621 2.131 4.378 8.742
4.929 5.061 5.778 5.416
time, respectively. From cH in Table 3, it is observed that the execution time of the proposed algorithm is almost linearly proportional to the product p;d within a small range centered around 5. Hence, these results con"rmed the theoretical analysis.
5. Conclusions The signi"cance of region "lling is due to its popular use in the "elds of computer graphics, and computational geometry. Given a contour represented by p bincodes, we have presented the O(p;d)-time algorithm for region "lling on bincodes, where d denotes the depth of the corresponding bintree. In addition, some experimentations have been carried out to justify the performance of
References [1] Pavlidis T. Filling algorithms for raster graphics. Computer Graphics and Image Processing 1979;10:126}41. [2] Pavlidis T. Algorithms for graphics and image processing. New York: Springer, 1982. [3] Shani U. Filling regions in binary raster images * a graph theoretical approach. Proceedings of the ACM SIGGRAPH '80, 1980. p. 321}7. [4] Brassel KE, Fegeas R. An algorithm for shading of regions in vector display graphics. Proceedings of the ACM SIGGRAPH '79, 1979. p. 126}33. [5] Samet H. Applications of spatial data structures. New York: Addison-Wesley, 1990. [6] Samet H. The design and analysis of spatial data structures. New York: Addison-Wesley, 1990. [7] Samet H. Region representation: quadtrees from boundary codes. Communications of the ACM 1980;23:163}70. [8] Mark DM, Abel DJ. Linear quadtrees from vector representations of polygons. IEEE Transactions on Pattern Analysis and Machine Intelligence 1985;7(3):344}9. [9] Gargantini I, Atkison HH. Linear quadtrees: a blocking technique for contour "lling. Pattern Recognition 1984;17(3):285}93.
Y.-H. Tsai, K.-L. Chung / Computers & Graphics 24 (2000) 529}537 [10] Atkison HH, Gargantini I, Walsh TRS. Filling by quadrants or octants. Computer Vision Graphics Image Processing 1986;33:138}55. [11] Ouksel MA, Yaagoub A. The interpolation-based bintree and encoding of binary images. CVGIP: Graphical Models and Image Processing 1992;54(1):75}81. [12] Sha!er CA, Juvvadi R, Health LS. Generalized comparison of quadtree and bintree storage requirements. Image and Vision Computing 1993;11(7):402}12.
537
[13] Huang CY, Chung KL. Faster neighbor "nding on images represented by bincodes. Pattern Recognition 1996;29(9):1507}18. [14] Knowlton K. Progressive transmission of gray-scale and binary pictures by simple, e$cient, and lossless encoding schemes. Proceedings of the IEEE 1980;68:885}96. [15] Wu JG, Chung KL. The logical representation of bincode and its applications in manipulating binary images. Journal of Real-Time Imaging 1999;5:397}408.