An Effective Way to Represent Quadtrees - Irene Gargantini

download An Effective Way to Represent Quadtrees - Irene Gargantini

of 6

Transcript of An Effective Way to Represent Quadtrees - Irene Gargantini

  • 8/3/2019 An Effective Way to Represent Quadtrees - Irene Gargantini

    1/6

    References1 . Berz t iss , A.T. Data S tructures: Theory and Pract ice . 2 n d E d .Ac a d e m ic P r es s , Ne w Y o r k , 1 9 7 5 .2 . C a v o u r a s , J . C . On th e c o n v e r s io n o f p r o g r a m s to d e c i s io n t a b l e s :M e th o d s a n d o b j e c t i v e s . C o m m , A C M 17, 8 (Aug. 1974), 456--462.3 . C o o p e r , C .B . , B 6 h m a n d Ja c o p in i ' s r e d u c t i o n o f f l o wc h a r t s ,C o m m . A C M 1 0, 8 (Aug. 1967), 463, 4734 . I m b r a s h a , M . a n d R a j a r a m a n , V . De te c t i o n o f l o g i c a l e r r o r s i nd e c i s io n t a b l e p r o g r a m s . C o m m . A C M 2 1 , 12 (De c. 1978), 1016-1025.5 . K n u t h , D . E ., S t ru c t u r e d p r o g r a m m i n g w i t h G O T O s t a t e m e n ts .Co m p u t in g S u rv e y s 6 , 4 (Dec. 1974), 261-301 .6 . L e d g a r d , H . F . a n d M a r c o t ty , M . A g e n e a lo g y o f c o n t r o ls t ructures . C o m m . A C M 18 , l 1 (Nov . 1975), 629-639 .7 . L e w, A . Op t im a l c o n v e r s io n o f e x t e n d e d - e n t r y d e c i s io n t a b l e swi th g e n e r a l c o s t c r i t e r i a . C o m m . A C M 2 1, 4 (Apr . 1978) , 269-279 .8 . L e w, A ., a n d T a m a n a h a , D . D e c i s i o n t a b l e p r o g r a m m i n g a n dre l iab i l i ty . Proc. 2nd In t l . Conf . on So f twar e Engr., 1976 , 345-349 .9 . M a e s , R . On th e r e p r e se n t a t i o n o f p r o g r a m s t r u c tu r e s b y d e c i s io nt a b l e s : A c r i t i c a l a s se s sm e n t . Comptr. J. 21, 4 (Nov. 1978) , 290-295 .1 0 . M c C a b e , T . J . A c o m p le x i ty m e a su r e . 1 E E E Tra n s . o n S o f twa reEngrg . , SE-2 , 4 (Dec. 1976) , 308-320 .1 1 . M e tz n e r , J . R . , a n d B a r n e s , B .H . D e c is ion Ta b le La n g u a g e s a n dS y s t e m s . Ac a d e m ic P r e s s , Ne w Yo r k , 1 9 7 7 .1 2 . M o n ta lb a n o , M . Decision Tables. Sc ie n c e R e se a r c h A sso c i a te s ,Chicago , 1974 .1 3 . Pe t e r so n , W . W . , Ka sa m i , T . , a n d T o k u r a , N . O n th e c a p a b i l i t y o fwh i l e , r e p e a t , a n d e x i t s t a t e m e n t s . C o m m . A C M 1 6 , 8 (Aug. 1973),503-512 .14 . Po l lack , S.L. , Hicks, H. , and Harr ison , W.J. Decision Tables:Theory and Pract ice, W ile y , Ne w Yo r k , 1 9 7 1 .1 5 . Po o c h , U . W . T r a n s l a t i o n o f d e c i s io n t a b l e s . Co m p u t in g S u rv e y s 6 ,2 (June 1974), 125-151.1 6 . T r a k h t e n b r o t , B . A . A lg o r i t h m s a n d A u to m a t i c Co m p u t in gMa c h in e s . D. C . H e a th a n d C o . , L e x in g to n , M a ss . , 1 9 63 .

    G r a p h i c s a n dI m a g e P r o c e s s i n g Jam es Fo ley*Edi torAn Ef fect ive Way toRepresent QuadtreesIrene GargantiniTh e U nivers ity o f Western Ontar io

    A quadtree may be represented without pointers byencoding each b lack node wi th a quaternary integerwh ose digits ref lect succe ss ive quadrant subdivis ions . W erefer to the sorted array of black nodes as the "l inearquadtree" and show that i t introduc es a saving o f at least66 percent o f the com puter s torage required by regularquadtrees . Som e a lgor i thm s us ing l inear quadtrees arepresented , nam ely , ( i ) encoding a p ixe l f rom a 2" 2"array (or screen) into i ts quaternary code; ( i i ) findingadjacent nodes; ( i i i ) determining the colo r of a node; ( i v )superpos ing two im ages . I t i s shown that a lgor ithm s (1 ) -( i i i ) can be execu ted in logar ithm ic t im e , whi le superpo-s it ion can be carried out in l inear t ime with respect tothe to ta l num ber o f b lack nodes . The paper a l so showsthat the dynamic capabil i ty of a quadtree can be ef fec-tively simulated.

    CR Categories and Subject Descriptors: 1 .4.2 [ImageProcessing]: C o m p r e s s i o n - - q u a d t r e e sGenera l Term : A lgor i thm sAdditional Key Words and Phrases: d i g i t a l i m a g e s ,i m a g e e n c o d i n g

    905

    1. IntroductionTh e qu a d t re e [2 , 3 , 7 , 8 ] i s a h i e ra rc h ic a l r e p re se n ta -t i o n o f a 2 n x 2 n b i n a r y a r r a y t h a t i s m a d e o f u n i t s q u a r e

    p i x e ls t h a t c a n b e b l a c k o r w h i t e . W e r e f e r t o t h e s u b s e t* F o r m e r e d i t o r o f G r a p h i c s a n d I m a g e P r o c e s si n g . R o b e r t H a r -a l ic k i s t h e c u r r e n t e d i t o r o f t h e d e p a r t m e n t , w h i c h h a s r e c e n t l y b e e nr e n a m e d I m a g e P r o c e s s in g a n d C o m p u t e r V i s i o n . (S e e A p r i l ' 8 2 C o m -

    munications, p p . 3 1 1 - 3 1 2 . )T h i s w o r k w a s s p o n so r e d b y th e C a n a d i a n G o v e r n m e n t u n d e r t h eN S E R C g r a nt # A 7 1 36 .A u t h o r ' s P r e s e n t A d d r es s : 1 . G a r g a n t i n i , T h e U n i v e r s i t y o f W e s t -e r n O n t a r i o, D e p a r t m e n t o f C o m p u t e r S c i e n ce , L o n d o n , C a n a d a N 6 A5B9.Pe r m i ss io n t o c o p y wi th o u t f e e a l l o r p a r t o f t h i s m a te r i a l i sg r a n t e d p r o v id e d t h a t t h e c o p i e s a r e n o t m a d e o r d i s t r i b u t e d f o r d i r e c tc o m m e r c i a l a d v a n ta g e , t h e A C M c o p y r i gh t n o t i c e a n d t h e t i tl e o f t h ep u b l i c a t i o n a n d i t s d a t e a p p e a r , a n d n o t i c e i s g iv e n t h a t c o p y in g i s b yp e r m i s s i o n o f t h e A s s o c i a t i o n f o r C o m p u t i n g M a c h i n e r y . T o c o p yo th e r wi se , o r t o r e p u b l i sh , r e q u i r e s a f e e a n d /o r sp e c i f i c p e r m i ss io n . 1 9 82 AC M 0 0 0 1 - 0 7 8 2 /8 2 /1 2 0 0 ~ 0 9 0 5 $ 0 0 .7 5 .C o m m u n i c a t i o n s D e c e m b e r 1 98 2o f V o l u m e 2 5t h e A C M N u m b e r 1 2

  • 8/3/2019 An Effective Way to Represent Quadtrees - Irene Gargantini

    2/6

    o f b l a c k p i x el s a s t h e " r e g i o n " a n d t o th e s u b s e t o f w h i t ep i x el s a s th e " r e g i o n ' s b a c k g r o u n d . " A s e x t e n s i v e l y d is -c u s s e d i n t h e l i t e r a t u r e [ 1 - 3 , 9 - 1 1 ] , a q u a d t r e e c a n b ee f f e c t i v e l y u s e d t o d e s c r i b e t h e s u c c e s s iv e p a r t i t i o n s o f a2 " x 2 " a r r a y ( n ->_ 1 ) i n t o qu a d r a n t s , t o s e p a r a t e a r e g i o nf r o m i t s b a c k g r o u n d , a n d t o r e p r e s e n t a s e t o f p i x el sb e l o n g i n g t o t h e s a m e q u a d r a n t ( a t a n y l e v e l o f s u b d i -v i s io n ) a s a s in g l e n o d e . T h e q u a d t r e e c a n b e d e f i n e d a sa t r e e w h o s e n o d e s a r e e i t h e r le a v e s o r h a v e f o u r s o n s . An o d e c a n b e g r e y , w h i t e , o r b l a c k a n d i s , i n g e n e r a l ,s t o r e d a s a r e c o r d w i t h s i x fi e ld s , f i v e o f w h i c h a r ep o i n t e r s ( t o t h e N W , N E , S W , a n d S E q u a d r a n t s ) a n dt h e s i x t h i s a c o l o r ' s i d e n t i f i e r .T h e p r e s e n t p a p e r i n t r o d u c e s a n e w s t r u ct u r e , c a ll e da " l i n e a r q u a d t r e e , " w i t h t h e f o l l o w i n g c h a r a c t e r i s t i c s :

    ( i) O n l y b l a c k n o d e s a r e s t o re d .( i i ) T h e e n c o d i n g u s e d f o r e a c h n o d e i n c o r p o r a t e s

    a d j a c e n c y p r o p e r t i e s i n t h e f o u r p r i n c i p a l d i r e c ti o n s .( i i i ) T h e n o d e r e p r e s e n t a t i o n i m p l i c i t l y e n c o d e s t h e

    p a t h f r o m t h e r o o t to t h e n o d e .T h e m a i n a d v a n t a g e s o f l in e a r q u a d t r e e s , w i t h r e-

    s p e c t to q u a d t r e e s , a r e :( i) S p a c e a n d t i m e c o m p l e x i t y d e p e n d o n l y o n t h en u m b e r o f b la c k n o d e s .

    ( i i ) P o i n t e r s a r e e l i m i n a t e d .

    2 . E n c o d i n g B l a c k P i x e l sT h e f i r s t s t e p i s t o d e v i s e a n a l g o r i t h m t o e n c o d e

    b l a c k p i x e ls . T h e r e a r e t w o d i s ti n c t c a se s o f t h is p r o b l e m ,o n e o f w h i c h i s a s s o c i a t e d w i t h i m a g e p r o c e s s i n g [ 2, 8 , 9 ]w h e r e t h e i n p u t i s r a n d o m l y g e n e r a t e d . I n t h e o t h e r c a se ,f o u n d m a i n l y i n n u m e r i c a l a n a l y si s , a ll th e b l a c k p i x e lsi n t h e 2 " x 2 ~ a r r a y m a y b e r a n d o m l y a c c e s s ed a n d t h eo r d e r o f in p u t m a y t h u s b e p r e d e t e r m i n e d .

    S i n c e t h e f i r s t c a s e i s m o r e g e n e r a l , w e c o n c e n t r a t eo n i t. W e a d o p t t h e f o l l o w i n g c o n v e n t io n s :

    ( i) T h e N W q u a d r a n t i s e n c o d e d w i t h 0 , t h e N E w i t h1 , t h e S W w i t h 2 , a n d t h e S E w i t h 3 . E a c h b l a c k p i x e l i st h e n e n c o d e d i n a w e i g h t e d q u a t e r n a r y c o d e , i . e . , w i t hd i g i t s 0 , 1 , 2 , 3 i n b a s e 4 , wh e r e e a c h s u c c e s s i v e d i g i tr e p r e s e n ts t h e q u a d r a n t s u b d i v i s io n f r o m w h i c h i t o r ig i -n a t e s . Th u s , t h e d i g i t o f we i g h t 4 ~ - h , 1 < h _-< n , i d e n t i f i e st h e q u a d r a n t t o w h i c h t h e p i x e l b e l o n g s a t t h e h t hs u b d i v i s i o n . ( Se e F i g . 1 . )

    ( i i ) T h e p a i r o f i n t e g er s (I , J ) , w i t h I , J = 0 , 1 . . . . 2 "- 1 i d e n t i f ie s t h e p o s i t i o n o f a p i x e l i n t h e 2 " x 2 " a r r a y .T h e e n c o d i n g p r o c e d u r e c o n si s ts o f m a p p i n g t h e p a i r ( I ,J ) i n t o a n i n t e g e r K i n b a s e 4 , w h i c h e x p r e s s e s t h es u c c e s s i v e q u a d r a n t s t o w h i c h t h e ( I , J ) p i x e l b e l o n g s .F o r e x a m p l e , i f n = 3 a n d ( I , J ) = ( 6, 5 ), K w i l l b e 3 2 1 .T h i s m e a n s t h a t p i x e l ( 6, 5 ) b e l o n g s t o t h e S E q u a d r a n ti n t h e f ir s t s u b d i v is i o n , t o th e S W q u a d r a n t i n t h e s e c o n d ,a n d t o t h e N E i n t h e t h i r d ( f i n a l ) s u b d i v i s i o n . ( S e e F i g .1. )

    Fig. 1. Q uadrants Labeling and G eneration of Quaternary Codes.0 i" , , : /

    3 0 3021 03 0 031i02.5 032 0:33

    2 1 0 2 1 112230C 301 3 10 311

    212 213 302 30 3 312 3133 2 0 32 1 3 3 0 33 13 2 2 !3 2 3 3 3 2 3 3 3

    / I I \2 3

    T o f i n d K , w e f ir s t w r i t e I a n d J a sI = C n - 1 2 n - 1 + C ~ - 2 2 ~ - 2 + + COJ = d n - 1 2 n - 1 + d n - 2 2 n- 2 + . . . + d o (1 )

    w h e r e c i , d i i s e i t h e r 0 o r 1 . ( S i n c e / , J l i e b e t w e e n 0 a n d2 " - 1 , 2 " - 1 is th e h i g h e s t p o w e r i n t h e b i n a r y r e p r e s e n -t a t i o n . ) T h u s , c i a n d d / i n d i c a t e w h i c h q u a d r a n t ( / , J )b e l o n g s t o a t e a c h l e v e l o f s u b d i v i s i o n . I n f a c t , i f ( /, J )is i n th e N W q u a d r a n t , b o t h h i g h e s t p o w e r s o f 2 a r em i s s i n g , i . e. , c , - 1 = d , - ~ = 0 . I f p i x e l ( 1 , J ) i s i n t h e N Eq u a d r a n t , t h e h i g h e s t p o w e r o f 2 in t h e ! r e p r e s e n t a t i o ni s m i s s i n g , wh i l e t h e o n e r e l a t i v e t o J i s n o t , i . e . , Cn - x =0 , d , - x = 1 . S i m i l a r l y , i f ( / , J ) i s i n th e S W q u a d r a n t ,c , _ ~ = 1 , d n - x = 0 , a n d i f ( L J ) i s i n t h e S E q u a d r a n t ,t h e n c , - ~ - -- d ~ -~ = 1 . O n c e t h e f i r s t ( l a r g e s t ) qu a d r a n t i si d e n ti f ie d , w e p a r t i t io n i t i n t o f o u r q u a d r a n t s a n d r e p e a tt h e p r o c e d u r e f o r ( n - 2 ) a n d s o o n , u n t i l t h e l a s tq u a d r a n t c o n s is t s o f o n l y f o u r e l e m e n t s . T h e r e f o r e , w eh a v e :

    p r o c e d u r e E N C O D I N G (n , c , d , K ); v a lu e n ;re f e re n c e c , d , K ; i n t e g e r n ; i n t e g e r a r ra y c , d , K ;(* g iven n , c and d , f ind qua ternary code K *)b e g in i n t e g e r l ;

    fo r l , ,--- n - 1 step - 1 unt il 0 dob e g ini f c [ l ] = 0 a n d d [ l ] = 0 t he n K [ I ] = 0;i f c [ l ] = 0 a n d d [ l ] - - 1 t h e n K [ l ] = 1;i f c [ l ] = 1 a n d d i l l = 0 th e n K [ I ] = 2;i f c [ l ] = 1 a n d d i l l - ' 1 t h en K [ I ] = 3e n d

    end;I n o u r p r e v i o u s e x a m p l e , 1 = 1 . 2 2 + 1 . 2 1 + 0 . 2 , J =1 . 2 2 + 0 . 2 1 + 1 . 2 0 a n d t h u s, K = 3 2 1.

    A f t e r w e h a v e e n c o d e d a l l th e b l a c k p i x e l s in t o t h e i rc o r r e s p o n d i n g q u a t e r n a r y c o d e s , w e s o r t t h e m a n d s t o r e

    9O 6 Comm unications Decem ber 1982of Volume 25the ACM Num ber 12

  • 8/3/2019 An Effective Way to Represent Quadtrees - Irene Gargantini

    3/6

    3 . A d j a c e n c y

    Fig. 2. Quad tree for Reg ion of Fig. 1.

    M o s t f r e q u e n t l y , s t o r i n g p i x e l s i s m o t i v a t e d b y s o m ek i n d o f a p p l i c a ti o n s u c h a s d e t e r m i n a t i o n o f t h e b o u n d -a r y o f a r e g i o n , d e t e r m i n a t i o n o f c o n n e c t iv i t y , a n d t h el i k e . D i r e c t l y o r i n d i r e c t l y , t h e s e p r o b l e m s r e q u i r e f i n d -i n g t h e p i x e l a d j a c e n t t o a g i v e n o n e i n a s p e c i f i e dd i r ec t io n . W i t h t h e n e w l y p r o p o s e d s t ru c t u r e , t h e d e t e r -m i n a t i o n o f a d j a c e n c y i s q u i t e s i m p le .

    L e t K = ( K n - l K n - 2 . K 0 )4 b e t h e g i v e n p i x e l a n d S= ( S n - I S n - 2 . . . S 0 ) 4 i t s a d j a c e n t n o d e i n t h e s o u t h e r n

    d i r e c t i o n . A s a n e x a m p l e , w e s h o w h o w t o d e t e r m i n ed i g i ts S n - 1 , S n -2 . . . . . S o .W e d i s t i n g u i s h t w o c a s e s : i n t h e f i r s t o n e , K a n d S

    b e l o n g t o t h e s a m e q u a d r a n t r e l a ti v e to t h e n t h s u b d i v i -s i o n ; i n t h e o t h e r , K a n d S d o n o t . I f t h e l a s t d i g i t K 0 i se i t h e r 0 o r 1 , t h e n t h e n o d e a d j a c e n t t o K i n t h e s o u t h e r nd i r e c t i o n l i e s i n t h e s a m e q u a d r a n t ( r e l a t i v e t o t h e n t hs u b d i v i s i o n ) a s K . ( S e e F i g . 3 . ) T h e r e f o r e , S o = 2 i f K o= 0 , a n d So = 3 i f Ko = 1 . A l l o t h e r d i g i t s a r e t h e s a m e .F o r i n s t a n c e , f o r K = 2 1 0 1 ( s ee F i g . 3) , t h e a d j a c e n t n o d ei n t h e s o u t h e r n d i r e c t i o n is S = 2 1 0 3 .

    I n t h e s e c o n d c a s e , i .e . , i f K0 is e i t h e r 2 o r 3 , at r a n s it i o n f r o m t w o ' d i f fe r e n t ' q u a d r a n t s o c c u r s. W e n o t eth a t S 0 = 0 i f K o = 2 , a n d S 0 = l i f K 0 = 3 . S l = ( K ~ +2 ) m o d u l o 4 i n b o t h c a s e s . ( S e e F i g . 3 . ) F o r a l l o t h e rdigi ts , w e r em ar k th at Si , 2 =< i _-- 3 . W e r e f e r t o th i s s o r t e d a r r a y a s t h e l i n e a rq u a d t r e e . T h e r e g i o n s h o w n i n F i g . 1 , f o r in s t a n c e , i se n c o d e d b y t h e s e q u e n c e , 0 0 3 02 1 0 2 3 0 3 X 1 2 2 2 1 X 3 X X .N o t e t h a t t h e l i n e a r q u a d t r e e c o r r e s p o n d s t o th e p o s t o r -d e r t r a v e r s a l o f b l a c k n o d e s o f a q u a d t r e e , e . g . , F i g . 2 .

    T h e p r o c e s s o f d e c o d i n g a q u a t e r n a r y c o d e i s l ef t t ot h e r e a d e r. W e n o t e t h a t t h e c o n c e p t o f l a b e l in g s q u a r ep i x e l s w i t h q u a t e r n a r y c o d e s a n d c u b i c p i x e l s ( v o x e l s )wi t h o c t a l c o d e s i s a l s o d i s c u s s e d i n [ 4 - 6 , 1 2 ] .

    907 Comm unications Decem ber 1982of Volume 25the ACM Num ber 12

  • 8/3/2019 An Effective Way to Represent Quadtrees - Irene Gargantini

    4/6

    Fig. 3. Qu ater nary Codes for n = 4.

    0000 0001

    0 0 0 2 0 0 0 3

    ] 0 2 0 00213 0 2 2 0 0 2 3

    3200 0201]202 02033220 02213222 0223

    I

    2000 20012002 2003 . o . o

    ooio o o i i0012 0013

    0030 00310032 0033I l l0210 02110212 0213:0230 02310232 0233

    2010 20112012 2013o q . o o o ~

    0100 0101 0110 0111

    0102 0103 0112 0113

    0120 0121 0130 01310122 0123 0132 0133

    a O O Q

    I O I Q

    21002102o o t

    2 1 o2103. o o o

    NW$W

    r0001002

    NE$ E

    J o o

    4 . SearchL e t K ' d e n o t e t h e p i x e l a d j a c e n t t o K i n t h e w a n t e d

    d i r e c t i o n . S i n c e w e s t o r e o n l y b l a c k n o d e s , w e h a v e t od e t e r m i n e w h e t h e r K ' i s b l a c k o r w h i t e b y s e a r c h i n g t h el i n e a r q u a d t r e e . L e t Y d e n o t e th e n o d e u s e d a s c o m p a r -a n d . I t s r e p r e s e n t a t i o n c o n s i s t s o f 1 _-_ k ==_ n qu a t e r n a r yd i g i ts (0 , 1 , 2 , 3 ) a n d ( n - k ) m a r k e r s t h a t w e d e n o t e , a sb e f o r e , b y X ' s. T h e c r i t e r i o n u s e d t o d e c i d e i f K ' i s b l a c ko r w h i t e i s c o n t a i n e d i n t h e f o l l o w i n g i n f o r m a l a l g o -r i t h m i c d e s c r i p t i o n .

    A p p l y a b i n a r y s e a r c h a n d d i s t i n g u i s h t h e f o l l o w i n gcases :( i ) A n o d e Y i s f o u n d w i t h n d i g i ts i d e n t i c a l t o t h ed i g i ts o f K ' , i . e. , K ' = Y . I t fo l l o w s t h a t K ' i s b l a c k .( i i ) A n o d e Y i s f o u n d w i t h k < n d ig i t s i d e n t i c a l t ot h o s e o f K ' a n d w i t h ( n - k ) m a r k e r s ( X ' s) in t h er e m a i n i n g ( w e i g h t e d ) p o s i ti o n s . I n th i s c a se , K ' C Y , a n d

    i t fo l l o w s t h a t K ' i s b l a c k .( i i i ) I f n e i t h e r o f t h e p r e v i o u s c a s e s h o l d , t h e n i tf o l l o w s th a t K ' i s w h i t e .5 . Dynam ic Struc ture

    O n e o f t h e m o s t u s e f u l f e a tu r e s o f th e q u a d t r e e s i st h a t a n i n c r e a se o f t h e r e s o l u t i o n p a r a m e t e r , f o r i n s t a n c e,f r o m n t o n + 1 , r e q u i r e s f u r t h e r s u b d i v i s i o n o f t h et e r m i n a l n o d e s o n l y , l e a v i n g t h e r e m a i n i n g p a r t o f t h et r e e u n c h a n g e d . R o u g h l y s p e a k i n g , a ' r e f in e m e n t ' o f t h ei m a g e m e r e l y r e q u i r es a ' r e f i n e m e n t ' o f t h e l e a v es . Q u a d -908

    t r e e s, t h e r e f o r e , a r e e x c e l l e n t s tr u c t u r e s f o r th e s t o r a g e o f( s lo w l y ) c h a n g i n g p i c tu r e s . W e r e f e r to t h i s p r o p e r t y a st h e ' d y n a m i c r e f in e m e n t ' o f a q u a d t r e e .

    A t t h i s p o i n t t h e r e a d e r m a y w o n d e r h o w m u c h w eh a v e l o s t, in t e r m s o f d y n a m i c r e f i n e m e n t , b y r e p l a c i n ga q u a d t r e e w i t h a l i s t o r a r r a y . W e d i s t i n g u i s h t w o c a s e s .T h e f i r s t c o r r e s p o n d s t o r e f i n i n g o n l y b l a c k n o d e s ; i nt h is c a s e, t h e c r e a t i o n o f a n e w a r r a y t o s t o r e t h e u p d a t e db l o c k s a t le v e l ( n + 1 ) r e q u i r e s n o m o r e n o d e s t h a n i n at r a d i t io n a l q u a d t r e e . T h e a r r a y a t l e v e l n c a n b e r e l e a s e dt o t h e f r e e s p a c e .

    T h e s e c o n d c a s e o c c u r s w h e n w e w a n t t o r e f i n e a l ln o d e s , b l a c k a n d w h i t e . I n a q u a d t r e e w e r e f i n e al lt e r m i n a l n o d e s . R e f i n i n g a w h i t e n o d e i s t r e a t e d i ne x a c t l y t h e s a m e w a y a s r e fi n i n g a b l a c k o n e . I n o u rl i n e a r q u a d t r e e w e h a v e t o s u p p l y a f a s t a l g o r i t h mc a p a b l e o f i d e n t if y i n g t h e w h i t e n o d e s n o t e x p l i c i tl ys t o r e d . W e c a n c o m p e t e w i t h q u a d t r e e s , i n t e r m s o fs t r u c t u r e f l e x i b i li t y , o n l y i f w e c a n g e n e r a t e t h e l a r g e s tp o s s i b l e c o v e r i n g s o f w h i t e p i x e l s i n t h e c o r r e c t p o s i t i o na s w e t r a v e rs e t h e l is t o f b l a c k n o d e s . W e c a n d o t h is b ya n a l y z i n g t h e e n c o d i n g o f s u c c es s iv e p a i rs o f b l a c k n o d e s .L e t u s s t a rt w i t h a n e x a m p l e . T a k e n = 4 a n d t w oc o n s e c u t i v e b l a c k b l o c k s, Q = 0 1 0 x a n d Q 1 = 0 3 3 1 . ( S e eF i g . 3 .) W e g e n e r a t e t w o s e q u e n c e s o f w h i t e b l o c ks : o n es o r t e d i n i n c r e a s i n g o r d e r ; t h e o t h e r s o r t e d i n d e c r e a s i n go r d e r . T o c r e a t e th e f ir s t o n e , w e c o n s i d e r t h e e n c o d i n go f Q , a n d i n c r e a se b y o n e , o n e d i g i t a t a t i m e , a n ds t a r t i n g f r o m t h e r i g h t m o s t , a l l q u a t e r n a r y d i g i t s , wh i l ei n s e r t i n g a s m a n y X ' s a s p o s s i b l e a t t h e r i g h t o f t h ei n c r e a se d d i g it . W e k e e p g o i n g u n t i l w e p r o d u c e a l l w h i t eCommun icati ons Decembe r 1982of Volume 25the ACM Numbe r 12

  • 8/3/2019 An Effective Way to Represent Quadtrees - Irene Gargantini

    5/6

    b l o c k s w i t h e n c o d i n g l e s s t h a n Q 1 . I n t h e e x a m p l e w ep r o d u c e t h e s e q u e n c e

    0 1 IX , 0 1 2 X , 0 1 3 X , 0 2 X XT o g e n e r a t e t h e d e s c e n d i n g s e q u e n c e , w e b a s i c a l l y

    f o l lo w t h e s a m e s c h e m e : w e d e c r e a s e ( b y o n e ) t h e r i g h t -m o s t q u a t e r n a r y Q l - d i g i t s , o n e a t a t i m e , a n d p r o d u c ea l l w h i t e b l o c k s w i t h e n c o d i n g g r e a t e r t h a n t h e l a s te l e m e n t o f t h e p r e v i o u s s e q u e n c e . I n t h e e x a m p l e , w eo b t a i n

    0 3 3 0 , 0 3 2 X, 0 3 1 X, 0 3 0 XW e n o w f o r m a l i z e t h e a b o v e d i s c u s s i o n . L e t t h e

    m i x e d q u a t e r n a r y r e p r e s e n t a ti o n o f Q b eQ = q ~ - 1 4 ~ - 1 + . . . + q i 4 i + . . . + q o (2 )

    w h e r e e i t h e r i = 0 o r i i s t h e s u b s c r i p t f o r w h i c h q i # Xa n d q i - 1 . . . . . q o - - X . L e t i l o f

    Q 1 = j q ~ _ 1 4 n -1 + . . . + q ~1 4 l + . . . + q~ (3 )b e s i m i l a r l y d e f m e d , i a n d i l i n d i c a t e w h i c h q u a t e r n a r yd i g it s s h o u l d b e i n c r e a s e d a n d d e c r e a s e d , r e s p e c t i v e ly , i no r d e r t o s t a r t t h e g e n e r a t i o n o f t h e t w o s e q u e n c e s . ( A sb e f o r e , Q a n d Q 1 a r e t w o s u c c e s s i ve e l e m e n t s o f t h el i n e a r q u a d t r e e w i t h Q < Q 1 .)

    T h e p r o c e d u r e u s e d i n t h e e x a m p l e i s b a s i c a l l y c o r -r e c t, b u t s t il l r e q u i r e s s o m e i m p r o v e m e n t . F i r s t , w e n o t et h a t i f Q a n d Q 1 h a v e t h e f i r s t j d ig i ts i n c o m m o n , t h e s ev e r y s a m e d i g i t s m u s t b e l o n g t o a l l t h e w h i t e b l o c k sb e t w e e n Q a n d Q 1 . ( I n t h e e x a m p l e , t h e r e i s o n l y o n ed i g i t , 0 . ) S e c o n d , w e n o t e t h a t t h e l a s t b l o c k p r o d u c e d i nt h e a s c e n d i n g s e q u e n c e ( 2 X X i n t h e e x a m p l e ) h a s t h ed i g it i n t h e ( n - j - 1 ) p o s i t i o n e q u a l t o ( q ' _ j _ x - 1 ) a n da l l o t h e r d i g i t s r e p r e s e n t e d b y X ' s . T h e l a s t b l o c k p r o -d u c e d i s , t h e r e f o r e , t h e l a r g e s t b l o c k c l o s e s t t o Q 1 ' f r o mb e l o w . 'L e t U b e f o r m a l l y g i v e n b yU = Un--14 n-1 +. .+ Un_j 4 n- j + Un_j _14 n- j - 1 +. .+ UO

    w h e r ep .Un- 1 ---- qn -1 = ' "" u n - i = q n - 2 = q n _ j ,n - - 1 , ,

    tU n - j - 1 = q n - - j - 1 --1;Un -j-2 . . . . . u0 = X 0 _--__j_--__n - 1F o r j = 0 , Q a n d Q 1 d o n o t h a v e a n y c o m m o n d i g it s ,a n d f o r j = n - 1, U h a s n o X ' s . B y c o n s t r u c t i o n , Q _ U< Q1 .

    T h e i n t r o d u c t i o n o f U w o u l d n o t b e n e c e s s a r y i f w ec o u l d a l w a y s p r o d u c e i ts v a l u e a s t h e l a s t c o d e o f t h ea s c e n d i n g s e q u e n c e , a s i n t h e e x a m p l e a b o v e . U n f o r t u -n a t e l y , t h i s is n o t a l w a y s t h e c a s e . C o n s i d e r , f o r i n s t a n c e ,Q = 0 1 0 0 a n d Q 1 = 0 1 1 3 . T h e t w o s e q u e n c e s a r e 0 1 0 10 1 0 2 0 1 0 3 an d 0 1 1 2 0 1 1 1 0 1 1 0 . U , eq u a l t o 0 1 0 X, i s n o ta w h i t e b l o c k ( i t a c t u a l l y c o v e r s Q ) a n d , t h e r e f o r e , s h o u l dn o t b e g e n e r a t e d b y e i t h e r s e q u e n c e . T h e d e c r e a s i n gs e q u e n c e , o n th e o t h e r h a n d , s h o u l d p r o d u c e a l l q u a t e r -9 0 9

    n a r y c o d e s > U . O u r a l g o r i t h m , t h e r e f o re , s h o u l d p r o -d u c e t h e a s c e n d i n g s e q u e n c e i n th e w a y d e s c r i b e d b e f o r e ,b u t s h o u l d g e n e r a t e t h e d e s c e n d i n g s e q u e n c e b e t w e e nQ 1 a n d U .

    T h e a l g o r i t h m i s f o r m a l l y g i v e n b e l o w a s ' p r o c e d u r eW H I T E B L O C K S . ' I t a c c e p t s , a s i n p u t , n, i, i l , U a n dt w o a r r a y s Q a n d Q 1 c o n t a i n i n g t h e c o e f f i c i e nt s o fe x p a n s i o n s ( 2 ) a n d ( 3 ) , r e s p e c t i v e l y . I t o u t p u t s a l l t h ew h i t e b l o c k s ( i f a n y ) w i t h e n c o d i n g b e t w e e n Q a n d Q 1.T h e f u nc t i on S U M _ O F _ P O W E R S i s d e s i g ne d to e v a l-u a t e f o r m s o f t y p e ( 2 ).

    p r o c e d u r e W H IT E B L O C K S (n , i , i l , Q , Q1 , U ) ;va lue n , i , i l , U; re f erence Q , Q I;i n t eger n , i , i l , U; in t eger array Q , Q I;

    (* W H IT E B L O C K S g en e ra te s a l l w h i t e b lo ck s b e tw een Q a n dQ1;Q[ 0 ] , Q[ I ] , . . a r e t h e Q-digi ts ; QI [ 0 ] , QI [ 1 ] . . . a r e th eQl-d ig i ts ;QIS U M i s t h e Q l -m ix ed q u a te rn a ry co d e * )beg in i n t eger m , k , S , X , Q 1 S U M ;i n t eger array LAST[0: n - 1], LA ST I[0: n - 1];Q1SU M ~ SUM__OF_POWERS(QI);

    (* to g en e ra t e t h e a scen d in g seq u en ce ca l l W H I T E w i th fo rm a lp a ram ete r C H O IC E = 1; t o g en e ra te t h e l a rg es t w h i t e b lo ck sse t fo rm a l p a ram ete r L IM IT = Q1 S U M ; in i t ia l l y L A S T =Q; a ft e r each in v o ca tio n o f W H IT E , L A S T co n ta in s a w h i t eb lock * )f o r m ~ 0 s t ep 1 un t i l n - 1 do L A S T [ m ] , ,- - Q[ m ] ;k *--i;wh i l e S U M _ O F _ P O W E R S ( L A S T ) < Q I S U M an d k U a n d < Q 1 S U M s e t f o r m a l p a r a m e t e r L I M I T = U ;in it i a ll y L A S T 1 = QI ; a f t e r each in v o ca t io n o f W H IT E ,L A S T co n ta in s a w h i t e b lo ck * )f o rm * - - O s t ep 1 un t i l n - 1 do L A S T I [ m ] ~ Q l [ m ] ;k = il;

    whi l e S U M _ O F _ P O W E R S ( L A S T I ) > U and k

  • 8/3/2019 An Effective Way to Represent Quadtrees - Irene Gargantini

    6/6

    i n t e g er k , S S , C H O I C E , L I M I T ; in te ge r ar ray W H I T E -N O D E ;be gin in te ge r s , m , S U M ; B o o l e a n F L A G ;F L A G ~ true;

    f o r s ~ 1 s t ep 1 u n t i l S S d o/ f F L A G = t rue dobe ginW H I T E N O D E [ k ] ~ - - W H I T E N O D E [ k ] +C H O I C E ;S U M ~ S U M _ O F _ P O W E R S ( W H I T E N O D E ) ;

    ( * CHO I CE = 1 c o r r e sp o n d s to t h e p r o d u c t i o n o f a wh i t e b lo c kw i th S U M < Q I S U M ;CH OI C E = - 1 c o r r e sp o n d s t o th e p r o d u c t io n o f a wh i teb l o c k wi t h SUM > U * )/ f (C H O I C E = 1 a n d S U M < L I M I T ) o r( C H O I C E = - 1 a n d S U M > L I M I T ) the nfo r m ~ n - l s t e p - l un t i l O doo u t pu t W H I T E N O D E [ m ]

    else F L A G *- - fa l s ee n d

    e n d ( * o f W H I T E * );i n t e g er p r o c e d u r e S U M _ O F _ P O W E R S ( n , Y ) ;v alue n ; r e fe re nc e Y; in te ge r ar ray Y;

    ( * e v a l u a t io n o f Y[ 0 ] + Y[ 1 ]* 4 + . . . + Y [ n - 1]*4**(n - 1)*)be gin in te ge r m;S U M _ O F _ P O W E R S ~-- Y [ n - 1] ;

    fo r m ~ n - 2 s t e p - 1 unt i l 0 doS U M _ O F _ P O W E R S ~-- S U M _ O F _ P O W -ERS* 4 + Y [ m ]e n d ( * o f S U M _ O F _ P O W E R S * );

    t h e n u m b e r o f b i ts f o r e a c h q u a t e r n a r y c o d e i s 3 ( n - 1 )+ 2 . A l i n e a r q u a d t r e e , t h e r e f o r e , c a n b e s t o r e d i n ( 3 ( n- 1 ) + 2 ) N b it s o f m e m o r y . S p a c e c o m p l e x i t y , m e a s u r e di n t e rm s o f n u m b e r s o f n o d e s , i s N f o r l in e a r q u a d t r e e sa n d M f o r r e g u l a r q u a d t re e s , w h e r e M c a n b e a s h i g h a s4 n N + 1 [ 1 1 ] . I f w e a s s u m e t h a t t h e s i x f i e l d s o f aq u a d t r e e ' s n o d e c a n b e s t o r e d in t h r e e m e m o r y l o c a t i o n s ,l i n e a r q u a d t r e e s r e d u c e t h e s t o r a g e r e q u i r e m e n t b y af a c t o r o f N / ( 3 M ) , w i t h N < M . T h i s m e a n s a s av i n g s o fm o r e t h a n 6 6 p e r c e n t o f t h e s t o r ag e r e q u i r e d b y q u a d -t r e e s i n a l l c a s e s a n d a s a v i n g s h i g h e r t h a n 9 0 p e r c e n tw h e n M i s c l o se t o th e a b o v e - m e n t i o n e d b o u n d .

    W o r s t -c a s e t i m e c o m p l e x i t y o f t h e g i v e n a l g o r i t h m si s a l s o e a s i l y e v a l u a t e d . E n c o d i n g a b l a c k p i x e l r e q u i r e st i m e p r o p o r t i o n a l t o n ; e n c o d i n g a r e g i o n i n c l u d i n gc o n d e n s a t i o n , r e q u i r e s O ( n * N P ) t i m e ; f i n d i n g a n a d -j a c e n t p i x e l a n d d e t e r m i n i n g i t s c o l o r c a n b e c a r r i e d o u tin O ( n ) t i m e ; r e f i n i n g b l a c k n o d e s r e q u i r e s O ( n * N )t i m e a n d r e f in i n g a ll te r m i n a l n o d e s c a n b e e x e c u t e d i nO ( n * ( N + W ) ) t im e . F i n a l ly , s u p e r p o s i t i o n o f t w or e g i o n s f o r m e d o f N 1 a n d N 2 b l a c k n o d e s c a n b e c a r d e do u t i n t i m e p r o p o r t i o n a l t o O ( N 1 + N 2 ) . T h e s e t i m ee s t i m a t e s a r e e s t a b l i s h e d i n t h e p r e s e n c e o f a s e q u e n t i a lm o d e l o f c o m p u t a t io n . H o w e v e r , s o m e o f t h e p r e s e n te dp r o c e d u r e s , f o r e x a m p l e , e n c o d i n g a r e g i o n , c o u l d b ee x e c u t e d m u c h f a s te r i f p a r a l l e l p r o c e s si n g w e r e a v a i l-a b l e .

    A c k n o w l e d g m e n t s . T h e a u t h o r w i s h e s t o t h a n k t h er e f e re e s f o r th e i r c ri ti c is m a n d M r . Z a l e T a b a k m a n f o rh i s e x c e ll e n t j o b o f p r o g r a m m i n g a n d t e s ti n g t h e a l g o -r i t h m s .

    6 . Un ion and Intersec t ion o f L inear QuadtreesT h e m o s t e x t e n s i v e d i s c u s si o n o n u n i o n , i n t er s e c ti o n ,

    a n d s u p e r p o s i t io n ( t h e l a tt e r b e i n g d e f i n e d a s t h e o c c l u -s i o n o f w h i t e n o d e s b y b l a c k o n e s ) c a n b e f o u n d i n [ 2].O u r d a t a s t r u c t u r e s u g g e s ts a s i m p l e p r o c e d u r e , b a s e d o nm e r g i n g t h e t w o l i n e a r q u a d t r e e s i n t o a n e w s o r t e d a r r a y .I f t w o n o d e s a r e t h e s a m e , w e s t o r e o n l y o n e . I f o n eb l o c k c o v e r s t h e o t h e r , w e s t o r e o n l y t h e l a rg e r . E x e c u -t i o n t i m e i s, o f c o u rs e , p r o p o r t i o n a l t o t h e t o t a l n u m b e ro f b l a c k n o d e s o f th e t w o r e g io n s . S u p e r p o s i t io n o f k >2 r e g io n s c a n b e p e r f o r m e d i n p a r a l l e l b y a p p l y i n g a k -w a y m e r g e . C o n d e n s a t i o n s h o u l d b e i n c o r p o r a t e d i n t ot h e m e r g i n g p h a s e , s o t h a t s u p e r p o s i t io n o f k r e g io n s c a nb e c a r r i e d o u t i n l i n e a r t i m e . I n t e r s e c t io n c a n b e t r e a t e ds i m i l a r l y .

    7 . C om plex i ty Cons iderat ionsL e t N P b e t h e t o t a l n u m b e r o f b la c k p i x e ls , W t h en u m b e r o f w h i te n o d e s , a n d M t h e t o ta l n u m b e r o f n o d e s

    o f a q u a d t r e e . N a n d n a r e th e n u m b e r o f b l a c k n o d e sa n d t h e r e s o l u t i o n p a r a m e t e r , r e s p e c t i v e l y . S p a c e c o m -p l e x i ty i n t e r m s o f n u m b e r o f b i ts i s e a si ly e v a l u a t e d .E a c h q u a t e r n a r y d i g i t re q u i r e s t w o b i ts , w h i l e t h e m a r k e rX r e q u i r e s t h r e e b i t s . S i n c e n o n o d e c a n s t a r t w i t h a n X ,

    Received 8 /81 ; rev ised 10 /81 ; accep ted 8 /82R e f e r e n c e s! . Dy e r , C .R . , R o se n fe l d , A . , a n d Sa me t , H . R e g i o n re p rese n t a t io n :B o u n d a ry c o d e s f ro m q u a d t re e s . C o m m . A C M 2 3 , 3 (March 1980),171-179.2 . Hun ter , G.M. and Ste ig li tz , K. Op era t ions on images usingquadt rees . IE EE T r an s . o n Pa t t e r n An a l y s is a n d M a c h i n e In t e l l. 1 , 2(Apri l 1979), 145-153 .3 . Hun ter , G.M . and Ste ig l itz , K. Linear t ransform at ions of p ic turesre p re se n te d b y q u a d t re e s . Comptr . Graphics an d Imag e Process ing 10,3 (Ju ly 1979), 289-296 .4 . Jack ins , C.L. and Tan imo to , S.L. Oct - t rees and the i r use inre p re se n t in g t h re e -d i me n s i o n a l o b j e c ts . Comptr . Graphics and Ima geProcess ing 14 , 3 (Nov . 1980), 249-270 .5 . Jones , L. and Iyen gar, S.S. Repr esen ta t ion o f a reg ion as a fo resto f q u a d - t re e s . P r o c. I E E E - P R 1 P 8 1 C o n fe r en c e Da l l a s , TX , IEEEPubl. 81 CH1595-8, (1981), 57-59.6 . Ka wa g u c h i , E . a n d En d o , T . On a me t h o d o f b i n a ry -p i c t u rerepresen ta t ion and i t s app l ica t ion to da ta compression . I E E E T r a n s .o n Pa t t e r n An a l y s i s a n d M a c h i n e In t e ll . 2, 1 (Jan. 1980), 27-35.7 . Kl inger, A. Pa t t e r n s a n d S e a r c h S t a t i s ti c s , O p t i mi z i n g M e t h o d s i nStatistics. R u s t a g i , J.D . (Ed . ) Ac a d e mi c P res s , Ne w Y o rk , 1 9 7 1 .8 . Kl i n g e r , A . a n d R h o d e s , M .L . Org a n i z a t i o n a n d a cc e ss o f ima g edata by areas . 1 E EE T r an s . o n Pa t t e r n An a l y s i s a n d M a c h i n e l n t e l l. 1 ,1 (Jan. 1979), 50-60.9 . Sam et , H. An a lgori th m for conv ert ing ras ters to quadt rees .IE EE T ra n s. o n Pa t t e r n An a l y s i s a n d M a c h i n e l n t e l l. 3, 1 (Jan . 1981)93-95.1 0 . Sa m e t , H . C o n n e c t e d c o m p o n e n t l a b e l i n g u s i n g q u a d t re e s .J A C M 28, 3 (Ju ly 1981), 487-501 .1 1 . Sa me t , H . R e g i o n re p re se n ta t i o n : q u a d t re e s f ro m b o u n d a rycodes. C o m m . A C M 2 3 , 3 (March 1980), 163-170 .1 2 . S r i h a r i , S .N. R e p re se n t a t i o n o f t h re e -d i me n s i o n a l d i g i t a l i ma g e s.TR -1 6 2 , C o m p t r . Sc i. De p t . S t a t e Un i v . o f Ne w Yo rk a t B u f fa l o , Ju l y1980.

    9 1 0 C o m m u n i c a ti o n s D e c e m b e r 1 9 82o f V o l u m e 2 5t h e A C M N u m b e r 1 2