Contour codes of isothetic polygons

Contour codes of isothetic polygons

COMPUTER . VISION, GRAPHICS, AND IMAGE PROCESSING 9, 353-363 (1990) NOTE Contour Codes of lsothetic Polygons? PRABIRBHATTACHARYA Department o...

628KB Sizes 4 Downloads 63 Views

COMPUTER

.

VISION,

GRAPHICS,

AND IMAGE

PROCESSING

9,

353-363 (1990)

NOTE Contour Codes of lsothetic Polygons? PRABIRBHATTACHARYA Department

of Computer

Science and Engineering, University Lincoln. Nebraska 68588-01 I5

of Nebraska-Lincoln,

AND

AZRIELROSENFELD Center

for Automution

Research,

University

of Maryland,

College

Pa&

Maryland

20742-3411

Received September 1,1988; revised May 22, 1989 An isothetic polygonal arc is one that has all its sides oriented in two orthogonal directions, so that all its angles are right angles. Such an arc is determined (up to congruence) by specifying a “code” sequence of the form oi Ala, . . . a,,,- 1A,- lam, where the a’s are positive real numbers representing side lengths, and the A’s are single bits that specify whether the arc turns left or right between one side and the next. In this paper we develop basic properties of this code and show how to derive various geometric properties of the arc (or the region it bounds, if it is closed) directly from the code. o IWO academic PRSS, IIIC.

1. INTRODUCTION

An isothetic, “orthogonal,” or “rectilinear” polygonal arc is one that has all its sides oriented in two perpendicular directions, e.g., horizontal and vertical, so that all its angles are right angles. Such an arc is determined (up to congruence) by specifying a “code” sequence of the form (Y&Y~ . . - a,-,A,-+,, where the (Y’S are positive real numbers representing side lengths, and the A’s are single bits that specify whether the arc turns left or right between one side and the next. Note that when we consider the sides of an arc in sequence, we must regard their directions as having signs, e.g., east vs west and north vs south, so that we should regard the sides as having four, rather than two, possible directions. If the (Y’S are specified digitally with bounded precision, they are all integer multiples of a smallest (Y,and we can regard them as integers. (We can thus assume that the vertices of the arc all have integer coordinates.) Our code is then related to the four-direction contour codes used to represent digital arcs or region boundaries [l]. Such a code is a sequence of two-bit numbers representing unit lengths in the four principal directions (i = 9OP). If we represent each run of i’s by its length, we obtain a sequence of integers, each with an associated direction. Consecutive runs *The support of the U.S. Air Force Office of Scientific Research under Grant AFOSR-86-0092, is gratefully acknowledged as is the help of Sandy German in preparing this paper. Prabir Bhattachatya gratefully acknowledges partial support by a Summer Faculty Development Award from the College of Engineering and Technology, University of Nebraska-Lincoln. Also, he is grateful to the Director, Center for Automation Research, University of Maryland, College Park for his kind hospitality during a 3-week visit in May 1988, when part of this research was done. 353 0734-189X/90

$3.00

Copyright 0 1990 by Academic Press, Inc. AI1 rights of reproductiou in any form reserved.

362

BHATTACHARYA

FIG. 8.

AND

ROSENFELD

S n 7’ may have many components, and S U T may have many holes.

and S U T once we know the intersections complicated and will not be given here. 9. DECOMPOSITION

INTO

of C and D; but the details are RECTANGLES

THEOREM 9.1. A simple isothetic polygon having 2n sides (n 2 2) can be decomposed into a disjoint union of at most n - 1 rectangles.

Prooj This is clear if n = 2. If n > 2, by Theorem 5.3 the polygon has n - 2 concave vertices. As in the proof of Theorem 5.3, when we extend a side of any such vertex until it hits another side, we obtain two simple isothetic polygons (A and B) having (between them) strictly fewer concave vertices than the original polygon. Repeating this process for A and B, we eventually obtain polygons with no concave vertices, i.e, rectangles. If we always extend (say) the vertical sides of the concave vertices, the extended sides cannot intersect (except when two of them are collinear, as in Fig. 5). Thus the number of rectangles we finally obtain is at most one greater than the number of “cuts” (= extensions of sides), i.e., (n - 2) + 1 = n - 1. 0

A similar result has been obtained by Ferrari et al. [4]. On efficient algorithms for polygon decomposition see [5]. 10. ISOTHETIC

CONVEXITY

A subset S of the plane is called horizontally (vertical&) convex if any horizontal (vertical) straight line intersects S in at most one segment. If S is both horizontally and vertically convex, we call it isothetically convex. PROPOSITION 10.1. The region surrounded by a simple isothetic polygon is horizontally (vertically) convex if and only if the polygon does not have two consecutive concave vertices joined by a horizontal (vertical) side.

CONTOUR

CODES OF ISOTHETIC

POLYGONS

355

turns, the effect of Aj is as follows:

Evidently this implies that dj+l = -Ajdj; but since j must be odd, we can write this as dj+l = ( -l)jAjdj. Similarly, for a turn from a horizontal direction to a vertical direction we have

+ +

+ -

+ -

-

-

+

so that dj+l = Ajdj = (- l)‘Ajdj, since j is even. Since the sign of d, is +, the proposition immediately follows. 0 COROLLARY 2.2. or 3 (modulo 4).

if i = 0 or 1 (modulo 4); di = -llj:tAj

di = IJj:iAj

if i = 2

Let the coordinates of ui (the terminal vertex of the ith side) be (xi, J+). (By assumption, the coordinates of u0 are (x,,, yO) = (0, O).) If i is odd, si is vertical and we have xi = xi-i, yj = y,-i + d,q. Similarly, if i is even, si is horizontal and we have y, = yi-i, xi = xiel + diaj. We thus have PROPOSITION 2.3.

xi = E;c1d2ja2j (i+

y, =

for i even;

1)/2

c

d2j-1a2j-1

foriodd.

j-l

3. (NON)SELF-INTERSECTION

An isothetic polygonal arc will be called simple if no two of its nonconsecutive sides intersect. When two vertical (or horizontal) line segments intersect, either they have a common endpoint, or an endpoint of one of them is an interior point of the other. When a horizontal and a vertical line segment intersect, either they have a common endpoint (an “L” intersection), or an endpoint of one of them is an interior point of the other (a “T “), or they have a common interior point (a “ + “); It follows from these remarks that if an isothetic polygonal arc is nonsimple, one of its vertical sides must intersect one of its horizontal sides (other than its predecessor and successor). Indeed, if two vertical (horizontal) sides intersect, an endpoint ui of each of them must lie on the other; but vi is also an endpoint of a horizontal (vertical) side. This argument breaks down if the arc has an odd number

356

BHATTACHARYA

AND

ROSENFELD

of sides, and the first and last sides intersect. Thus to determine whether an isothetic polygonal arc is nonsimple, we must check whether any vertical side intersects any horizontal side (other than its predecessor and successor), and if the number of sides is odd, we must also check whether the first and last sides intersect. In Section 2 we saw that for a vertical side s,,, x = x,,-i = x,, is constant and y occupies the closed interval Ih whose endpoints are y,,-i and yh. Similarly, for a horizontal side s,,,, y = yh,-r = yh, is constant and x occupies the closed interval I,,, whose endpoints are xh,-i and x,,,. Thus s,, and s,,, intersect if and only if xh lies in Iht and y,, lies in I,,. (Checking this straightforwardly takes m2 steps; but, in fact, if we first sort the sides into an appropriate order, we can find the intersections in O(m log m) steps; see [2].) 4. CLOSEDNESS

We call an isothetic polygonal arc closed if its first and last vertices coincide, i.e., (xm, y,,,) = (xc, ya) = (0, 0), and its first and last sides are perpendicular. It follows from the second requirement that a closed isothetic polygonal arc must have an even number of sides. Such a closed arc will be called an isothetic polygon. The condition that (x,, y,) = (0,O) can be expressed in terms of the A’s and (Y’Susing Propositions 2.1 and 2.3. In an isothetic polygon, we can regard the first side as succeeding the last side; equivalently, we can regard the sides as numbered modulo m. Thus in writing the code for an isothetic polygon, we should explicitly include A,,,, the direction of the turn between s, and si. Using our sign conventions, it is easily verified that PROPOSITION

4.1.

A, has the sume sign us d,.

Proof. Since the direction of sr is north, the polygon makes a left turn if the direction of s, is east, and a right turn if it is west. 0 The value of A, Proposition 2.1.

is thus determined

5. TURN

from the values of the other A’s using

SEQUENCES

We call an isothetic polygon simple if no two of its nonconsecutive sides (modulo m) intersect. In this section we characterize the possible sequences of turns that such a polygon can make; specifically, we show that if it has m = 2n sides, it must make n + 2 right turns and n - 2 left turns, or vice versa, and that any such sequence of turns is the turn sequence of some simple isothetic polygon. Since a simple isothetic polygon is a Jordan curve, it separates the plane into two regions, its inside and its outside. A vertex ui is conuex (concave) if, when we choose a point P on si and a point Q on si+i sufficiently close to ui, the line segment m lies inside (outside) the polygon. When we go around the polygon counterclockwise, we make left turns at convex vertices and right turns at concave vertices; and when we go around it clockwise, the opposite is true. We shall show in this section that a simple isothetic polygon having 2n sides has n + 2 convex vertices and n - 2 concave vertices. Note that since a polygon is closed, we evidently have n 2 2. We first need a basic result about a simple isothetic polygonal arc that turns only in one direction.

CONTOUR

CODES OF ISOTHETIC

POLYGONS

357

si+l I

I

si

1si+2

. .. . . . . . S-3

FIG. 2. Proof of Lemma 5.1.

LEMMA 5.1. Let ~iA,~i+lA,+~a,+~Ai+~a,+3 be a portion of the code of a simple isothetic polygonal arc, and let A i, A i + 1, Ai+ all have the same sign. Then (Y~+~I ai implies (Y,+~ < 1~~+~.

Proof. As we see from Fig. 2, if we had LY~+~2 (Y~+~,side s~+~ would intersect side s,. q We can now prove THEOREM 5.2. The turns of a simple isothetic polygon all have the same sign if and only if the polygon is a rectangle.

Proof. “If’ is clear. To see “only if,” consider a portion of the code as in Lemma 5.1. If CY~+~ s air we must have (Y~+~-C c++~, hence (Y~+~-C ai+*, hence and so on. Thus the (x or y) intervals occupied by the sides ai+ < ai+3) sj+l> si+3T si+5>*” are strictly nested, and similarly for the (y or x) intervals occupied by the sides si, s~+~,s~+~,. . . (see Fig. 3). But as we continue around the polygon, since it is closed, we must return to our starting point: contradiction. We can use the same argument if ai I ai+*, by numbering the sides in the opposite order. The only loophole in this argument is the possibility that ai+ = ai and so that side si+3 intersects side si at its endpoint, giving us a closed = ai+l? ai+ polygon; but this means that these are the only four sides, i.e., the polygon is a rectangle. Cl THEOREM 5.3. A simple isothetic polygon with 2n sides has n + 2 convex and n - 2 concave vertices.

FIG. 3. Proof of Theorem 5.2.

358

BHATTACHARYA

AND ROSENFELD

I I

Sk-1

a! A

i

I I I I .

B

b

, sk+l

sk

FIG. 4.

Proof

of Theorem

5.3 (first

case).

Proof: This is clear for n = 2, since the polygon must then be a rectangle and we have 2n = n + 2 = 4, n - 2 = 0; we proceed by induction on n. If the polygon has no concave vertices, it is a rectangle by Theorem 5.2; suppose it has a concave vertex. Without loss of generality, let the sides of this vertex be s1 and sZn. Extend si inside the polygon until it hits another side, say sk. Suppose first, as in Fig. 4, that si extended hits sk at an interior point, dividing it into two segments, a and b. Let A be the polygon whose sides are s,(extended), s2,. . . , sk-i, a, and B the polygon whose sides are the extension of si, b, sk+i, . . . , sZn. It is easy to see that A and B are simple isothetic polygons having k and 2n - k + 2 sides, respectively, so that in particular k = 2h is even. Moreover, evidently k 2 4 and 2n - k + 2 2 4, so that 2n - k + 2 < 2n - 2 and k < 2n - 2. Thus the induction hypothesis holds for A and B, so that A has h + 2 convex and h - 2 concave vertices, and B has n - h + 3 convex and n - h - 1 concave vertices. But A has k - 1 of the original polygon’s vertices plus one new convex vertex, and B has 2n - k of the original vertices plus two new convex vertices, and the only original vertex not in either A or B is concave. Hence the original polygon has (h + 2) - 1 convex vertices from A and n - h + 1 convex vertices from B, for a total of n + 2; and it has h - 2 concave vertices from A, n - h - 1 concave vertices from B, and one concave vertex that is,neither in A or B, for a total of n - 2. It remains only to consider the cases where s1 extended hits sk at an endpoint, as in Fig. 5. (This endpoint must itself be a concave vertex.) Here A and B have k - 1 and 2n - k + 1 sides, and the induction hypothesis holds. Thus k - 1 = 2h is even, and A has h + 2 convex and h - 2 concave vertices, while B has n - h + 2 convex and n - h - 2 concave vertices. But (in the first example shown) A and B each have one new convex vertex, and two of the original concave vertices are neither in A nor in B; thus the original polygon has ((h + 2) - 1) + [(n - h + 2) - l] = n + 2 convex vertices and (h - 2) + (n - h - 2) + 2 = n - 2 concave vertices. The proof for the second example is analogous. • I Conversely, we have THEOREM 5.4. A simple isothetic polygon with 2n sides can have any sequence of n + 2 convex and n - 2 concave vertices.

.... St -L CONTOUR

CODES OF ISOTHETIC

. . . .

I I 1

S2n

A; ....

%-I

.... l-

359

POLYGONS

% . . . . I I I

=2n

A;

B

B I I I

: I

.... 'k+l

'k

sk

-L

. . . .

. . . .

'k+l FIG. 5.

-Isk-l

Proof

of Theorem

FIG. 6.

5.3 (second

Proof

of Theorem

case; two examples).

5.4.

Proof: This is clear for n = 2; we proceed by induction on n. As Fig. 6 shows, given any simple isothetic polygon, we can add two more sides to it in the neighborhood of any vertex to obtain two vertices of the same type and one of the opposite type, in any order. Consider any (cyclic) sequence of n + 2 convex and n - 2 concave vertices, where n > 2. Evidently this sequence must contain a consecutive pair of vertices of opposite types, and so must contain three consecutive vertices two of which are of one type and the third of the other type. Thus the given sequence could have been obtained from a sequence of n + 1 convex and n - 3 concave vertices using one of the constructions in Fig. 6. Since by induction hypothesis the latter sequence is realizable, so is the former. q The fact that the number of convex vertices of a simple isothetic polygon must be four greater than the number of concave vertices is a special case of the well-known fact that the integral of the curvature of a simple closed curve, or the sum of the turns in a complete circuit of a simple closed polygon, must be 360”; it follows in the case of an isothetic polygon that the number of + 90” turns must be four greater than the number of - 90” turns. The special nature of isothetic polygons allowed us to prove this result by an induction argument. 6. GEOMETRIC

PROPERTIES

In this section we show how to compute geometric properties of an isothetic polygonal arc directly from its code. We also show how to compute properties of the

360

BHATTACHARYA

region surrounded polygon.

AND

ROSENFELD

by a simple isothetic polygon directly from the code of the

6.1. Arc Properties Let a,A,a, . . - A,-1a, be an isothetic polygonal arc. The arc length of this arc is, of course, L = Cy=iai. The horizontal and vertical extents of the arc are H = max(x,) - min(x,) and V = max(y,) - min(yi), respectively, where the x’s and y’s can be computed from Proposition 2.3. If we regard the arc as having uniformly distributed mass, we can compute the coordinates of its centroid. Each side si has mass proportional to its length ai. For centroid computation we can regard this mass as concentrated at the midpoint of si, which has coordinates (xi-l,

bi3 %) =

Yi-1

+

idiai)

(Xiel + {di(Yi, yivl)

for i odd ( si vertical) for i even (si horizontal)

Then the coordinates of the centroid of the arc are x = CUi’yi/L,

7 = CVi”JL.

We can thus compute X and j straightforwardly using Proposition 2.3. Higher-order (central) moments of the arc can be computed similarly. From these moments we can compute various useful properties of the arc, e.g., the equation of its principal axis of inertia or the straight line that best approximates the arc in the least squares sense. 6.2. Region Properties Let a,A,a, ..- Amdla,,,Am be a simple isothetic polygon, where m = 2n is even. Such a polygon divides the plane into two regions, one inside the polygon and the other outside it. The area A of the region inside the polygon can be computed as follows: For each horizontal side si (i even), consider the rectangular strip ui bounded by si and the x-axis. Let us regard the area of ai as positive if d,y, > 0, and as negative if d,y, -C 0, where di is the direction of si when we go around the polygon clockwise. It can be shown (see [3]) that A is just the algebraic sum of these areas, e.g., A = C;-,d,,a,,y,,. The computation of A is illustrated in Fig. 7. We can also use the strips ui to compute the coordinates of the centroid of the region, which we regard as having uniformly distributed mass. The centroid of ui has coordinates (ri, si) = (xi-i + +diai, $yi-i), and its mass is diaiyi (note that we are treating the mass as a signed quantity). Thus the coordinates of the centroid of the region are

h=l

h=l

Higher-order (central) computed similarly.

moments

of the region, its principal

axis, etc., can be

CONTOUR

CODES OF ISOTHETIC

POLYGONS

361

y=o

FIG. 7. An example of area computation (see text): strips 9, u,,, q2, q6 make positive contributions to the area, and strips a,, +,, u,,,, q4 make negative contributions.

7. CROSSINGS AND SURROUNDEDNESS

Let qAia, . .. A,- i(~, be a simple isothetic polygonal arc, and let H be a horizontal line. If a vertical side sZi+i of the arc intersects H, and the intersection point is not an endpoint of sZi+i, we say that H crosses the arc at a point of sZi+i. If a horizontal side sZi of the arc is contained in H, and the preceding and succeeding vertical sides s2; *I are on opposite sides of H (i.e., one is above H and the other below it), we say that H crosses the arc at szi. Let a,A,cu, e-e A ,,-~(Y,A, be a simple isothetic polygon, and let P = (x, y) be any point that does not lie on any of its sides. Let HP be the horizontal half-line whose left endpoint is P. It is not difficult to prove that if HP crosses the polygon an odd number of times, P is inside the polygon (i.e., the polygon surrounds P) and if it crosses an even number of times (where 0 is regarded as even), P is outside the polygon. More generally, let A be any arc (or curve) that does not intersect the polygon. (Determining whether two isothetic polygonal arcs intersect can be done as in Section 3.) Then every point of A is either inside or outside the polygon, and we can determine which is true by applying the crossing test to any point P on A. 8. UNIONS

AND INTERSECTIONS

Let C and D be two simple isothetic polygons, and let S, T be the regions inside C and D, respectively. If C and D do not intersect, then either C surrounds D, D surrounds C, or neither surrounds the other. In the first case we have S n T = T and S U T = S; in the second case, S n T = S and S U T = T, in the third case, S n T = 0. Thus in all of these cases we immediately know the borders of S 17 T and S u T (in the last case, S n T has no border, and S U T has the two disjoint borders C and 0). The situation is more complicated if C and D intersect. Here S n T may consist of many connected components, and S U T may have many holes (see Fig. 8), so that they may have many borders. These borders consist of concatenations of subarcs of C and D, where each subarc begins and ends at an intersection of C and D. Thus we can construct the codes of the borders of S n T

362

BHATTACHARYA

FIG. 8.

AND

ROSENFELD

S n 7’ may have many components, and S U T may have many holes.

and S U T once we know the intersections complicated and will not be given here. 9. DECOMPOSITION

INTO

of C and D; but the details are RECTANGLES

THEOREM 9.1. A simple isothetic polygon having 2n sides (n 2 2) can be decomposed into a disjoint union of at most n - 1 rectangles.

Prooj This is clear if n = 2. If n > 2, by Theorem 5.3 the polygon has n - 2 concave vertices. As in the proof of Theorem 5.3, when we extend a side of any such vertex until it hits another side, we obtain two simple isothetic polygons (A and B) having (between them) strictly fewer concave vertices than the original polygon. Repeating this process for A and B, we eventually obtain polygons with no concave vertices, i.e, rectangles. If we always extend (say) the vertical sides of the concave vertices, the extended sides cannot intersect (except when two of them are collinear, as in Fig. 5). Thus the number of rectangles we finally obtain is at most one greater than the number of “cuts” (= extensions of sides), i.e., (n - 2) + 1 = n - 1. 0

A similar result has been obtained by Ferrari et al. [4]. On efficient algorithms for polygon decomposition see [5]. 10. ISOTHETIC

CONVEXITY

A subset S of the plane is called horizontally (vertical&) convex if any horizontal (vertical) straight line intersects S in at most one segment. If S is both horizontally and vertically convex, we call it isothetically convex. PROPOSITION 10.1. The region surrounded by a simple isothetic polygon is horizontally (vertically) convex if and only if the polygon does not have two consecutive concave vertices joined by a horizontal (vertical) side.

-

CONTOUR

CODES OF ISOTHETIC

POLYGONS

363

Prooj Let vertices vi and vi+t be concave, and consider a line segment s just outside the region and parallel to side si. Evidently, the straight line containing s crosses sides si _ i and si + r, and hence intersects the region (at least) twice. 0 COROLLARY 10.2. The region surrounded by a simple isothetic polygon is isothetitally convex if and only if the polygon does not have two consecutive concave vertices. 11. CONCLUDING

REMARKS

Isothetic polygonal arcs can be represented by compact codes. We have shown how various types of geometric information can be recovered from these codes. In particular, we have characterized the codes that represent simple isothetic polygons. O’Rourke [6] has shown that a simple isothetic polygon is uniquely determined by the coordinates of its vertices, {(xi, yi), . . . , (xm, y,)}, which can be listed in any order. Our code requires the side lengths and turn directions to be listed in order; but it is about twice as compact as the vertex coordinate code, since (in a 2” X 2” image, where the coordinates and lengths are n-bit numbers) we need m(n + 1) bits, while the coordinates need 2nm bits.’ Our code can be regarded as a run-coded, difference-coded four-neighbor contour code. It would be of interest to develop algorithms for converting between our code and other types of codes for regions, such as the medial axis transformation or the quadtree. (On conversion between four-neighbor contour codes and quadtrees, or vice versa, see [7, 81.) It would also be of interest to define parallel algorithms for processing our code, using various models of parallel computation (mesh, hypercube, shared-memory, etc.). (On parallel processing of ordinary contour codes see [9-111.) Finally, we remark that it would be of interest to investigate the possibility of extending the ideas in this paper to isothetic polyhedra in three (or more) dimensions. REFERENCES 1. A. Rosenfeld and A. C. Kak, Digital Picture Processing, Chap. 11, Academic Press, New York, 1982. 2. F. P. Preparata and M. I. Shames, Computational Geometry-An Introduction, Chap. 7, SpringerVerlag, New York, 1985. 3. H. Freeman, Computer processing of line-drawing images, ACM Comput. Surveys 6, 1974, 76. 4. L. Ferrari, P. V. Sankar, and J. Sklansky, Minimal rectangular partitions of digitized blobs, Comput. Vision

Graphics

Image

Process.

28, 1984, 58-71.

5. S. Nahar and S. Sahni, Fast algorithm for polygon decomposition, IEEE Trans. Comput. Aided Design 7, 1988, 473-483. 6. .I. O’Rourke, Reconstruction of Orthogonal Polygons from Vertices, Johns Hopkins University Technical Report JHU/EECS-84/H, 1984. 7. H. Samet, Region representation: Quadtrees from boundary codes, Commun. ACM 23, 1980, 163-170. 8. C. R. Dyer, A. Rosenfeld, and H. Samet, Region representation: Boundary codes from quadtrees, Commun. ACM 23,1980, 171-179. 9. A. Y. Wu, T. Dubitzki, and A. Rosenfeld, Parallel computation of contour properties, IEEE Trans. Pattern Anal. Mach. Intell. 3, 1981, 331-337. 10. A. Y. WU, S. K. Bhaskar, and A. Rosenfeld, Parallel processing of region boundaries, Pattern Recognit., 22, 1989, 165-172. 11. Y. Hung and A. Rosenfeld, Processing Border Codes on a Mesh-Connected Computer, J. Parallel Distributed Computing, in press. ‘One of the referees pointed out that when the coordinates are stored in order, only (m + 1)n bits are needed, since two successive vertices either have the same x-coordinate or the same y-coordinate. For example, if the first side is vertical, we need only list (xl, yi) together with ya, x3, y4, xs,