COMPUTER VISION, GRAPHICS, AND IMAGE PROCESSING36, 10-30 (1986)
Digital Straight Segments SONPI~AM Department of Computer Science, California State University, Northridge, Northridge, California 91330 Received April 23, 1985; accepted May 30, 1986 It will be proved in this paper that all digital straight segments connecting two given pixels form an area whose lower bound and upper bound are also the digital straight segments. These two bounds will be simply calculated and have the following properties; one is different from the other by unity in the x or y coordinate, and their chain codes of directions reverse from each other. Hence, for practical purposes, these results are used to define general digital straight segments as the digital arcs in this area, and also used to define the convexity and concavity of digital arcs. 9 1986 AcademicPress, Inc. I. I N T R O D U C T I O N
On a grid a set of chain pixels is called a digital arc. Chain means every pixel in the set except the endpoints has exactly two neighbors in the same set. Hence a digital arc can be represented by either the sequence of directional changes between neighboring pixels [6, 9], or the sequence of x y coordinates of the centers. A number of authors have studied criteria for a digital arc to be the digitization of a real convex arc or a real concave arc [11, 12]. In other words, they have studied the characterization of digital arcs digitized from the convex or concave real arcs. A well-known definition of the convexity of a real arc is that the real straight segment connecting two real points on the arc is under the arc. But finding a similar definition for digital convex arcs is very difficult, because there are many digital segments between two pixels. So the problem of characterization of digital convex arcs leads to the problem of characterization of digital straight lines [4, 5]. Numerous authors have researched this problem [1, 3, 21, 24, 25] and studied criteria of characterization. The importance of the straightness of digital arcs has been studied extensively in the works of Kim [15] and Kim and Rosenfeld [14] in 1982. So far, three characterizations are found: the first one is Freeman's criteria [8], the second one is Rosenfeld's chord property [24], and the last one is the absence of unevenness of Hung [13]. Most works based on these characterizations lead to complex algorithms [4, 5, 39]. Many other works are also related to arcs; examples include handwritten contour analysis [10], convex polygons [33-35, 38], contours [16-23], and languages on grids [4, 5, 30-32, 36-37]. In practice, digitization of straight lines might not satisfy, due to the digitization error, the criteria mentioned above. Hence, a digital arc with few errors in digitization cannot be recognized as a digital straight line. So, how do we know when a digital arc is "nearly" a digital straight arc? To answer this question, the paper will deal with all digital straight segments between two given pixels P and Q. If the area of all digital segments is achieved and calculated, then a digital arc with endpoints P, Q can be defined generally as a digital straight segment if it falls in the area. Such a general definition makes more sense if the upper bound and lower bound of the area are the digital straight segments between P and Q. Therefore, general digital straight segments might not 10 0734-189X/86 $3.00 Copyright 9 1986 by AcademicPress, Inc. All rights of reproduction in any form reserved.
DIGITAL STRAIGHT SEGMENTS
11
y axis
3\~
/1
" /N/\ 5
/
6
I \
(a) FIG. 1.
0 7
1
2
3
- - x axis
(b)
(a) 8 neighbors and 8 directions.(b) a xy coordinatesystemon a grid.
satisfy all three of Freeman's criteria. Moreover, if the upper bound and lower bound of the area are determined precisely, then an algorithm for determining a digital arc to be straight can be derived easily. A chain code is a set of pixels so that all pixels except endpoints in the set have exactly two neighbors. If the set has infinitely many elements, we call it an infinite chain code, otherwise we call it a finite chain code or a digital arc. Mathematical Notation. Some notation is introduced for use throughout the entire paper. Ig[c: the remainder of the integer g divided by the integer c. It is also called g modulo c. abs_(x): the absolute value of the number x. /-1, 1 2 , . . - , / c - i : a sequence.., l o, 11,..., It_ 1, l o, 1t . . . . . 1c_1,... (the bars indicate the repetition of numbers between bars). (g, c) = 1: two integers g and c are relatively prime. Ix ]: the greatest integer less than or equal to the number x. [ x ]: the smallest integer greater than or equal to the number x. Preoious Work. A pixel on a grid has 8 neighbors: 2 horizontal, 2 vertical, and 4 diagonal. These 8 neighbors are denoted from 0 to 7 counterclockwise. Henceforth we have also 8 directions from one pixel to its 8 neighbors. The numbers 0 to 7 can also be used for directions. (See Fig. la). Coordinates of Pixels. A n x - - y coordinate system can be used on the grid to locate the positions of pixels. The origin of the system is placed at the center of a pixel, and moreover all centers of pixels have integer coordinates. (See Fig. lb). Henceforth, a chain code can be represented by one of the followingtwo sequences:
(a) It can be represented by coordinates of centers of the pixels. (This representation can be used for any set of pixels and is not restricted only to the chain code). (b) It can be represented by a sequence of directions from pixels to their neighbors. In this representation, if the picture is to be recovered from the chain code, one pixel should be known as a starting point. Freeman [7] suggested three criteria for a chain code (represented by a set of directions) to be a digital straight line: (1) At most two basic directions are presented and these can differ by unity modulo 8.
12
SON PHAM
(2) One of these values always singly occur. (3) Successive occurrences of the principal direction occurring singly are as uniformly spaced as possible. We observe that if a chain code satisfies these three criteria above, then its finite subchain code might not satisfy criteria 3. Several authors [1, 2, 3, 7, 23, 39] provided algorithms to digitize a drawn real line to obtain digital straight lines. Since each real line can be drawn if the slope is given, then many algorithms attempt to find the digital straight lines if rational slopes are given. Later Rosenfeld [24-29] introduced the chord property and proved that a digital arc (finite chain code) having the chord property is a digital straight line segment. In this case the chord property implies the first two of Freeman's criteria. This result can be extended to the infinite chain codes, and the chord property is equivalent to Freeman's three criteria [24, 39]. The chord property is defined as follows: Let ~ be a finite digital arc. Let P, Q be two pixels in this arc. Let PQ denote the real straight segment between the centers of P and Q. We say that PQ lies near ~ if, for any real point (x, y) of PQ, there exits a pixel in ~ with center (i, j ) in ~ such that max(abs(i - x), a b s ( j - y ) ) < 1. We say that ~" has the chord property if, for every P, Q in ~, the straight segment PQ lies near ~'. Recent Results. The Chain Code Formulas. The chain codes of directions of digital straight lines with slope g/c,(g, c) = 1 can be obtained by the following formulas. We note that the formulas are slightly different on 8 regions of slopes, and these chain codes satisfy the three criteria of Freeman.
Region 0 (0-45~ periodic sequence: 1o, I t . . . . . Ic_1, where a i -- Ig" ilc; i = 0,1 . . . . . (c - 1); l i = (a i - ai+ x + g)/c; Region 1 (45-90~ periodic sequence: (l o + 1),(l t + 1) . . . . ,(lg_ 1 + 1 ) , where (lo, 11. . . . . Is_t) is the chain code of the line with slope (g - c)/g. (See Region 0.) Region 2 (90-135~ periodic sequence: (3 - lo),(3 - 11). . . . ,(3 - lg_t), where (l o, 11,..., Is_l) is the chain code of the line with slope (g - c)/g. (See Region 0.) Region 3 (135-180~ periodic sequence: (4 - lo) , (4 - ll) . . . . . (4 - /c-l), where (1o, ll, . . . . lc_1) is the chain code of the line with slope g/c. (See Region 0.) Region 4 (180-225~ periodic sequence: (4 + 10),(4 + lt) . . . . ,(4 + lc_t), where (l o, I t , . . . , 1c_1) is the chain code of the line with slope (g)/c. (See Region 0.) Region 5 (225-270~ periodic sequence: (5 + 1o),(5 + ll) . . . . ,(5 + l~_1), where (l o, l t . . . . , ls_t) is the chain code of the line with slope (g - c)/g. (See Region 0.) Region 6 (270-315~ periodic sequence: (7 - 10), (7 - l l ) , . . . , ( 7 - / g - l ) , where (10, 1t . . . . . /g-l) is the chain code of the line with slope (g - c)/g. (See Region 0.) Region 7 (315-360~ periodic sequence: (8 - lo), (8 - ll) . . . . , (8 - lc_l), where (/0, / 1. . . . . lc_l) is the chain code of the line with slope g/c. (See Region 0.)
D I G I T A L STRAIGHT SEGMENTS
13
Results of this Paper. Given two pixels P and Q whose centers are denoted respectively by (x~, y~) and (xQ, yq). The slope of the real straight line passing through these two centers is g / c where g = abs(yQ - Y e ) and c = abs(xQ - Xp). Since the chain code of directions from P to Q is slightly different depending on the position of P and Q (see Recent Results), we assume throughout this paper that P is the origin (0, 0) and Q is in region 0. The results for Regions 1-7 are similar and will not be reported here in order to avoid the technical complexity of this paper. With two given pixels P and Q above, how many digital straight segments are there to connect P to Q? In order to answer this question, shift and reverse operators are introduced. The segment l o, l 1 . . . l c_ 1 is a periodic sequence of the digital straight line of slope g / c . It can be viewed geometrically as a path to connect a pixel to another pixel which has distant c pixels horizontally and g pixels vertically. Therefore, shift or reverse operators can be used to obtain other digital straight segments from P to Q. Let 1 = (l o, l I . . . . . / c - 1 ) , then shift and reverse operators are defined as follows: shift(/o, l l , . . . , lc_l) -- ll, 12,..., lc_l, 10; reverse(/0, ll . . . . . lc_1) = lc_1, 1c_2,..., ll, lo. If the shift operator is used multiple times, then there is a set of c different digital segments from P to Q: 1, shift(l), shift2(l) . . . . . shiftc-l(l). The superscript indicates the number of times the shift operator is applied. Similarly, the shift operator can be applied multiple times on reverse(l) to get a set of c different digital segments: reverse(l), shift(reverse(l)),..., shiftc-l(reverse(l)). The two above set are the same, i.e., reverse(l) is shiftk(l) for some k. So far there are c different digital straight segments being generated. Are there any other digital straight segments from P to Q? It is rather difficult to find all digital straight segments, because as a conceptual definition of digital straight segments, they are digital arcs from P to Q of digital straight lines passing through P and Q. Therefore, we consider the set of these c different digital straight segments which form an area between P and Q. It is proved that this area has an upper bound and a lower bound as digital straight segments from P to Q. Moreover, any straight line passing through P and Q will have their line segments between P and Q in the area. For practical purposes, the lower bound is l and the upper bound is calculated from the lower bound by one of the following two methods: (a) the upper bound is different from lower bound by unity in some sense of y coordinates and (b) the upper bound chain code can be obtained from the lower bound chain code by the reversed operator.
II. ILLUSTRATION Given two pixels with centers P = (0,0) and Q = (12, 5). All digital straight segments connecting P to Q will be illustrated in here. One of these segments is calculated as follows:
ai =
15ih2 and l i = (a i - ai+ 1 + g ) / c
for i = 0,1 . . . . . 11.
14
SON PHAM TABLE 1 i
0
1
2
3
4
5
6
7
8
9
10
11
ai
0
5
10
3
8
1
6
11
4
9
2
7
Ii
0
0
1
0
1
0
0
1
0
1
0
1
Yi
0
0
0
1
1
2
2
2
3
3
4
4
t endpoint
12
5
T endpoint
Note. First row: the integer x coordinates; second row: the cyclic group of modulo 12; third row: the periodic sequence of directions; fourth row: the integer y coordinates.
Each direction /~ will connect two pixels whose centers are (i, y~) and (i + 1, where Yo = 0 yi =
i--1 ~,lj
Yi+I)
(an endpoint), for i = 1,2 . . . . . 11,
j=O
Y12 = 5
(an endpoint).
So we have the summary in Table 1. We call ( l i ) i =c- ox the sequence of directions connecting P to Q. We call (Yi)~l the corresponding sequence of y levels. The shift operator will generate the other 11 digital straight segments from P to Q depending the number of shifts. These digital straight segments are listed below with their corresponding y levels. (See Table 2). The reversed operator will generate 12 digital straight segments by reversing the 12 previous sequences listed above. For example, lo, l 1 9 9 9 l c _ 2 l c _ 1 w i l l be reversed TABLE 2 12 Digital Straight ~gmentsConnecfing P to Q Generatedby theShifts # of shifts
0 1 2 3 4 5 6 7 8 9 10 11
Digital straight segment 001010010101 010100101010 101001010100 010010101001 100101010010 001010100101 010101001010 101010010100 010100101001 101001010010 010010100101 100101001010
The corresponding y levels 0001122233445* 0011222334455 0112223344555 0011122334445 0111223344455 0001122333445 0011223334455 0112233344555 0011222334445 0112223344455 0011122333445 0111223334455
Note. *: the minimum of all y levels; t: the maximum of all y levels.
t
15
DIGITAL STRAIGHT SEGMENTS TABLE 3 12 Digital Straight Segments Connecting P to Q # of shifts
Reversed digital straight segment
0 1 2 3 4 5 6 7 8 9 10 11
y level of the reversed digital straight line
101010010100 010101001010 001010100101 100101010010 010010101001 101001010100 010100101010 001010010101 100101001010 010010100101 101001010010 010100101001
0112233344555* 0011223334455 0001122333445 0111223344445 0011122334445 0112223344555 0011222334455 0001122233445 0111223334455 0011122333445 0112223344455 0011222334445
to be lc_llc_ 2 . . . 111o. The new 12 reversed sequences are also digital segments connecting P to Q. They are listed below with their corresponding y levels. (See Table 3.) They are reversing segments of the 12 segments in Table 2. We notice that Tables 2 and 3 have the same digital straight segments. In Table 2, the minimum of y levels is at the beginning of the table, while in Table 3, the maximum is. Figure 2 will show two segments rain I and max l. III. DIGITAL STRAIGHT SEGMENTS: SHIFT AND REVERSED OPERATORS
Given P = (Xe, yp) and Q = (Xo, yo). The slope g / c of the real line passing these two real points are calculated by g = abs(y o - Ye) and c = abs(x o - xe). We assume without loss of generality that P = (0, 0) and (g, c) = 1, We also refer to pixels by their centers, for example, the pixel P means the pixel with center P. Consider the sequence l = (l 0, l 1. . . . . l c _ l ) calculated by the formulas: ai =
Ig" ilc,
l i = ( a i -- a i + 1 + g ) / / c
for i = 0,...,c
/-
5
._/
4 3
_/ o
max,.
r
min~
/ /
/
J f-
/
2 1
/-
-- 1.
/---
__/ J
1
/"'"/.
2
/
f ,
3
4
5
6
7
8
9
10
11
12
FIG. 2. Area of digital straight segments from P = (0, 0) to Q ~ (12, 5). It is bounded by two digital straightsegmentsm.inandmax:minl=(001010010101);maxl=(101010010100).
16
SON PHAM
So, we have the following results reported in [40]: THEOREM (see proof in [40]). The sequence l = (l o, I 1. . . . . lc_l) is a digital straight line with slope g / c . There are g direction l ' s and ( c - g) direction O's in the period l o, l 1. . . . . lc- i. As a result of this theorem, we can conclude that l = (l o, 11,---, lc-1) connects P to Q, because Q has a distance from P of g pixels vertically and c pixels horizontally. Shift and Reversed Operators. We define the shift operato r as a function on the Cartesian p r o d u c t D x D •
... •
c tithes where D = ( 0 , 1 , . . . , 7} is the set of 8 directions. We also denote s" as a composite function of s applied n times, for all positive integers n . The notation s o is considered as the identity function. Here are some examples s~
ll . . . . , / c - t ) = (lo, l l , - . . , l~-1),
sl(lo, l t , - . . , / c - t )
= (ll, 12 . . . . . lc-1, lo).
9 W e define the reversed operator r on the same Cartesian product D • D • . . . • D (multiphed c times). F o r example, r(lo, It . . . . . / c - i ) = ( / c - i , lc-2 . . . . . ll, lo).
T h e following are the properties of s and r: (1) s and r are two bijections on D • D • . . . • D, (2) r 2 __ s~ (identity), (3) r ( s k) = sC-k(r), for all intege r k. Y-leoel Function. F o r each digital straight segment in Region 0, the directions can only be 0 or I (as we mentioned above, we restrict our discussion to this region only because the discussions are similar for the other regions). Hence, the coordinates of the pixels forming a digital straight segment can be constructed as partial sums of the sequence of directions. F o r example, the digital straight segment l -- (l o, l 1. . . . . / c - i ) has the coordinates (i, Yi); i = 1,2 . . . . . c - 1, where i--1
y,=Elj. j=0
F o r completeness, let Yo = 0 and Yc = g. Henceforth, for i = 0,1 . . . . . c - 1; l i will c o n n e c t the center (i, y~) to the center (i + 1, Yi+t). W e call (Y~)~=o the corresponding sequence of y levels, and denote it level (1).
17
DIGITAL STRAIGHT SEGMENTS
Some properties of y levels are listed as follows: For every two digital straight segments 1, 1' connecting P to Q in Region 0, we have: (1) their corresponding sequences of y levels are increasing, (2) 1 ~ l' iff their corresponding sequences of y levels are different. The y-level function is very convenient to define the positional order of the chain segments: l < l'
(it is said that I is below 1')
iff level(l) < level(l').
This order will be used to define the upper bound and lower bound of the area of all digital straight segments from P to Q, and it will be used widely in the later proofs. Now we prove the following proposition which describes the relationships amongst 1, s, and r. PROPOSITION 1.
We have the following properties:
(1) 1, s(l), s2(l) . . . . , s C - l ( l ) are all different; (2) r(l), r(s(l)), r(s2(l)) . . . . . r(sC-l(l)) are all different; (3) two sets in (1) and (2) are the same, i.e., (s
Proof. and (3).
(t)lk=0,1 . . . . .
I} =
c-
{r(sJ(l))~'=O,
1 .....
c-
1}.
Result (2) can be derived from result (1). So, we prove only results (1)
Proof of Result (1).
We have
j-1 Eli i=0
j - 1 ai =
E i=O
+ g -- a i + l C
ao
+ j 9g
-- a j
= C
We extend the definition of a i for i < c to the case where 1 > c. That is, ai = Ig 9 ilc for all integer i. Therefore, a i = ai+kc for all integer k and c. We have j--i
j-I
i=0
i=0
,_,Y' .,,,[s~'tl~l ' =
,.Y' a i + k
a i + k + 1 + g = a k -- a j + k + j . c
g
c
We will prove s k ( l ) ~ sk'(l) for all k :g k', by showing their corresponding sequences of y levels are different. Applying the above equations for k and k', we have j-1
,.r
L,,,rsk l , -- ak - a j + , + j . g
i=O
C
j--1
,.i,v'
i=O
=
-
aj+
, + j. c
g
18
SON PHAM
T h e p r o o f of result (1) is then reduced to finding an integer j so that a k - aj+ k ~ a k, - aj+ k, or (by definition of a's)
Ig" k l ~ - Ig" k'l ~ ] g ( J + k ) l ~ - I g ( j
+ k') 1~.
(1)
N o w we find an integer j to satisfy this inequality. We have Ig " kit ~ Ig " k'l~ as a result of (g, c) = 1. So, we can assume without loss of generality that Ig" klc < Ig " k'l~. Therefore, a constant M, less than c, can be found such that Ig" kl~ + M < c < Ig" k'l~ + M. Since a~'s, with i = 0 , 1 , . . . , c 1, generate the set {0,1 . . . . . c - 1}, i.e., { I g ' J l c IJ = 0 . 1 , . . . , c - 1} = {0, 1 . . . . . c - 1}, therefore we can select an integer J0 so that M = Ig'JolcW e have IIg" kl~ + J 0 " g]~ = Ig" kl~ + IJ " g l c - c IIg" k'lc + J o " g[~ = Ig" k'l~ + lJ " gl~ and their difference is
Jig" k i t + Jo " g l c - [ I g " k'l + Jo" glc = Ig " k l ~ - Ig" k'l - c. This implies inequality (1) is valid for this J0.
Q.E.D.
Proof of Resuh (3). T o prove result (3), i.e., to prove (l, s(l), s2(l) . . . . , s c - l ( l ) } = { r ( l ) , r[s(l)] . . . . ,r[sC-l(l)]}, we need to prove only r ( l ) = sg(l), for some integer k. So, we prove the following proposition. PROPOSITION 2.
Proof.
There exists an integer k so that r ( l ) = sk(l).
W e have by definitions of l, r, and s, j--1
j--1
y, [r(/)]i=
Elc_l_i=
i=0
i=0
j--1
j--1
E [sl"(l)]i = E 11,+i = i=0
i=0
ac_j-ar
+jg _ ar
C
C
+jg
ak
- al"j C
Hence, the problem is reduced to finding an integer k, independent of j, so that ar = a k - ak+ j, for all j = 0,1 . . . . , c - 1. It is equivalent to prove with j =
19
DIGITAL STRAIGHT SEGMENTS 1 . . . . . c - 1 (case j = 0 is evident),
Ig( c - g)I~ = Ig" kl~ - I g ( k + J ) I ~ -
(2)
W e prove this equation as follows: since (g, c) = 1, therefore all a~'s; i = 0, 1 . . . . . c - 1 are all different and form the set {0, 1 . . . . . c - 1}. Hence, there exists an integer k 0 so that ako = C-- 1,
i.e., [ g . ( k o ) l ~ = c -
1.
This k o is the candidate for Eq. (2). Indeed, with ko, the right-hand side of Eq. (2) is calculated as
Ig" (k0)[c-Ig" (ko +
J)lc = c -
1 -I(c
-
1) + g "Jlc-
Because Ig "Jl~ > 0, for all j = 1,2 . . . . . c - 1, therefore ](c - 1) + g "Jlc = (c - 1) + Ig "Jl~ - c = - 1 + Ig "Jl~. Hence [g.(ko)
lc-[g.(k0+j)[
1-(-l+[g-j[c)=C-[g.j[c
=c-
which is equal to the left-hand side of (2).
Q.E.D.
IV. LOWER BOUND AND UPPER BOUND DIGITAL STRAIGHT SEGMENTS
In the previous sections, we constructed c different digital straight segments from the pixel P = (0,0) to the pixel Q -- (c, g), where (g, c) = 1. These c segments are shifted f r o m each other and one of them is l = (l o, l 1. . . . . lc_1) which is given b y the formulas l i = (a~ - a~+ 1 + g ) / c , with a~ = [g 9 i[c, for i = 0, 1 . . . . , c - 1. Also each segment corresponds to a sequence of y levels which are y coordinates of the centers of pixels in the segment. Let min y and max y be respectively the minimum and m a x i m u m of all these c corresponding sequences of y levels, i.e., (min Y ) i = m i n i m u m { ( s k ( 1 ) ) i [
k = 0,1,...,c
(max Y ) i = m a x i m u m { ( s k ( l ) ) i [
k = 0,1 . . . . . c -- 1}
min y < level(sk(l))
< max y
- 1}
for all k.
W e will prove in this section that min y and max y are also two corresponding sequences of l and r(l), respectively. In other words, we have the following property: l<_sk(l)
for all k = 0,1 . . . . , c - 1 .
Moreover, min y and max y are different by unity except for the endpoints, i.e., (max y)o = (min y)o = 0, (max y)~ = (min y)~ + 1 (max y ) c = (min Y ) c = g.
for all i = 1,2 . . . . . c - 1 ,
20
SON P H A M
PROPOSITION 3.
W e have the following order l
forallk=0,1
..... c-1.
(This proposition hence implies that l = rain l.) Proof. F o r all j = 0 , 1 , . . . , c - 1 the sum of the first j terms (from index 0 to index j - 1) of the code is calculated as follows: j-1
Eli
=
iffiO
j=l E
ai
__
iffiO
ai+l
+ g = ao
aj + j . g
C
(3)
C
Similarly, the new code being shifted k times (any integer k) from the code will have the sum of the first j terms (from index 0 to index j - 1) being calculated as follows: j-1
E[s(k)(l)]i
=
E
li+k
if0 jffil
-- E
..[_g
ai+k-- ai+k+l
i=O
(4)
C
a k - aj+ k + j 9 g c
T o prove min I = l, it is sufficient to show that l is lower than any of its shiftings, i.e., to show the value in (3) is less than the one in (4) for all j and k. Simply it is equivalent to show aj + a k < a j+ k
for all j and k.
We prove this inequality as follows: by the definition of a ' s we have aj = Ig "Jlc and a k = Ig " kit. Therefore there exists m and n so that g.j
= aj + mc
and
g . k = a k + n 9 c.
Hence, g ( j + k ) = (aj + ak) + ( m + n)c. Therefore, aj+ k = l g ( j
PROPOSITION 4.
+ k ) l c = laj + aklc<_ aj + a k.
Q.E.D.
m a x ! = r(l).
Proof. The proof is similar to the one to prove min l = l. We can calculate the following equalities: c-1
E l,-- c
(5)
iffi0
j--1 E [r(l)l,---c-
j--1 El,=c-
iffiO
iffiO
j--1
E i~O
a o -- a j + j 9 g
j-1
(s
(6) c
a k -- ak+ j - t - j 9 g
= r -- E l,+k = c -iffi0
(7) c
21
DIGITAL STRAIGHT SEGMENTS
T o prove max l = r(l), it is sufficient to show that r ( l ) is greater than any of its shiftings because { s i ( l ) l i = 0 . . . . } = ( r ( s i ) l i = 0 . . . . }, i.e., to show the value in (6) is less than in (7) for all j, k. Again, the problem is reduced to proving aj + a~ > a j+ k for all j, k which was proved int he proof of nan l = l. Q.E.D. PROPOSITION 5.
[level(max 1)] j-x = [level(min 1)] j-1 + 1 for all j = 1,2 . . . . . c - 1 and [level(max l)1 c-x = [level(nan/)1 ~-1
Proof.
(case j = c).
We have for all j = 1, 2 . . . . . c - 1 j--1
j--1
(max/)/=
~
i=0
i~0
j-I
j-1
[r(l)]i=
ac-j -ac+j'g
ac_ j +j"
g
c
a o-
aj +j.
g
E (nan t), = E ti = i=0
i=0
c
The difference of the two above is ( a ~ _ j - a j ) / c . To prove the equality we need to show that a c_j+aj=c
for a l l j = l , 2 , . . . , c - 1 .
Indeed, if we let u = Ig" J lc then for any j = 1, 2 . . . . . c - 1, we have g . j = u + m c for some m. Hence, g . c - g - j = - u + (g - m)c. Therefore [gc - gj[ ~ = (c - u or a c - j + a j = c. For the special case j = 0 or j = c we have ac_ j + aj = a c + a o = O.
Q.E.D.
V. AREA OF ALL DIGITAL STRAIGHT SEGMENTS
In this section, we will prove that the area of all digital straight segments from P to Q is bounded by two digital straight segments min l and max I (i.e., between l and r ( l ) ) . In other words, we will prove that for any digital straight line passing through P and Q, and denote its segment from P to Q by l', then the following orders are valid: 1 < l" < r ( l ) .
In order to prove this property, we will show that (a) First, the real line segment P Q is completely inside the area bounded by min l and max I. (b) Second, there is no other straight line segment from P to Q having at least one pixel outside the area. In other words, we will show that, as a result of (a) and the chord property, a straight segment from P to Q having one of its pixels outside the area will not have the chord property.
22
SON PHAM THEOREM 1.
We have the following properties:
(1) There are no center points of rain l and m a x l on PQ, i.e.,
j-1
j-1
Eli i=0 -
E (r(l)), g
~ c
-
j
i=o
and
g
~ c
j
for a l l j = 1, 2 , . . . , c - 1. (2) The real straight segment PG is above min l, i.e., the real straight segment PQ is above the real broken segment formed by connecting the centers:
j,
(0,0),
Ii
= 1.... c - 1 ,
and
(g,c).
(3) The real straight line P Q is below max l, i.e., the real straight segment P Q is below the broken real segment formed by connecting the centers:
(o,o),
J, i~o E [r
Proof of Property (1).
j = 1, . .. c -.
1
.
.and
(c,g)
Suppose there is an integer j, ( j > 2) so that
i~ 0 1
= g/c.
Therefore, b y the definition of li's we have
-aj+j.g
g
j.e
e
or
-aj +j-
g = g-j.
H e n c e aj = 0, i.e., j -- 0 or c. therefore, a contradiction occurs. W e conclude that there are n o center points of min I on PQ. Similarly, there are no center points of m a x / on PQ, and the proof is similar to the one above. Indeed, suppose there is an integer j ( j < c) such that
[r(l)],
=
H e n c e ac_ j - a c + j . g / j . c = g / c or ac_ j = 0. Hence, c - j = 0 or c, i.e., j = 0 or j = c. Therefore a contradiction occurs. Proof of Property (2). We will show that every real point R = ( x R, YR) on the real straight segment P---Q, the value YR is greater that y~, where (XR, y~) is on the
DIGITAL
STRAIGHT
23
SEGMENTS
/ theupperbound a
$/
/
/
thelowerbound
/ P FIG. 3.
The l o w e r b o u n d
; the u p p e r b o u n d
; the B r e s e n h a m ' s . . . . . . . .
b r o k e n segment formed by connecting the centers
(0, 0),
j,
Ii
= 1. . . . . c - 1
and
(c, g ) .
i=
(See Fig. 3 for more details). It is equivalent to prove that for the point R with x R = 1,2 . . . . . c - 1
Y~o/j >
l, .
(8)
T h e reason that we can reduce to the R ' s with integer coordinates x R because in general two points R, S are under R', S ' in the sense that xR = xR,, Xs = Xs,, YR < YR', and Ys < Ys', then the segment R S is under R'S'. N o w we prove the inequality (8): for j -- 1, 2 . . . . , c - 1, we consider the center and the point ( j , YR), we have j-1
--aj + j 9g
~_~ li = i=0
c
and since R is on PQ, then YR/J = g/c. Hence YR = g . j / c inequality (8) hold.
Proof of Property (3). center points of max l,
which makes the Q.E.D.
Similarly, to prove P Q is under max l, we consider the
(11 / j, ~ (max/),
,
j=
1,2,...,c-
j=0
and the real point R = ( j , YR) on PQ, and we need to prove j-1
s i=0
(max/)/>YR.
1,
24
SON PHAM
Indeed, j
x
E(maxl)i=
c-aj+j.g Ii
+1=
i=0
j.g > --
c
c
= YR.
Q.E.D.
No Digital Straight Segment Outside the Area
We will prove in this section that there are no digital straight segments which fall outside the area bounded by min l and max l. THEOREM 2. All digital straight segments from P to Q are in the area bounded by min l and max l. Proof. Let LINE be a digital straight line passing through the pixels P and Q (i.e., the L I N E contains P and Q). Therefore, LINE satisfies Rosenfeld's chord property. Consider the segment of LINE between P and Q. We want to prove that this segment is inside the area mentioned above. We will prove it by a contradiction. Assume that this digital straight segment is not inside the area, then there is a pixel, say S = ( x s, Ys) of this segment and outside the area. We have two cases: S is under re_in l and S is above max 1. We prove the case S is under min l; the proof of the other case will be omitted because of the similarity. Case S Is under mini. We will show that LINE does not satisfy the chord property for a contradiction. More precisely we will show the point R -- (xR, YR) with x R = x s on the real straight segment P Q has a distance to LINE greater than 1, i.e., we will show that for all points (i, j ) of LINE
max(abs(i- xR),abs(j-
xR) } > 1.
(The quality on the left-hand side of inequality is the distance between (i, j ) and R = (x R, YR))- Since LINE can only have two directions 0 and 1, so the next pixel after S cannot be above the pixel S' = (x s + 1, Ys + 1). Since S is under min l hence S' cannot be above rain l. (It is possible that S' is on min l.) Similarly, the pixel before S, cannot be above S " = (x s - 1, Ys) we need only to prove (1)
dist(R,S) > 1
(2) dist(R, S ' ) > 1 (3) dist(R, S " ) > 1. Indeed, because S is strictly under rain and PQ is strictly above min therefore abs(ys - YR) > 1. Hence dist(R, S) > 1. Since S " and S have the same y coordinates, dist(R, S " ) is greater than or equal to 1. Similarly R and S have the same x coordinates and x s, = x x + 1 therefore dist(R, S') > 1. Q.E.D. VI. CONVEX ARCS A N D CONCAVE ARCS
In the previous sections, we prove that given two pixels with centers P = (xe, Ye) and Q = (xQ, yQ), the area containing all digital straight segments from P and Q can be determined by its lowerbound and upperbound. These two bounds are also
25
DIGITAL STRAIGHT SEGMENTS
digital straight segments from P to Q. The lower bound is proved to be t = (10 .....
to_l),
where l i = (a i - ai+ x + g ) / c ; ai = IJ " ilc; g = abs(yo - Yp), c = abs(x o - x~,). The upperbound is r(l) = (lc_ 1. . . . lo). These two bounds can also be represented by their xy coordinates of pixels:
l:(0,0),
r(l):(O,O),
(( ), j,
li
j,
)
= 1,2 . . . . . c - 1 ,
li + l ) j = l , 2
.... ,c-1
(c, g)
,
(c,g).
i=
The reason for the bound l (resp. r(l)) to be called lower bound (resp. upper bound) is it has the smallest (resp. largest) y coordinates among all digital straight segments from P to Q. We call (~i=0J-1 li ' j = 1, 2,.. ., c - 1) the corresponding sequence of y coordinates of the digital straight segment and denote it by level (l). The reason for the bound l (resp. r ( l ) to be called lower bound (resp. upper bound) is that it has the smallest (resp. largest) corresponding sequence of y coordinates among all digital segments from P to Q., i.e., for all digital straight segments from P to Q we have level (1) < level (1') < level (r (1)). So, we can define the order relationships among all digital straight segments from P to Q as follows:
l' < l " iff level ( l ' ) < level ( l " ) for all digital straight segments l' and l". Up to this section, we refer to a digital straight segment from P to Q as a segment between P and Q of a digital straight line which satisfies three Freeman's criteria or Rosenfeld's chord property. It is clear that in the area bounded by I and r(l), there are many digital arcs from P to Q without the Rosenfeld's chord property of the three Freeman's criteria. So, we redefine the digital straight segment from P to Q as follows. GENERAL DEFINITION OF DIGITAL STRAIGHT SEGMENT. A digital arc f r o m P to O is a digital straight segment if it is inside the area bounded by l and r(l), i.e., let ( j , yj) be a pixel-ccnter point of this digital arc, then
( , ~ 0 l`
< yj <
l,
+1.
(We note that the digital arc in this definition might have many pixels of the same x coordinates). With the previous definition, a digital straight arc does not necessarily satisfy Rosenfeld's chord property or all the three Freeman's criteria. But it
26
SON PHAM
lies between two bounds satisfying this chord property or these criteria. For a practical purpose, a digital arc lying between two bounds can be detected, hence it can also be represented by either the lower bound or the upper bound. The lower-bound and upper-bound digital straight segments are useful in defining convex and concave arcs. DEFINITION OF CONVEX AND CONCAVE ARCS.
Given a
digital arc ~:
(a) It is called digital convex arc if
Ij, Q < PQ
for every P, Q in ~, and for some l,
where P Q denotes a sub-arc of ~ from P to Q, and lpe denotes the lowerbound digital straight segment from P to Q. (b) It is called digital concave arc if
P"-Q
for every P, Q in ~,
where r(lpQ) denotes the upper-bound digital straight segment from P to Q. APPENDIX: IMPLEMENTATION: NEW ALGORITHM AND BRESENHAM'S ALGORITHM
In this section, a new algorithm for digital straight segments will be introduced. It is well suited to implementation in hardware or on simple microprocessors because it avoids multiplications and divisions. First, a new algorithm will be introduced, and then compared with the Bresenham's algorithm. NEW ALGORITHM. Given two pixels with centers P = (0, 0) and Q = (c, g), the lower-bound digital straight segment (li)~21o connecting P to Q is given by the formulas:
ai = Ig" il~
l~ = (a~ - a~+ 1 + g ) / c
and
for i = 0 . . . . , c - 1.
If an algorithm is based strictly on these above formulas, it will require the modulo operator and division operator. To avoid these operators, we use the following results to produce an algorithm, requiring neither divisions nor multiplications: (1) For i = 0, 1. . . . . c - 1,
or
a~ - a~+ 1 = - g
(Case 1)
a~ - ai+ 1 = c - g.
(Case 2)
(2) a o = 0, al = la0 + glc
(
ai+
ai+l = [ai + g[c =
g
ai + g - c
(3) Case 1 produces Direction 0 Case 2 produces Direction I
ifai+g_c
(Case 1) (Case 2)
27
DIGITAL STRAIGHT SEGMENTS
(* all variables are integers *) a -'= 0; y : = 0 ; x - ' = 0; for
i := to c do begin
ifa+g
i := l to c do
begin ife>O then
e := e -
g
else begin
e:=e+c-g; y:=y+l end; x:=x+l; plot(x, y); end; Bm~SENHAM'S ALGOmTrlM. The Bresenham's algorithm constructs a digital segm e n t of the centers to PQ in the following sense. F o r all centers of the same x coordinates, the one closest to the real line PQ will be selected (see Fig. 3). To express it mathematically, we let R = (i, YR), with an integer x coordinate i, be a point on the real line P-Q, then YR i
-
g c
or
YR --
g c
-
" i.
T h e selected center (i, J0) is the one among the centers {(i, J)lJ = O, +1 . . . . } and closest to R. That means
lJo - yRI -< lJ y~l -
or IJo
for all j -- O, + 1 , . . . .
-
(g/c)
" ~1 <-12 - ( g / c )
" ~1
28
SON PHAM
The following is Bresenham's algorithm to construct these above closest centers: (* e is real; all other variables are integers *) x := 0; y := 0; e := (g/c); for i : = l t o c d o
begin if e > 0.5 then begin y := y + 1; e .'= e - 1 + (g/c); end else e := e + (g/c); x:=x+l; plot(x, y); end; To avoid multiplications and divisions by multiplying e by 2"c, the above algorithm can be modified to be as follows: (* all variables are integers including e *) x:=0; y:=0; e:=g+g-c; for i : = l t o c d o begin ife>l then begin y : = y + l ; e:=g+g-c+c;end else e : = e + g + g ; x:=x+l; plot(x, y); end;
Discussion. (1) The new algorithm can be interpreted as to find the highest centers under the real line segment ~ in the following sense: for all centers under PQ and of the same x coordinates, the one with the largest y coordinate will be selected (see Fig. 4). We express this mathematically as follows: let R = (i, YR), with an integer i, be a real point on the real segment PQ, then a center to be selected is (i, [YR]) or ( i , t ( g / c ) " il). (A.1) These centers will form the lower bound of all digital straight segments from P to Q. Similarly, the centers
( i, [( g/c) . i]), for all integers i, such that xe <_ i < xe,
(A.2)
will form the upper bound of all digital straight segments. (2) In contrast with the new algorithm, the Bresenham's algorithm will select the closest centers to PQ. In other words, in every pair of the centers in (A.1) and (A.2), a closest center will be the one with less truncation, i.e., we select
(i, [ ( g / c ) . i])
if ( g / c ) 9 i - [ ( g / c ) 9 i] < 0.5.
If the condition does not satisfy, we select the other center. (3) The centers in Bresenham's algorithm will also form a digital straight segment. This segment is a lower bound up to a number of shifts. The number of
DIGITAL STRAIGHT SEGMENTS
29
shifts varies depending on the positions of P and Q (more precisely, it depends on the slope of the line connecting P to Q). These results are not proved in this paper. ACKNOWLEDGMENTS
This work has been done during my tenure at the University of California at Berkeley, EE/CS Department, with Professor Ramamoorthy, and the University of Southwestern Louisiana, Center for Advanced Computer Studies, with Professor Rao. I thank them for their support. I thank Professor Hon Li of the Department of Computer Science, Concordia University for introducing this topic. I would like to acknowledge the anonymous referees for the constructive comments. I thank Cathy Pomier for the high-quality job of typing and retyping the manuscript. Finally, I thank Hoa, Mai Lan, and Minh Thu for their encouragement. REFERENCES 1. C. Areelli and A. Massarotti, Regular arcs in digital contours, Comput. Graphics Images Process. 4, 1975, 339-360; Erratum, Comput. Graphicslmages Process. 5, 1976, 280. 2. F. L. Bongiovanni and A. Zorat, The discrete equation of the straight line, IEEE Trans. Comput. C-24, 1976, 310-313. 3. J. E. Bresenham, Algorithm for computer control of a digital plotter, IBM Systems J. 4, 1965, 25-30. 4. R. Brons, Linguistic methods for the description of a straight line on a grid, Comput. Graphics Image Process. 3, 1974, 48-62. 5. S. K. Chang, Picture processing grammar and its applications, Inform. Sci. 3, 1971, 121-148. 6. J. D. Foley and A. Van Dam, Fundamentals of Interactive Computer Graphics, Addison-Wesley, Reading, Mass., 1982. 7. H. Freeman, Computer processing of line-drawing image, Comput. Surveys 6, 1974, 57-97. 8. H. Freeman, On the digital computer dassilication of geometric line patterns, Proc. Natl. Electron. Conf. 15, 1962, 312-324. 9. H. Freeman, A scheme for the efficient encoding of graphical data for communication and information processing, Proc. 16th Int. Congress on Electronics, Rome, 1969, pp. 341-348. 10. E. C. Greanias et al., The recognition of handwritten numerals by contour analysis, IBM J. Res. Dev. 7, 1963, 14-21. 11. B. J. Hansen, Recognizing Convex Blobs by Computer, M.S. thesis, School of Engineering, University of California, Irvine, 1969. 12. L. Hodes, Discrete approximation of continuous convex blobs, SIAM J. Appl. Math. 19, 1970, 477-485. 13. S. H. Y. Hung, On the straightness of digital arcs, IEEE Trans. PatternAnal. Mach. Intell. PAMI-7, No. 2, 1985, 203-215. 14. C. E. Kim and A. Rosenfeld, Digital straight lines and convexity of digital regions, IEEE Trans. Pattern Anal. Mach. Intell. PAMI-4, 1982, 149-153. 15. C. E. Kim~ On cellular straight line segments, Comput. Graphics Image Process. 18, 1982, 369-381. 16. G. U. Montanari, A note on minimal length polygonal approximation to a digitized contour, Comm. Assoc. Comput. Mach. 13, 1970, 41-47. 17. G. U. Montanafi, On limit properties in digitization schemes, J. Assoc. Comput. Mach. 17, 1970, 348-360. 18. S. Morse, A mathematical model for the analysis of contour-line data, J. Assoc. Comput. Mach. 15, 1968, 205-220. 19. T. Pavlidis, Structural Pattern Recognition, Springer-Verlag, New York, 1977. 20. M. L. V. Pitterway, Algorithm for drawing ellipses or hyperbolae with a digital plotter, ComputerJ. 10, 1967, 282-288. 21. J. L. Pfaltz and A. Rosenfeld, Computer representation of planar regions by their skeletons, Comm. Assoc. Comput. Mach. 10, 1967, 119-125. 22. U. Ramer, An interactive procedure for the polygonal approximation of plane curves, Comput. Graphics Image Process. 1, 1972, 244-256. 23. G. B. Reggiori, Digital Computer Transformation for Irregular Line Drawings, Technical Report 403-22, New York University, 1972.
30 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40.
SON PHAM A, A. A, A, A.
Rosenfeld, Digital straight line segments, IEEE Trans. Comput. C-23, 1974, 1264-1269. Rosenfeld, Picture Processing by Computer, Academic Press, New York, 1969. Rosenfeld, Connectivity in digital pictures, J. Assoc. Comput. Mach. 17, 1970, 146-160. Rosenfeld, Arcs and curves in digital pictures, J. Assoc. Comput. Mach. 20, 1973, 81-87. Rosenfeld and J. L. Pfaltz, Sequential operations in digital picture processing," ,1". Assoc. Comput. Mach. 13, 1966, 471-494. A. Rosenfeld, Distance functions on digital pictures, Pattern Recoguit. 1, 1968, 33-61. J. Rothstein and C. Weiman, Parallel and sequential specification of a context sensitive language for straight line grids, Comput. Graphics Image Process. 5, 1976, 106-124. D. Rutovitz, An algorithm for in-line generation of a convex cover, Comput. Graphics Image Process. 4, 1975, 74-78. A. C. Shaw, Parsing of graph-representable pictures, J. Assoc. Comput. Mach. 17, No. 3, 1970, 453-481. J. Sklansky, Measuring Concavity on a Rectangular Mossiac, School of Engineering, Univ. of California, Irvine, Technical Report TP-71-4, Sept. 1971. J. Sklansky, Recognition of convex blobs, Pattern Recognit. 2, 1970, 3-10. J. Sldansky, R. L. Chazin, and B. J. Hansen, Minimum-perimeter polygons of digitized silhouettes, IEEE Trans. Comput. C-21, 1972, 260-268. P. H. Swain and K. S. Fu, Stochastic programmed grammars for syntactic pattern recognition, Pattern Recognit. 4, No. 1, 1972, 83-101. M. Symons, New self-organizing pattern recognition system," IEE Conf. Publ., London, Eng., July 1968, pp. 11-20. I. M. Yaglom and V. G. Boltyansld, Convex Figures, Holt, Rhinehart & Winston, New York, 1961. L. D. Wu, On the chain code of a line, IEEE Trans. Pattern Anal. Mach. Intell. PAMI-4, No. 3, 1982, 347-353. S. Pham, Digital straight lines, IEEE Trans. Pattern Anal. Mach. Intell., in press.