1 Introduction

Hierarchical clustering has a long history in data processing [11]. In image processing, it is called hierarchical image segmentation [32]. Given an initial partition, a hierarchy is created by successively merging neighbouring regions together. In practice, a common way to create the initial partition is often to use the watershed operator [4, 8, 23, 24], whether we are dealing with data [5] or images. When merging the regions (called catchment basins in this context) of an initial watershed segmentation in a sequence provided by a given total ordering (such as for instance [34]), we obtain a hierarchical watershed [3, 7, 22, 28]. The watershed operator is still widely used in today’s deep learning era as a pre-processing [13] or post-processing step [1], achieving state-of-the-art results.

In the framework of edge-weighted graphs, hierarchical watersheds benefit from many interesting theoretical and practical properties [6, 9]. In particular, they globally optimize a well-defined cost function, both for the original graph and for every partition of the hierarchy, and they can be computed by efficient algorithms such as the ones proposed in [6, 26], whose time complexity is the same as minimum spanning tree algorithms. In order to leverage from those properties in a practical context, we study in this paper the problem of recognizing hierarchical watersheds. More precisely, we aim to solve the following problem:

(P):

given a weighted graph and a hierarchy of partitions \({\mathcal {H}}\), determine if \({\mathcal {H}}\) is a hierarchical watershed of this graph.

Solving this problem has a strong potential impact on the practice. It can help us, for example, in designing an algorithm to transform any hierarchy of partitions into a hierarchical watershed [17].

The problem of recognizing hierarchical watersheds is related to the one studied in [2, 12]. In [2, 12], the authors search for a minimum set of markers which lead to a given watershed segmentation. In our case, given a hierarchy, we investigate if, for every partition \(\mathbf{P }\) of this hierarchy, there is a subset \({\mathcal {M}}\) of the set of the minima of the graph such that \(\mathbf{P }\) is the watershed segmentation for \({\mathcal {M}}\) (Sect. 2.3). In the affirmative case, the given hierarchy is a hierarchical watershed or at least a hierarchy composed of partitions of a hierarchical watershed, which we call a flattened hierarchical watershed (Sect. 5).

This article is an extension of the conference paper [16]. Our main contributions are the following: (1) a characterization of hierarchical watersheds in the framework of weighted graphs (Theorem 5); (2) an efficient algorithm to recognize hierarchical watersheds (Algorithm 1); (3) the study of the notion of flattened hierarchical watersheds and an algorithm to recognize such hierarchies (Algorithm 2); and (4) experimental results with the proposed algorithms applied to the combinations of hierarchical watersheds assessed in [14]. To ease the reading of the paper, proofs of the various properties and theorems are postponed to “Appendix”.

In Sect. 2, we present the basic notions for handling hierarchies with graphs. In Sect. 3, we formally state the problem of recognizing hierarchical watersheds and we present a characterization of hierarchical watersheds on arbitrary graphs. In Sect. 4, we present an efficient algorithm to recognize hierarchical watersheds. In Sect. 5, we introduce the notion of flattened hierarchical watersheds, which are hierarchies composed of a subset of the partitions of a hierarchical watershed. Then, we propose an algorithm that recognizes this type of hierarchy. In Sect. 6, we present some experimental results using the proposed algorithms. Finally, we conclude the paper, in particular, by providing a glimpse on a possible extension of this work in the design of a watersheding operator [17].

2 Background Notions

In this section, we first introduce hierarchies of partitions. Then, we review the definition of graphs, connected hierarchies and saliency maps. Subsequently, we define hierarchical watersheds.

2.1 Hierarchies of Partitions

Let V be a set. A partition (of V) is a set \(\mathbf{P }\) of non empty disjoint subsets of V whose union is V. Any element of a partition \(\mathbf{P }\) is called a region of \(\mathbf{P }\). Let \(\mathbf{P }_1\) and \(\mathbf{P }_2\) be two partitions. We say that \(\mathbf{P }_1\) is a refinement of \(\mathbf{P }_2\) if every element of \(\mathbf{P }_1\) is included in an element of \(\mathbf{P }_2\). A hierarchy (of partitions) is a sequence \({\mathcal {H}} =(\mathbf{P }_0, \ldots , \mathbf{P }_\ell )\) of partitions such that \(\mathbf{P }_{i-1}\) is a refinement of \(\mathbf{P }_{i}\), for any i in \(\{1, \ldots , \ell \}\) and such that \(\mathbf{P }_n = \{V\}\). Let \({\mathcal {H}}=(\mathbf{P }_0, \ldots , \mathbf{P }_{\ell })\) be a hierarchy of partitions. Any region of a partition \(\mathbf{P }\) of \({\mathcal {H}}\) is called a region of \({\mathcal {H}}\).

A hierarchy of partitions can be represented as a tree whose nodes correspond to regions, as shown in Fig. 1a. Given a hierarchy \({\mathcal {H}}\) and two regions X and Y of \({\mathcal {H}}\), we say that Xis a parent ofY(or thatYis a child ofX) if \(Y \subset X\) and X is minimal for this property, i.e. if there is a region Z such that \(Y \subseteq Z \subset X\), then we have \(Y=Z\). It can be seen that any region \(X\ne V\) of \({\mathcal {H}}\) has exactly one parent. For any region X such that \(X \ne V\), we write \(parent(X)=Y\) where Y is the unique parent of X. For any region R of \({\mathcal {H}}\), if R is not the parent of any region of \({\mathcal {H}}\), we say that Ris a leaf region (of\({\mathcal {H}}\)). Otherwise, we say that Ris a non-leaf region (of \({\mathcal {H}}\)).

In Fig. 1a, the regions of a hierarchy \({\mathcal {H}}\) are linked to their parents (and to their children) by straight lines.

Fig. 1
figure 1

a A representation of a hierarchy of partitions \({\mathcal {H}}=(\mathbf{P }_0, \mathbf{P }_1, \mathbf{P }_2, \mathbf{P }_3)\) on the set \(\{a,b,c,d,e,f,g,h\}\). b A weighted graph (Gw)

2.2 Graphs, Connected Hierarchies and Saliency Maps

A graph is a pair \(G=(V, E)\), where V is a finite set and E is a set of pairs of distinct elements of V, i.e. \(E \subseteq \{\{x,y\} \subseteq V \mid x \ne y\}\). Each element of V is called a vertex (of G), and each element of E is called an edge (of G). To simplify the notation, the set of vertices and edges of a graph G will be also denoted by V(G) and E(G), respectively.

Let \(G= (V,E)\) be a graph, and let X be a subset of V. A sequence \(\pi =( x_0, \ldots , x_n)\) of elements of X is a path (in X) from \(x_0\)to \(x_n\) if \(\{x_{i-1},x_i\}\) is an edge of G for any i in \(\{1, \ldots , n\}\). Given a path \(\pi =( x_0, \ldots , x_n)\), for any i in \(\{1, \ldots , n\}\), we say that \(u=\{x_{i-1},x_i\}\)is an edge in\(\pi \) and that uis in\(\pi \). The subset X of V is said to be connected if, for any x and y in X, there exists a path from x to y. The subset X is a connected component of G if X is connected and maximal. In the following, we denote by CC(G) the set of all connected components of G. This set CC(G) of all connected components of G is a partition of the set V.

Let \(G = (V,E)\) be a graph. A partition of Vis connected for G if each of its regions is connected, and a hierarchy on Vis connected (for G) if every one of its partitions is connected. For example, the hierarchy of Fig. 1a is connected for the graph of Fig. 1b.

Let G be a graph. If w is a map from the edge set of G to the set \(\mathbb {R}\) of real numbers, then the pair (Gw) is called an (edge) weighted graph. If (Gw) is a weighted graph, for any edge u of G, the value w(u) is called the weight of u(for w).

Important notation in the remaining part of this article, the symbol (Gw) denotes a weighted graph whose vertex set is connected. To shorten the notation, the vertex set of G is denoted by V and its edge set is denoted by E. Without loss of generality, we also assume that the range of w is included in the set \(\mathbb {E}\) of all integers from 0 to \(|E|-1\). (Otherwise, one could always consider an increasing one-to-one correspondence from the set \(\{w(u) \mid u \in E\}\) into the subset \(\{0,...,| \{w(u) \mid u \in E\}|-1\}\) of \(\mathbb {E}\).)

Let \(\lambda \) be any element in \(\mathbb {R}\). The \(\lambda \)-level set of (Gw) is the graph \((V,E_{\lambda }(G))\) such that \(E_{\lambda }(G)=\{u \in E(G)\ | \ w(u)\le \lambda \}\). The sequence

$$\begin{aligned} \mathcal {QFZ}(w) = (CC(G_{\lambda ,w}) \mid \lambda \in \mathbb {E}) \end{aligned}$$
(1)

where \(G_{\lambda ,w}\) is the \(\lambda \)-level set of (Gw), is a hierarchy called the quasi-flat zones (QFZ) hierarchy (of w) [6, 20, 25, 33].

As established in [9], a connected hierarchy can be equivalently treated by means of a weighted graph through the notion of a (contour) saliency map. Given a hierarchy \({\mathcal {H}} = (\mathbf{P }_0, \ldots , \mathbf{P }_\ell )\) which is connected for G, the saliency map of \({\mathcal {H}}\) is the map from E into \(\{0, \ldots , \ell \}\), denoted by \(\varPhi ({\mathcal {H}})\), such that, for any edge \(u=\{x, y\}\) in E, the value \(\varPhi ({\mathcal {H}})(u)\) is the lowest value i in \(\{0, \ldots , \ell \}\) such that x and y belong to a same region of \(\mathbf{P }_{i}\). It follows that any connected hierarchy has a unique saliency map. Moreover, any hierarchy \({\mathcal {H}}\) connected for G is precisely the quasi-flat zones hierarchy of its own saliency map: \({\mathcal {H}} = \mathcal {QFZ}(\varPhi ({\mathcal {H}}))\).

For instance, the map depicted in Fig. 1b is the saliency map of the hierarchy of Fig. 1a.

Saliency maps are closely related to the notion of ultrametric distances [1, 27]. Let \({\mathcal {H}}\) be a hierarchy on V. Let d be a map from \(V \times V\) into \(\mathbb {R}\) such that, for any pair (xy) of vertices in \(V \times V\), the value d(xy) is the greatest edge weight \(\lambda \) in a path \(\pi \) from x to y (resp. y to x) in \((G, \varPhi ({\mathcal {H}}))\) such that, for any other path \(\pi '\) from x to y (resp. y to x), the greatest edge weight in \(\pi '\) is greater than or equal to \(\lambda \). We can affirm that (Vd) is an ultrametric space. Moreover, for any two vertices x and y in V, by the definition of saliency maps, we may say that d(xy) is the lowest value \(\lambda \) such that x and y belong to a same region of the partition \(\mathbf{P }_\lambda \) of \({\mathcal {H}}\). Furthermore, if G is a complete graph, we can conclude that \((V,\varPhi ({\mathcal {H}}))\) is an ultrametric space.

2.3 Hierarchical Minimum Spanning Forests and Watersheds

The watershed segmentation, see, for example, [4, 8, 28], derives from the topographic notion of watershed lines and catchment basins. In [8], the authors formalize watersheds in the framework of (edge) weighted graphs and show the optimality of watersheds in the sense of minimum spanning forests. In this section, we present hierarchical watersheds following the definition of hierarchies of minimum spanning forests presented in [6, 7].

We say that the graph \(G=(V,E)\)is a forest if, for any edge u in E, the number of connected components of the graph \((V, E{\setminus } \{u\})\) is greater than the number of connected components of G. Given another graph \(G_s\), we say that \(G_s\)is a subgraph of G, denoted by \(G_s \sqsubseteq G\), if \(V(G_s)\) is a subset of V and if \(E(G_s)\) is a subset of E. Let \(G_1\) be a subgraph of G and let \(G_2\) be a subgraph of \(G_1\). The graph \(G_1\) is a minimum spanning forest (MSF) of Grooted in \(G_2\) if:

  1. 1.

    the graphs G and \(G_1\) have the same set of vertices, i.e.  \(V(G_1) = V\); and

  2. 2.

    each connected component of \(G_1\) includes exactly one connected component of \(G_2\); and

  3. 3.

    the sum of the weight of the edges of \(G_1\) is minimal among all subgraphs of G for which the above conditions 1 and 2 hold true.

Given a path \((x_0, \ldots , x_n)\) in G, we say that \(\pi \)is a cycle if \(x_0\) and \(x_n\) are equal and if the edges in \(\pi \) are pairwise distinct. A MSF of (Gw) rooted in a single vertex of G and which does not contain any cycles is a tree (connected forest) called a minimum spanning tree (MST) of (Gw).

Let k be a value in \(\mathbb {R}\). A connected subgraph \({G_s}\) of G is a (regional) minimum (of w) at level k if:

  1. 1.

    the set of edges \({E(G_s)}\) of \({G_s}\) is not empty; and

  2. 2.

    for any edge u in \({E(G_s)}\), the weight of u is equal to k; and

  3. 3.

    for any edge \(\{x,y\}\) in \(E{\setminus } {E(G_s)}\) such that \(| \{x,y\} \cap {V(G_s)} | \ge 1\), the weight of \(\{x,y\}\) is strictly greater than k.

Important notation in the remaining part of this article, we denote by n the number of minima of w. Every sequence of minima of w considered in this article is a sequence of n pairwise distinct minima of w and, therefore, for the sake of simplicity, we use the term sequence of minima ofw instead of sequence ofnpairwise distinct minima ofw.

Let \(\{G_1, \ldots , G_{\ell }\}\) be a set of graphs. We denote by \(\sqcup \{G_1, \ldots , G_{\ell }\}\) the graph \(( \cup \{V(G_j) \mid j \in \{1, \ldots , \ell \}\}, \cup \{E(G_j) \mid j \in \{1, \ldots , \ell \}\})\). In the following, we define hierarchical watersheds based on minimum spanning forests, as done in [6, 7].

Definition 1

(Hierarchical watershed [6, 7]) Let \({\mathcal {S}} = (M_1, \ldots , M_n)\) be a sequence of minima of w. Let \((G_0, \ldots , G_{n-1})\) be a sequence of subgraphs of G such that:

  1. 1.

    for any i in \(\{0, \ldots , n-1\}\), the graph \(G_i\) is a MSF of G rooted in \(\sqcup \{M_j \mid j \in \{i+1, \ldots , n\}\}\); and

  2. 2.

    for any i in \(\{1, \ldots , n-1\}\)\(G_{i-1}\) is a subgraph of \(G_i\).

The sequence \({\mathcal {T}} = (CC(G_0), \ldots , CC(G_{n-1}))\) is called a hierarchical watershed of (Gw) for \({\mathcal {S}}\). Given a hierarchy \({\mathcal {H}}\), we say that \({\mathcal {H}}\)is a hierarchical watershed of (Gw) if there exists a sequence \({\mathcal {S}}\) of minima of w such that \({\mathcal {H}}\) is a hierarchical watershed of (Gw) for \({\mathcal {S}}\).

For instance, let (Gw) and \({\mathcal {H}}\) be, respectively, the weighted graph and the hierarchy shown in Fig. 2a, b, respectively. We can see that \({\mathcal {H}}\) is the hierarchical watershed of (Gw) for the sequence (CABD) of minima of w.

Fig. 2
figure 2

a A weighted graph (Gw) with four minima delimited by the dashed lines. b The hierarchical watershed of (Gw) for the sequence (CABD) of minima of w. c The unique binary partition hierarchy \({\mathcal {B}}\) of (Gw)

3 Characterization of Hierarchical Watersheds

In this section, we solve the following recognition problem:

(P):

given a weighted graph (Gw) and a hierarchy of partitions \({\mathcal {H}}\), determine if \({\mathcal {H}}\) is a hierarchical watershed of (Gw).

A naive approach to solve Problem (P) is to test if there is a sequence \({\mathcal {S}}\) of minima of w such that \({\mathcal {H}}\) is the hierarchical watershed of (Gw) for \({\mathcal {S}}\). However, there exist n! sequences of minima of w, which leads to an algorithm of factorial time complexity.

To solve Problem (P) more efficiently, we propose in Sect. 3.2 a characterization of hierarchical watersheds (Lemma 4) based on the binary partition hierarchy by altitude ordering (Sect. 3.1) which, as stated in [6], is known to be closely related to hierarchical watersheds. Then, we present a sketch of the proof of Lemma 4 by linking one-side increasing maps to the notion of extinction values as defined in [6]. Based on our proposed characterization of hierarchical watersheds, we design an efficient algorithm (Algorithm 1) to solve Problem (P).

3.1 Binary Partition Hierarchies by Altitude Ordering

Binary partition trees [30] are widely used for hierarchical image representation. In this section, we describe the case where regions linked by the lowest edge weights are the first regions to be merged in the hierarchy [6]. As stated in [9], this particular case is deeply connected to single-linkage clustering.

Let \(\prec \) be a total ordering (on E), i.e. \(\prec \) is a binary relation that is transitive and trichotomous: for any u and v in E only one of the relations \(u \prec v\)\(v \prec u\) and \(v = u\) holds true. We say that \(\prec \)is an altitude ordering (on E) for w if, for any u and v in E such that \(w(u) < w(v)\), we have \(u \prec v\). Hence, given an altitude ordering \(\prec \) for w and given any two edges u and v such that \(w(u) = w(v)\), we can have either \(u \prec v\) or \(v \prec u\). Let \(\prec \) be an altitude ordering for w. Let k be any element in \(\{1,\ldots ,|E|\}\). We denote by \(u_k^{\prec }\) the k-th element of E with respect to \(\prec \). We set \(\mathbf{B }_0 = \{\{x\} \mid x \in V\}\). The k-partition of V(by the ordering \(\prec \)) is defined by \(\mathbf{B }_k = \{ \mathbf{B }_{k-1}^y \cup \mathbf{B }_{k-1}^x \} \cup (\mathbf{B }_{k-1} {\setminus } \{\mathbf{B }_{k-1}^x, \mathbf{B }_{k-1}^y\})\) where \(u_k^{\prec } = \{x,y\}\) and \(\mathbf{B }_{k-1}^x\) and \(\mathbf{B }_{k-1}^y\) are the regions of \(\mathbf{B }_{k-1}\) that contain x and y, respectively. The sequence \((\mathbf{B }_i \mid i=0\ or\ \mathbf{B }_i \ne \mathbf{B }_{i-1})\), denoted by \({\mathcal {B}}_\prec \), is a hierarchy on V called the binary partition hierarchy (by altitude ordering) of (Gw) by \(\prec \). We can observe that successive k-partitions can be equal. In such case, only one of the repeated partitions is in \({\mathcal {B}}_\prec \).

Let \({\mathcal {B}}\) be a hierarchy on V. We say that \({\mathcal {B}}\)is a binary partition hierarchy (by altitude ordering) of (Gw) if there is an altitude ordering \(\prec \) for w such that \({\mathcal {B}}\) is the binary partition hierarchy of (Gw) by \(\prec \).

Let \(\prec \) be an altitude ordering for w. We can associate any non-leaf region X of the binary partition hierarchy \({\mathcal {B}}_{\prec }\) of (Gw) by \(\prec \) to the lowest rank r such that \(\mathbf{B }_r\) contains X. This rank is called the rank of X. Let X be a non-leaf region of \({\mathcal {B}}_{\prec }\) and let r be the rank of X. The building edge of X is the r-th edge for \(\prec \). Given an edge u in E, if u is the building edge of a region of \({\mathcal {B}}_{\prec }\), we say that uis a building edge for \(\prec \). Given a building edge u for \(\prec \), we denote the region of \({\mathcal {B}}_{\prec }\) whose building edge is u by \(R_u\). The set of all building edges for \(\prec \) is denoted by \(E_{\prec }\).

Let (Gw) be the weighted graph illustrated in Fig. 2a and let \({\mathcal {B}}\) be the binary partition hierarchy of (Gw) illustrated in Fig. 2c. We can see that \({\mathcal {B}}\) is the binary partition hierarchy of (Gw) by the altitude ordering \(\prec \) such that \(\{a,b\} \prec \{c,d\} \prec \{e,f\} \prec \{g,h\} \prec \{a,c\} \prec \{e,g\} \prec \{c,e\}\). The building edge of each non-leaf region R of \({\mathcal {B}}\) is shown above the node that represents R.

Let \({\mathcal {B}}\) be a binary partition hierarchy of (Gw) and let X and Y be two distinct regions of \({\mathcal {B}}\). If the parent of X is equal to the parent of Y, we say that X is a sibling of Y, that Y is a sibling of X and that X and Y are siblings. It can be seen that any region \(R \ne V\) of \({\mathcal {B}}\) has exactly one sibling and we denote this unique sibling of R by sibling(R).

Important remark by abuse of terminology, when no confusion is possible, if M is a minimum of w, we call the set V(M) of vertices of M as a minimum of w.

As established in [26], given an altitude ordering \(\prec \) for w, the minima of w can be extracted from the binary partition hierarchy \({\mathcal {B}}_\prec \) as well as the watershed-cut edges for \(\prec \), whose definition is given bellow.

Definition 2

(Watershed-cut edge) Let \(\prec \) be an altitude ordering for w and let u be a building edge for \(\prec \). We say that uis a watershed-cut edge (of (Gw)) for \(\prec \) if each child of the region \(R_u\) of \({\mathcal {B}}_\prec \) includes at least one minimum of w.

3.2 Characterization of Hierarchical Watersheds

In [16], the authors propose a characterization of hierarchical watersheds in the following case: the given graph is a tree with pairwise distinct edge weights. In this section, we generalize the characterization of hierarchical watersheds introduced in [16] to arbitrary graphs. To ease the reading of this section, the proofs of the properties and theorems stated here are delayed to “Appendix”.

Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\). The supremum descendant value of Rfor fand \(\prec \) is the supremum edge weight among the building edges of the regions included in R\(\vee \{f(v)\ |\ v \in E_\prec , R_v \subseteq R\}\), where \(\vee \) maps any set of values into the supremum value in this set, and the supremum of an empty set is zero.

The next definition introduces the notion of one-side increasing map. As established later in Lemma 4, the notion of one-side increasing map is linked to the saliency maps of hierarchical watersheds.

Definition 3

(One-side increasing map) Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\). We say that f is one-side increasing for \(\prec \) if:

  1. 1.

    \(\{f(u) \mid u \in E_\prec \}=\{0, \ldots , n-1\}\);

  2. 2.

    for any edge u in \(E_\prec \), the weight f(u) is greater than zero if and only if u is a watershed-cut edge for \(\prec \); and

  3. 3.

    for any edge u in \(E_\prec \), there exists a child R of \(R_u\) such that f(u) is greater than or equal to the supremum descendant value of R for f and \(\prec \).

The next lemma, whose proof is given in “Appendix F”, states that hierarchical watersheds can be characterized as the hierarchies whose saliency maps are one-side increasing maps.

Lemma 4

Let \({\mathcal {H}}\) be a hierarchy on V. The hierarchy \({\mathcal {H}}\) is a hierarchical watershed of (Gw) if and only if there is an altitude ordering \(\prec \) for w such that the saliency map \(\varPhi ({\mathcal {H}})\) is one-side increasing for \(\prec \).

Let \({\mathcal {H}}\) be the hierarchy of Fig. 3a, let \(\varPhi ({\mathcal {H}})\) be the saliency map of \({\mathcal {H}}\) shown in Fig. 3b, and let \({\mathcal {B}}\) be the binary partition hierarchy of (Gw) (Fig. 2) shown in Fig. 3b. As the edges of G have pairwise distinct weights for w, we can conclude that \({\mathcal {B}}\) is the binary partition hierarchy of (Gw) by the unique altitude ordering \(\prec \) for w. We can verify that \(\varPhi ({\mathcal {H}})\) is one-side increasing for \(\prec \). By Lemma 4, we may affirm that \(\varPhi ({\mathcal {H}})\) is the saliency map of a hierarchical watershed of (Gw) and that, consequently, the hierarchy \({\mathcal {H}}\) is a hierarchical watershed of (Gw).

Let us now consider the hierarchy \({\mathcal {H}}'\) and the saliency map \(\varPhi ({\mathcal {H}}')\) of Fig. 3d, e, respectively. We can see that \(\varPhi ({\mathcal {H}}')\) is not one-side increasing for \(\prec \). Indeed, the weight \(\varPhi ({\mathcal {H}}')(\{c,e\})\) of the building edge of the region \(Y_7\) of \({\mathcal {B}}\) is 1, which is lower than both \(\vee \{\varPhi ({\mathcal {H}}')(v) \mid R_v \subseteq Y_5\}=2\) and \(\vee \{\varPhi ({\mathcal {H}}')(v) \mid R_v \subseteq Y_6\}=3\). Hence, the condition 3 of Definition 3 is not satisfied by \(\varPhi ({\mathcal {H}}')\). Thus, by Lemma 4, as \(\prec \) is the unique altitude ordering for w, we may deduce that \(\varPhi ({\mathcal {H}}')\) is not the saliency map of a hierarchical watershed of (Gw) and that \({\mathcal {H}}'\) is not a hierarchical watershed of (Gw).

Fig. 3
figure 3

a, d The hierarchies \({\mathcal {H}}\) and \({\mathcal {H}}'\), respectively. b, e The weighted graphs \((G,\varPhi ({\mathcal {H}}))\) and \((G,\varPhi ({\mathcal {H}}'))\), respectively. c, f The maps \(\varPhi ({\mathcal {H}})\) and \(\varPhi ({\mathcal {H}}')\) represented on the hierarchy \({\mathcal {B}}\) of Fig. 2c, where, for each edge u, the values \(\varPhi ({\mathcal {H}})(u)\) and \(\varPhi ({\mathcal {H}}')(u)\) are shown above the region \(R_u\) of \({\mathcal {B}}\)

In the case where (Gw) has pairwise distinct edge weights, there exists a unique altitude ordering for w. Hence, we can use Lemma 4 to verify that a given map f is the saliency map of a hierarchical watershed of (Gw) by simply checking if f is one-side increasing for the unique altitude ordering for w. Otherwise, let us consider that (Gw) has arbitrary edge weights. Thus, in order to test if a map f is the saliency map of a hierarchical watershed of (Gw), we need to test if there is an altitude ordering \(\prec \) for w such that f is one-side increasing for \(\prec \). In the worst case, there exist |E|! possible altitude orderings for w. Hence, the naive approach to verify that f is one-side increasing for an altitude ordering for w has a factorial time complexity, which is the same time complexity as the algorithm to verify that f is the saliency map of a hierarchical watershed for a sequence of minima of w. Actually, as we establish later in Theorem 5, it is sufficient to test if f is one-side increasing for a single altitude ordering for w, which is the key idea behind our efficient algorithm (Algorithm 1) to recognize hierarchical watersheds.

Let f and g be two maps from E into \(\mathbb {R}\). A lexicographic ordering for (fg) is a total ordering \(\prec \) on E such that, for any two edges u and v in E, we have \(u \prec v\) if \(f(u) < f(v)\) or if \(f(u) = f(v)\) and \(g(u) \le g(v)\). We can note that any lexicographic ordering for (fg) is an altitude ordering for f.

Theorem 5

Let \({\mathcal {H}}\) be a hierarchy on V and let \(\prec \) be a lexicographic ordering for \((w,{\varPhi ({\mathcal {H}})})\). The hierarchy \({\mathcal {H}}\) is a hierarchical watershed of (Gw) if and only if \(\varPhi ({\mathcal {H}})\) is one-side increasing for \(\prec \).

The following is a sketch of the proof of Theorem 5, whose formal proof is given in “Appendix B”. Given any hierarchy \({\mathcal {H}}\) and any altitude ordering \(\prec \) for w, we can obtain a lexicographic ordering for \((w, \varPhi ({\mathcal {H}}))\) by iteratively reordering the pairs of edges (uv) such that \(u \prec v\) and such that \(\varPhi ({\mathcal {H}})(u) \ge \varPhi ({\mathcal {H}})(v)\), similar to the sorting steps of the bubble sort algorithm. Let \(\prec '\) be an altitude ordering resulting from the reordering of two edges u and v such that \(u \prec v\) and such that \(\varPhi ({\mathcal {H}})(u) \ge \varPhi ({\mathcal {H}})(v)\). To prove Theorem 5, we prove that, if \(\varPhi ({\mathcal {H}})\) is one-side increasing for \(\prec \), then \(\varPhi ({\mathcal {H}})\) is also one-side increasing for \(\prec '\). For each reordering, we prove that the three statements of Definition 3 for \(\varPhi ({\mathcal {H}})\) to be one-side increasing still hold true in the following five cases: (a) neither u nor v is a building edge for \(\prec \) (Lemma 30); (b) both u and v are building edges for \(\prec \) and \(R_u \cap R_v = \emptyset \) (Lemma 31); (c) both u and v are building edges for \(\prec \) and \(R_u \subset R_v\) (Lemma 32); (d) only u is a building edge for \(\prec \) (Lemma 33); and (e) only v is a building edge for \(\prec \) (Lemma 34).

In the remaining of this section, we present the building blocks of the proof of Lemma 4. More precisely, we state the link between the notions of one-side increasing map, hierarchical watershed and the method to compute hierarchical watersheds introduced in [6, 26].

Let \(\prec \) be an altitude ordering for w and let \({\mathcal {S}}=(M_1, \ldots , M_n)\) be a sequence of minima of w. Let R be a region of the binary partition hierarchy \({\mathcal {B}}_{\prec }\) by \(\prec \). Using the terminology of [6], the extinction value of R (for \(\prec \)and \({\mathcal {S}}\)) is zero if there is no minimum of w included in R and, otherwise, it is the maximum value i in \(\{1, \ldots , n\}\) such that the minimum \(M_i\) is included in R. Let \(\epsilon \) be the map from the set of regions of \({\mathcal {B}}_{\prec }\) into \(\mathbb {R}\) such that, for any region R of \({\mathcal {B}}_{\prec }\), the value \(\epsilon (R)\) is the extinction value of R. We say that \(\epsilon \)is the extinction map for \(\prec \)and \({\mathcal {S}}\), that \(\epsilon \)is an extinction map for \(\prec \) and that \(\epsilon \)is an extinction map for \({\mathcal {S}}\). The following property, whose proof is detailed in “Appendix C”, characterizes extinction maps.

Property 6

Let \(\prec \) be an altitude ordering for w and let \(\epsilon \) be a map from the regions of \({\mathcal {B}}_{\prec }\) into \(\mathbb {R}\). The map \(\epsilon \) is an extinction map for \(\prec \) if and only if the following statements hold true:

  1. 1.

    \(\{\epsilon (R) \mid R\) is a region of \({\mathcal {B}}_{\prec }\} = \{0, \ldots , n \}\);

  2. 2.

    for any two distinct minima \(M_1\) and \(M_2\) of w, we have \(\epsilon (M_1) \ne \epsilon (M_2)\); and

  3. 3.

    for any region R of \({\mathcal {B}}_{\prec }\), we have that \(\epsilon (R)\) is equal to \(\vee \{\epsilon (M)\) such that M is a minimum of w included in \(R\}\).

We provide an example of an extinction map in Fig. 4. We can see that the map \(\epsilon \) is the extinction map for the unique altitude ordering for w (Fig. 2a) and for the sequence \({\mathcal {S}} = (B, A, D, C)\) of minima of w.

Fig. 4
figure 4

An extinction map \(\epsilon \) for the unique altitude ordering of (Gw) of Fig. 2a

The next property clarifies the relation between hierarchical watersheds and extinction maps. As established in [6], given a sequence \({\mathcal {S}}\) of minima of w, we can compute the saliency map of a hierarchical watershed for \({\mathcal {S}}\) by considering any extinction map for \({\mathcal {S}}\). As the edge weights of w are not necessarily pairwise distinct, given any sequence \({\mathcal {S}}\) of minima of w, there might be several distinct hierarchical watersheds of (Gw) for \({\mathcal {S}}\). Let \({\mathcal {S}}\) be a sequence of minima of w. As established in the following property, we can associated any hierarchical watershed \({\mathcal {H}}\) of (Gw) for \({\mathcal {S}}\) with an altitude ordering \(\prec \) for w such that, for any building edge u for \(\prec \), the weight \(\varPhi ({\mathcal {H}})(u)\) is obtained from the extinction map for \(\prec \) and \({\mathcal {S}}\).

Property 7

Let \({\mathcal {H}}\) be a hierarchy on V. The hierarchy \({\mathcal {H}}\) is a hierarchical watershed of (Gw) if and only if there exists an altitude ordering \(\prec \) for w and an extinction map \(\epsilon \) for \(\prec \) such that:

  1. 1.

    \((V,E_\prec )\) is a MST of \((G,\varPhi ({\mathcal {H}}))\); and

  2. 2.

    for any edge u in \(E_\prec \), the value \(\varPhi ({\mathcal {H}})(u)\) is equal to \(\min \{\epsilon (R)\) such that R is a child of \(R_u\}\).

The proof of Property 7 is detailed in “Appendix A”. The intuition of the forward implication of Lemma 4 can be obtained from the definition of hierarchical watersheds (Definition 1) and from Property 7. Let \({\mathcal {H}}\) be a hierarchical watershed of (Gw). By the definition of hierarchical watersheds, we can infer that \({\mathcal {H}}\) is a sequence \((\mathbf{P }_0, \ldots , \mathbf{P }_{n-1})\) of n partitions, and that only the vertices connected by watershed-cut edges are in distinct regions of the partition \(\mathbf{P }_0\) of \({\mathcal {H}}\). Hence, we can infer that the range of \(\varPhi ({\mathcal {H}})\) is the set \(\{0, \ldots , n-1\}\) and that only the watershed-cut edges have nonzero weights for \(\varPhi ({\mathcal {H}})\), which correspond to the conditions 1 and 2 of Definition 3 for \(\varPhi ({\mathcal {H}})\) to be one-side increasing for an altitude ordering for w. By the statement 3 of the property on extinction maps (Property 6), we can infer that any extinction map is increasing on the regions of a binary partition hierarchy of (Gw). By Property 7, there exist an altitude ordering \(\prec \) for w and an extinction map \(\epsilon \) for \(\prec \) such that, for any edge u in \(E_\prec \), the value \(\varPhi ({\mathcal {H}})(u)\) is equal to \(\min \{\epsilon (R)\) such that R is a child of \(R_u\}\). As \(\epsilon \) is increasing on the regions of \({\mathcal {B}}_\prec \), we may say that, for any edge u in \(E_\prec \), there is a child R of \(R_u\) such that \(\varPhi ({\mathcal {H}})(u)\) is greater than the weight of any building edge of the regions included in R. The latter statement corresponds to the condition 3 of Definition 3 for \(\varPhi ({\mathcal {H}})\) to be one-side increasing. The reader can refer to “Appendix F” for a formal and complete proof of the forward implication of Lemma 4.

In order to present the intuition behind the backward implication of Lemma 4, we introduce the notion of approximated extinction maps. To introduce approximated extinction maps, we first present the auxiliary notions of non-leaf ordering and dominant region.

Definition 8

(Non-leaf ordering) Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\). The non-leaf ordering for fand \(\prec \) is the total ordering \(\ll \) on the building edges for \(\prec \), such that, for any two building edges u and v for \(\prec \), we have \(u \ll v\) if either the descendant value of \(R_u\) (for f and \(\prec \)) is strictly lower than the descendant value of \(R_v\), or if the descendant values of \(R_u\) and \(R_v\) are equal and \(u \prec v\).

Definition 9

(Dominant region) Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\). Let \(\ll \) be the non-leaf ordering for f and \(\prec \). Let R be a non-leaf region of \({\mathcal {B}}_{\prec }\) different from V. Let u and v be the building edges of R and of the sibling of R, respectively. We say that Ris a dominant region for fand \(\prec \) if:

  1. 1.

    there is a minimum of w included in R; and

  2. 2.

    either:

    • \(v \ll u\); or

    • there is no minimum of w included in the sibling of R.

For instance, let (Gw) be the weighted graph shown in Fig. 2a, let \(\prec \) be the unique altitude ordering for w, let \({\mathcal {B}}\) be the binary partition hierarchy by \(\prec \) shown in Fig. 2c, and let \(\varPhi ({\mathcal {H}})\) be the map illustrated in Fig. 3b. Let \(\ll \) be the non-leaf ordering for \(\varPhi ({\mathcal {H}})\) and \(\prec \) such that \(\{a,b\} \ll \{c,d\} \ll \{e,f\} \ll \{g,h\} \ll \{a,c\} \ll \{c,e\} \ll \{e,g\}\). The dominant regions of \({\mathcal {B}}\) for \(\varPhi ({\mathcal {H}})\) and \(\prec \) are the regions BD and \(Y_6\).

Definition 10

(Approximated extinction map) Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\). The approximated extinction map for fand \(\prec \) is the map \(\xi \) from the set of regions of \({\mathcal {B}}_{\prec }\) into \(\mathbb {R}\) such that:

  1. 1.

    \(\xi (R) = k+1\) if R is the vertex set V of G, where k is the supremum descendant value of R for f and \(\prec \); and

  2. 2.

    \(\xi (R) = \xi (parent(R))\) if R is a dominant region for f and \(\prec \); and

  3. 3.

    \(\xi (R) = f(u)\), where u is the building edge of the parent of R, otherwise.

The next lemma establishes that the approximated extinction map of any one-side increasing map is indeed an extinction map.

Lemma 11

Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\) such that f is one-side increasing for \(\prec \). The approximated extinction map for f and \(\prec \) is an extinction map for \(\prec \).

For instance, let us consider the weighted graph (Gw) of Fig. 2a and its unique altitude ordering \(\prec \). We can verify that the extinction map \(\epsilon \) of Fig. 4 is precisely the approximated extinction map for \(\varPhi ({\mathcal {H}})\) (Fig. 3b) and \(\prec \).

The next lemma is the key result for establishing the backward implication of Lemma 4.

Lemma 12

Let \(\prec \) be an altitude ordering for w and let f be a map from E into \(\mathbb {R}\) such that f is one-side increasing for \(\prec \). Let \(\xi \) be the approximated extinction map for f and \(\prec \). Then, for any edge u in \(E_\prec \), we have:

$$\begin{aligned} f(u) = min\{\xi (R) \text { such that } R \text { is a child of } R_u\}. \end{aligned}$$

The proof of Lemmas 11 and 12 are presented in “Appendices D and E”, respectively. The backward implication of Lemma 4 is a consequence of Lemmas 11 and 12 and the backward implication of Property 7. Let \({\mathcal {H}}\) be a hierarchy and let \(\prec \) be an altitude ordering for w such that \(\varPhi ({\mathcal {H}})\) is one-side increasing for \(\prec \). Let \(\xi \) be the approximated extinction map for \(\varPhi ({\mathcal {H}})\) and \(\prec \). By Lemma 12, for any edge u in \(E_\prec \), we have \(\varPhi ({\mathcal {H}})(u) = min\{\xi (R)\) such that R is a child of \(R_u\}\). By Lemma 11, the map \(\xi \) is an extinction map for \(\prec \). Then, by the backward implication of Property 7, we conclude that \(\varPhi ({\mathcal {H}})\) is the saliency map of a hierarchical watershed of (Gw) and that \({\mathcal {H}}\) is a hierarchical watershed of (Gw).

Let (Gw) be the graph of Fig. 2a and let \(\varPhi ({\mathcal {H}})\) be the saliency map of Fig. 3b. Let \(\prec \) be the unique altitude ordering of w. As stated previously, \(\varPhi ({\mathcal {H}})\) is one-side increasing for \(\prec \). To illustrate Lemma 12, we can verify that the value \(\varPhi ({\mathcal {H}})(u)\) is equal to \(\min \{\epsilon (R)\) such that R is a child of \(R_u\}\) for any edge u in \(E_\prec \) where \(\epsilon \) (shown in Fig. 4) is the approximated extinction map for \(\varPhi ({\mathcal {H}})\) and \(\prec \).

4 Recognition Algorithm for Hierarchical Watersheds

In this section, we present an efficient algorithm to recognize hierarchical watersheds based on Theorem 5. Given any hierarchy \({\mathcal {H}}\) on V, to test if \({\mathcal {H}}\) is a hierarchical watershed of (Gw), it is sufficient to verify that the saliency map \(\varPhi ({\mathcal {H}})(u)\) of \({\mathcal {H}}\) is one-side increasing for a lexicographic ordering for (wf).

Algorithm 1 provides a description of our algorithm to recognize hierarchical watersheds. The inputs are a weighted graph ((VE), w) and a saliency map f of a hierarchy \({\mathcal {H}}\) on V. The first step of Algorithm 1 is to compute a lexicographic ordering \(\prec \) for (wf). Then, the binary partition hierarchy \({\mathcal {B}}\) by \(\prec \) and the set of building edges \(E_\prec \) for \(\prec \) are computed at lines 2–3 using the algorithm proposed in [26]. Subsequently, the minima of w and the watershed-cut edges for \(\prec \) are obtained at lines 4–5 using the method proposed in [26]: the number of minima included in each region of \({\mathcal {B}}\) is iteratively counted by browsing the regions of \({\mathcal {B}}\) from the leaves to the root. At lines 6-7, we compute the supremum descendant value for f and \(\prec \) of each region of \({\mathcal {B}}\). Finally, the last for loop (lines 8-13) verifies that the three conditions of Definition 3 for f to be one-side increasing for \(\prec \) hold true. The condition 1 of Definition 3 is verified by the two tests between lines 9 and 10. The conditions 2 and 3 of Definition 3 are verified by the tests at lines 11 and 13, respectively. If any of those three conditions is not satisfied, then the algorithm halts and returns false and, otherwise, it returns true.

figure e

Let us now analyse the time complexity of Algorithm 1. Given that the lexicographic ordering for (wf) can be obtained through the merging sort algorithm, the time complexity of this step is O(|E|log|E|). As established in [26], any binary partition hierarchy can be computed in quasi-linear time with respect to |E| provided that the edges in E are already sorted or can be sorted in linear time. More specifically, the time complexity to compute the binary partition hierarchy \({\mathcal {B}}\) is \(O(|E| \times \alpha (|V|))\), where \(\alpha \) is a slowly growing inverse of the single-valued Ackermann function. Having computed the binary partition hierarchy \({\mathcal {B}}\), the computation of the minima of w and of the watershed-cut edges for \(\prec \) can be performed in linear time with respect to |V| as stated in [26]. At lines \(6-7\), the supremum descendant values of the building edges for \(\prec \) are iteratively computed from the leaves to the root in linear time O(V). Finally, each instruction between lines 9 and 13 can be performed in constant time, which implies that the last for loop has a linear time complexity with respect to |V|. Therefore, the overall time complexity of Algorithm 1 is O(|E|log|E|).

We illustrate Algorithm 1 in Figs. 5 and 6. Let us first explain the example of Fig. 5. The inputs are the weighted graph (Gw) and the saliency map f of Fig. 5. We first obtain a lexicographic ordering \(\prec \) for (wf) such that \(\{a,b\} \prec \{c,d\} \prec \{e,f\} \prec \{g,h\} \prec \{i,j\} \prec \{a,c\} \prec \{g,i\} \prec \{c,e\} \prec \{d,f\} \prec \{e,g\} \prec \{b,d\} \prec \{f,h\} \prec \{h,j\}\). Then, we obtain the binary partition hierarchy \({\mathcal {B}}\) by \(\prec \), the minima of w (in red) and the four watershed-cut edges for \(\prec \) (underlined) illustrated in Fig. 5c. Subsequently, we compute the supremum descendant values (for f and \(\prec \)) illustrated in Fig. 5e. For each edge u of G, the supremum descendant value of u is the greatest value in the set \(\{f(v) \mid R_v \subseteq R_u\}\). We can verify that the range of f is \(\{0, 1, 2, 3, 4\}\) and that, among the building edges for \(\prec \), all (and only) the watershed-cut edges for \(\prec \) have nonzero weights in f. Therefore, the conditions 1 and 2 of Definition 3 for f to be one-side increasing for \(\prec \) hold true. Finally, we test the condition 3 of Definition 3. For each watershed-cut edge u of G, we test if f(u) is greater than the supremum descendant value of at least one child of \(R_u\). For the building edges of the regions \(Y_6\)\(Y_7\) and \(Y_8\) the condition 3 holds true, but this is not the case for the region \(Y_9\). Consequently, the map f is not one-side increasing for \(\prec \) and Algorithm 1 returns false.

We will now explain the example of Fig. 6. The inputs are the weighted graph (Gw) and the saliency map g of Fig. 6. We first obtain a lexicographic ordering \(\prec '\) for (wG) such that \(\{a,b\} \prec ' \{c,d\} \prec ' \{e,f\} \prec ' \{g,h\} \prec ' \{i,j\} \prec ' \{a,c\} \prec ' \{g,i\} \prec ' \{e,g\} \prec ' \{c,e\} \prec ' \{d,f\} \prec ' \{b,d\} \prec ' \{f,h\} \prec ' \{h,j\}\). Then, we obtain the binary partition hierarchy \({\mathcal {B}}\) by \(\prec '\), the minima of w (in red) and the four watershed-cut edges for \(\prec '\) (underlined) illustrated in Fig. 6c. Subsequently, we compute the supremum descendant values (for g and \(\prec \)). We can verify that the range of g is \(\{0, 1, 2, 3, 4\}\) and that, among the building edges for \(\prec '\), all (and only) the watershed-cut edges for \(\prec '\) have nonzero weights. Therefore, the conditions 1 and 2 of Definition 3 for g to be one-side increasing for \(\prec '\) hold true. Moreover, for each building edge u for \(\prec '\), the supremum descendant value of \(R_u\) is greater than the supremum descendant value of at least one of the children of \(R_u\). Hence, the condition 3 for g to be one-side increasing for \(\prec '\) also holds true. Therefore, the map g is one-side increasing for \(\prec '\) and Algorithm 1 returns true.

Fig. 5
figure 5

Illustration of Algorithm 1 applied to a saliency map which is not the saliency map of a hierarchical watershed of the input graph (Gw). Given the weighted graph (Gw) and the saliency map f, we test if f is the saliency map of a hierarchical watershed of (Gw). We first compute the lexicographic ordering \(\prec \) for (wf) such that \(\{a,b\} \prec \{c,d\} \prec \{e,f\} \prec \{g,h\} \prec \{i,j\} \prec \{a,c\} \prec \{g,i\} \prec \{c,e\} \prec \{d,f\} \prec \{e,g\} \prec \{b,d\} \prec \{f,h\} \prec \{h,j\}\). Then, we obtain the binary partition hierarchy \({\mathcal {B}}\) by \(\prec \), along with the minima of w (in red) and the watershed-cut edges for \(\prec \) (underlined). Subsequently, we obtain the supremum descendant values for g and \(\prec \). We may conclude that conditions 1 and 2 of Definition 3 hold true for f, but not the condition 3. Hence, f is not the saliency map of a hierarchical watershed of (Gw)) (Color figure online)

Fig. 6
figure 6

Illustration of Algorithm 1 applied to a saliency map which is the saliency map of a hierarchical watershed of the input graph (Gw). Given the map (Gw) and the saliency map g, we test if g is the saliency map of a hierarchical watershed of (Gw). We first compute the lexicographic ordering \(\prec '\) for (wg) such that \(\{a,b\} \prec ' \{c,d\} \prec ' \{e,f\} \prec ' \{g,h\} \prec ' \{i,j\} \prec ' \{a,c\} \prec ' \{g,i\} \prec ' \{e,g\} \prec ' \{c,e\} \prec ' \{d,f\} \prec ' \{b,d\} \prec ' \{f,h\} \prec ' \{h,j\}\). Then, we obtain the binary partition hierarchy \({\mathcal {B}}\) by \(\prec '\), along with the minima of w (in red) and the watershed-cut edges for \(\prec '\) (underlined). Subsequently, we obtain the supremum descendant values for f and \(\prec \). We may conclude that three statements of Definition 3 hold true for f. Hence, g is one-side increasing for \(\prec '\). By Theorem 5, the map g is the saliency map of a hierarchical watershed of (Gw)) (Color figure online)

5 Flattened Hierarchical Watersheds

In order to compute a hierarchical watershed of (Gw), a sequence of minima of w is often defined by extinction values [34]. When distinct minima of w have the same extinction value, the order between those minima is defined arbitrarily. Given a watershed segmentation of (Gw), we may say that a hierarchical watershed of (Gw) can be obtained by filtering, one by one, the regions of this segmentation. Now, let us consider a framework in which the minima with equal extinction values are treated in parallel. In this new framework, the regions of the watershed segmentation containing minima of w with equal extinction values are filtered out simultaneously. We can affirm that the resulting partitions of this framework are a subset of the partitions of a hierarchical watershed of (Gw), and hence a simplified or “flattened” hierarchical watershed.

Definition 13

(Flattening of hierarchies [15]) Let \({\mathcal {H}}\) and \({\mathcal {H}}'\) be two hierarchies on V such that any partition of \({\mathcal {H}}\) is a partition of \({\mathcal {H}}'\). We say that \({\mathcal {H}}\)is a flattening of\({\mathcal {H}}'\).

Let \({\mathcal {H}}\) and \(\mathcal {H'}\) be two hierarchies on V such that \({\mathcal {H}}\) is a flattening of \({\mathcal {H}}'\). If \({\mathcal {H}}'\) is a hierarchical watershed of (Gw), then we say that \({\mathcal {H}}\)is a flattened hierarchical watershed of (Gw).

In a hierarchical watershed \({\mathcal {H}}=(\mathbf{P }_0, \ldots , \mathbf{P }_{n-1})\) of (Gw), every partition \(\mathbf{P }_i\), for i in \(\{1, \ldots , n-1\}\), includes exactly one region R such that R is the union of two regions of the partition \(\mathbf{P }_{i-1}\). However, given a flattened hierarchical watershed \({\mathcal {H}}'\) of \({\mathcal {H}}\), there can be partitions of \({\mathcal {H}}'\) which includes regions that are the union of several regions of the previous partition in the sequence. Therefore, some subsets of partitions of \({\mathcal {H}}\) are “flattened” into a single partition of \({\mathcal {H}}'\). The ultimate flattening of any hierarchical watershed of (Gw) is the hierarchy \((\{V\})\) composed of a single partition in which all vertices belong to the same region.

We can see that the notion of flattened hierarchical watersheds, even though not formally defined previously, arise naturally in the context of marker-based watershed segmentation. It is noteworthy that, like a hierarchical watershed, all partitions of a flattened hierarchical watershed are optimal in the sense of minima spanning forests. Hence, based on our proposed characterization of hierarchical watersheds, we derive a characterization of flattened hierarchical watersheds.

The following property characterizes flattened hierarchical watersheds.

Property 14

Let \({\mathcal {H}}\) be a hierarchy on V. The hierarchy \({\mathcal {H}}\) is a flattened hierarchical watershed of (Gw) if and only if there is an altitude ordering \(\prec \) for w such that:

  1. 1.

    \((V, E_\prec )\) is a MST of \((G,\varPhi ({\mathcal {H}}))\); and

  2. 2.

    for any edge u in \(E_\prec \), if u is not a watershed-cut edge for \(\prec \), then \(\varPhi ({\mathcal {H}})(u)\) is zero; and

  3. 3.

    for any edge u in \(E_\prec \), there exists a child R of \(R_u\) such that f(u) is greater than or equal to the supremum descendant value of R for \(\varPhi ({\mathcal {H}})\) and \(\prec \).

We can remark the similarity between Property 14 and Lemma 4, which links hierarchical watersheds to the notion of one-side increasing maps. Let \({\mathcal {H}}\) be a hierarchy and let f be the saliency map of \({\mathcal {H}}\). To test if \({\mathcal {H}}\) is a flattened hierarchical watershed of (Gw), the first condition of Property 14, which is an implication of the first statement of Definition 3, makes sure that we take into account the range of f and not only a subset of the range of f. The second condition of Property 14, which is the forward implication of the second statement of Definition 3, guarantees that the lowest level of \({\mathcal {H}}\) is equal or coarser than the lowest level of a hierarchical watershed of (Gw). Finally, the third condition of Property 14 is equivalent to the third statement of Definition 3 and, allied to the second condition of Property 14, it ensures that each partition of \({\mathcal {H}}\) is induced by a MSF rooted in a subset of the set of minima of (Gw).

Algorithm 2 describes our algorithm to recognize flattened hierarchical watersheds, which is very similar to the algorithm to recognize hierarchical watersheds (Algorithm 1). The only difference between Algorithms 2 and 1 is that, in Algorithm 2, we do not test if the first condition of Definition 3 holds true, and we test if \((V, E_\prec )\) is a MST of the input map ((VE), f), where \(E_\prec \) is the set of building edges for \(\prec \). The verification that \((V, E_\prec )\) is a MST of (Gf) can be done in time O(|E|log|E|) by checking if the sum of the edge weights of a MST of ((VE), f) is equal to the sum of the edge weights of \(((V,E_\prec ),f)\). Hence, the overall time complexity of Algorithm 2 is the same of Algorithm 1: O(|E|log|E|).

figure f

6 Experimental Results

In this section, we present an immediate application of the recognition of (flattened) hierarchical watersheds on the combinations of hierarchical watersheds assessed in [14]. In [14], the authors showed that combining hierarchies is a good alternative method to outperform individual hierarchical watersheds, which raises the question of whether the resulting combinations are hierarchical watersheds or flattened hierarchical watersheds. This problem has already been tackled in our companion paper [18], where we study if, and which, combinations of hierarchical watersheds result in flattened hierarchical watersheds. In [18], we concluded that combinations of hierarchical watersheds are not hierarchical watersheds in general. However, when the input hierarchical watersheds are one-side increasing for the same altitude ordering, then their combination by infimum is a flattened hierarchical watershed. The experiments presented in this section reinforce those theoretical results.

Table 1 In each cell, we show the number of combinations of pairs of hierarchical watersheds by average (bold), by supremum (italic) and by infimum (bold italic) among 200 that are flattened hierarchical watersheds. In those results, we consider hierarchies obtained through the non-deterministic hierarchical watershed algorithm

We first present the set-up of our experiments. We consider hierarchical watersheds for sequences of minima ordered by their extinction values [34]. Such extinction values are based on the following attributes: area [21, 34], diagonal of bounding box [31], dynamics [22], (topological) height [31], number of descendants, number of minima, volume [34] and number of parent nodes [29]. In the combination of hierarchical watersheds, we consider the following functions: infimum, supremum and linear combination (average). The experiments were performed on the 200 images of the test set of the Berkeley Segmentation Dataset and Benchmark 500 [19].

As established in [18], combinations of hierarchical watersheds with the aforementioned combining functions are not hierarchical watersheds in general. Indeed, by applying Algorithm 1 to the combinations of hierarchical watersheds by infimum, supremum and average, we verified that the first condition of the definition of one-side increasing maps (Definition 3) is not satisfied by any combination. Hence, by Theorem 5, none of those combinations is a hierarchical watershed. In fact, combining hierarchies often act by simplifying the input hierarchies in the sense that, from a level i to a level \(i+1\) of the resulting combination, zero or more than one pair of regions are merged, which suggests that some combinations may result in flattened hierarchical watersheds.

To test how many combinations result in flattened hierarchical watersheds, we consider two hierarchical watershed algorithms:

  1. 1.

    Non-deterministic algorithm: when there are ties between edges of equal weights, an arbitrary choice is made. Using this algorithm, two hierarchical watersheds computed from the same graph are not necessarily one-side increasing for the same altitude ordering.

  2. 2.

    Deterministic algorithm: when there are ties between edges of equal weights, a deterministic choice is made. In this case, any two hierarchical watersheds computed from the same graph are one-side increasing for the same altitude ordering.

We applied Algorithm 2 to combinations of pairs of hierarchical watersheds which were computed using each of those algorithms. The results using the non-deterministic algorithm are shown in Table 1. In each cell of Table 1, we present the number of combinations by average, by supremum and by infimum (among 200) that are flattened hierarchical watersheds. We can observe that the majority of the combinations, nearly two-thirds, are flattened hierarchical watersheds.

Fig. 7
figure 7

a An image I. b The gradient G of I computed with the edge detector introduced in [10]. c From left to right: the saliency map of a circularity based hierarchy \({\mathcal {H}}_{cc}\), and three partitions of \({\mathcal {H}}_{cc}\) containing 20, 30 and 40 regions, respectively. d The watersheding \({\mathcal {H}}_w\) of the saliency map of \({\mathcal {H}}_{cc}\) (for Grad), and three partitions of \({\mathcal {H}}_w\) containing 20, 30 and 40 regions, respectively

We now consider the second algorithm, in which ties between edge weights are treated deterministically. In this case, all hierarchical watersheds of a given weighted graph are one-side increasing for the same altitude ordering. Consequently, any combination with infimum is a flattened hierarchical watershed, as established by Property 7 of [18]. By applying Algorithm 2 to combinations of hierarchies obtained through the deterministic algorithm, we observed that all combinations with infimum are flattened hierarchical watersheds, as expected. Interestingly, this was also the case for the combinations with average. Regarding the combinations with supremum, among all 5600 combinations, only one combination with volume and diagonal of bounding box, and three combinations with volume and height are not flattened hierarchical watersheds.

Our experimental results suggest that most of the combinations of hierarchical watersheds assessed in [14] are “approximations” of flattened hierarchical watersheds in the sense that, by swapping the weight of a few edges in the combinations of saliency maps, we could obtain a flattened hierarchical watershed. This speculative conclusion may be investigated in future research linking the results established here with our method to convert any hierarchy into a hierarchical watershed introduced in [17].

7 Conclusion and Perspectives

We believe that having a better understanding of hierarchical watersheds may help to develop new image processing tools. For example, this is the case of the watersheding operator introduced in [17], which converts any hierarchy into a hierarchical watershed and which is deeply connected to the characterization of hierarchical watersheds introduced in this article. In Fig. 7, we illustrate a result of this operator. We present an image, a gradient Grad of this image, and two hierarchies \({\mathcal {H}}_{cc}\) and \({\mathcal {H}}_w\) both computed from Grad. Each hierarchy is represented by its saliency map, in which the darkest boundaries represent the contours that persist at the highest levels of the hierarchy. The hierarchy \({\mathcal {H}}_{cc}\) highlights the circular regions of Grad and, using Algorithm 1, we verified that \({\mathcal {H}}_{cc}\) is not a hierarchical watershed of Grad. The hierarchy \({\mathcal {H}}_w\) is a hierarchical watershed of Grad and was obtained using the watersheding operator [17] applied to \({\mathcal {H}}_{cc}\). We can observe that both hierarchies include the most circular regions of the original image, but the hierarchical watershed \({\mathcal {H}}_w\) also brings to the fore the region covering the arm, which is a perceptually significant region highlighted by the gradient Grad.