3
Information Processing North-Holland (1989) 87-89 30 January 1989 AN IMAGE ALGORITHM FOR COh@I?UTk--G THE SDORFF DISTANCE EFFICIENTLY IN LIlWAR TIME R. SHONKWILER School of Mathematics, Georgia Instrtute of Z’echnology, Atlanta, GA 30332, U.S.A. Communicated by David Gries Received 11 April 1988 Revised 20 June 1988 Keywork Hausdorff distance algori thn , computational gewnetry, parallel processing 1. Introduction A well-known metric on the class of subse@ of a metric space (S, p) is the Hausdorff distance. For two subsets of A and B of S define their forward distance df and backward distance db as df(A, B)= sup inf p(a, b), aEA PEB db(A, B) =df(B, A). These distarmcesare not necessarily symmetric. For example, if A C B but A C B, the11 df(A, B) = 0, but db(A- B) + 0. To obtain a symmetric distance function, we take d(A, B)=df(A, B)+db(A, B). In this paper, the above equation is used as the definition of Hausdorff distance. (The classical Hausdorff metric as described in ‘Kelley [l] is, in our notation, max(df( A, B), db( A, B)). It is easy to show that the two are topologically equivalent.) The Hausdorff distance is useful in computer graphics as a measure of the closeness of two images. As a concrete example, suppose one has a “goal” %dimensional black-and-white image known pixel by pixel in some @d of resolution m x n. Then, given an m x n “ test” image with the same resolution, the Hausdorff distance be- tween the two provides a numerical measure of their nearness. In this case, the Hausdorff distance is in the range O..p + ((1, 1), (m, n)). In this setting, a convenient metric p is the L1 or “Manhattan” metric: the distance between pixels (xl, yl) and (x2, y2) is defined by P((X2, Y2b (~19 YW = abs( x2 - xl) + abs( y2 - yl). Using this metric, two images have Hausdorff distance 0 iff they coincide exactly, while their maximum separation is 2(n + m - 2). 2. Building a distance table A key ;o our algorithm is the computation of a distance table. Let A be an m x n array of l’s and 0’s. Distance table D for A is an m x n array D each element of which contains the Manhattan distance from the corresponding element of A to a 1 of A. We now outline the calculation of D. As step 0 of the algzitbm, set each D[i, j] to the distance in A from A[i, j] to a 1 of A, but allowing move; in no directions at all. Using m + n, which is greater than the longest possible distance, to denote infinity, the definition of D at this point is D[i, j] = if A[i, j] = 1 then 0 else m + n fi. Step 1 of the algorithm is to change D so that each element D[i, j] is the distance from A[i, j] 0020-0190/89/$3.50 0 1989, Elsevier Science Publishers B.V. (North-Holland) 87

An image algorithm for computing the Hausdorff distance efficiently in linear time

Embed Size (px)

Citation preview

Page 1: An image algorithm for computing the Hausdorff distance efficiently in linear time

Information Processing North-Holland

(1989) 87-89 30 January 1989

AN IMAGE ALGORITHM FOR COh@I?UTk--G THE SDORFF DISTANCE EFFICIENTLY IN LIlWAR TIME

R. SHONKWILER School of Mathematics, Georgia Instrtute of Z’echnology, Atlanta, GA 30332, U.S.A.

Communicated by David Gries Received 11 April 1988 Revised 20 June 1988

Keywork Hausdorff distance algori thn , computational gewnetry, parallel processing

1. Introduction

A well-known metric on the class of subse@ of a metric space (S, p) is the Hausdorff distance. For two subsets of A and B of S define their forward distance df and backward distance db as

df(A, B)= sup inf p(a, b), aEA PEB

db(A, B) =df(B, A).

These distarmces are not necessarily symmetric. For example, if A C B but A C B, the11 df(A, B) = 0, but db(A- B) + 0. To obtain a symmetric distance function, we take

d(A, B)=df(A, B)+db(A, B).

In this paper, the above equation is used as the definition of Hausdorff distance. (The classical Hausdorff metric as described in ‘Kelley [l] is, in our notation, max(df( A, B), db( A, B)). It is easy to show that the two are topologically equivalent.)

The Hausdorff distance is useful in computer graphics as a measure of the closeness of two images. As a concrete example, suppose one has a “goal” %dimensional black-and-white image known pixel by pixel in some @d of resolution m x n. Then, given an m x n “ test” image with the same resolution, the Hausdorff distance be- tween the two provides a numerical measure of their nearness. In this case, the Hausdorff distance is in the range O..p + ((1, 1), (m, n)).

In this setting, a convenient metric p is the L1 or “Manhattan” metric: the distance between pixels (xl, yl) and (x2, y2) is defined by

P((X2, Y2b (~19 YW

= abs( x2 - xl) + abs( y2 - yl).

Using this metric, two images have Hausdorff distance 0 iff they coincide exactly, while their maximum separation is 2(n + m - 2).

2. Building a distance table

A key ;o our algorithm is the computation of a distance table. Let A be an m x n array of l’s and 0’s. Distance table D for A is an m x n array D each element of which contains the Manhattan distance from the corresponding element of A to a 1 of A. We now outline the calculation of D.

As step 0 of the algzitbm, set each D[i, j] to the distance in A from A[i, j] to a 1 of A, but allowing move; in no directions at all. Using m + n, which is greater than the longest possible distance, to denote infinity, the definition of D at this point is

D[i, j] = if A[i, j] = 1 then 0 else m + n fi.

Step 1 of the algorithm is to change D so that each element D[i, j] is the distance from A[i, j]

0020-0190/89/$3.50 0 1989, Elsevier Science Publishers B.V. (North-Holland) 87

Page 2: An image algorithm for computing the Hausdorff distance efficiently in linear time

Volume 30, Number 2 INFORMATION PIZOCESSING LETI’ERS 30 January 1989

to the nearest 1 in row i. This requires two phases. The first phase stores in each D[i, j ] the mini-

mum distance from A[i, j] to a 1 allowing only moves to the left (m + n if no such one):

Phase(a): for i:=l to Mdo {Calculate elements of row i of D. harkant:

d = &stance to the nearest 1 strictly to the left of

A[i, jl.) d:= D[i, l] + 1; for j:=2toNdo w!N

if d < D[i, j] ahen D[i, j] := d else d := D[i, j];

d:=d+l end.

Phase b for the first dimension changes D to contain, at each element D[i, j], the minimum distance from A[i, j] to a 1 to its left or right.

Phase(b): for i := 1 to A4 do (Compute elements of row i. Invariant: d is the distance to the nearest 1 strictly to the right of D[i, jl.}

d:= D[i, N] + 1; for j:=N-1 downtol do begin

if d < D[i, j] then D[i, j] := d else d := D[i, j];

d:=d+l end.

Step 2 changes D[i, j] to its final value. The two phases of step 2 are similar to those of step 1. The first phase moves from top to bottom through the columns; the second phase from bottom to top. That this algorithm computes the distance table can be seen in terms of the following obvious property of the Manhattan distance.

For a.ny minimal-length path from an element of A to a 1, there exists a path with the same begin- ning, end and Manhattan distance in which all the horizontal moves (moves within a row) precede all the vertical moves (moves within-a column). Fur- ther, all the vertical moves are of the same kind-either all up or all down.

Thus, the min mum distant from an element to a 1 is min(a, b), where a is the minimum distance looking down and then horizontally and b is the minimum distance looking up and then horizon- tally. And this is what step 2 computes, for all array elements. The forms of the two phases of step 2 are sir&r to those of step 1.

Computing a distance table requires time linear in the number of elements in the array. Each array element is processed five times-once in step 0 and twice in each of steps 1 and a-where processing an array element requires constant time. Step 0 can be integrated into phase (a) of step 1, thus reducing the time further.

3. Computing the Wausdorff distance of two images

Suppose we have two m x n images (array of l’s and O’s) A and B. Let D be the distance matrix for A. Then db(A, B) is the maximum D[i, j] such that B[i, j] = 1. Since, df( A, B) = db( B, A), df( A, B) is calculated in a similar fash- ion. The desired result d( A, B) is then db(A, B) + df( A, B).

me time is obviously linear in m * n. Pn the simplest implementation, the extra space required ism*Pt -one distance table has to be maintained at any time. It is possible to elimnate the distance table completely by changing A itself into its distance table D, since it is easy to reconstruct A from D.

4. Using breadth-fiit search to compute the dis- tance table

A referee has pointed out that th,e Hausdorff distance can also be calculated using breadth-first search (BFS). Consider the graph consisting of the elements of the array together with one more node, the root. The graph has an edge from the root to each 1 of the array and an edge from each array elelilent to its four adjacent elements. Per- forming BFS starting from the root procedures a spanning tree whose depth is one more than the maximum distance from a 0 to a l-the extra one is because of the additional root node.

Page 3: An image algorithm for computing the Hausdorff distance efficiently in linear time

Volume 30,, Number 2 INFORh4ATION PROCESSING LETTERS 30 January 1989

BFS requires space for a queue of pairs (each pair gives the coordinates of an array element), and the size of the queue may in general be as Xarge as m * n (in this application, it is possible to implement the algorithm using a queue of maxi- mum size m * n * 3). Since the algorithm of Sec- tion 3 requires no extra space, it is to be preferred.

BPS also requires about 35% more time, due partly to the need to maintain the queue and partly to the need for more range checking on array element bounds.

Remark. Both our algorithm and BFS generalize easily to more dimensions. In the algorithm of Section 3, in constructing a distance table, each additional dimension requires an extra pass with two phases, where each phase scans the whole array. Thus, for n dimensions, constructing a dis- tance table requires 2 + n passes through the array.

Remark. Our algorithm has a very obvious parallel implementation. In step 1, each row can be processed by a different processor. In step 2, each column can be processed by a different processor. For that matter, calculation of df and db are totally independent.

Acknowledpnent

I thank Brandon Dixon, James Simon, and the two anonymous referees for their critical com- ments on various versions of this note.

[l] J.L. Kelley, General Topo&y (Van Nostrand, New York, 1955).

89