COMPUTER
VISION,
GRAPHICS,
AND
IMAGE
PROCESSING
41, 101-106 (1988)
Restoration of Binary Images Using Contour Direction Chain Codes Description ZUGUANG CAI The Third
Research Institute 225 Xikang Road,
of Ministry of Posts and TelecommunicationsJ Shanghai, Peoples Republic of China
Received February 19,1987; accepted June 12,1987 A binary image can be described by using one or several direction chain codes which record the contour of this binary image. According to the chain codes mentioned, a simple algorithm for getting the original image is given in this paper. 0 1988 Academic Press, 1~.
INTRODUCTION
By using one or several direction chain codes, the contour of a binary image can be described, so, the image itself is described. Suppose the codes O-7 are assigned to the 8 directions as shown in Fig. 1, then the chain code 500765555100071115544321013344 can represent the contour of the image shown in Fig. 2, starting from point “a,” therefore, the image itself is described. The main problem discussed in this paper is how to obtain the original image simply according to the chain codes given above. Obviously, according to the direction chain codes, it is easy to set the contour of the original image. So, the problem turns to getting the original image or filling the holes in a hollow image, if the contour of the image is known. It seems that the problem of getting the original image or Iilling the holes in a hollow image is easy to be solved by filling between each odd point and the next even point on the contour in each line of the image. However, it will cause mistakes when we fill simply between points “n” and “x” or “y” and “z” in Fig. 2. Therefore, we should find a proper method for filling the hollow image in order to “ fill” correctly. Of course, several methods can be used to solve this problem [l-4]. In this paper, a very simple algorithm is presented.
DESCRIPTION
RULES
An image restoration algorithm is strongly dependent upon the image description rules. The description rules corresponding to the image restoration algorithm above are presented. 101 0734-189X/88
$3.00
Copyright Q 1988 by Academic Press, Inc. All rights of reproduction in any fom reserved.
102
ZUGUANG
CA1
6
FIG. 1.
(1) Description
The definition
of the direction
codes.
Form
The description forms of the contours of a binary image described using direction chain codes are given as follow:
where “s ” and “e ” are the starting and ending symbols describing the subdescription of the ith contour path. “xiO” and “yiO” are the coordinates of the starting point in the ith contour path. “dij” is the direction code from the jth point to the ( j + 1)th point in the ith contour path (for the starting point, j = 0). The definition of the direction code is shown in Fig. 1. A subdescription starts from (xjO,yiO) and ends in (xiO, yio). “$” is the end symbol of the whole description of an image. (2) Tracing Rule
The right-edge rule is used for contour tracing in this paper. That is, the contour tracing can be described in terms of an observer who walks along pixels belonging to the contour and selects the rightmost pixel OS[4]. In other words, tracing an out-contour is counterclockwise, and tracing an inner-contour is clockwise. abc def
gh 1 f
k
lm x
n 0
w
P
qrstuv Y
FIG. 2.
z
A sample of a binary
image.
RESTORATION
According to the above chain codes mentioned in Of course, the left-edge some corrections in Table
OF
103
IMAGES
tracing rule, the image shown in Fig. 2 is described using the Introduction and cannot be described as 00775.. . . rule for contour tracing can also be adopted by making 1 below.
(3) The DeJinition of Connectivity Here, we use 8-neighbor connection. As shown in Fig. 2, the contour points from “f ” to “i ” are described by code 7 and cannot use codes O-6 to describe the points
“f 7,to “g >,and“g 9,to “i.” FUNDAMENTAL
CONSIDERATIONS
OF
THE
ALGORITHM
Under the influence of the primary idea stated in the Introduction, to till between two points on the contour of each line, the fundamental consideration of the algorithm is to take away some “unsuitable” points (singular points) temporarily; to make the x-direction scanning fillings in the incomplete contour image which contains “suitable” points (marking points) only; to add all the singular points, taken away temporarily, to the image; and to end the algorithm. Now, the problem turns to what kind of points are singular points (nonmarking points) in the contour. This can be solved in two steps. First, according to the two direction codes connecting a certain point in the contour, some singular points can be found. Suppose the two direction codes connecting the jth point in the ith contour path are d,Ci-lj and dij. Because of the 8 possibilities of diCj-ij and dij each, the total number of their combination is 64. After analyzing each combination, we can get the results as shown in Table 1. Sj in Table 1 has 3 possibilities: “0, ” “1,” and “2.” They represent the singular point, the temporary marking point, and “no direction combination occurs,” respectively. For example, dicj- i) = 7, dij = 6, as point “i” shown in Fig. 2, is the starting point in the x-direction, so it is a marking point. If di(j-lj = 1, di, = 3, as point “k ” in Fig. 2, it is an x-direction ending point and also a marking point. If = 7, dij = 1, the right and left neighbors of the point, as point “z” in Fig. 2, di(j-l) are OS, this point is not only an x-direction starting point but also an x-direction ending point which cannot be adopted as a marking point for x-direction fillings, so
TABLE
1 1 1 1 2 0 0 0
1 1 1 1 2 2 0 0
1 1 1 1 0 0 2 0
1
1 1 1 1 0 0 2 2
2 0 0 0 1 1 1 1
2 2 0 0 1 1 1 1
0 0 2 0 1 1 1 1
0 0 2 2 1 1 1 1
104
ZUGUANG
CAI
it is a singular point. Similarly, if diti-r) = 0, dij = 7, point “f” in Fig. 2, is a singular point too, because, certainly, it is not an x-direction starting point and it cannot be proved an ending point. And if dicJ_ 1j = 1, dij = 6, this situation would~ not occur, because it violates the right-edge tracing rule. Second, during the formation of a contour based on the direction chain codes, if a certain point is assigned as a marking point (S = 1) twice, then this point is a singular point. Because, if a certain point was assigned as a marking point once, this point would be a starting or ending point which is used to fill in the x-direction. If a point like “n” shown in Fig. 2 is assigned as a marking point twice, it is a starting point and also an ending point; so this point is a singular point. In addition, a point in a contour may be met one, two, three, or four times during the contour forming, as well as the contour tracing, but it can only be considered as an x-direction starting point and ending point once for each; so any point in the contour can be assigned as a marking point only twice at most. ALGORITHM.
0. Establish x - y coordinates in A and B areas, respectively. The size of areas A and B is just the same as that of the original image. Area B is used for storing singular points, area A is used for storing the marking point first, then, getting the reconstructed original image by adding the point in area B, after doing the x-direction scanning in that area. 1. Clear areas A and B. 2. For each subdescription (from s to e) do steps 3-15 till symbol $. Begin 3. Let X0 = x,~, Y0 = y,,, X0, Y, are used to record the coordinate of the starting point of a subdescription. 4. Let Da = djo. D, is used to record the first direction code in a subdescription. 5. Evaluate the coordinate of the second point in the contour path represented by the subdescription and store in X and Y, according to X0, Ya, and D,. X. Y are used to record the coordinate of the current tracing point. 6. Do steps 7-13 for each direction code of the subdescription till symbol e. Begin
0123456789101112 2 3 4
.
x l
.
.
. .
.
.
l
5
.
.
.
6 7
l
.
8
. .
9 10
11 12 13 14
l l
.
. . .
l
. . .
. . .
.
. . .
.
*.
.
. l
. .
.
.
.
l
Y
FIG. 3. An example of original image.
RESTORATION
7.
8. 9.
10. 11. 12. 13.
Obtain the status Sj of the current tracing point by looking up Table 1 according to d,Cj-lj and dij which are the two direction codes connecting to the current tracing point M( X, Y). is the previous direction code towards the current tracing point di(j-l) M( X, Y), fij is the direction code of the current tracing point M( X, Y) to the next point. If Sj = 1, then Begin If MA = 1 (MA is the point in area A of M( X, Y), Then Set MA = 0 Set MB = 1 (MB is the point in area B of M( X, Y)). Else Set MA = 1. End Else, if Sj = 0, then set MB = 1. Else, this subdescription is wrong. Obtain coordinate of next tracing point, according to the coordinate X, Y of the current tracing point and the direction code d,,, and store in X, Y.
a 0123456789101112 1 2 * a 3 4 * 5 a a 6 a a 7 a 8 a a 9 a a 10 a 11 a aa 12 aa 13 14
I a a a a a
a
Y
d 0123456789101112 I 2 d 3 d 4 5 6 d 7 d 8 d 9 d 10 11 d dd dd 12 d 13 14 Y
105
OF IMAGES
b 0123456789101112 1 2 3 4 5 6 I 8 9 10 11 b bb 12 b 13 14
C
x
0123456789101112 1 2 3 c 4 cc 5 cc 6 I 8 9 10cc 11 c 12 13 14
b
b bb
Y
x
d
e 0123456789101112 1 2 ea a 3 eea a ca ee 4 5 ee cc a a 6 7 ae 8 ee ae 9 10 eee ee 11 aaaaaacea 12 ae 13 aa 14
I c
c c cc cc cc
cc cc cc cc C
cc cc
Y
f I.
Y
FIG. 4. An illustration to the algorithm.
0123456789101112 1 2 f 3 f f 4 ff 5 ff 6 1 7 f f 8 f f 9 f f 10fff If 11 fffffffff 12 f f 13 f 14 Y
x if ff if ff If f f f f f 1
106
ZUGUANG
CA1
End If X = X,,, Y = Y, then do step 15. Else, the subdescription is wrong. Take din,, the last direction code of the subdescription, do steps 7-13.
14. 15.
as dicJ~ Ij, D, as d, !.
End
16.Do x-direction fillings for each y in area A. Begin 17. Set “1” between the (2n - 1)th “1” and the 2nth “1,” n = 1,2,3... . End l&Do “OR” operation for each point in area A and B, and store the results in area A.
19.End of the algorithm. EXAMPLE. An original binary image is shown in Fig. 3. The description
of the
image is s0702556655656071076021015432221222244es070376656665433211121e$ After executing steps O-15 of the algorithm for the first subdescription, areas A and B are formed as shown in Figs. 4(a) and (b). Figures 4(c) and (d) show the image formed after the second subdescription. After step 16, area A is as shown in Fig. 4(e). Figure 4(f) is the image in area A which is the reconstructed result. CONCLUSIONS
Using the Table 1 constructed by the author, an original image described with contour direction chain codes can be obtained simply by using the algorithm given in this paper. The algorithm has been used to reconstruct more than 10,000 handwritten numerals successfully and no mistake was found. ACKNOWLEDGMENTS
The author would I&e to take this opportunity to express his sincere appreciation to Professor Theo Pavlidis at the State University of New York at Stony Brook for giving important guidance after reading the original manuscript. The author also would Iike to express his sincere appreciation to Mr. Earl Jones, a senior engineer in the Engineering Support Center of United States Postal Service for spending much time showing him the use of the advanced graphics equipment for preparing figures. REFERENCES 1. A. Distante and N. Veneziani, A two-pass filling algorithm for raster graphics, Comput. Image
Process.
Gruphrcs
20,1982,288-295.
2. Y. Yanagihara, M. Tanaka, S. Tamura, and K. Tanaka, Generation of Rader echo images from a contour map, Comput. Graphics Image Process. 24, 1983, 114-126. 3. T. Pavlidis, Filling algorithm for raster graphics, Comput. Graphics Image Process. 10, 1979,126-141. 4. T. Pavlidis, Algorithm for Graphics and Image Processing, Comput. Sci., Washington, D.C., 1982.