Weighted distances based on neighbourhood sequences

8
Weighted distances based on neighbourhood sequences Robin Strand Centre for Image Analysis, Uppsala University, Box 337, SE-75105 Uppsala, Sweden Received 6 November 2006; received in revised form 30 May 2007 Available online 7 June 2007 Communicated by G. Sanniti di Baja Abstract By combining weighted distances and distances based on neighbourhood sequences, a new family of distance functions with poten- tially low rotational dependency is obtained. The basic theory for these distance functions, including functional form of the distance between two points, is presented. By minimizing an error function, the weights and neighbourhood sequence that give the distance func- tion with the lowest rotational dependency are derived. To verify that the low rotational dependency of the proposed distance function is valid also in applications, the constrained distance transform on a binary image is computed and compared with some traditionally used distance functions. Ó 2007 Elsevier B.V. All rights reserved. Keywords: Digital geometry; Distance functions; Neighbourhood sequences; Weighted distance; Discrete geometry 1. Introduction In (Rosenfeld and Pfaltz, 1966), the classical city-block and chessboard distances, defined as the shortest path between two grid points using only 4- and 8-neighbours respectively, are considered. In (Rosenfeld and Pfaltz, 1968), it is noted that the distance function obtained by mix- ing the city-block and chessboard distances is not as rota- tional dependent. These distances are called octagonal distances, since they generate octagonal shaped discs. There is another very common way of modifying the city-block and chessboard distances in order to obtain a less rotational dependent distance, the weighted distances (Montanari, 1968; Borgefors, 1984, 1986). With these distances, each local step is given a weight, which is considered when com- puting the distance, i.e., when finding the minimal cost path. The distances obtained by mixing steps corresponding to 4- and 8-neighbours suggested in 1968 by Rosenfeld and Pfaltz (1968) are also called distances based on neighbour- hood sequences (or octagonal distances, here denoted by n.s.-distances). The literature on n.s.-distances is rich; a theory for periodic n.s. not connected to any specific neigh- bourhood relations in Z n is presented in (Yamashita and Honda, 1984; Yamashita and Ibaraki, 1986) and further developed for the natural neighbourhood structure by the so-called octagonal distance in (Das and Chakrabarti, 1987; Das et al., 1987). Results for general (not necessarily periodic) n.s. are presented in (Fazekas et al., 2002; Nagy, 2003; Strand and Nagy, 2007). Many approaches where the deviation from the Euclid- ean distance is minimized in order to find the optimal n.s. (n.s.-distances) or weights (weighted distances) have been proposed for Z 2 . In most papers, error functions minimiz- ing the asymptotic maximum difference of a Euclidean ball and a ball obtained by using n.s.-distances (Yamashita and Ibaraki, 1986; Das, 1992; Das and Chatterji, 1990) or weighted distances (Borgefors, 1986; Verwer, 1991) are minimized. Other approaches have also been considered for n.s.-distances. In (Hajdu and Nagy, 2002), optimal neighbourhood sequences for the 2D hexagonal and trian- gular grids are found using a compactness ratio – the ratio between the squared perimeter and the area of the convex hull of the disks obtained by using neighbourhood 0167-8655/$ - see front matter Ó 2007 Elsevier B.V. All rights reserved. doi:10.1016/j.patrec.2007.05.016 E-mail address: [email protected] www.elsevier.com/locate/patrec Pattern Recognition Letters 28 (2007) 2029–2036

Transcript of Weighted distances based on neighbourhood sequences

Page 1: Weighted distances based on neighbourhood sequences

www.elsevier.com/locate/patrec

Pattern Recognition Letters 28 (2007) 2029–2036

Weighted distances based on neighbourhood sequences

Robin Strand

Centre for Image Analysis, Uppsala University, Box 337, SE-75105 Uppsala, Sweden

Received 6 November 2006; received in revised form 30 May 2007Available online 7 June 2007

Communicated by G. Sanniti di Baja

Abstract

By combining weighted distances and distances based on neighbourhood sequences, a new family of distance functions with poten-tially low rotational dependency is obtained. The basic theory for these distance functions, including functional form of the distancebetween two points, is presented. By minimizing an error function, the weights and neighbourhood sequence that give the distance func-tion with the lowest rotational dependency are derived. To verify that the low rotational dependency of the proposed distance function isvalid also in applications, the constrained distance transform on a binary image is computed and compared with some traditionally useddistance functions.� 2007 Elsevier B.V. All rights reserved.

Keywords: Digital geometry; Distance functions; Neighbourhood sequences; Weighted distance; Discrete geometry

1. Introduction

In (Rosenfeld and Pfaltz, 1966), the classical city-blockand chessboard distances, defined as the shortest pathbetween two grid points using only 4- and 8-neighboursrespectively, are considered. In (Rosenfeld and Pfaltz,1968), it is noted that the distance function obtained by mix-ing the city-block and chessboard distances is not as rota-tional dependent. These distances are called octagonaldistances, since they generate octagonal shaped discs. Thereis another very common way of modifying the city-blockand chessboard distances in order to obtain a less rotationaldependent distance, the weighted distances (Montanari,1968; Borgefors, 1984, 1986). With these distances, eachlocal step is given a weight, which is considered when com-puting the distance, i.e., when finding the minimal cost path.

The distances obtained by mixing steps corresponding to4- and 8-neighbours suggested in 1968 by Rosenfeld andPfaltz (1968) are also called distances based on neighbour-

hood sequences (or octagonal distances, here denoted by

0167-8655/$ - see front matter � 2007 Elsevier B.V. All rights reserved.

doi:10.1016/j.patrec.2007.05.016

E-mail address: [email protected]

n.s.-distances). The literature on n.s.-distances is rich; atheory for periodic n.s. not connected to any specific neigh-bourhood relations in Zn is presented in (Yamashita andHonda, 1984; Yamashita and Ibaraki, 1986) and furtherdeveloped for the natural neighbourhood structure by theso-called octagonal distance in (Das and Chakrabarti,1987; Das et al., 1987). Results for general (not necessarilyperiodic) n.s. are presented in (Fazekas et al., 2002; Nagy,2003; Strand and Nagy, 2007).

Many approaches where the deviation from the Euclid-ean distance is minimized in order to find the optimal n.s.(n.s.-distances) or weights (weighted distances) have beenproposed for Z2. In most papers, error functions minimiz-ing the asymptotic maximum difference of a Euclidean balland a ball obtained by using n.s.-distances (Yamashita andIbaraki, 1986; Das, 1992; Das and Chatterji, 1990) orweighted distances (Borgefors, 1986; Verwer, 1991) areminimized. Other approaches have also been consideredfor n.s.-distances. In (Hajdu and Nagy, 2002), optimalneighbourhood sequences for the 2D hexagonal and trian-gular grids are found using a compactness ratio – the ratiobetween the squared perimeter and the area of the convexhull of the disks obtained by using neighbourhood

Page 2: Weighted distances based on neighbourhood sequences

pr

2030 R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036

sequences. In (Hajdu and Hajdu, 2004), the symmetric dif-ference is used for n.s. in Z2 and in (Nagy and Strand,2006), the following error functions are considered forn.s. in the face-centered cubic and the body-centered cubicgrids: absolute error, relative error, compactness ratio,maximal inscribed ball, and minimal covering ball. In thispaper, the compactness ratio is considered.

In this paper, the constrained distance transform (cDT)is computed using some distance functions. The image isdivided into object pixels and obstacle pixels. The cDTlabels each object pixels with the distance between the pixeland one or more source pixels, where paths defining thedistance are not allowed to cross obstacle pixels. The con-strained DT using path-based distance functions can becomputed using standard shortest-path techniques forweighted graphs resulting in a linear time algorithm. In(Piper and Granum, 1987), the Dijkstra’s graph searchalgorithm is used. A bucket sorting implementation ofthe Dijkstra’s algorithm is used in (Verwer et al., 1989).

When the cDT is computed using the Euclidean distancefunction, only the pixels that are visible from the sourcepixel can be assigned the Euclidean distance in linear time.To assign the distance to the other pixels, a set of pixelsdefining discrete straight line segments (DSSs) not inter-secting the obstacles are found such that the sum of lengthsof the segments is the shortest constrained distance. Thus,the time complexity for algorithms computing cDT for theEuclidean distance depends on the number of obstacle pix-els. In (Coeurjolly et al., 2004), a 2D-algorithm that runs inO(nm), where n is the number of object pixels and m is thenumber of obstacle pixels, is presented together with anapproximate solution that runs in O(n log(m)). In the algo-rithm presented in (Ragnemalm, 1993), the DSSs is alsoupdated only for visible pixels. In Section 6, the cDT forthe proposed distance function is compared with some tra-ditionally used distance functions including the Euclideandistance.

In this paper, we restrict the discussion to distancesusing the 3 · 3 neighbourhood with two weights. The dis-tance function presented here uses n.s. to restrict somesteps to the 4-neighbourhood. In conjunction to this, eachstep is weighted resulting in a less rotational dependent dis-tance function. A functional form of the distance betweentwo points is derived for both Z2 and R2. The latter is usedin the optimization where the optimal (according to theerror function) pair of n.s. and weights are derived. Thetheoretic results are evaluated by computing the con-strained DT using some distance functions.

p0

p1

p2

p3

p4

p5

6

q1 q2 q3 q4 q5 q6

r1

r2

r3

r4

r56

Fig. 1. Three B-paths when B = (1,2).

2. Basic notions

Two grid points p1 ¼ ðx1; y1Þ; p2 ¼ ðx2; y2Þ 2 Z2 areq-neighbours, q 2 {1,2}, if

jx1 � x2j þ jy1 � y2j6 q and maxfjx1� x2j; jy1� y2jg ¼ 1:

ð1Þ

The points p1, p2 are adjacent if p1 and p2 are q-neighboursfor some q. Two q-neighbours such that equality holds forboth equations in (1) are called strict q-neighbours. A n.s.B is a sequence B ¼ ðbðiÞÞ1i¼1, where each b(i) denotes aneighbourhood relation in Z2. If B is periodic, i.e., if forsome fixed strictly positive l 2 Zþ, b(i) = b(i + l) is validfor all i 2 Zþ, then we write B = (b(1),b(2), . . . ,b(l)).

The following notation is used for the number of 1:s and2:s in the n.s. B up to position k:

1kB ¼ jfi : bðiÞ ¼ 1; 1 6 i 6 kgj and

2kB ¼ jfi : bðiÞ ¼ 2; 1 6 i 6 kgj:

A path, denoted P, in a grid is a sequence p0,p1, . . . ,pn ofadjacent grid points. A path is a B-path of length n if, forall i 2 {1,2, . . . ,n}, pi�1 and pi are b(i)-neighbours. Thenumber of 1-steps and strict 2-steps in a given path P is de-noted 1P and 2P, respectively.

Definition 1. Given the n.s. B, the n.s.-distance d(p0,pn; B)between the points p0 and pn is the length of (one of) theshortest B-path(s) between the points.

Let the real numbers a and b (the weights) and a B-pathP of length n, where exactly l (l 6 n) pairs of adjacent gridpoints in the path are strict 2-neighbours be given. The cost

of the (a,b)-weighted B-path P is (n � l)a + lb. The B-pathP between the points p0 and pn is a (a,b)-weighted minimal

cost B-path between the points p0 and pn if no other (a,b)-weighted B-path between the points has lower cost thanthe (a,b)-weighted B-path P.

Definition 2. Given the n.s. B and the weights a, b, theweighted n.s.-distance da,b(p0,pn; B) is the cost of (one of)the (a,b)-weighted minimal cost B-path(s) between thepoints.

In the figures, each grid point is represented by the cor-responding pixel. The following example illustrates some ofthe notions introduced in this section.

Example 3. This example refers to Fig. 1. For the(periodic) n.s. B = (1,2), the path P : p0; p1; . . . ; p6 andthe path Q : p0; q1; . . . ; q6 are both B-paths. The pathp0, r1, . . . , r6 is not a B-path. We have 16

B ¼ 3 and 26B ¼ 3.

Also, 1P ¼ 3, 2P ¼ 3, 1Q ¼ 6, 2Q ¼ 0. Considering the(a,b)-weighted paths, the cost of path P is 3a + 3b and thecost of path Q is 6a.

Page 3: Weighted distances based on neighbourhood sequences

R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036 2031

3. Distance function in discrete space

We state now a functional form of the distance betweentwo grid points (0,0) and (x,y), where x P y P 0. Observethat by translation-invariance and symmetry, the distancebetween any two grid points is given by the formula pre-sented in Theorem 5. The following lemma is intuitivelyvalid. Since it will be used in several proofs in this paper,it is proved for the completeness.

Lemma 4. Given a n.s. B and a point ðx; yÞ 2 Z2 such thatx,y P 0, there is a shortest B-path between 0 and (x,y)

consisting of only the steps (1,0), (1,1), and (0,1).

Proof. Let P be a B-path that reaches the point (x,y) suchthat x,y P 0 using a1 number of (1,0)-steps, a2 number of(0,1)-steps, and a3 number of (1, 1)-steps for some non-neg-ative integers a1, a2, a3 such that the length l = a1 + a2 + a3

is minimal. Assume that there is a B-path P0 (using arbi-trary local steps) of length l 0 < l that reaches the point(x,y). The B-path P00 of length l 0 is constructed by takingthe absolute value of the local steps ((1,0) instead of(�1,0) and so on). This corresponds to addingd1(1,0) + d2(0, 1) + d3(1, 1) to (x,y) for some non-negative(even) integers d1, d2, d3. We get a B-path of length l 0 usingc1 number of (1,0)-steps, c2 number of (0, 1)-steps, and c3

number of (1,1)-steps reaching a point (x 0,y 0) =c1(1, 0) + c2(0, 1) + c3(1, 1) = (a1 + d1) Æ (1, 0) + (a2 + d2) Æ(0,1) + (a3 + d3) Æ (1,1). Now, a1 + a2 + a3 = l > l 0 = c1 +c2 + c3 = (a1 + d1) + (a2 + d2) + (a3 + d3) P a1 + a2 + a3.Contradiction. h

Theorem 5. Let the n.s. B and the point p = (x, y), where

x P y P 0 be given. The n.s.-distance between 0 and p is

given by

dð0; p; BÞ ¼ minfk 2 N : k P xþmaxð0; y � 2kBÞg:

Proof. By Lemma 4, there is a shortest B-path between 0

and (x,y) consisting of only the steps (1, 0), (1, 1), or(0,1). So a 1-step is either a (1,0)-step or a (0, 1)-step anda 2-step is a (1, 1)-step. Starting from (0,0), to find theshortest B-path to (x,y) either (1,0), (1,1), or (0,1) canbe used in each step. Obviously, y 2-steps and x � y 1-steps(x steps in total) should be used if the n.s. B allows, i.e. if2x

B P y. In that case, the distance is d(0,p; B) = x. For each‘‘missing’’ 2x

B, the 2-step (1, 1) must be replaced by two 1-steps (1, 0) and (0,1). To reach (x,y) in k steps, y � 2k

B suchreplacement must occur. Thus, at least xþmaxð0; y � 2k

BÞsteps are used, but as few as possible. The formulafollows. h

Lemma 6. Let the point (x,y), where x P y P 0, and the

value of k ¼ minfk : k P xþmaxð0; y � 2kBÞg be given. If

only the steps (1,0), (1,1), and (0,1) are used for a shortest

B-path P between 0 and (x,y), then 1P ¼ 2k � x� y and

2P ¼ xþ y � k.

Proof. First of all, by Lemma 4, there is a shortest B-pathwith only the steps (1,0), (0, 1), (1,1). Therefore,(x,y) = a1(1,0) + a2(0,1) + a3(1,1) for some a1; a2; a3 2 N.Obviously, the length k of the B-path is a1 + a2 + a3,1P ¼ a1 þ a2, and 2P ¼ a3. We get

k ¼ a1 þ a2 þ a3;

x ¼ a1 þ a3;

y ¼ a2 þ a3:

Thus, 1P¼a1þa2¼2k�x�y and 2P¼a3¼xþy� k. h

Lemma 7. Let the n.s. B and the point ðx; yÞ 2 Z2, where

x P y P 0 be given. If P is a shortest B-path between 0

and (x,y) consisting of only the steps (1,0), (1,1), and

(0,1) and the weights a; b 2 R are such that 0 < a 6 b 6 2a,then P is also a (a,b)-weighted minimal cost B-path between

0 and (x,y).

Proof. Let n be the length of P and let l be the number of2-steps in P. The length of P can then be writtenn = (n � l)1 + l1. The cost of (a,b)-weighted B-path is(n � l)a + lb. Assume that P0 is a (a,b)-weighted B-pathof lower cost between 0 and (x,y). Let the length of P0

be n 0 (n 0 P n since P is a shortest B-path) and the numberof 2-steps l 0. The cost of the (a,b)-weighted B-path P0 is(n 0 � l 0)a + l 0b. Thus, the assumption is

ðn0 � l0Þaþ l0b < ðn� lÞaþ lb: ð2Þ

We get the following cases:

(i) l 0 < l

Since P0 is a B-path between 0 and (x,y) and the only2-step that is used in P is (1,1), there are at least2(l � l 0) more 1-steps in P0 compared to P. We get(n 0 � l 0) P (n � l) + 2(l � l 0)) (n 0 � l 0) � (n � l) P2(l � l 0)) ((n 0 � l 0) � (n � l))a P 2(l � l 0)a. By theassumption (2), we have ((n 0 � l 0) � (n � l))a <(l � l 0)b. Thus, 2(l � l 0)a 6 ((n 0 � l 0) � (n � l))a <(l � l 0)b) 2a < b.

(ii) l 0 > l

ðn� l0Þaþ l0b6 ðn0 � l0Þaþ l0b< ðn� lÞaþ lb by ð2Þðl0 � lÞb< ðl0 � lÞa;b< a since l0 > l:

This proves that when 0 < a 6 b 6 2a, l 0 = l, i.e., the num-ber of 2-steps are equal in P and P0. Now, since P0 is a(a,b)-weighted minimal cost B-path, it follows that alsothe number of 1-steps are equal. Thus n 0 = n. h

The following main theorem is obtained by summing upthe results from Theorem 5, Lemmas 6 and 7. Given a short-est B-path consisting of only the steps (0,1), (1,1), and (0,1)between 0 and (x,y) with x P y P 0, if 0 < a 6 b 6 2a, weknow by Lemma 7 that the path is also a (a,b)-weightedminimal cost B-path. Using Lemma 6, where the number

Page 4: Weighted distances based on neighbourhood sequences

2032 R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036

of 1-steps and 2-steps in the B-path is given, the formula inTheorem 5 can be rewritten such that the length of the pathis given by summing the number of 1-steps and 2-steps. Bymultiplying these numbers with the corresponding weights,we get the following functional form of the weighted n.s.-distance between two points.

Theorem 8. Let the n.s. B, the weights a,b such that

a 6 b 6 2a, and the point (x, y), where x P y P 0, be given.

The weighted n.s.-distance between 0 and (x, y) is given by

da;bð0; ðx; yÞ; BÞ ¼ ð2k � x� yÞ � aþ ðxþ y � kÞ � b;where k ¼ min

k: k P xþmax 0; y � 2k

B

� �:

Note that the distance function derived here is a generaliza-tion of n.s.-distances ((a,b) = (1, 1)) and weighted distances(B = (2)). By setting B = (2), we have mink : k P xþmaxð0; y � 2k

BÞ ¼ mink : k P xþ 0 ¼ x. This gives da,b(0,(x,y); (2)) = (x � y) Æ a + y Æ b, which is consistent withthe formula presented in e.g. Arcelli and Sanniti di Baja(1988). Note that for weighted distances defined by largerneighbourhoods, a similar closed formula can be derived,see e.g. Sanniti di Baja and Thiel (1996).

3.1. Metricity of the distance function

In this section, we derive some conditions for the dis-tance function to be a metric. To prove Theorem 10 below,the following lemma is used.

Lemma 9. For any a,b such that 0 < a 6 b 6 2a, there is a

(a,b)-weighted minimal cost B-path P between the points

(x1,y1) and (x2,y2) such that

1P þ 2 � 2P ¼ jx2 � x1j þ jy2 � y1j:

Proof. From Lemmas 4 and 7, we know that there is a(a,b)-weighted minimal cost B-path between (0, 0) and(x,y) with x P y P 0 such that the only 1-steps that areused are (1, 0) and (0, 1) and the only 2-step that is usedis (1,1). For some a1; a2; a3 2 Z, a1(1,0) + a2(0,1) +a3(1,1) = (x,y). Thus

xþ y ¼ a1 þ a2 þ 2a3:

Since 1P ¼ a1 þ a2 and 2P ¼ a3, the lemma holds forx P y P 0. The lemma follows by translation invarianceand symmetry. h

Theorem 10. If

XN

i¼1

bðiÞ 6XjþN�1

i¼j

bðiÞ 8j; N P 1 and ð3Þ

0 < a 6 b 6 2a ð4Þ

then da,b(Æ, Æ; B) is a metric.

Proof. The positive definiteness and symmetry are trivial.We prove the triangular inequality. Let p; q; r 2 Z2 begiven. We will prove d(a,b)(p, r; B) 6 d(a,b)(p,q; B) +d(a,b)(q, r; B), i.e. that

1Paþ 2Pb 6 1Q1aþ 2Q1

bþ 1Q2aþ 2Q2

b: ð5ÞThe shortest B-path between p and r is denoted P, theshortest B-path between p and q is denoted Q1 and theshortest B-path between q and r is denoted Q2. In (Nagy,2003), it is proved that when (3) is fulfilled, then the n.s.-distance defined by B is metric. Therefore, d(1,1)(p, r; B) 6d(1,1)(p,q; B) + d(1,1)(q, r; B). Thus,

1P þ 2P 6 1Q1þ 2Q1

þ 1Q2þ 2Q2

: ð6ÞBy Lemma 9, it follows that:

1P þ 2 � 2P 6 1Q1þ 2 � 2Q1

þ 1Q2þ 2 � 2Q2

: ð7ÞWe consider three cases:

(i) 1P > 1Q1þ 1Q2

By (6) and since ab 6 1 by (4)

�2P þ 2Q1þ 2Q2

1P � 1Q1� 1Q2

P 1 Pab:

Rewriting this gives (5).

(ii) 1P ¼ 1Q1

þ 1Q2

By (6) and since b > 0 by (4), (5) is fulfilled.

(iii) 1P < 1Q1

þ 1Q2

By (7) and since 2ab P 1 by (4)

�2 � 2P þ 2 � 2Q1þ 2 � 2Q2

1P � 1Q1� 1Q2

6 1 62ab:

Rewriting this gives (5).

Thus, (5) holds for all possible cases, so d(a,b)(p, r; B) 6d(a,b)(p,q; B) + d(a,b)(q, r; B). h

4. Distance function in continuous space

For x P y P 0, the following distance function isconsidered:

da;bð0; ðx; yÞ; cÞ ¼ ð2k � x� yÞ � aþ ðxþ y � kÞ � b;where k ¼ min

k: k P xþmaxð0; y � ð1� cÞkÞ;

where x; y; k 2 R and c 2 R, 0 6 c 6 1 is the fraction of thesteps where 2-steps are not allowed (so 1k

B and 2kB corre-

sponds to ck and (1 � c)k, respectively). In this way weget a generalization of the distance function in discretespace Z2 valid for all points (x,y) where x P y P 0 in con-tinuous space R2. By considering

da;bð0; ðx; yÞ; cÞ ¼ r ð8Þthe points on a circle of constant radius are found.

Since y � (1 � c)k is decreasing with respect to k and0 6 x + max(0,y) (i.e. when k = 0), k = x + max(0,y � (1 � c)k) has a solution k 2 R, we can simplify andrewrite the expression for k;

da;bð0; ðx; yÞ; cÞ ¼ ð2k � x� yÞ � aþ ðxþ y � kÞ � b;where k ¼ x� ð1� cÞk þmaxðy; ð1� cÞkÞ:

Page 5: Weighted distances based on neighbourhood sequences

Fig. 2. Shapes of discs for a = 1 and some values of b and c. Top tobottom: b = 1, 1.25, 1.5, 1.75, 2. Left to right: c = 0, 0.25, 0.5, 0.75, 1.

Weighted distance n.s.-distance

a = 1 a = 1b ¼

ffiffiffi2p

b = 1c = 0 c ¼ 2�

ffiffiffi2p

E ¼ 8ðffiffi2p�1Þ � 1 � 0:0548 E ¼ 8ð

ffiffi2p�1Þ � 1 � 0:0548

R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036 2033

Thus, there are two cases:

(i) y P (1 � c)kk ¼ x� ð1� cÞk þ y ) k ¼ xþy

2�c.

p p

da;bð0; ðx; yÞ; cÞ ¼ ð1� cÞk � bþ ðk � ð1� cÞkÞ � a

¼ ð1� cÞ xþ y2� c

� bþ cxþ y2� c

� a

and

(ii) y 6 (1 � c)kk ¼ x� ð1� cÞk þ ð1� cÞk ) k ¼ x

da;bð0; ðx; yÞ; cÞ ¼ y � bþ ðx� yÞ � a;

Together with (8), this describes the portion of a poly-gon satisfying x P y P 0. By using symmetry, the entirepolygon is described. This polygon is a circle with the pro-posed distance function with vertices (up to permutation ofthe coordinates)

�r1

bð1� cÞ þ ac;�r

1� cbð1� cÞ þ ac

� �and � r

a; 0

� �:

In Fig. 2, the shape of the polygon given by radius 1 isshown for a = 1 and some values of b and c.

Fig. 3. The optimal shapes of discs using weighted distance (a), n.s.-distance (b), and weighted n.s.-distance (c).

5. Optimization

Using the vertices of the discs with the proposed dis-tance function, we can derive the area (A) and perimeter(P) of the discs. This is used in the following error function:

E ¼ P 2

A1

4p� 1;

where P and A is the perimeter and area of a disc, respec-tively. The closed formula of E for arbitrary a, b, c islengthy and is therefore omitted. Using this error function,often denoted the compactness ratio, the optimal values ofb and c are computed for a = 1. The error function, whichequals 0 only for Euclidean discs, is differentiable for1 < b < 2, 0 < c < 1, so its minimum is derived by standardminimization techniques. It has its minimum value forweighted n.s.-distance with a = 1 for

b ¼ 3�ffiffiffi3p

;

c ¼ 1�ffiffiffi3p

3;

E ¼ 12ð3�ffiffiffi3pÞ

pð3þffiffiffi3pÞ� 1 � 0:0235:

This should be compared with the case with weighted dis-tance (c = 0) and n.s.-distance (b = 1). The optimal valuesare then

The shapes of the corresponding discs are shown inFig. 3.

6. Evaluation in discrete space

The value of c represents the fraction of the n.s. with 1:s.Since this value is irrational, approximations are consid-ered in applications. The approximations that are used inthis section are of increasing complexity. The aim is tohighlight the difference between the simple distances thatweighted n.s. distances build on compared to the generalcase of weighted n.s.-distances presented in this paperand also to compare with the Euclidean distance. We con-sider B1 = (1), B2 = (2), B3 = (1, 2), B4 = (1, 2,1,2,2) andthe n.s. B5 defined recursively as

Page 6: Weighted distances based on neighbourhood sequences

2034 R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036

b5ðk þ 1Þ ¼ if 1kB5< ck;

2 otherwise:

(

In this way, B5 will approximate the value of c as close aspossible and

limk!1

1kB5

k¼ c and lim

k!1

2kB5

k¼ 1� c:

The following distance functions are considered:

Distance function (Asymptotic)E

Name of thedistance function

d1(Æ, Æ) = d1,1(Æ, Æ ;B1) E � 0.2732 City-blockdistance

d2(Æ, Æ) = d1,1(Æ, Æ ;B2) E � 0.2732 Chessboarddistance

d3(Æ, Æ) = d3,4(Æ, Æ ;B2) E � 0.0610 3-4 weighteddistance

d4(Æ, Æ) = d1,1(Æ, Æ ;B3) E � 0.0601 Octagonaldistance

d5(Æ, Æ) = d2,3(Æ, Æ ;B3) E � 0.0603 Weightedn.s.-distance

d6(Æ, Æ) = d4,5(Æ, Æ ;B4) E � 0.0361 Weightedn.s.-distance

d7ð�; �Þ ¼ d1;3�ffiffi3p ð�; �; B5Þ E � 0.0235 Optimal weighted

n.s.-distanced8(Æ, Æ) = de(Æ, Æ) E = 0 Euclidean

distance

In Fig. 4, the constrained distance transform (cDT)from a single pixel is shown for some distance functions.Note that the limitation of using only one source pixel isnot used in (and thus not needed for) the algorithm forpath-based distance functions, but is used in the algorithmfor the Euclidean metric (Coeurjolly et al., 2004).

In Algorithm 1, pseudo-code for computing the cDT forweighted n.s.-distances is presented. The basic idea is toupdate pixels that are neighbours with a pixel in thewave-front. Similar algorithms are presented in (e.g. Ver-wer et al., 1989; Piper and Granum, 1987). The algorithmin the pseudo-code in Algorithm 1 is not optimized, butthe techniques outlined in (Verwer et al., 1989; Piper andGranum, 1987) can be used to get an optimized algorithm.When (a,b) 5 (1, 1), the algorithm, in addition to the dis-tance (the cost), must keep track of the length of the short-est path to the source pixels. The length is needed todetermine which neighbourhood that is allowed in eachstep. Note that only the object pixels S and not the set ofobstacle pixels are processed by the algorithm.

Algorithm 1. Computing cDT for weighted n.s. distancesInput: B, a, b, an object S � Z2, and source pixels s 2 S.Initialisation: Set DTcost(p) 0 for source pixels and

DTcost(p) 1 for remaining pixels. Set DTlength = DTcost.

For all source pixels p : push (p,DTcost(p), DTlength(p)) tothe list L of ordered triplets sorted by increasing DTcost(p).

while L is not emptyforall p in L with lowest DTcost(p)

Pop (p,DTcost(p), DTlength(p)) from Lforall q that are b(DTlength(p) + 1)-neighbourswith p:if DTcost(q) > DTcost(p) + wp�q

DTcost(q) DTcost(p) + wp�q

DTlength(q) DTlength(p) + 1Push (q,DTcost(q), DTlength(q)) to L

endif

endfor

endfor

endwhile

Comment: The following notation is used: wp�q is a if p, qare 1-neighbours and b if p, q are strict 2-neighbours.

In Fig. 4, Algorithm 1 is applied to compute the cDTusing d1–d7 defined above. The cDT for the Euclidean dis-tance d8 can be computed using e.g. the algorithm in(Coeurjolly et al., 2004).

7. Conclusions

A new distance function, obtained by combining n.s.-distances and weighted distances, has been presented. Therotational dependency is significantly reduced by using thisapproach compared to using only n.s. or weights to definethe distance function. The asymptotic shape of the digitaldisc is a 12-sided polygon. This polygon is regular and,thus, has minimal rotational dependency when using theoptimal weights and n.s. derived in this paper. For n.s.-dis-tances and weighted distances, the optimal asymptoticshape is a regular octagon. This is the reason why the samevalue of E is obtained for the n.s.-distances and weighteddistances. Observe that, when using the error function pro-posed in (Borgefors, 1986) for weighted distances, weightsdifferent from the ones derived here are obtained. The rea-son is that in (Borgefors, 1986), the optimization (minimi-zation of the absolute difference) is carried out on theregion x = C, x P y P 0. As a result of the non-circularregion, the optimal weights give an octagon that is not reg-ular. When a circular region is used, i.e. when the con-straint x2 + y2 = C2, x P y P 0 is used, the same weightsas those derived in this paper ðb ¼

ffiffiffi2p

aÞ are obtained(see Verwer, 1991).

The algorithm for computing the cDT presented in Sec-tion 6 for the proposed distance function is simple, fast,and easy to generalize to higher dimensions. This is notthe case when the Euclidean metric is used (Coeurjollyet al., 2004). As the experimental results in Fig. 4 suggest,the difference between the cDT when the Euclidean metricis used compared to the optimal weights and n.s. derived inSection 5 is small. Since the 3 · 3 neighbourhood is used,

Page 7: Weighted distances based on neighbourhood sequences

Fig. 4. Constrained DT on the object (the source pixel is marked by a cross in the upper left figure) for the distance functions defined in the text.

R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036 2035

obstacles of unit thickness can be used. This is not the casewhen larger neighbourhoods are used. If a 5 · 5 neighbour-hood is used, then the obstacles must be two pixels thick toavoid paths from passing through obstacles (Piper andGranum, 1987).

With the low rotational dependency derived in Section 5and verified in Section 6, it is likely that the proposeddistance function will be of value also in other applica-tions.

References

Arcelli, C., Sanniti di Baja, G., 1988. Finding local maxima in a pseudo-Euclidean distance transform. Comput. Vision Graphics Image Pro-cess. 43, 361–367.

Borgefors, G., 1984. Distance transformations in arbitrary dimensions.Comput. Vision Graphics Image Process. 27, 321–345.

Borgefors, G., 1986. Distance transformations in digital images. Comput.Vision Graphics Image Process. 34, 344–371.

Coeurjolly, D., Miguet, S., Tougne, L., 2004. 2D and 3D visibility indiscrete geometry: an application to discrete geodesic paths. PatternRecognition Lett. 25 (5), 561–570.

Das, P.P., 1992. Best simple octagonal distances in digital geometry. J.Approx. Theory 68, 155–174.

Das, P.P., Chakrabarti, P.P., 1987. Distance functions in digital geometry.Inform. Sci. 42, 113–136.

Das, P.P., Chatterji, B.N., 1990. Octagonal distances for digital pictures.Inform. Sci. 50, 123–150.

Das, P.P., Chakrabarti, P.P., Chatterji, B.N., 1987. Generalized distancesin digital geometry. Inform. Sci. 42, 51–67.

Fazekas, A., Hajdu, A., Hajdu, L., 2002. Lattice of generalizedneighbourhood sequences in nD and 1D. Publicat. Math. Debrecen60, 405–427.

Hajdu, A., Hajdu, L., 2004. Approximating the Euclidean distance usingnon-periodic neighbourhood sequences. Discr. Math 283, 101–111.

Hajdu, A., Nagy, B., 2002. Approximating the Euclidean circle usingneighbourhood sequences. In: Proc. 3rd Hungarian Conf. on ImageProcessing. Domaszek, pp. 260–271.

Montanari, U., 1968. A method for obtaining skeletons using a quasi-euclidean distance. J. ACM 15 (4), 600–624.

Nagy, B., 2003. Distance functions based on neighbourhood sequences.Publicat. Math. Debrecen 63 (3), 483–493.

Nagy, B., Strand, R., 2006. Approximating euclidean distance usingdistances based on neighbourhood sequences in non-standard three-dimensional grids. In: IWCIA, pp. 89–100.

Piper, J., Granum, E., 1987. Computing distance transformations inconvex and nonconvex domains. Pattern Recognition 20 (6), 599–615.

Page 8: Weighted distances based on neighbourhood sequences

2036 R. Strand / Pattern Recognition Letters 28 (2007) 2029–2036

Ragnemalm, I., 1993. The euclidean distance transform. Ph.D. thesis,Linkoping Studies in Science and Technology.

Rosenfeld, A., Pfaltz, J.L., 1966. Sequential operations in digital pictureprocessing. J. ACM 13 (4), 471–494.

Rosenfeld, A., Pfaltz, J.L., 1968. Distance functions on digital pictures.Pattern Recognition 1, 33–61.

Sanniti di Baja, G., Thiel, E., 1996. Skeletonization algorithm running onpath-based distance maps. Image Vision Comput. 14 (1), 47–57.

Strand, R., Nagy, B., 2007. Distances based on neighbourhood sequencesin non-standard three-dimensional grids. Discr. Appl. Math. 155 (4),548–557.

Verwer, B.J.H., 1991. Local distances for distance transformations intwo and three dimensions. Pattern Recognition Lett. 12 (11), 671–682.

Verwer, B.J.H., Verbeek, P.W., Dekker, S.T., 1989. An efficient uniformcost algorithm applied to distance transforms. IEEE Trans. PatternAnal. Machine Intell. 11 (4), 425–429.

Yamashita, M., Honda, N., 1984. Distance functions defined byvariable neighbourhood sequences. Pattern Recognition 17 (5), 509–513.

Yamashita, M., Ibaraki, T., 1986. Distances defined by neighbourhoodsequences. Pattern Recognition 19 (3), 237–246.