Saudi Arabia. Marina Gavrilova ... It follows that a + b < c + d, contradicting the quadrangle inequality. â· w x y z. w x y z. > +. +. The Quadrangle Inequality: a c b d.
On the Distance Transform of Binary Images Muhammad H. Alsuwaiyel Department of Information and Computer Science King Fahd University of Petroleum & Minerals Dhahran 31261 Saudi Arabia
Abstract Given an n × n binary image I of white and black pixels, we present a simple algorithm for computing the distance transform and the nearest feature transform using the Euclidean metric. The running time of the algorithm is linear in the input size.
Keywords:
Distance transform, Feature transform, Image processing, Pattern recognition
1
Introduction
Given an n × n binary image I of white and black pixels, the distance transform of I is a map that assigns to each pixel the distance to the nearest black pixel, referred to as feature. The feature transform of I is a map that assigns to each pixel the nearest feature. The distance transform was first introduced by Pfaltz and Rosenfeld [5], and it has a wide range of applications in image processing, robotics, pattern recognition and pattern matching [4]. The distance metrics used to compute the distance transform include the L1 , L2 and L∞ metrics, with the L2 (Euclidean) metric being the most natural, and the rotational invariant. Several algorithms have been proposed for these metrics. In particular, for the Euclidean metric, some algorithms were proposed in [1, 2, 3]. However, the algorithms in [1, 2], though linear, are computationally expensive compared to those devised for other metrics. For instance, in [1], the divide-and-conquer al-
Marina Gavrilova Department of Computer Science University of Calgary Calgary, Alberta Canada
gorithm for computing the Voronoi diagram is modified so that the merge step takes o(n2 ) time, which results in a linear time algorithm. The algorithm in [3] is obscure and lacks a proof of its correctness. In this paper, we propose a simple linear time algorithm for computing the distance transform and the nearest feature transform using the Euclidean metric. The possibility of extention to other metrics and algorithm implementation in parallel are discussed briefly.
2
Definitions
Henceforth, the ordered pair (i, j), 1 ≤ i, j ≤ n, will refer to the pixel in the ith row and jth column, where the numbering is raw major, top to bottom and left to right. Let (i, j) be a pixel, 1 ≤ i, j ≤ n. We define the following: • f (i, j) is the feature nearest to (i, j). Ties are broken arbitrarily. • g(i, j) is the feature nearest to (i, j) on column j. Ties are broken arbitrarily. • δ(i, j) is the square of the Euclidean distance between pixel (i, j) and f (i, j). • µ (i, j) = min1 ≤i ≤n {δ (i, j)}, that is, µ (i, j) is the square of the Euclidean distance between (i, j) and the nearest feature to (i, j) in column j.
3
The algorithm
The following two lemmas provide the basis of the proposed algorithm. Lemma 1 Suppose that for some point (i, j), f (i, j) is to the left of column j. Then, f (i, j) is on the same column as or to the right of f (i, j − 1). Proof. Let a, b, c and d be as in Figure 1. Clearly, f (i, j) is closer to row i than f (i, j − 1). Moreover, by the definition of feature, a ≤ d and b ≤ c. Since (i, j) = (i, j − 1), these inequalities are strict. It follows that a + b < c + d, contradicting the quadrangle inequality. ✷
ordered clockwise in order for lemma to be correct. Then any pixel p from row i belongs either to the half-plane originated by the perpendicular bisector pb (q, r) that contains q or to the halfplane originated by the perpendicular bisector pb (r, s) that contains s. Thus p is closer to either q or s than to r. ✷ Note that as shown in Figure 2(b), p lies to the right of the perpendicular bisector pb (q, s). It follows that s is closer to p than q. Hence, unless there is another black pixel that is closer to p than s, the latter is the nearest feature to pixel p. r
(a) s q
f ( i , j) a
x
f ( i , j - ) i b
p
c d
(b) ( i , j - ) (i , j)
s
q
y w
i
p
x z The Quadrangle Inequality: w+x > y+ z
Figure 1: Proof of Lemma 1.
Lemma 2 Let p be a white pixel, and q, r and s three black pixels. If the two perpendicular bisectors pb (q, r) and pb (r, s) intersect above row i, then r cannot be the nearest feature to any pixel on row i. Proof. Consider Figure 2(a), in which a white pixel p and three black pixels q, r and s are shown. The two perpendicular bisectors pb (q, r) and pb (r, s) intersect at point x, which is above row i. Note that q, r and s must be
Figure 2: Proof of Lemma 2. Lemmas 1 and 2 imply the following simple approach for computing f (i, j) and δ(i, j), 1 ≤ i, j ≤ n. As a preprocessing step, all µ (i, j) and g(i, j), 1 ≤ i, j, ≤ n, are computed. This is followed by two sweeps on the image: top-down and bottom-up. As the final values of f (i, j) and δ(i, j), we select the nearest of the two features obtained by the two sweeps, and its corresponding δ value. Hence, in what follows, we will describe the top-down scan, and the bottom-up scan is essentially the same. As a result of the preprocessing step, each row can now be processed independently. For i = 1, 2, . . . , n, row i is scanned twice: once from left to right to compute δl (i, j), and the other from right to left to compute δr (i, j). Here δl (i, j) is δ(i, j) constrained to pixels on
and to the left of column j, and δr (i, j) is δ(i, j) constrained to pixels on and to the right of column j. The value δ(i, j) is set to the minimum of δl (i, j) and δr (i, j). The algorithm to be presented describes only how to compute δl (i, j). Hence, we will write δ in place of δl , and f (i, j) will denote the nearest feature on or to the left of column j. The algorithm maintains a sequence σ of features whose µ value is increasing from left to right. The sequence can be accessed using two pointers Succ and Pred such that Succ (p) and Pred (p) point to the next and previous elements of p ∈ σ, respectively. Initially, σ = g (i, 1). After the algorithm finds f (i, j − 1), it appends g(i, j) to σ. It then backtracks by testing the elements in σ in reverse order. A black pixel that cannot be a nearest feature to any pixel in row i will be called “inactive”; otherwise, it will be called “active”. If for some j , 1 ≤ j < j, g (i, j ) is found inactive, then, by Lemma 2, it can safely be deleted from σ. After the deletion of inactive features, the algorithm searches in the forward direction starting at the leftmost element in the sequence. The search halts when either σ consists of exactly one feature, namely g(i, j), or a feature q is found for the first time with the property that the perpendicular bisector pb (p, q) intersects with column j above row i. In the former case, g (i, j) is set as the nearest feature to (i, j), and in the latter, q is set as the nearest feature to (i, j). Successive deletions of inactive features as well as updating the sequence is depicted in Figure 3 (the pointer Pred is not shown for clarity). Parts (a) and (b) of the figure exhibit the map Succ before and after the reversed and forward traversals. The features between p and q were removed from σ, as they are inactive. In Figure 3, we have assumed, without loss of generality, that g (i, j) is on or above row i. The process of backtracking and deleting elements of σ, if necessary, continues until f (i, j) is found. Once it is found, then Lemma 1 implies that f (i, j + 1) is either f (i, j) or to the
p (a) q r
(i , j) p (b) q
(i , j)
Figure 3: Removing inactive features and finding f (i, j). right of it. Hence, all elements to the left of f (i, j) are removed from σ. Assume that (i, j) is the current pixel to be processed, and |s| ≥ 3. Let p = (i, j), q = g(i, j), r = Pred (q) and s = Pred (Pred (q)). Let L1 = pb (q, r), the perpendicular bisector of q and r and L2 = pb (r, s), the perpendicular bisector of r and s. Then, evaluating f (i, j) can be described in the following steps. 1. If |σ| = 1, then set f (i, j) = g(i, j). 2. If |σ| = 2 and g (i, j) is closer to (i, j) than Pred (g (i, j)), then set f (i, j) = g (i, j) and remove Pred (g (i, j)) from σ. 3. If |σ| = 2 and Pred (g (i, j)) is closer to (i, j) than g (i, j), then set f (i, j) = Pred (g (i, j)). 4. Starting from pixel (i, j), traverse σ backward, deleting those features whose µ value is larger than µ (i, j). 5. If L1 and L2 intersect above row i, then delete r from σ: Set Pred (q) = s and Succ (s) = q. Repeat this step until either |σ| = 2 or L1 and L2 do not intersect to the left of column j above row i.
6. Starting from the leftmost element in σ, traverse σ in the forward direction, bipassing all features that are farther from (i, j) than g (i, j). Stop the traversal as soon as either |σ| = 1 or there is a feature q that is closer to (i, j) than g (i, j). In the first case, set f (i, j) = g (i, j), and in the second case, set f (i, j) = q. By Lemma 1, remove from σ all elements to the left of f (i, j). The correctness of the algorithm follows from Lemma 1, Lemma 2, and the details embedded in its description. Now, we analyze the running time of the algorithm. Computing the µ values in the preprocessing step costs Θ(n2 ). As to finding the nearest features to all pixels in each row, we see that the operations needed are advancing forward and backward, deletion of elements from the sequence σ, and computing the δ values. Every element in σ, is inserted exactly once and deleted at most once, and the δ values are assigned exactly once per cell. Let “advance” and “retreat” denote, respectively, the operations of moving the pointer Succ forward, and moving the pointer Pred backward. In the forward direction, once a column is skipped, it will not be visited in subsequent iterations, that is, there are at most n − 1 advances. As to the number of retreats, we observe that once an element has been removed from the sequence σ, it will not be considered in subsequent iterations. As a result, the number of operations needed in each row is Θ(n), for an overall of Θ(n2 ) operations. It follows that the algorithm is linear.
4
Conclusion
We have presented a simple linear time algorithm for computing the distance transform and the nearest feature transform using the Euclidean metric of an n × n binary image I of white and black pixels. With simple modifications, the algorithm also works for other metrics such as the L1 and L∞ metrics. Since the algorithm works on each row independently, it
can be parallelized naturally. It is well suited for a mesh of p processors, 1 ≤ p ≤ n. In this case, each row of processors work on one row of the image independently.
References [1] H. Breu, J. Gil, D. Kirkpatrick, and M. Werman. Linear time Euclidean distance transform algorithms. IEEE Transactions on Pattern Analysis and Machine Intelligence, 17529-533, 1995. [2] L. Chen and H. Y. H. Chuang. A fast algorithm for Euclidean distance maps of a 2-D binary image. Information Processing Letters, 5 1:25-29, 1994. [3] T. Hirata. A unified linear-time algorithm for computing distance maps. Information Processing Letters, 58: 129- 133,1996. [4] D. W. Paglieroni. Distance transforms: Properties and machine vision applications. CVGIP: Graphical Models and Image Processing, 54:56-74, 1992. [5] A. Rosenfeld and J. L. Pfalz. Sequential operations in digital picture processing. Journal of the ACM, 13:471-494, 1966.