An Adaptive Algorithm For Conversion From Quadtree To Chain Codes
An Adaptive Algorithm For Conversion From Quadtree To Chain Codes
Abstract
An adaptive algorithm is presented for converting the quadtree representation of a binary image to its chain code
representation. Our algorithm has the advantage of constructing the chain codes of the resulting quadtree of the Boolean
operation of two quadtrees by re-using the original chain codes. This algorithm is adaptive because it can adjust the total
number of internal nodes to be stored and retrieve it later in the reconstruction stage. The algorithm possesses parallelism
and is suited for pyramid architecture. Our algorithm requires time O(H#¸) in sequential and time O(N) in parallel,
where H is the height of the quadtree, ¸ is the length of the chain code sequence generated, and N;N is the size of the
input image. 2001 Pattern Recognition Society. Published by Elsevier Science Ltd. All rights reserved.
Keywords: Image representation; Chain code; Quadtree; Parallel algorithm; Adaptive conversion
0031-3203/01/$20.00 2001 Pattern Recognition Society. Published by Elsevier Science Ltd. All rights reserved.
PII: S 0 0 3 1 - 3 2 0 3 ( 0 0 ) 0 0 0 0 8 - X
632 F.Y. Shih, W.-T. Wong / Pattern Recognition 34 (2001) 631}639
wPne, rootPne, sPne, swcPne); and the resulting chain set from the merging or
chain coding. H/
/H traverse the southeast son of root H/
CHAIN Hlink}nw, Hlink}ne, Hlink}sw, Hlink}se, H#ink;
traverse (rootPse, rootPsw, rootPne, ePsw,
/H If it is a white node, no need to go further H/
sPne
if (rootPcolor""WHITE)
rootPnw, ePnw, secPnw, sPnw);
return (DUMMY}EMPTY}CHAIN);
,
else
function call( ); /H If the current level is not equal to the tree level,
, continue
the traverse H/
This algorithm descends the quadtree by recursively
if
calling the function of traverse with nine parameters
((ptype""NW}TYPE "" pptype""NW}TYPE)
which are the current node pointer (root) and
&& ctype""SW}TYPE)
the pointers of its eight neighbors (w, n, e, s, nwc,
pptype"NW}TYPE;
nec, sec, swc; where w is west, n is north, e is east, s is
else if
south and c is mnemonic for corner). Once the traversal
((ptype""SW}TYPE "" pptype""SW}TYPE)
step reaches the leaf node, the procedure functioncall( ) is
&& ctype ""NW}TYPE)
performed. The tracking for the 8 neighbors of the SE
pptype"SW}TYPE;
child of the most north-west quadrant in a 64;64 image
else if
is shown in Table 1. Here, we modify the quadtree traver-
((ptype""NE}TYPE "" pptype""NE}TYPE)
sal algorithm [14] to be suited for the chain coding
&& ctype""SE}TYPE)
process.
pptype"NE}TYPE;
/H return a chain set H/ else if
CHAIN Htraverse ( ((ptype""SE}TYPE "" pptype""SE}TYPE)
/H the left upper corner and the right lower corner && ctype "" NE}TYPE)
coordinates H/ pptype"SE}TYPE;
int x , y , x , y , else
/H parent, pseudo-parent and child type H/ pptype"ctype;
ptype, pptype, ctype,
/H grand parent node's color, current level and /H traverse the NW child H/
tree level H/ if (gpcolor""BLACK && ctype""SE}TYPE) +
grandparentcolor, current}level, tree}level, /H If its grandparent is a black node, its parent is
/H current root node and its 8-neighbors H/ SE}TYPE,
NODE root, w, n, e, s, nwc, nec, sec, swc) and it is a NW child, then no contour will be
+ generated.
/H new node pointers for the next traverse H/ No need to go further H/
NODE Hroot p, Hwp, Hnp, Hep, Hnwcp, Hnecp, Hsecp, link}nw"DUMMY}}EMPTY}}CHAIN;
Hswcp; ,
/H chain sets for the four children of the current root else +
node /H assign new node pointers for the NW child H/
Table 1
The tracking of the traversal alogrithm for the 8-neighbors of the SE child of the most northwest quadrant in a 32;32 image
Neighbor type 1st stage 2nd stage 3rd stage 4th stage
the correct color. For example, a leaf node which repres- image, if an object pixel is visited, a weighted window is
ents a pixel in the southern boundary of an image has no convolved with the 3;3 neighborhood centered at that
south neighbor-hood. This traversal algorithm will pre- pixel. This 3;3 window, which is incorporated with
tend that it has `whitea colored neighbors in s, swc and di!erent weights at each element exploring the presence
sec. The assignment for a node's 8 neighbors is done of eight neighboring locations, is used to calculate the
in the xx}child}assign procedure, where xx denotes index value of the look-up table. Then, a series of opera-
nw, ne, sw and se. If the color of a neighbor node is not tions are applied to concatenate these individual
gray, we use the same node instead for being no descend- codelinks with an existing chain or to create a new chain.
ant of that node. During the traverse, if it is a white node, The conditions for the concatenation are based on the
the further traverse will be skipped for being no contour matching of the coordinates and the chain codes of the
generation. If it is a black node and its grandparent's joining point.
color is black, then this black node may not generate To convert from the quadtree to chain code, the link-
a contour. The criteria are quite simple and described as list data structures which are convenient in the merging
follows: process are used. The nine link-list data structures in
Fig. 2 are used to describe a chain set. There are four
1. The black node is NW}TYPE and its parent is shared boundaries for a sub-image in a quadrant: north,
SE}TYPE. south, east and west boundaries. Link-lists H , H , H
2. The black node is NE}TYPE and its parent is L Q C
and H store the Head coordinates and the Headcode of
SW}TYPE. U
a chain. Link-lists ¹ , ¹ , ¹ , and H store the Tail
3. The black node is SW}TYPE and its parent is L Q C U
coordinates and the Thead of a chain. The subscripts
NE}TYPE. n, s, e and w denote the north (upper), south (lower), east
4. The black node is SE}TYPE and its parent is (right), and west (left) boundaries. The last link-list ¸ is
NW}TYPE.
for the code strings and information. The subscript c de-
notes the chain.
Therefore, this algorithm will only traverse the black When a codelink is created, we must "nd out which
nodes which lie on the boundary of objects. When the shared boundary its Head and Tail are located at. Later,
traverse reaches the deepest tree level, the chain}coding the merging procedure can be performed correctly. When
procedure is performed. The location (coordinates) of the the codelink is 0, it points to the east direction. It will
pixel is computed and passed down from each traversal connect to a chain whose Thead is located at its eastern
step. The X coordinate is equal to x and the > coordi-
shared boundary. Therefore, its Head coordinates of the
nate is equal to y . The coordinates start from left to
right and top to bottom. The coding procedure is per-
formed only on the leaf nodes including all virtual leaf
nodes of the quadtree at the tree level (deepest one). For
any other node, the merging procedure is performed. The
input of the merging procedure is the coding result or the
merging result of the subtree of that node.
codelink are added to H . When the codelink is 4, it If the codelink is 1, it points to the northeast direction.
C
points to the west direction. It will connect to a chain If the pixel which generates the codelink is a SE child in
whose Thead is located at its western shared boundary. a quadrant, the codelink will connect on the eastern
Therefore, its Head coordinates are added to H . From shared boundary. Therefore, its Head coordinates are
U
the similar concept, if the codelink is 2, we add them to added to H . If the pixel which generates the codelink is
C
H . If the codelink is 6, we add them to H . When a N= or S= child in a quadrant, the codelink will
L Q
the codelink is 1, 3, 5, or 7, it points to the northeast, connect on the northern shared boundary. Therefore, its
northwest, southwest and southeast direction, respectively. Head coordinates are added to H . If the pixel which
L
The shared boundary type where the connection occurs generates the codelink is a NE child in a quadrant, the
will depend on its pixel location. Before an example codelink will connect whether on the eastern shared
illustration, we need to describe the order in the merging boundary or the northern boundary. The exact shared
procedure to make it easier to understand the assignment boundary type where the connection will occur depends
of the Head coordinates. As aforementioned, a non-leaf on the pairs which are the parent type or the pseudo
node has four children. When a non-leaf node is merged, parent type and the type of the node. The pseudo-parent
we "rst merge the sub-trees of its nw and ne children. The type is an inheritance based on some special combina-
merging direction is the x-direction. Then, it follows by tions of the node and its parent. For example, if the type
the merging the sub-trees of its sw and se children. The of a node is S= and that of its parent is NE but its
merging direction is the y-direction. pseudo-parent type is N=, then it will pass down the
parent type S= and pseudo-parent type N= to its child.
If the situation is not in these rules, the pseudo-parent
Table 2 type is equal to the parent type. These rules are shown in
The generation rules of the pseudo-parent type of a node Table 2. From the similar deduction, the shared bound-
aries where the Tail of codelinks are located are classi"ed.
Parent type or Node type Pseudo-parent type Both of them are shown in Table 3. Codelink `1a is used
pseudo-parent type passed down to child as an example to illustrate the classi"cation in Fig. 3. In
the "gure, we only show the N= portion (16;16) of
NW SW NW
a 32;32 input image. All other cases can be derived by
NE SE NE
SW NW SW the same concept.
SE NE SE The order of the codelink creation on a pixel has been
shown in Fig. 4. The order granted to each codelink
Table 3
Classi"cation of the shared boundary occurrence for the Head and Tail of a codelink
Headcode; Thead Child type in a quadrant Parent or pseudo-parent type Add to shared boundary
Table 4
The particular cases of the two Heads generated on a pixel will
not be in order
3&5 NW SW
SW NW
1&7 NE SE
SE NE
The link-list data structure used to describe the subtree nodes in a quadtree. The result of the merging process for
of a node is simple and easy to maintain. During the each internal node and leaf with black color is shown in
merging procedure, we can save the intermediate result of Table 5. C, E, F, J and K are leaves with black color but
all the nodes which are smaller than a given level. This only J and K are individual pixels in the input image.
information will be retrieved when we want to "nd the B, D, G, H and I are internal nodes. Root node is marked
chain code contour of the new constructed quadtree. This as A which has two closed chains. They are represented
step will avoid lots of unnecessary re-coding processes. as (0, 1) 0246 and (3,4) 444666000001044432102465,
During the Boolean operation of two quadtrees, if the where (x, y) are column and row coordinates of the start-
color of a node in the original quadtree is changed, ing pixel in the chain code description and the numbers
we will mark it and all its ancestors are changed. After follow denote the code sequence of the contour. The code
the Boolean operation is "nished, we perform the chain sequence is read from left to right.
coding algorithm. If a node is not marked, we need not
go further. Instead, we retrieve the link-list information 6. Analysis
from what we have saved before. If a node is marked, we
track its four children to see whether any of them is Let the size of an image be N;N and the number of
marked. This process is performed recursively until the boundary pixels be O(B). The height of the quadtree
leaf node is reached or all the four children of the node (including virtual leaves) is H, where N"2&. Each chain
are not marked. coding and merging operation costs time O(1). The time
Since it is impossible to save all the nodes' information, requiring merging of the chains of two quadrants is
we can save a portion of nodes instead. By given a level dependent on the number of joints in these two quad-
number, we can save all the information of nodes which rants. There is no need to sort or search for the two pairs
are less than or equal to that level number. During the of the joint (the boundary pixel) since the Heads and
chain coding of the new quadtree, if a node is not marked Tails are in order. In the sequential mode, the total
and its level number is less than or equal to the given number of joints in the image is in the same order of
level, we can retrieve their chain codes directly. If its level boundary pixels and that is the time needed in the tra-
number is larger than the given level, we must track all its verse of nodes. The complexity of our algorithm is depen-
descendant. This approach is adaptive. It the hardware dent on the height of the traverse plus the operation of
has plenty of memory, we should use a small level num- the boundary pixels. Therefore, it is O(H#B).
ber. Otherwise we use a big number. For a pyramid architecture with O(N) processors and
each processor handling a subquadrant from its parent,
the traverse of the tree needs time O(H) in parallel. The
5. Experimental results chain coding operation is O(1). The merging operation in
a processor of level l requires time which is in the same
We use Fig. 1 as an example to illustrate to apply our order of the number of open chains. The worst case
conversion algorithm. Recall that the chain coding pro- occurs when the order open chains' number is equal to
cess is performed on the leaf nodes or the virtue leaf the order of the side's length of the quadrant in level l. If
Table 5
The result of the merging process for each node in Fig. 5
C (0, 1) 0246
B (0, 1) 0246
E H (3, 4) ¹ (4, 3) 02465
Q Q
D H (3, 4) ¹ (4, 3) 02465
Q Q
F H (4, 7) ¹ (3, 4) 444666000
C L
H (4, 3) ¹ (3, 5) 21
L C
H H (3, 5) ¹ (5, 6) 43
U C
H (6, 6) ¹ (4, 7) 01
C U
J H (7, 6) ¹ (6, 6) 0
C U
H (5, 6) ¹ (6, 6) 4
U C
K H (6, 6) ¹ (7, 6) 4
U U
I H (5, 6) ¹ (6, 6) 044
U U
G H (3, 5) ¹ (4, 7) 0104443
U U
A (0, 1) 0246
(3, 4) 444666000001044432102465
F.Y. Shih, W.-T. Wong / Pattern Recognition 34 (2001) 631}639 639
the number of the pixels of a quadrant in level l is n;n, [4] Y. Hung, A. Rosenfeld, Parallel processing of linear quad-
the time for the merging operation will require O(n). If we tree on a mesh-connected computer, J. Parallel Distrib.
sum up the merging time for all levels in parallel, which is Comput. 7 (1989) 1}27.
2&\#2#2, it will be O(2&) or O(N). [5] H. Freeman, Computer processing of line drawing images,
Comput. Surveys 6 (1) (1974) 57}97.
[6] F.Y. Shih, W-T. Wong, A new single-pass algorithm
for extracting the mid-crack codes of multiple regions, J.
7. Conclusion Visual Commun. Image Rep. 3 (3) (1992) 217}224.
[7] W-T. Wong, Y-L. Chen, F.Y. Shih, A fully parallel algo-
In this paper, an adaptive algorithm for converting the rithm for the extraction of chain and mid-crack codes of
quadtree representation to the chain code representation multiple contours, Conference Proceedings of Interna-
is presented. Di!erent from other algorithms which need tional Computer Symposium, HsinChu, Taiwan, R.O.C.,
to search for the neighbors of the current node, the 1994, pp. 565}570.
algorithm is based on traversing the quadtree. Therefore, [8] P. Zingaretti, M. Gasparroni, L. Vecci, Fast Chain Coding
it is recursive and has the parallelism. It can be easily of Region Boundaries, IEEE Trans. Pattern Anal. Mach.
Intell. 20 (4) (1998) 407}415.
implemented in a pyramid architecture. Besides, we in-
[9] H. Samet, Region Representation: quadtree from bound-
troduce an adaptive method to construct the chain codes
ary codes, Commun. ACM 23 (3) (1980) 163}170.
for the resulting quadtree of the Boolean operation of [10] M.R. Lattanzi, C.A. Sha!er, An optimal boundary to
two quadtrees by re-using the original chain codes. This quadtree conversion algorithm, CVGIP: Image Under-
adaptive method is quite useful and signi"cantly speeds standing 53 (3) (1991) 303}312.
up the conversion in all kinds of applications. [11] C.R. Dyer, A. Rosenfeld, H. Samet, Region representation:
boundary codes from quadtrees, Commun. ACM 23 (3)
(1980) 171}179.
References [12] G.N. Kumar, N. Nandhakumar, E$cient object contour
tracing in a quadtree encoded image, SPIE Appl. Artif.
[1] H. Samet, Applications of Spatial Data Structures Intell. IX 1468 (1991).
[13] D.R. Fuhrmann, Quadtree traversal algorithms for
* Computer Graphics, Image Processing, and GIS,
Addison-Wesley, New York, 1990. pointer-based and depth-"rst representation, IEEE Trans.
[2] T-W. Lin, Set operations on constant bit-length Pattern Anal. Mach. Intell. 10 (6) (1988) 955}960.
[14] H. Samet, A top-down quadtree traversal algorithm, IEEE
linear quadtrees, Pattern Recognition 30 (7) (1997)
1239}1249. Trans. Pattern Anal. Mach. Intell. (T-PAMI) 7 (1) (1985)
[3] F. Dehne, A. Rau-Chaplin, A.G. Ferreira, Hypercube 94}98.
[15] F.Y. Shih, W-T. Wong, A one-pass algorithm for local
algorithms for parallel processing of pointer-based quad-
tree, Comput. Vision Image Understanding 62 (1) (1995) symmetry of contours from chain code, Pattern Recogni-
1}10. tion 32 (7) (1999) 1203}1210.
About the Author*FRANK Y. SHIH received the B.S. degree from National Cheng-Kung University, Taiwan, in 1980, the M.S. degree
from the State University of New York at Stony Brook, in 1984, and the Ph.D. degree from Purdue University, West Lafayette, Indiana,
in 1987, all in electrical engineering. He is presently a professor jointly appointed in the Department of Computer and Information
Science (CIS) and the Department of Electrical and Computer Engineering (ECE) at New Jersey Institute of Technology, Newark, New
Jersey. He currently serves as an associate chairman of the CIS department and the director of Computer Vision Laboratory. Dr. Shih is
on the Editorial Board of the International Journal of Systems Integration. He is also an associate editor of the International Journal of
Information Sciences, and of the International Journal of Pattern Recognition. He has served as a member of several organizing
committees for technical conferences and workshops. He was the recipient of the Research Initiation Award from the National Science
Foundation in 1991. He was the recipient of the Winner of the International Pattern Recognition Society Award for Outstanding Paper
Contribution. He has received several awards for distinguished research at New Jersey Institute of Technology. He has served several
times in the Proposal Review panel of the National Science Foundation on Computer Vision and Machine Intelligence. He holds the
IEEE senior membership. Dr. Shih has published over 110 technical papers in well-known prestigious journals and conferences. His
current research interests include image processing, computer vision, computer graphics, arti"cial intelligence, expert systems, robotics,
computer architecture, fuzzy logic, and neural networks.
About the Author*WAI-TAK-WONG received the B.S. degree in Chemical Engineering from National Taiwan University in 1986. He
received the M.S. degree and the Ph.D. degree in Computer and Information Science from New Jersey Institute of Technology in 1992
and 1999. He has worked for Syncsort Incorporate as a Software Engineer to develop enterprise level heterogeneous client server
computing software since 1995. He received the Pattern Recognition Society Award in 1995. His current research interests are parallel
algorithms, image processing, client server computing, distributed computing and software engineering.