Int. J. Man-Machine Studies (1983) 18, 253-263
Translation, rotation and superposition of linear quadtrees IRENE GARGANTINI
Department of Computer Science, The University of Western Ontario, London, Ontario, Canada (Received 14 February 1982) In Gargantini (1982a) it has been shown that storing black nodes of a quadtree is sufficient to retrieve any basic property associated with quadtrees. To achieve this, each black node must be represented as a quaternary integer whose digits (from left to right) describe the path from the root to that node. The sorted sequence of quaternary integers representing a given region is called the linear quadtree associated with that region. Such a structure has been shown to save more than two-thirds of the memory locations used by regular quadtrees. In this paper we present procedures for translating and rotating a region and consider the superposition of binary images with different characteristics (such as different resolution parameter, different pixel size and/or different center). Translation, rotation, and superposition are shown to be O(N log N) operations; for translation N is the number of black pixels; for rotation N is the number of black nodes; for superposition N is the sum of black nodes or black pixels of the two images, depending on whether or not the two regions are centered on the same raster.
1. Introduction Gargantini (1982a) has introduced a memory-saving structure for representing the black pixels of a binary image. W e refer to the set of black pixels as "the region" and to the just-mentioned structure as "the linear quadtree". In this article we indicate how-translation, rotation and union can be carried out when the given regions are represented by linear quadtrees. The p a p e r is organized as follows. First, we present a fast way of encoding and decoding pixels using "global" operations as opposed to the digit-by-digit procedure given in Gargantini (1982a, b). Secondly, we consider the translation of a region in the southern, eastern, northern and western directions. Thirdly, we analyse the union of the two regions arising (or suitably projected) f r o m different screens and superimposed on a larger screen. Fourthly, we show that rotation of a given region can be performed by a simple one-digit-onto-one-digit mapping, followed by a sorting procedure. The reader is referred to the author's early paper (Gargantini, 1982a) for the basic idea behind linear quadtrees, their use, their relation with respect to regular quadtrees, and their memory-saving characteristics.
2. Encoding and decoding revisited Let I and J be the two integers representing the row and column positions, respectively, of a black pixel in the given 2 n x 2"-array, where n > 1 and I, J = 0, 1 . . . . . 2" - 1. Let 253 0020-7373/83/030253 + 11503.00/0
O 1983 Academic Press Inc. (London) Limited
254
I. G A R G A N T I N I
the binary representation of I and J be +c121 +Co2 ~
i = c._12--1 +...
j = d,_12"-1 +...
+dr21 +do2 ~
The (I, J)-pixel is encoded into the quaternary integer Q = q , - 1 4 " - 1 + 9 . . +q141 + q 0 4 ~
(2.1)
with qi = d l + 2ci,
i = 0, 1 . . . . .
n - 1.
(2.2)
For example, the pixel with row-index I---7 and column-index J = 9, relative to the region shown in Fig. 1 with n = 4, is encoded as 1223 to the base four. As shown in
0
I
2
3
4
5
6
7
8
9
I0
II
12
13 14 15
0 I
2
(NW=O)
(NE=t)
3 4
5
03XX
~o2 20
6 7 8
210X 9
20XX
I 21IX
50
1
I0
12 (SW= 2)
(SE = 3 )
14 15 FIG. 1. Encoding a region with mixed-quaternary codes.
Gargantini (1982a), this encoding reflects the successive quadrant subdivisions with the following convention: 0 for the North-West quadrant, 1 for the North-East, 2 for the South-West, and 3 for the South-East. This means that pixel 1223 belongs to the North-East quadrant in the first subdivision, to the South-West in the second and third subdivisions, and to the South.East quadrant in the last subdivision. An "X" is introduced to represent the set {0, 1, 2, 3} and indicates that condensation (Gargantini, 1982a, Samet, 1981) of four nodes relative to the same subquadrant has occurred. For example, pixels 2110, 2111, 2112, and 2113 all belong.to the same quadrant relative to the fourth subdivision and can, therefore, be represented by the mixed-quaternary code 211X (see Fig. 1). X must be encoded with an integer > 3 as explained in Gargantini (1982a).
LINEAR QUADTREES
255
Relation (2.2) suggests a fast way for encoding pixels into quaternary codes and vice versa. Encoding can be carried out by expressing I and J into their binary form, multiply I by 2 (to the base four) and add this product to J: the result is Q. For instance, for I = 7 = 0 1 1 1 2 and J = 9 = 10012, Q is given by Q = (J+2
* 1)4 =
(1001 +0222)4 = 1223.
This way of determining Q is correct, because there is no carry propagation in the evaluation of (2.1): in fact, by (2.2), each qi-< 3. TABLE 1 Q u a t e r n a r y codes f o r n = 4 0000 0002 0020 0022 0200 0202 0220 0222 2000 2002 2020 2022 2200 2202 2220 2222
0001 0003 0021 0023 0201 0203 0221 0223 2001 2003 2021 2023 2201 2203 2221 2223
0010 0012 0030 0032 0210 0212 0230 0232 2010 2012 2030 2032 2210 2212 2230 2232
0011 0013 0031 0033 0211 0213 0231 0233 2011 2013 2031 2033 2211 2213 2231 2233
0100 0102 0120 0122 0300 0302 0320 0322 2100 2102 2120 2122 2300 2302 2320 2322
0101 0103 0121 0123 0301 0303 0321 0323 2101 2103 2121 2123 2301 2303 2321 2323
0110 0112 0130 0132 0310 0312 0330 0332 2110 2112 2130 2132 2310 2312 2330 2332
0111 0113 0131 0133 0311 0313 0331 0333 2111 2113 2131 2133 2311 2313 2331 2333
1000 1002 1020 1022 1200 1202 1220 1222 3000 3002 3020 3022 3200 3202 3220 3222
1001 1003 1021 1023 1201 1203 1221 1223 3001 3003 3021 3023 3201 3203 3221 3223
1010 1012 1030 1032 1210 1212 1230 1232 3010 3012 3030 3032 3210 3212 3230 3232
1011 1013 1031 1033 1211 1213 1231 1233 3011 3013 3031 3033 3211 3213 3231 3233
Decoding is the converse process; given Q, find I and J. To speed up decoding, observe Table 1. By construction, the first row represents, in binary form, the columnindex J. A n y quaternary code relative to rows with I > 0 is obtained from the top one by applying the mapping 04(0or2);
1 4 ( 1 or3).
It follows that, given Q, J is found by applying the inverse transformation 040;
240;
141;
341
to all Q-digits 9 For example, if Q = 2131, J = 01112 = 7. Because of (2.2) 2I = (Q - J)4, from which we can derive I. In the example, 2I = ( 2 1 3 1 - 0 1 1 1 ) 4 = 2020, or, equivalently, I = 10102 = 10. In the decoding process we might encounter a node expressed as a mixed-quaternary code (i.e. a code with Xs) instead of a pixel (which, on the other hand, is always expressed as a quaternary integer). In this case we need to decode only the first pixel,
256
I. GARGANTINI
obtained by equating to 0 all Xs, since we know that there are as m a n y consecutive values of I and J as 2 to the power of the n u m b e r of Xs in the node. For example, if Q = 20XX, we consider 2000 and find J = 00002 = 0 and I = 10002 = 8. All pixels corresponding to J = 0, 1, 2, 3 and I = 8, 9, 10, 11 are k n o w n to be present. The encoding and decoding schemes presented here offer several advantages dver those given in Gargantini (1982a, b), especially when translation is considered.
3. Translation of a pixel Consider the translation of a pixel l rows in either vertical direction and m columns in either horizontal direction, with - ( n - 1 ) _ < / , m - < ( n - 1 ) . Assume also that the required translation leaves the region within the original 2 ~ x 2~-array or screen. To translate pixel Q l rows, we first express its associated parameters J and I in binary f o r m (using the procedure described in section 2), then add binary l to binary I, multiply the obtained sum by 2 (to the base four) and add this product to J (to the base four). In short, the translated pixel Q' is given by
O ' = (J+(2 * (I+/)2)4)4.
(3.1)
For example, if Q = 3012 and l = 5 = 01012, we find J = 10102, I = 10012, I + l = 11102, and Q'= (1010+2.
1110)4=3230
(see, for instance, Table 1). As a second example, take Q = 1223 and l = - 6 = - 01102. We obtain J = 1001. I = 01112, and Q' = (1001 + 2 * 0001)4 = 1003. We r e m a r k that the sum ( I + l) is assumed performed to the base two; in general, Q' = (J + (2 * (I +/)2)4)45;~ (J + (2 * I)4 + (2 */)4)4. If ( I + / ) is either < 0 or > n - 1 , the translation is not possible within the original screen, and a "suitable" enlargement must be devised by increasing n accordingly. Translation of m columns can be achieved in a similar way, since, by (2.2), all quaternary codes with J > 0 can be obtained by adding the column-index to the quaternary code relative to J = 0 (see, once more, Table 1). Therefore the following procedure can be applied; first find I by means of the m a p p i n g 2-~2;
0-~0;
1~0;
3~2.
Second, evaluate J by subtracting I from Q; third, add to binary J binary m, so that the translated pixel Q' is given by Q' = (I + (J + m)2),.
(3.2)
For instance, if Q = 1200 and m = 3 = 00112, we first find I = 0200, J = 10002, J + m = 10112, and Q' = (0200+ 1011)4 = 1211. If ( J + m ) is either < 0 or >(n - 1 ) then, as in the vertical case, the translation cannot be carried within the given 2" • 2~-array.
LINEAR QUADTREES
257
Translation of a node grouping more than one pixel (like, for instance, Q = 03XX in Fig. 1), must take place by considering each individual pixel, since, only if l is a suitable multiple of the number of Xs, that particular node grouping is an invariant under translation. We can condense our findings on translation in the following THEOREM 1 (PIXEL TRANSLATION). Given a quaternary code Q and two integers ! and m, with - ( n - 1 ) <- 1, m - n - 1, pixel Q', translated I places vertically and m places
horizontally, is given by Q' = (2 * (I +l)e + (J + m)2)4.
(3.3)
Proof. Just combine (3.1) and (3.2). Translation of a pixel requires (at most) n digit-by-digit comparisons to determine J and two rational operations to the base four to determine I. The evaluation of (3.3) requires two binary operations and two rational operations to the base four. Translating a pixel is, therefore, accomplished in time proportional to n.
4. Adjacency revisited Finding the pixel adjacent to Q in the southern direction corresponds to a translation with m = 0 and l = 1; similarly, by setting m = 0 and l = - 1, l = 0 and m = + 1 in (3.3) we can generate the adjacent pixels in the other principal directions. We therefore have THEOREM 2 (ADJACENCY). Given a quaternary code Q, the adjacent nodes in the four
principal directions (hereby denoted by S, N, E and W) are given by S =(]+(2
* (I+1)2)4)4;
E = ( 2 "1+(.I+1)2)4;
N = ( J + ( 2 * (1-1)2)4)4;
(4.1)
W=(2 *[+(J-l)e)4.
We remark that determining an adjacent pixel in the northern and southern directions requires (n + 5) elementary operations, while only (n + 3) operations are required for the eastern and western directions. Since both I and J are evaluated, determining whether or not a given pixel is on the border corresponds to check for I, J = 0 and I, J = n - 1. The case of finding a node adjacent to Q when Q itself is expressed as a mixedquaternary code, is treated by simply ignoring all Xs in (4.1), then " a p p e n d " those Xs (in the same position) at the end of the (incompletely) translated node. Consider, for instance, finding the node adjacent to Q = 300X in the western direction (see Fig. 1). We get 2 * I = 200, J = 100, and Q' (incompletely translated) = (200 + 011)4 = 211. We now append one X to 211 and obtain the final value of 211X.
5. Region translation Consider the region shown in Fig. 1, formed of NP = 51 black pixels and N = 12 black nodes, for whcih the associated array of mixed-quaternary codes is 03XX, 1200, 1202, 1220, 1222, 1223, 20XX, 210X, 211X, 2131, 300X, 3012.
(5.1)
We call the increasing sequence (5.1) the linear quadtree associated with that region.
258
I. GARGANTINI
Translating a linear quadtree is carried out by translating each individual pixel represented in it. For a region with NP black pixels, the total amount of computation work is then (2 * n * NP + constant). As the reader has probably already guessed, the set of translated pixels may not form a linear quadtree. To express such a set as an ordered array of black nodes, the quaternary codes must be sorted and condensed (four-by-four) whenever it is possible. We suggest to use Heapsort to sort the quaternary codes for two reasons: (i) Heapsort can be implemented using a one-dimensional array as explained, for instance, in Augenstein & Tenenbaum (1979); and (ii) Heapsort has the same worst-case timecomplexity as the translation procedure. Sorting and condensing should be combined together by checking whether the last four (sorted) nodes belong to the same subquadrant or not. As an example, consider the linear quadtree (5.1) relative to the region of Fig. 1 and translate it l = 2 unit pixels vertically and m = 1 unit pixels horizontally. The linear quadtree obtained after translation, sort and condensation have been applied is: 0321, 0323, 033X, 122X, 2021, 2023, 203X, 2101, 2103, 211X, 212X, 213X, 2201, 2203, 221X, 2300, 2302, 300X, 3012, 302X, 3030, 3032, 3033, 3200. Time considerations on the translation of a region are summarized in: THEOREM 3 (REGION TRANSLATION). Translating a region formed of N P black pixels is an O(n * NP) procedure.
Proof. Translating NP pixels requires time proportional to 2 * n * NP; sorting NP quaternary codes with Heapsort requires time proportional to NP* log2 NP, with NP < 22~. Condensation is a linear algorithm in the total number of black pixels, so that translating a region requires time proportional to (4 * n * NP + NP).
R2/
X_m
-
R I
FIG. 2. Rt and R2 have the same center, different pixel size and resolution parameter.
6. Union of two linear quadtrees In this section we consider the superposition of two regions R1 and R2, both represented by linear quadtrees. Let NP1 be the number of black pixels representing R1, N1 the
LINEAR
QUADTREES
259
number of black nodes, and n~ the resolution parameter relative to Rx. Let NP2, N2 and n2 be similarly defined for the region R2. In the sequel we shall distinguish between the superposition of two regions referenced to the same binary array (or image or screen), and the superposition of regions referenced to different binary images, here identified by BIx and BI2. The first case--denoted here CASE A - - h a s been already considered in Gargantini (1982a) for two dimensions and in Gargantini (1982b) for three dimensions. The union algorithm basically consists of merging the two linear quadtrees (or octtrees in three dimensions) while incorporating condensation. In both the planar and spatial cases the union of two regions is shown to be executable in O(Nx +N2) time.
~_. _ R 2
0
I
2 3
b
0
I
2
3
FIG. 3. R1 and R2 h a v e t h e s a m e c e n t e r a n d r e s o l u t i o n p a r a m e t e r , b u t d i f f e r e n t p i x e l size.
The second case--here denoted CASE B---deals with the superposition of R1 c BII and R2 c BI2, where BI1 and BI2 might have different characteristics, like different pixel size, different resolution parameter and/or different center. To the author's knowledge, this kind of superposition has been hardly considered in the case of regular quadtrees. CASE B is subdivided into the following cases: (a) BI1 and BI2 have the same (geometrical center), different pixel size and different resolution parameter. Let us assume nl > n2. The screen holding the union of R~ and R2 must have resolution parameter n at least equal to nl. Let us assume n = nl. To evaluate R1 u R2, we append (n 1- n2) Xs at the end of each quaternary code representing R2 and then merge R~ and the just modified R2 as in CASE A. As an example, consider n~=3, R1={120,121,123}, n 2 = 2 and R2 = {00, 01, 03, 10, 11, 13} as shown in Fig. 2. R2 is first modified to be R2 = {00X, 01X, 03X, 10X, l l X , 13X} and then R1 and R2 are merged to form R1 u R 2 = {00X, 01X, 03X, 10X, l l X , 120, 121,123, 13X}. If n > n~, we just append (n - n O Xs at the end of each quaternary code of R~ u R2. In this case, as in CASE A, the union of R1 and R2 can be performed in O(Na +N2) time.
260
i. GARGANTINI
(b) BI1 and B12 have the same center, different pixel size, but the same resolution parameter (i.e. nl = n2). Let us assume that BI1 is the image with smaller pixel side and that the latter is equal to 1. Let the pixel side of BI2 be 2 s times that of BI1, with s >- 1. The union of R1 and R2 can be correctly carried out only if the screen on which the superposition occurs has resolution parameter n -->n ~+ s, and its pixel side equals the unity. Let us consider n = n~ +s. We first modify R2 by appending s Xs to each quaternary code of R2. The other region, R~, is first referenced to the North-West-most subquadrant (with dimensions 2"' x 2"') of the union image by preceding each R~quaternary code by s 0s. After this preliminary operation, we translate the just modified R1 by 2 "1-1 (2 s - 1) unit pixels to the South and to the East. As an example, consider n ~ = n 2 = 2 , R I = { 1 0 , 11, 13} and R 2 = { 0 0 , 0 1 , 0 3 , 10, 11, 13}, where the pixel side of R2 is 2 ~ = 22 times that of R~, as shown in Fig. 3. T h e binary image holding the union is assumed to have resolution parameter n = n t + s = 4. R2 is simply modified to be R2 = {00XX, 01XX, 03XX, 10XX, l l X X , 13XX}. R~ is first mapped into R1 = {0010, 0011, 0013} and then translated 2 nl-~ (2' - 1) = 6 pixels to the South and East. The result is R~(translated) = {1220, 1 2 2 L 1223}. Merge and condensation are carried out as usual, giving R1 w R2 = {00XX, 01XX, 03XX, 10XX, 11XX, 1220, 1221, 1223, 13XX}. Since in this case a translation is required, re-sorting may be necessary for either region, so that the union of R1 and R2 can be executed in time proprtional to (n * max (NP1, NP2)). The case n > n ~ + s is treated by appending (rt--tll--S) Xs at the end of each quaternary code of R1 u R2. (c) The third case we consider occurs when the pixel size and the resolution parameter are the same, but the centers of BI1 and BI2 are different. We also assume that, in this case, the user requiring the superposition of the two images has the freedom to specify: (i) an integer n >n~ = n2 identifying the resolution parameter of the screen on which the superposition of R1 and R2 must occur, and (ii) two pairs of integers (ll, rnl) and (12, m2), identifying how many rows (to the South and to the East) BI~ and BI2 must be shifted with respect to the North-most row and the West-most column of the union screen. The simplest way to proceed is to refer both Rt and R2 to the North-West-most subquadrant (of dimensions 2"' x 2 n~) of the union image by preceding each quaternary code by ( n - n ~ ) 0s. Then we operate a translation of R~ ll places to the South and m~ places to the East, with the algorithm described in section 5. R2 is similarly treated. The linear quadtrees representing the translated R1 and R2 regions are then merged to produce a new (condensed) linear quadtree.
261
LINEAR QUADTREES
0
I
/
- R ,
5
6
7
8
9
I0
II
12
13
14
15
R2--/
FIG. 4. Superposition of regions with different centers.
As an example, consider n l = n 2 = 3 , R l = { 0 0 3 , 0 1 2 , 0 1 3 , 0 2 X , 031} and R2 = {112, 122, 123, 13X, 300, 301,310, 311} as shown in Fig. 4. Assume that the user request the union to be performed with n = 4, (l~, rn~)= (4, 10) and (/2, m2)= (5, 6). With the procedure we propose the first reference R1 and R2 to the North-West-most quadrant of the union image, so that RI = {0003, 0012, 0013, 002X, 0031} and R2 = {0112, 0122, 0123, 013X, 0300, 0301, 0310, 0311}. Translating R1 four places to the South and 10 places to the East results in Rl(translated) = {1213, 123X, 1302, 1303, 1321}. Translating R2 five places to the South and six places to the East gives R2(translated) = {1320, 1322, 1323, 301X, 310X}. After merging and condensing have been applied we obtain R1 u R2 ={1213, 123X, 1302, 1303, 132X, 301X, 310X}. The procedure for finding the union of regions with different screen centers consists of three phases: in the first phase the mixed-quaternary codes are scanned and 0s appended at the beginning of each code; in the second phase the two regions are
262
i. GARGANTINI
translated; in the third phase the linear quadtrees are merged and condensed. In a sequential model of computation, the first phase requires O(n * (N1 +Nz)) time; the second phase can be executed in O(n * (NPI+NP2)) time and the third phase in O(NP1 +NP2) time. We conclude that superposing two regions, in this case, can be carried out in O(n * (NP1 + NP2)) time. (d) The case in which R1 and R2 belong to images with different center, different pixel size and different resolution parameter, can be dealt with by combining (a), (b) and (c). Since any step involved in (a), (b), and (c) is timewise bounded by (c * n * (NP1 + NP2)) with c > 1, we have: THEOREM 4 (SuPERPOSITION OF REGIONS WITH DIFFERENT SCREEN, RESOLUTION PARAMETER AND PIXEL SIZE). The superposition of two regions R I c B I t and R2 c BI2 on a screen with resolution parameter n > max (nl, n2) can be executed in O(n * (NP1 + NPe) ) time.
7. Rotation by ~r/2 Rotation of a node by zr/2 in the clockwise direction around the center of the image can be carried out by applying the mapping 0~1~3~2~0;
X~X,
(7.1)
to all digits of the mixed-quaternary code representing that node. Each digit can be mapped independently of one another, since (7.1) is valid for all subquadrants. Rotating a linear quadtree of length N requires, therefore, time proportional to n * N. Re-sorting is necessary to re-arrange the newly obtained mixed-quaternary codes in ascending order. Rotating a region is, therefore, an O(n * N) procedure. As an example, consider the region shown in Fig. 1, whose linear quadtree is (5.1). The rotated linear quadtree is {01XX, 031X, 0323, 033X, 12XX, 211X, 2130,3000,3001,3002,3010,3011}.
8. Concluding remarks In Gargantini (1982a) it has been shown that the case of linear quadtrees saves more than 66% of the memory locations used by regular quadtrees. The goal of the present article was to show that with linear quadtrees it is possible to generate efficient procedures for elementary opeations. For the reader's convenience we have prepared Table 2 where running times are estimated in the presence of a sequential model of computation. If some kind of parallel processing were available, the procedures Encoding, Decoding and Rotation could be remarkably improved; Translation and Superposition could be somewhat improved. The corresponding bounds for regular quadtrees can be found in Hunter & Steiglitz (1979a, b), Jacking & Tanimoto (1980), and Samet (1981). The procedures described in this article could be easily extended to deal with objects in three dimensions: an octal, instead of a quaternary, integer should be used to represent a node.
LINEAR QUADTREES
263 TABLE 2 Time complexities Worst-case time complexity
Operation Encoding Decoding Condensation Translation Adjacency Rotation Superposition CASE A CASE B (a) (b) (c), (d)
Pixel
Region
O(n) O(n)
O(n * NP) O(n * N) O(NP) O(n * NP)
O(n) O(n) O(n)
Two regions
O(n * N) 0 (N1 + N2) O (Nt + N2) O(n * max (NP1, NP2)) O(n * (NP1 +NP2))
A software package, dealing with both the planar and spacial cases, and written in P A S C A L for the D E C s y s t e m - 1 0 , is in prepartion at the University of Western Ontario, and will be available on request. This work was partially supported by the Canadian Government through the Natural Sciences and Engineering Research Council, grant A7136.
References AUGENSTEIN, M. J. & TENENBAUM, A. M. (1979). Data Structures and PL/IProgramming, pp. 481--487. Englewood Cliffs, New Jersey: Prentice-Hall. GARGANTINI, I. (1982a). An efficient way to represent quadtrees. Communications of the Association for Computing Machinery (to appear). GARGANTINI, I. (1982b). Linear octrees for fast processing of three-dimensional objects. Computer Graphics and Image Processing (to appear). HUNTER, G. M. & STEIGLITZ, K. (1979a). Operations on images using quadtrees. IEEE Transactions on Pattern Analysis and Machine Intelligence, 1,145-153. HUNTER, G. M. & STEIGLITZ, K. (1979b). Linear transformations of pictures represented by quadtrees. Computer Graphics and Image Processing, 10, 289-296. JACKING, C. L. & TANIMOTO, S. L. (1980). Oct-trees and their use in representing threedimensional objects. Computer graphics and Image Processing, 14, 249-270. SAMET, H. (1981). An algorithm for converting rasters to quadtrees. [EEE Transactions on Pattern Analysis and Machine Intelligence, 3, 93-95.