forked from PRBonn/4d_plant_registration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
somSkeleton.py
208 lines (162 loc) · 6.46 KB
/
somSkeleton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import minisom
import numpy as np
import scipy.spatial as spatial
from scipy.sparse.csgraph import minimum_spanning_tree
from sklearn.neighbors import NearestNeighbors
import copy
def trainSom(data, x, y, epochs, verbose=False):
"""
trains a som given the input data
:param data: input, must be [Nx3]
:param x: number of nodes in the x direction
:param y: number of nodes in the y direction
:param epochs: number of training iterations
:param verbose: if True prints information during training
:return: result of the som training
"""
som = minisom.MiniSom(x, y, 3, sigma=0.5, learning_rate=0.5, random_seed=1)
som.random_weights_init(data)
som.train_random(data, epochs, verbose=verbose)
winmap = som.win_map(data)
return winmap
def somSkeleton(data, x=3, y=1, epochs=20000):
"""
trains a som and decodes the winning units into skeleton nodes
:param data: input, must be [Nx3]
:param x: number of nodes in the x direction
:param y: number of nodes in the y direction
:param epochs: number of training iterations
:return: the nodes that will form the skeleton
"""
winmap = trainSom(data, x, y, epochs)
skeleton = []
for w in winmap:
w_array = np.asarray(winmap[w])
w_mean = np.mean(w_array, axis=0)
skeleton.append(w_mean)
return skeleton
def computeSkelPoints(cluster, label):
"""
computes the number of nodes that will form the skeleton
:param cluster: input data
:param label: id of the class, 0 for stem, else leaf instances
:return: number of skeleton nodes in the given set
"""
if label == 0:
skel_points = len(cluster)//600
skel_points = 5 if skel_points < 5 else skel_points
skel_points = 50 if skel_points > 50 else skel_points
else:
skel_points = len(str(len(cluster))) - 1
skel_points = 2 if skel_points < 2 else skel_points
skel_points = 6 if skel_points > 6 else skel_points
return skel_points
def getSkeleton(organs):
"""
for each organ, returns the nodes of the skeleton
:param organs: the point cloud segmented into stem and leaves, each list in organs represents a class
:return: the nodes of the skeleton, each list in skeletons represents a class
"""
skeletons = []
for i, o in enumerate(organs):
if i == 0:
skel_points = computeSkelPoints(o, i)
skeleton = somSkeleton(o, x=skel_points, y=1)
skeletons.append(skeleton)
else:
skel_points = computeSkelPoints(o, i)
skeleton = somSkeleton(o, x=skel_points, y=1)
skeletons.append(skeleton)
return skeletons
# ------------------------------------------------------------------------------------------------------------------
def decodeAdjMat(adj_matrix, candidates):
"""
from adjacency matrix to list of edges
:param adj_matrix: adjacency matrix encoding the skeleton-like graph
:param candidates: xyz-coordinates of the nodes in the skeleton
:return: a list of edges, representing the skeleton-like graph
"""
refined_edges = []
n = len(candidates)
for i in range(n):
for j in range(n):
if adj_matrix[i, j] != 0:
refined_edges.append([candidates[i], candidates[j]])
return refined_edges
def connect2Stem(leaf, stem):
"""
given a leaf and the stem, finds the closest stem point to any of the leaf point
:param leaf: list of points of the current leaf instance
:param stem: list of points classified as stem
:return: the closest stem point to any of the leaf point
"""
candidates = []
for i, l in enumerate(leaf):
distances = [np.linalg.norm((l - s)) for s in stem]
candidates.append(distances)
stem_candidate = np.where(candidates == np.min(candidates))
idx = stem_candidate[1][0]
return stem[idx]
def buildLeafGraph(leaf):
"""
computes the edges, given the nodes in a leaf
:param leaf: list of nodes of the current leaf instance
:return: a list of edges, representing the skeleton-like graph
"""
adj_matrix = np.zeros((len(leaf), len(leaf)))
for i, p_i in enumerate(leaf):
for j, p_j in enumerate(leaf):
adj_matrix[i, j] = np.linalg.norm((p_j-p_i))
spanning_tree = minimum_spanning_tree(adj_matrix)
adj_matrix = spanning_tree.toarray()
edges = decodeAdjMat(adj_matrix, leaf)
return np.asarray(edges)
def buildStemGraph(stem, points):
"""
computes the edges, given the nodes of the stem
:param stem: list of nodes of the stem
:param points: all the points classified as stem
:return: a list of edges, representing the skeleton-like graph
"""
edges = []
adj_matrix = np.zeros((len(stem), len(stem)))
kdt = spatial.cKDTree(points)
nbrs = NearestNeighbors(n_neighbors=5, algorithm='ball_tree').fit(stem)
for i, s in enumerate(stem):
distances, indices = nbrs.kneighbors([s])
for j in range(len(distances[0])):
if 0 < distances[0][j] < 30:
e = [s, stem[indices[0][j]]]
midpoint = np.mean(e, axis=0)
nbr = kdt.query_ball_point(midpoint, 10) # set to 10 for synthetic data
if len(nbr) > 0:
edges.append(e)
idx = indices[0][j]
adj_matrix[i, idx] = distances[0][j]
spanning_tree = minimum_spanning_tree(adj_matrix)
adj_matrix = spanning_tree.toarray()
edges = [] # decoding spanning tree
for i in range(len(stem)):
for j in range(len(stem)):
if adj_matrix[i, j] != 0:
edges.append([stem[i], stem[j]])
return np.asarray(edges)
def getGraph(skeletons, pcd):
"""
computes the edges for each organ
:param skeletons: the nodes of the skeleton, each list in skeletons represents a class
:param pcd: the complete plant point cloud
:return: a list of edges, representing the skeleton-like graph
"""
graph = []
for i, s in enumerate(skeletons):
if i == 0:
stem = buildStemGraph(s, pcd)
graph.append(np.asarray(stem))
else:
branch_node = connect2Stem(s, skeletons[0])
current_leaf = copy.deepcopy(s)
current_leaf.append(branch_node)
leaf = buildLeafGraph(current_leaf)
graph.append(np.asarray(leaf))
return graph