COMPUTER
VISION,
GRAPHICS,
AND IMAGE
PROCESSING
9,
296-307 (1990)
A Fast Algorithm for the Restoration of Images Based on Chain Codes Description and Its Applications LONG-WEN CHANG AND KUEN-LONG LEU Institute
of Computer
Science, National Tsing Hua University, Taiwan 30043, Republic of China
Hsinchy
Received January 17,1989; revised May lo,1989 A fast algorithm for the restoration of an image is presented. The image is described by using chain codeswhich recordthe contours of the image. The algorithm gives a simple idea for region Wing. Some geometric properties for the image can be easily derived by our method. Comparison of the proposed method and one existing method is also provided. 6 1990 Academic
Press, Inc.
1. INTRODUCTION
In many applications of image processing, such as picture analysis, pattern recognition, and graphics, one of the most common problems is filling the region when its contour is given. There are several algorithms performing region filling by the technique of parity check [l, 21 or a criterion of connectivity [l, 31. A parity check algorithm is based on the fact that a straight line intersects the contour of a region an even number of times. It is solved by filling between each odd point and the next even point on the contour in each line of the image. Connectivity filling assumes that an interior point (seed point) is given. One then performs a traversal of the plane to find all pixels that can be reached from the seed without crossing the contour. Two problems arise in the use of the technique of parity check. First, it is possible to produce an incorrect count of the number of intersections if points from two or more sides are mapped on the same pixel. Second, problems may arise from lines that are tangent to the contour. Our approach also uses the technique of parity check based on contour direction chain codes description [4-61, and solves the above two problems. Comparing our algorithm with Cai’s method [4], our algorithm has the following advantages: 1. 2. 3. 4.
less storage faster speed not restricted to a binary image simpler idea.
On the other hand, in computer vision, image processing, and pattern recognition, the geometric properties are very important. For example, the analysis and characterization of a shape in biology is a problem of almost classic proportions in pattern recognition [8]. In general, for an irregular 2D line structure, the chain codes description has shown itself to be well-suited for most of the processing tasks likely to be encountered. Freeman [9] has illustrated the versatility of the chain codes 296 0734-189X/90 Copyright
$3.00
0 1990 by Academic Press. Inc. in any form reserved.
All rights of reproduction
RESTORATION
OF IMAGES BASED ON CHAIN CODES
297
2 3
1
4 +k
0
5
7 6
FIG. 1. The definition of the direction codes.
description by describing some of the basic processing algorithms such as length of a chain, moment, residue, and so on. By our filling method for chain codes description, we convert chain codes description into another data structure. The formulas of geometric properties of a contour in [9] can be easily derived. In Section 2, the image description rule is described and the method we propose is presented. In Section 3, an example is illustrated. In Section 4, the correctness of the algorithm is shown. In Section 5, some formulas for geometric properties are derived. Section 6 is the discussion and conclusion. 2. DESCRIPTION
RULE AND THE PROPOSED
METHOD
The boundary points are represented in the form of a string using &direction codes. The codes from 0 to 7 are assigned to the 8-directions as shown in Fig. 1. We use the rule “interior to the left” for contour tracing [7]; we have the description forms of the contours in counterclockwise for an out-contour and in clockwise for an in-contour. Let us consider the restoration of a binary image with m contours. We assume the description forms of the contours as SXooYoodoodol................. d,“JZ Sxloylod,, d Il.. ............... dl”? ........................................... .................... djcj-,,dij.. ........................................... sx m0 y m0 d m0 d ml""""'
......
d mn, e$,
where “$” is the end symbol of the description of an image, “s” is the starting symbol, and “e” is the ending symbol for each contour; “xio” and “yiO” are the coordinate of the starting point for the ith contour; “dij” is the direction code of j th point to the ( j + 1)th point for the i th contour. If we want to restore the image like Fig. 2a, we can first fill the region of out-contour with “ points.” Its result is shown in Fig. 2b. Then we till the region of in-contour with “blank,” that is we “erase” the region. Of course, if there are more than one in-contour, we can do this: Region filling problems with a chain code description can be reduced to the simple problem: “how to fill a region by a contour with a chain code description.”
298
CHANG AND LEU
FIG. 2. (a) is an image. If we want to restore Fig. 2(a), at first we can fill the region of out-contour as shown in 2(b), then “erase” the region of in-contour.
Suppose the two direction codes connecting the jth points Sj in the ith contour path are dj, j-1j and dij. Sj has four possibilities: “0,” “1,” “2,” and “3.” They represent the singular point, the marking point, the skipping point, and the unsuitable point, respectively. Their definitions are: 1. Singular point. A singular point is a point which does not connect any point in the x-direction. For example, dicj-lj = 1, dij = 5, as in Fig. 3a; d,cj-lj = 7, fii = 1, as in Fig. 3b, “A” does not connect any point in x-direction. “A” is a singular point.
I
I
5’/’* lY?Ysid a
b
d
h FIG. 3. (a) and (b) indicate “A ” is a singular poiht; (c) and (d) indicate “B” is a marking point; (e) and (f) indicate “C” is a skipping point; (g) and (h) indicate “D” is an unsuitable point.
RESTORATION
OF IMAGES
BASED
TABLE
ON CHAIN
CODES
299
1
si out-contour
2. Marking point. If a point is the x-direction beginning or the x-direction ending, then the point is a marking point. For example, d,(j-lj = 5, djj = 5, as in Fig. 3c, “B” is an x-direction beginning point; dicjel) = 1, d,, = 3, as in Fig. 3d, “23 ” is an x direction ending point. “B ” is a marking point. 3. Skipping point. A point that lies between an x-direction beginning point and an x-direction ending point is a skipping point. For example, d,cj-lj = 0, dij = 6, as in Fig. 3e; dici-lj = 5, dij = 3, as in Fig. 3f, “C” lies between an x-direction beginning point and an x-direction ending point. “C ” is a skipping point. 4. Unsuitable point. A point that violates the right-edge tracing rule is an unsuitable point. For example, dicj-,, = 1, d,, = 6, as in Fig. 3g; d,cj-lj = 0, dij = 5, as in Fig. 3h. It is not possible by right-edge tracing rule, so “D” is an unsuitable point. In our method, when a point is visited to be a singular point, then the point is recorded twice; if a point is a marking point, it is to be recorded once. All recorded points are for parity filling. If a point is a skipping point, we do nothing for this point; the description is considered as wrong if an unsuitable point is visited. With the above definitions for the four kinds of points, we can derive Table 1 for an out-contour and Table 2 for an in-contour. By using these two tables, our simple and fast region filling algorithm is described as the following:
TABLE S, In-Contour
2
CHANG AND LEU
ALGORITHM. /* List-record is used for storing the coordinates of marking points. According to each input point (X, Y), Y’s value is as the head of a list, X’s value is as the following node inserted to the list. X is not decreasing in every list. */ /*Process the second point to end point of the contour */ 1. Read the initial coordinate xiO, y,,. Let X0 = xiO, Y, = yiO. 2. Read the first direction dia. Let D,, = d,. 3. Evaluate the X and Y coordinates of the second point according to X0, YO,and DO.
4. Do steps 5-8 for each direction code tilI the end symbol e. 5. Read the next direction dij. 6. Obtain Sj by looking up Table 1 (for out-contour) or Table 2 (for in-contour) according to dicj-lj and d,. 7. Case Sj of 0 : input (X, Y) twice to the list-record 1: input (X, Y) to the list-record 2 : do nothing 3 : this description is wrong end Case. 8. Obtain next X and Y coordinates. /* Process the initial point. */ 9. If X = X0, Y = Y,, then do step 10 else the description is wrong. 10. Let dici-lj = di,,, dij = do. Do steps 6-7. /* Do x-direction fihing for each Y in list-record. */ 11. plot (X, Y) form (2n - 1)th X’s value to the (2n)th X’s value, for n = 1,2,. . . 12. End of algorithm 3. AN EXAMPLE
Figure 4 illustrates a contour “adegikjkpmnoqonmighfcb” description:
with its chain codes
s465075540710015443103144e After the start symbol “s “, “4” is the coordinate of X and “6” is the coordinate of Y. Then “5,” “0,” “ 7,” etc. are the directional codes (Table 3).
y ’ AXIS
ii L 5
f 2
2,
obc def igh
jklmno P
q
0123156789 X -
FIG. 4.
Axis
An image with size 8 x 10. Every location with labeled form
“a ”
to “q” is a pixel.
RESTORATION
OF IMAGES BASED ON CHAIN CODES TABLE
Direction d,( , _ 1), di,
%
570
1
037 7.5 5s 5,4 48 0,7 7J LO OS-J OJ 1.5 5,4 434 493 371 LO 0,3 3J L4 494 495
2
1 1 2
1 2 0 2 2
1 0 2 2 2
1 2
1 1 1 2
1
301
3 Action Record (3,5) Do nothing Record (5,4) Record (4,3) Do nothing Record (2,2) Do nothing Record (4,l) Do nothing Do nothing Record (7,2) Record (8,3) Do nothing Do nothing Do nothing Record (4,3) Do nothing Record (6,4) Record (5,5) Record (6,6) Do nothing Record (4,6)
(i.e., “d ") (i.e., “g”) (i.e., “i”) (i.e., “j “) (i.e., “p”) twice (i.e., “0”) (i.e., “4”) twice
(i.e., “i “) (i.e., “h”) (i.e., “f “) (i.e., “c”) (i.e., “n”)
We can see when the algorithm reads over the data, it converts the chain code description to the following data structure of list-record (more general; the details are in the “SOME DERIVED FORMULAS’ section): (1; 4,4) (2; W) (3; 4,4,8,8) (4; 5,6) (5; 3,5) (6; 46)
That is, while Y = 6, it fills from “Q ” to “c”; while Y = 5, it fills from “d ” to “f “; while Y = 4, it fills from “g ” to “h “; while Y = 3 it fills from “i” to “i” and from “q” to “q”; while Y = 2, it fills from “j” to “0,” and while Y = 1, it fills from “p” to “p.” Then the image is restored. Suppose we have another binary image as in Fig. 5a. All “b” are represented as an in-contour. If we want to restore Fig. 5a, we can first fill in the region of its out-contour as shown in Fig. 5b, then fill in the region of its in-contour with “blank” as shown in Fig. 5c. Now note the result as in Fig. 5c, all “b” have erased. Then we plot the in-contour and the image is restored.
302
CHANG AND LEU
FIG. 5. (a) is an image with one hole. “a” and “b” are the location of pixels. All “b” are represented as in-contour. If we want to restore Fig. 5(a), we can at first fill the region of out-contour as shown in Fig. 5(b), then fill the region of in-contour with “blank” as shown in Fig. 5(c), and plot points at the positions of “b” (i.e., the in-contour).
4. CORRECTNESS
OF THE ALGORITHM
Suppose the chain codes description has no unsuitable points. For simple discussions, Lemma 1 and Lemma 2 we only consider the out-contour case. The two lemmas are also valid in an in-contour case. Let G be the image restored by our algorithm and has no singular points. LEMMA 1. If a point is visited as a singular point and there exists another visit for the point, then the another visit must not be assigned as a marking point.
Proofi For our definition of singular point, we can see that there exists one horizontal line being tangent to the singular point, and the segments of diCi-lj and dij must both be above or below the tangent line. If the chain codes description of the contour visits the singular point with d,Cj-lj and d,,, then the another visit must be with ((dij + 4) mod 8) and ((dj, j- i) + 4) mod 8). Looking up Table 1, it must not be a marking point. LEMMA 2.
in G.
There must be even number points of marking points for each x-direction ,I
Proof. As shown in Fig. 6a, if the first point “A” is visited and it is a marking point, we start this point and continue traveling along x-direction till the point “B” without right point, then “B” is also a marking point. For (1) while “A” #“B,” note that the chain codes description of the out-contour is in counterclockwise, “A” must be with d,Cj-lJ = 4, 5, 6, or 7, dij = 5, 6, 7, or 0; “B” must be with = 0, 1, 2, or 3, dij = 1, 2, 3, or 4. Any combination of di, j- i) and d,,, as di(j-l) shown in Table 1, is a marking point. (2) While “A” = “B,” Fig. 6a is reduced to Fig. 6b, “A” must be with diCj-lj = 5, 6, or 7, dij = 5, 6, or 7; “B” must be with di(j-lj = 1, 2, or 3, dij = 1, 2, or 3, so it is also a marking point. Continue traveling and action above, this lemma is shown. In our algorithm, we record each singular point marking point is visited, we record it once. All of For a connected contour, the algorithm does not point and any x-direction ending point. For an example, as shown in Fig. 4: (1) line Y “ c,” line Y = 1 is tangent to “p,” by the algorithm,
twice when it is visited. When a them are for our tilling process. miss any x-direction beginning = 6 is tangent to “a,” “b,” and “a ” and “c” are marking points
RESTORATION
303
OF IMAGES BASED ON CHAIN CODES
/ _---------t._--------1
A ---------T
B
T
_----_----/
a
\
--A
-B
_-+
b FIG. 6. (a) X-direction condition that “A” is “B.”
scanning from left to right while “A”
is not “B.” Figure 6(b) shows the
and recorded, then filling from “a” to “c”; “ p” is a singular point, and filling “P” to “P.” The tangent problem of parity check is solved. (2) The “g’‘-“i’‘-“k” and Lcm”-“i”-“g” have one point “i” mapped on the same The point “i ” is considered as a marking point twice and recorded twice, then from “i” to “i,” the same pixel mapping problem of parity check is solved. 5. SOME DERIVED
from sides pixel. filling
FORMULAS
In general, chain codes description for a contour, by our filling method, can be converted as the following data structure of a list-record with m lists: (Y,; XII, X12, . . .. . . . . . . . . XInl) (Y,; xzl, x*2, . . . . . . . . . . . . &J (Yi,; xil, xi*, . . .. .. . . .. . . Xin,) (Y,; xml, xm2, . . .. .. . . .. .. X,,_). Each (5::; Xii, . . * , X,,,), i = 1, . . . , m, is a list with K as a head, and ,..., ni, is a node. Also q+i = yi + 1. Actually, the data structure is the same as Merrill’s y-axis representation first element of each list is the y-coordinate, followed by a list of “into” of’ x-coordinates. Given the chain codes description of a contour, we can easily derive the formulas based on our y-axis representation:
each Xij,
j=l
[lo]. The and “out following
1. Point membership property. Suppose one desires to know whether a particular point (X,., Y,) lies inside or on the closed contour. One need merely to examine the list corresponding to Y, and determine whether X, lies in the list whose left
CHANG AND LEU
304 0
12
3
0
1,
1
2
3
L
0 1 2 3 r,
a
b
FIG. 7. (a) Continuous case, each pixel is considered as a point with no area and the shadow area is 2 X 2 = 4. (b) Discrete case, each pixel is considered as a 1 X 1 “plane,” and the shadow area is the number of pixels, 3 x 3 = 9.
entry is an odd position and whose right entry is an even position. That is, yi I
Y, I Y,
and
X, E [Xrczi-lJ, x+i)]7
i = lp-..,
m/2.
(1)
The other solution for chain codes description can be solved by Green’s theorem [ll]. However, it is very complex. 2. Area. Up to now, the area computation of a contour discussed was in the continuous case [lo]. Now we give the discrete case. We do not consider the area of a line or a point as zero. We consider the area is the number of pixels of a region. Figures 7a and b illustrate the difference between a continuous case and a discrete case. Our formula is: m C i-j
in,/2 C
(xi(*j)
-
xi(2j-1)
+
(2)
')'
j-1
That is, for each list we calculate its number of pixels. Then the sum of pixels of M lists is the area. 3. Moments and centers. A system R of k particles having mass sr, sz,. . . , Sk and being situated at positions (x1, yr), (x2, y2), . . . , (xk, &) in the XY-plane is called a finite point-mass system (Fig. 8). The (first) moment of R about the X-axis and the Y-axis are defined by
Mx(R) = $ .YiSi,
MY(R)
= i
i=l
xisi,
(3)
i=l
respectively. -The center of mass (or centroid) of the system is defined as the point (X, Y), such that r = M,(R)/
Y=M,(R)/iSip i-l
i i=l
si.
(4)
RESTORATION
305
OF IMAGES BASED ON CHAIN CODES
Y t X2 __~_____ ------s --%3,x3,ys 1 I I
‘Xl’Yl) ------s1 t-----‘l I
(3,y).
I
FIG.
X1,
I l x
I
0
p?!---
&.y2’ I I I I
I
I
=1,
-
I ’
55
---wg-+x5.yg)
8. Moments and centroid of a finite point-mass system.
We also consider our region in a discrete case. We may take each si = 1, then k iFl
From Eq. (2), M,(R), 44R)
‘i
=
Areat
R)
(5)
M,,(R) are easily derived as
m ini’2 = c c i-1
(xi(Zj)
+
-
xi(Zj-1))(4(2j)
xi(Zj-1)
+ l)
2
j=l
Xitzjj - XjC2j-1j + 1)
.
(6)
(7)
Thus, we have
Int( a) is a function to round off a number. 4. Eccentricity. There are several measures of eccentricity. One of them is the ratio of the length of maximum chord A to maximum chord B perpendicular to A (Fig. 9). By this definition our algorithm is: A = max{ XjCzi, - XiCzj-ij + l(j = 1,2 ,...,
FIG.
in,/2,
9. An eccentricity measure A/B.
i = 1,2,. . ., m}.
(9)
306
CHANG AND LEU
Let i, E [l, ml, & E [l, ~an,o/21, and i,, j, are integers, such that
Let R be the region (including the inside and the closed contour), and U, and L, are the minimum integers such that the points (I, yi, + U, + 1) and (I, yi, - L, - 1) are not in the region R, then
Eccentricity = A/B
(12)
5. Intersection and union. The operations of intersection and union are very difficult for chain codes description. Ballald [12] gave strip trees representation for intersection and union operations, and these operations may be efficiently implemented. However, a strip tree may take up much space. Merrill [lo] also had algorithms for the two operations for y-axis representation. Our filling method converts chain codes description to y-axis representation. Thus, the problems for the two operations for chain code description is solved. 6.. DISCUSSION
AND CONCLUSION
Cai’s method established two areas, A and B, which size is the same as that of the original image. He also defined the singular point, the marking point, and the unsuitable point, but their meanings are not the same as ours. Area B is used for storing singular points and area A is used for storing marking points. It also decides the kind of points by looking up a table like our Table 1. If a point is marking point than it looks up the area A and if MA = 1 (MA is the point in area A of M( X, Y)), then set MA = 0 and set MB = 1. If a point is singular point then set MB = 1. If a point is unsuitable point that the description is wrong. After dealing with the chain codes, it does x-direction filling for each y in area A by parity check method, “1” in A is marking point. Then it does “OR” operation for each point in area A and B, and store the results in area A. The comparison of our algorithm and Cai’s method are: (1) If an image is 512 X 512, then Cai’s method requires 512 K memory. Our algorithm does not. (2) Our algorithm only looks up a table with size 8 X 8. It does not have to have “OR” operation between area A and B. Our algorithm is faster. (3) Suppose there exists a third gray-level (or color) in the in-contour region; we can just fill the region with the third gray-level (or color) after “erasing” the in-contour region. (4) We clearly define the singular point, the marking point, the skipping point, and the unstable point, and indicate their purposes.
RESTORATION
OF IMAGES BASED ON CHAIN CODES
307
Although chain codes description is terse and has less space required, it is difficult to use for geometric computations because it is a “boundary representation.” Our method can convert chain codes description to y-axis representation.’ Because y-axis representation is a “region representation,” it is much easier to derive geometric properties for a shape. REFERENCES 1. T. Pavlidis, Filling algorithms for raster graphics, Comput. Graphics Image Process. 10, 1979, 126-141. 2. B. D. Ackland and N. Weste, The edge flag algorithm--A fill method for raster scan display, IEEE Trans. Comput. 30, 1981, 41-47. 3. U. Shani, Filling regions in binary rester images, in SIGGRAPHXO, pp. 321-327. 4. Z. Cai, Restoration of binary images using contour direction chain codes description, Comput. Vision Graphics Image Process. 41, 1988, 101-106. 5. S. M. Ah and R. E. Burge, A new algorithm for extracting the interior of bounded regions based on chain coding, Comput. Vision Graphics Image Process. 43, 1988, 256-264. 6. G. Y. Tang, Region filling with the use of the discrete Green theorem, Comput. Vision Graphics Image Process. 42, 1988, 297-305. 7. T. Pavlidis, Algorithm for Graphics and Image Processing, Comput. Sci., Washington, DC, 1982. 8. I. T. Young, J. E. Walker, and J. E. Bowie, An analysis technique for biological shape I, Inform. and Control 25, 1974, 357-370. 9. H. Freeman, Computer processing of line drawing images, Compuf. Surveys 6, No. 1, 1974, 57-98. 10. R. D. Merrill, Representation of contours and regions for efficient computer search, Commun. ACM 16, No.
2, 1973, 69-82.
11. Y. Tang, A discrete version of Green’s theorem, IEEE Tran. Pattern Anal. Mach. Intell. PAMI-4, No. 3, 1983, 242-249. 12. D. H. Ballard, Strip trees: A hierarchical representation for curves, Commun. ACM 24, No. 5, 1981, 310-321.