scikit-image
138 строк · 4.2 Кб
1import numpy as np
2import heapq
3
4
5def _revalidate_node_edges(rag, node, heap_list):
6"""Handles validation and invalidation of edges incident to a node.
7
8This function invalidates all existing edges incident on `node` and inserts
9new items in `heap_list` updated with the valid weights.
10
11rag : RAG
12The Region Adjacency Graph.
13node : int
14The id of the node whose incident edges are to be validated/invalidated
15.
16heap_list : list
17The list containing the existing heap of edges.
18"""
19# networkx updates data dictionary if edge exists
20# this would mean we have to reposition these edges in
21# heap if their weight is updated.
22# instead we invalidate them
23
24for nbr in rag.neighbors(node):
25data = rag[node][nbr]
26try:
27# invalidate edges incident on `dst`, they have new weights
28data['heap item'][3] = False
29_invalidate_edge(rag, node, nbr)
30except KeyError:
31# will handle the case where the edge did not exist in the existing
32# graph
33pass
34
35wt = data['weight']
36heap_item = [wt, node, nbr, True]
37data['heap item'] = heap_item
38heapq.heappush(heap_list, heap_item)
39
40
41def _rename_node(graph, node_id, copy_id):
42"""Rename `node_id` in `graph` to `copy_id`."""
43
44graph._add_node_silent(copy_id)
45graph.nodes[copy_id].update(graph.nodes[node_id])
46
47for nbr in graph.neighbors(node_id):
48wt = graph[node_id][nbr]['weight']
49graph.add_edge(nbr, copy_id, {'weight': wt})
50
51graph.remove_node(node_id)
52
53
54def _invalidate_edge(graph, n1, n2):
55"""Invalidates the edge (n1, n2) in the heap."""
56graph[n1][n2]['heap item'][3] = False
57
58
59def merge_hierarchical(
60labels, rag, thresh, rag_copy, in_place_merge, merge_func, weight_func
61):
62"""Perform hierarchical merging of a RAG.
63
64Greedily merges the most similar pair of nodes until no edges lower than
65`thresh` remain.
66
67Parameters
68----------
69labels : ndarray
70The array of labels.
71rag : RAG
72The Region Adjacency Graph.
73thresh : float
74Regions connected by an edge with weight smaller than `thresh` are
75merged.
76rag_copy : bool
77If set, the RAG copied before modifying.
78in_place_merge : bool
79If set, the nodes are merged in place. Otherwise, a new node is
80created for each merge..
81merge_func : callable
82This function is called before merging two nodes. For the RAG `graph`
83while merging `src` and `dst`, it is called as follows
84``merge_func(graph, src, dst)``.
85weight_func : callable
86The function to compute the new weights of the nodes adjacent to the
87merged node. This is directly supplied as the argument `weight_func`
88to `merge_nodes`.
89
90Returns
91-------
92out : ndarray
93The new labeled array.
94
95"""
96if rag_copy:
97rag = rag.copy()
98
99edge_heap = []
100for n1, n2, data in rag.edges(data=True):
101# Push a valid edge in the heap
102wt = data['weight']
103heap_item = [wt, n1, n2, True]
104heapq.heappush(edge_heap, heap_item)
105
106# Reference to the heap item in the graph
107data['heap item'] = heap_item
108
109while len(edge_heap) > 0 and edge_heap[0][0] < thresh:
110_, n1, n2, valid = heapq.heappop(edge_heap)
111
112# Ensure popped edge is valid, if not, the edge is discarded
113if valid:
114# Invalidate all neighbors of `src` before its deleted
115
116for nbr in rag.neighbors(n1):
117_invalidate_edge(rag, n1, nbr)
118
119for nbr in rag.neighbors(n2):
120_invalidate_edge(rag, n2, nbr)
121
122if not in_place_merge:
123next_id = rag.next_id()
124_rename_node(rag, n2, next_id)
125src, dst = n1, next_id
126else:
127src, dst = n1, n2
128
129merge_func(rag, src, dst)
130new_id = rag.merge_nodes(src, dst, weight_func)
131_revalidate_node_edges(rag, new_id, edge_heap)
132
133label_map = np.arange(labels.max() + 1)
134for ix, (n, d) in enumerate(rag.nodes(data=True)):
135for label in d['labels']:
136label_map[label] = ix
137
138return label_map[labels]
139