1 Introduction

Distance functions and their transforms (DTs, where each pixel is assigned the distance to the closest seed pixel) have been used extensively in image processing applications. Since Rosenfeld and Pfaltz defined distance transforms on the binary images in the 1960’s [16, 17], a huge number of distance transform methods have been developed in theoretical and application setting. The classical, binary DTs are useful, for example, for measurement and description of binary objects. Distance functions and transforms that are defined as minimal cost paths in general images include geodesic distance [9], fuzzy distances [19], and the minimax cost function; they have been used, for example, in segmentation and saliency detection [18]. The most common algorithms for computing distance transforms are: (i) raster scanning methods, where distance values are propagated by sequentially scanning the image in a pre-defined order [1, 5]; (ii) wave-front propagation methods, where distance values are propagated from low distance value points (the object border) to the points with higher distance values, using data structure containing previously visited points and their corresponding distance values, until all points have been visited [15, 21]; and (iii) separable algorithms, where one-dimensional subsets of the image are scanned separately, until all principal directions have been scanned [4, 20].

Recently, we introduced in [22] the minimal barrier distance (MBD) function, based on the minimal length of the interval of intensity values along a path between two points, see Fig. 1. The MBD differs from traditional distance functions in a number of aspects. For example, the length of a path may remain constant during its growth until a new stronger barrier is met on the path. This subtle shift in the notion of path length allows the new distance function to capture separation between two points in a “connectivity”-sense. Our experiments have shown that the MBD is stable to noise, seed point position [3, 10, 22, 23]. The MBD has many interesting theoretical properties, and has been shown to be a potentially useful tool in image processing [3, 7, 10, 22, 25].

Fig. 1.
figure 1

One-dimensional example of the minimum barrier. Left: The graph of a function (blue) and, for each x on the horizontal axis, the maximum and minimum value attained in [0, x] (red). Right: The pointwise difference between the maximum and minimum values – the minimum barrier. The minimum barrier distance between two points in an image is defined by a path with minimum barrier between the points. (Color figure online)

This paper collects and compares published results on the MBD and presents them in a unified framework. In Sect. 2, the MBD is defined on \(\mathbb {R}^n\) with the two equivalent formulations \(\rho \) (Eq. 2) and \(\varphi \) (Eq. 3). In Sect. 3, their discrete counterparts \(\widehat{\rho }\) (Eq. 4) and \(\widehat{\varphi }\) (Eq. 5) are defined in \(\mathbb {Z}^n\). Section 4 gives results on convergence between the different versions of MBD. The different MBD versions are related as follows:

figure a

The arrows indicate conceptual relations. The top row shows the continuous formulations and the bottom row the two different discrete versions.

Section 5 describes the algorithms for computation of DTs. The MBD based on \(\widehat{\rho }\) is, similar to [8], not smooth in the sense of [2, 6]. As a consequence, exact DTs for \(\widehat{\rho }\) cannot be computed as efficiently as for \(\widehat{\varphi }\), but both approximate algorithms and efficient algorithms for exact computation have been developed. These are given in Sect. 5.1. Section 5.2 shows that DTs for \(\widehat{\varphi }\) can be efficiently computed by standard wave-front propagation techniques. Applications in image processing are presented in Sect. 6.

2 The Minimum Barrier Distance in \(\mathbb {R}^n\)

We consider bounded maps \(f:D \rightarrow \mathbb {R}\) and their graphs \(A=\left\{ \left( x, f(x) \right) :x\in D\right\} \). When \(f:D \rightarrow [0,1] \) the set A can be seen as a fuzzy subset of D with f being its membership function.

A path from p to q (where \(p,q\in D\) and \(D\subset \mathbb {R}^n\)) is any continuous function \(\pi :[0,1]\rightarrow D\) with \(p=\pi (0)\) and \(q=\pi (1)\). The symbol \(\Pi _{p,q}\) (without subscripts, when p and q are clear from the context) is used to denote the family of all such paths. We consider functions \(f:D\rightarrow \mathbb {R}\) such that

  • f is bounded;

  • f is continuous;

  • \(D\subset \mathbb {R}^n\) is path connected, that is, for every \(p,q\in D\) there exists a path \(\pi :[0,1]\rightarrow D\) from p to q.

The barrier of a path \(\pi :[0,1]\rightarrow D\) is the number

$$\begin{aligned} \tau (\pi )=\max _t f (\pi (t)) -\min _t f (\pi (t))=\max _{t_0 , t_1} \bigl ( f(\pi (t_1)) -f (\pi (t_0))\bigr ). \end{aligned}$$
(1)

The maxima and minima in Eq. 1 are attained due to the Extreme Value Theorem. The minimum barrier distance between \(p,q \in D\) is the number

$$\begin{aligned} \rho (p,q)=\inf _{\pi \in \Pi _{p,q}}\tau (\pi ). \end{aligned}$$
(2)

2.1 Metricity

Definition 1

A function \(d:D\times D \rightarrow [0,\infty )\) is a metric on a set D provided, for every \(x,y,z \in D\),

  1. (i)

    \(d(x,x)=0\) (identity);

  2. (ii)

    \(d(x,y)>0\) for all \(x\ne y\) (positivity);

  3. (iii)

    \(d(x,y) = d(y,x)\) (symmetry);

  4. (iv)

    \(d(x,z) \le d(x,y) + d(y,z)\) (triangle inequality).

It is easy to see that metricity property (ii) does not hold for the minimum barrier distance (take a constant function f for example). Metricity properties (i), (iii), and (iv) are obeyed, and the minimum barrier distance is therefore a pseudo-metric [22].

2.2 Alternative Formulation

Now, the mapping \(\varphi :D\times D\rightarrow [0,\infty )\) is defined by two separate paths, via the formula

$$\begin{aligned} \varphi (p,q)= \inf _{\pi _1 \in \Pi _{p,q}} \max _t f(\pi _1 (t))- \sup _{\pi _0 \in \Pi _{p,q}} \min _t f(\pi _0 (t)). \end{aligned}$$
(3)

In Theorem 1 below, we see that the mappings \(\varphi \) and \(\rho \) are identical under mild assumptions on the set D.

Recall, that a set \(D\subset \mathbb {R}^n\) is simply connected, provided it is path connected and for all \(p,q\in D\) the paths \(\pi _0,\pi _1\in \Pi _{p,q}\) are homotopic, that is, there exists a continuous function \(h:[0, 1]^2 \rightarrow D\), known as a homotopy between \(\pi _0\) and \(\pi _1\), such that \(h(\cdot , 0) = \pi _0 (\cdot )\), \( h(\cdot , 1) = \pi _1 (\cdot )\), and the maps \(h(0, \cdot )\), \(h(1, \cdot )\) are constant. Intuitively, the homotopy condition means that D has no holes.

Theorem 1

([22]). If \(D\subset \mathbb {R}^n\) is simply connected, then the mappings \(\rho \) and \(\varphi \) are equal, that is, \(\rho (p,q)=\varphi (p,q)\) for all \(p,q\in D\).

3 The Minimum Barrier Distance in \(\mathbb {Z}^n\)

In the digital setting, we consider the (bounded) functions \(\widehat{f}:\widehat{D}\rightarrow \mathbb {R}\), where the digital scene \(\widehat{D}\) is a finite subset of a digital space \(\langle \phi \mathbb {Z}^n , \alpha \rangle \), where \(\phi \) is a positive number and \(\alpha \) is an adjacency relation such that two points in \(\phi \mathbb {Z}^n\) are \(\alpha \)-adjacent provided that no coordinate differs by more than \(\phi \) and that the points differ in exactly one coordinate. Note that this is equivalent to the standard 6-adjacency [11] in a 3D digital space.

A digital path in a subset \(\widehat{D}\) of the space \(\langle \phi \mathbb {Z}^n, \alpha \rangle \) is any ordered sequence \(\widehat{\pi } = \langle \widehat{\pi }(0), \widehat{\pi }(1), \ldots , \widehat{\pi }(k)\rangle \) of points in \(\widehat{D}\) such that \(\widehat{\pi }(i)\) and \(\widehat{\pi }(i-1)\) are \(\alpha \)-adjacent for all \(i\in \{1,2, \ldots ,k\}\). If \(\widehat{\pi }(0)=p\) and \(\widehat{\pi }(k)=q\), we say that the path \(\widehat{\pi }\) is from p to q. For a fixed set \(\widehat{D}\), a family of all paths in \(\widehat{D}\) from p to q is denoted by \(\widehat{\Pi }_{p,q}\) (with the subscripts omitted when p and q are clear from the context). Note that the digital paths are denoted by \(\widehat{\pi }\), while the paths in the continuous space \(\mathbb {R}^n\) are denoted by \(\pi \).

In what follows, we assume that the digital scenes \(\widehat{D}\) are of the rectangular form \(\widehat{D}_{\phi } = D \cap \phi \mathbb {Z}^n\), where \(D=\{x\in \mathbb {R}^n :L_i \le x(i) \le U_i \}\) for some real numbers \(L_i , U_i\) such that \(L_i < U_i\) for all i. In particular, any two points in \(\widehat{D}\) are connected by a path.

In view of Theorem 1, there are two natural ways of defining the discrete minimum barrier distance for \(\widehat{f}:\widehat{D}\rightarrow \mathbb {R}\), the discretization of the formula for \(\rho (p,q)\) and of that for \(\varphi (p,q)\):

  • Discretization I

    $$\begin{aligned} \widehat{\rho }(p,q)= \min _{\widehat{\pi } \in \widehat{\Pi }_{p,q}} \left( \max _{ i} \left[ \widehat{f}(\widehat{\pi }(i)) \right] - \min _{ j} \left[ \widehat{f}(\widehat{\pi }(j)) \right] \right) , \end{aligned}$$
    (4)
  • Discretization II

    $$\begin{aligned} \widehat{\varphi } (p,q)= \min _{\widehat{\pi _1}\in \widehat{\Pi }_{p,q}} \max _{ i} \left[ \widehat{f}(\widehat{\pi }_1(i)) \right] - \max _{\widehat{\pi _0} \in \widehat{\Pi }_{p,q}} \min _{ j} \left[ \widehat{f}(\widehat{\pi }_0(j)) \right] . \end{aligned}$$
    (5)

We know from [22] that each of the functions \(\widehat{\rho }\) and \(\widehat{\varphi }\) is a pseudo-metric on \(\widehat{D}\) and that \(\widehat{\varphi }(p,q)\le \widehat{\rho }(p,q)\) for all \(p,q\in \widehat{D}\).

4 Convergence Properties

Next we will see, in Theorem 2, that if \(\widehat{f}:\widehat{D}_\phi \rightarrow \mathbb {R}\) is a discretization of a continuous function f defined on a rectangular region D, then, for a sufficiently small \(\phi \), the numbers \(\widehat{\varphi }(p,q)\) and \(\widehat{\rho }(p,q)\) well approximate \(\varphi (p,q)={\rho }(p,q)\).

Theorem 2

(Theorem 2 in [22]). Let D be a rectangular region in \(\mathbb {R}^n\) and \(f:D\rightarrow \mathbb {R}\) be continuous. Let \(\widehat{\rho }\) and \(\widehat{\varphi }\) be the discrete minimum barrier distance functions for the sampling \(\widehat{f}\) of f on \(\widehat{D}_{\phi }\), that is, with \(\widehat{f}(p)=f(p)\) for all \(p\in \widehat{D}_{\phi }\). Then, for every \(\varepsilon >0\) there exists a \(\phi _0>0\) such that for every \(\phi \in (0,\phi _0]\)

$$|\widehat{\rho } (p,q) -\rho (p,q)|<\varepsilon \text { and }|\widehat{\varphi }(p,q) -\varphi (p,q)|<\varepsilon \text { for all }p,q\in \widehat{D}_{\phi }.$$

More precisely, this holds for any \(\phi _0>0\) such that \(|f(x)-f(y)|<\varepsilon /4\) for any \(x,y\in D\) with \(\Vert x-y\Vert \le \phi _0\sqrt{n}/2\).

Since, by Theorem 2, the values \(\widehat{\rho } (p,q)\) and \(\widehat{\varphi }(p,q)\) converge, as \(\phi \rightarrow 0\), to \({\rho } (p,q)={\varphi }(p,q)\), we obtain the following corollary.

Corollary 1

(Corollary 1 in [22] and Theorem 1 in [3]). Let \(\widehat{D}_{\phi }\), \(\widehat{f}\), \(\widehat{\rho }\) and \(\widehat{\varphi }\) be as in Theorem 2. Then

$$\max _{p,q\in \widehat{D}_{\phi }} |\widehat{\rho } (p,q) - \widehat{\varphi }(p,q) | \rightarrow 0 \text { as }\phi \rightarrow 0.$$

More precisely, let \(\varepsilon =\max \left\{ |\widehat{f}(x)-\widehat{f}(y)|:x,y\in \widehat{D}_{\phi } \text{ are } (3^n-1)\text{-adjacent }\right\} \), with \(\widehat{f}\) as in Theorem 2. Then

$$0 \le \widehat{\rho } (p,q) - \widehat{\varphi }(p,q) \le 2\varepsilon \text { for all }p,q\in \widehat{D}_{\phi }.$$

5 Discrete Distance Transform Computation

Efficient distance transform computation is crucial for most applications of distance transforms. As described in the introduction, many different computation approaches have been proposed, including raster scanning and wave-front propagation. In image segmentation, it is natural to compute DTs from seed points in the background and in the object and then assign each point to a seed from which it has the minimal distance. In practice, this is often computed efficiently by propagating different labels from object and background seed points together with the distance values. In this way, the points get labeled during the DT computation, resulting in efficient computation of the labeling/segmentation. As described above, the MBD originally is formulated in the continuous space and offers two natural discretizations. This leads to two different problems to solve when developing methods for computing the DT, one for each discretization. Computing the DT of Discretization I turns out to be fairly easy, whereas computing it for Discretization II is an intricate problem.

5.1 Distance Transform Computation of Discretization I, \(\widehat{\rho }\)

Approximate Computation. By propagating the minimum barrier, i.e., the minimal interval of minimum and maximum value by a wave-front propagation approach using auxiliary data structures that hold the minimal and maximal attained values, an approximate distance transform can be computed [3, 22]. We call the algorithm the Dijkstra approximation algorithm (Algorithm 1 in [3]). The minimum barrier distance based on \(\widehat{\rho }\) is not smooth in the sense of [2, 6] and, as a result, the obtained distance transforms is not error-free [3, 14, 22, 25] with this approach. However, Zhang et al. [25] gave an error bound and also showed that for a very restricted class of 2D images, the approach gives exact minimum barrier distance transforms, see Theorem 3 which is here adapted to the Dijkstra approximation algorithm.

Theorem 3

[25] Let \(\widehat{f}\) be an integer-valued 2D image (\(n=2\)) on a rectangular domain \(\widehat{D}\) and let

$$\widehat{f'}=\left\lfloor \frac{\widehat{f}}{\epsilon } \right\rfloor \epsilon ,$$

where \(\epsilon =\max \left\{ |\widehat{f}(x)-\widehat{f}(y)|:x,y\in \widehat{D} \text{ are } (3^n-1)\text{-adjacent }\right\} \). If the set of seed points is an \(\alpha \)-connected set, then the absolute error in the distance map obtained by the Dijkstra approximation algorithm on \(\widehat{f'}\) is strictly less than \(\epsilon \).

The following Corollary holds since if \(\epsilon =1\) in Theorem 3, then the maximum absolute error is integer valued and strictly less than 1, that is, equal 0.

Corollary 2

Let \(\widehat{f}\) be an integer-valued 2D image (\(n=2\)) on a rectangular domain \(\widehat{D}\). If \(\max \left\{ |\widehat{f}(x)-\widehat{f}(y)|:x,y\in \widehat{D} \text{ are } (3^n-1)\text{-adjacent } \right\} =1\) (i.e., if \(\epsilon \) in Theorem 3 is 1) and if the set of seed points is an \(\alpha \)-connected set, then the absolute error in the distance map obtained by the Dijkstra approximation algorithm on \(\widehat{f}\) is error-free.

Exact Distance Transform Computation. Computing the exact discrete MBD efficiently for general images is far from trivial, but it is fairly easy to check if there exists a path between two points in a digital space within a given interval: threshold the image at the lower and upper limits of the interval and check if the two points are connected in the so-obtained connected regions. This approach gives a simple, but computationally very inefficient way to compute the minimum barrier distance transform: for a given seed point, compute for each interval the set of points it is connected to after the thresholding procedure explained above. The distance between the seed point and another given point is the minimum of all such intervals, for which they are connected.

A slightly more efficient approach is to loop over all possible lower bounds of the interval and compute a minimax transform from a given seed point. The minimum barrier distance is then obtained by a min-operator of the so-obtained distance maps.

This idea can be further optimized by sorting the priority queue in an efficient way. By popping points from the queue based on the upper limit of the attained barrier and propagating and pushing points to the queue based on the lower limit of the barrier while storing the minimum barrier attained at each point, it has been proved that an error-free algorithm can be obtained [3].

5.2 Distance Transform Computation of Discretization II, \(\widehat{\varphi }\)

The transform \(\widehat{\varphi }(p,\cdot )\) can be efficiently computed since the path cost functions \(\max _{ i} \left[ \widehat{f}(\widehat{\pi }_1(i)) \right] \) and \(\min _{ j} \left[ \widehat{f}(\widehat{\pi }_0(j)) \right] \) are smooth in the sense of [2, 6] and can therefore be computed by Dijkstra’s algorithm, where wave-front propagation is used to compute lowest cost paths by local propagation [3, 22].

6 The Minimum Barrier Distance in Image Processing Applications

Since color images are used in most applications, an important extension of the MBD is the vectorial MBD, where vectorial, i.e., multi-band images, are used as input. Different versions of the vectorial MBD were developed by Kårsnäs et al. [10] and applied to interactive color image segmentation, where images are segmented by manually placed seed points in the image background and in the object together with a label propagation approach to segment color images. Instead of considering the one-dimensional interval, Kårsnäs et al. consider the diameter and volume of the bounding box as well as the diameter of the (hyper-)volume of a convex hull in feature space (e.g. the RGB color space) as the basis for the path cost. This shift in the MBD cost function definition leads to additional problems in how the exact distance transform is computed. However, in [10], an approximate method based on Dijkstra-like wave-front propagation is used to compute the vectorial MBD.

The vectorial MBD on color images is used as a pre-processing step by Grand-Brochier et al. [7] in a comparison of different pre-processing step methods for image segmentation. They assume that the object is centered within the image and compute the DT from a single seed point in the center of the image. The pixels with low distance values are then assumed to be object pixels.

A slightly different, and more successful, approach based on a real-time implementation of the MBD by raster-scanning until convergence is presented by Zhang et al. [25]. They detect salient objects in images as follows. As initialization, the image border pixels are set to seed points. Using the assumption that the object does not touch the image border, the pixels with high distance values are those that belong to the object. They use a raster-scanning approach, where each row is scanned, first from upper left to lower right and the from lower right to upper left, until convergence, which is very well suited for parallel implementations. By a GPU implementation based on the raster-scanning technique, real-time performance, 80 MBD DTs per second, is achieved.

6.1 Example Applications of Different Versions of the Minimum Barrier Distance

In this section, different MBD DTs with different sets of seed points are illustrated by DTs of a single image from the MSRA database ([13]). In Fig. 2, all border pixels of the image are set to seed points (c.f. [25]) and in Fig. 3, a single seed point is centered in the image (as used in [7]).

Fig. 2.
figure 2

Different versions of Minimum Barrier Distance computed on (a) when all border pixels are set to seed points. a: Original color image; b: Gray scale image; c: b smoothed; d: c quantized such that the condition in Corollary 2 holds; e: exact MBD DT, Discretization I, of b; f: Dijkstra approximation of MBD, Discretization I, of b; g: MBD, Discretization I of d (exact MBD computed by Dijkstra approximation); h: MBD DT, Discretization II, of b; i: Absolute pointwise difference between e and f (gray scale between 0 (no difference) and 28 (maximum difference)) j: Absolute pointwise difference between e and h (gray scale between 0 (no difference) and 99 (maximum difference)) k: Color MBD of a in the RGB color space. l: Color MBD of a in the Lab color space. (Color figure online)

In Fig. 2 and Fig. 3, some of the methods described in this paper are illustrated by applying them to a color image and its gray-scale version. The examples show:

  • how restrictive the conditions in Corollary 2 are in order to guarantee that the obtained MBD DT is error-free,

  • the exact MBD DT of Discretization I, \(\widehat{\rho }\) (Sect. 5.1),

  • the Dijkstra approximation of Discretization I, \(\widehat{\rho }\) (Sect. 5.1),

  • the exact MBD DT of Discretization II, \(\widehat{\varphi }\) (Sect. 5.2),

  • the color/vectorial MBD DT using the \(L_1\)-diameter of the bounding box in the RGB and Lab color spaces, see [10] for details (Sect. 6).

Fig. 3.
figure 3

Different versions of Minimum Barrier Distance computed on (a) when only the center pixel is set to seed point. a: Original color image; b: Gray scale image; c: b smoothed; d: c quantized such that the condition in Corollary 2 holds; e: exact MBD DT, Discretization I, of b; f: Dijkstra approximation of MBD, Discretization I, of b; g: MBD, Discretization I of d (exact MBD computed by Dijkstra approximation); h: MBD DT, Discretization II, of b; i: Absolute pointwise difference between e and f (gray scale between 0 (no difference) and 38 (maximum difference)) j: Absolute pointwise difference between e and h (gray scale between 0 (no difference) and 62 (maximum difference)) k: Color MBD of a in the RGB color space. l: Color MBD of a in the Lab color space. (Color figure online)

7 Discussion

The basic idea behind the MBD is very easy to explain and MBD is straight-forward to define, also for color images. Still, the theory of MBD is surprisingly intricate and holds many interesting and to some extent surprising results, such as the equivalence between the two formulations \(\rho \) and \(\varphi \). Efficient algorithms for DT computation, together with the algorithm for exact computation, makes MBD easy to apply in real-life applications. Even though the methods presented and illustrated here are rather simple applications of MBD, it is a promising tool for image processing such as segmentation and saliency detection. A more complex method based on a minimum spanning tree representation together with the MBD for saliency detection was recently presented in [24].

In \(\mathbb {R}^n\), we assume that we have continuous images \(f:D\rightarrow \mathbb {R}\), which we in practice do not have. However, most image acquisition methods induce smoothing of the image scene by a point spread function, leading to continuous f. See for example [12] for a discussion. A smooth f can also be found by interpolation of a digital function (often, just an image intensity function).

Open problems include properties and efficient algorithms for DT computation of the vectorial MBD, using other feature spaces than the RGB color space, error bound for the Dijkstra approximation method in arbitrary dimensions, and parallel implementation of the algorithm for exact DT computation.