-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskeletonize_utils.py
227 lines (178 loc) · 7.34 KB
/
skeletonize_utils.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import random
import cv2
import numpy as np
from skimage.morphology import skeletonize
import sknw
def convert_to_visual_binarymap(topdown_map):
"""Convert habitat map data to 255 binary map.
Converted map has 255 or 0 value for visualization
"""
if len(np.shape(topdown_map)) == 3 or np.max(topdown_map) > 2:
raise ValueError("Input must be the map from habitat.utils.visualization.maps directly")
_, binary_map = cv2.threshold(topdown_map, 0, 255, cv2.THRESH_BINARY)
return binary_map
def convert_to_binarymap(topdown_map):
"""Convert habitat map data to 255 binary map.
Converted map has 255 or 0 value for visualization
"""
if len(np.shape(topdown_map)) == 3 or np.max(topdown_map) > 2:
raise ValueError("Input must be the map from habitat.utils.visualization.maps directly")
_, binary_map = cv2.threshold(topdown_map, 0, 1, cv2.THRESH_BINARY)
return binary_map
def convert_to_topology(binary_map):
"""Convert binary image to topology."""
skeleton = skeletonize(binary_map).astype(np.uint8)
skeleton[skeleton > 0] = 255
graph = sknw.build_sknw(skeleton)
return skeleton, graph
def convert_to_dense_topology(binary_map):
"""Convert binary image to topology."""
skeleton = skeletonize(binary_map).astype(np.uint8)
skeleton[skeleton > 0] = 255
graph = sknw.build_sknw(skeleton)
temp_graph = graph.copy()
for (s, e) in temp_graph.edges():
initial_dense_node_idx = len(graph.nodes())
ps = graph[s][e]["pts"]
dense_node_idx_list = []
dense_node_idx_list.append(s)
for i, edge_point in enumerate(ps):
graph.add_node(initial_dense_node_idx + i)
graph.nodes()[initial_dense_node_idx + i]["o"] = edge_point
dense_node_idx_list.append(initial_dense_node_idx + i)
dense_node_idx_list.append(e)
for i, dense_node_idx in enumerate(dense_node_idx_list):
if i + 1 < len(dense_node_idx_list):
graph.add_edge(dense_node_idx, dense_node_idx_list[i + 1])
graph.remove_edge(s, e)
return skeleton, graph
def get_one_random_directed_adjacent_node(graph, node, previous_node):
"""Choose one node among adjacent nodes. Excluding previous node."""
adjacent_nodes = list(graph.adj[node])
next_node = None
error_code = 0
if len(adjacent_nodes) == 0: # isolated node
print("Agent is on isolated node")
error_code = 1
next_node = node
if len(adjacent_nodes) == 1: # end node
next_node = node
error_code = 0
if len(adjacent_nodes) >= 2: # node on edge or bifurcation
while next_node is None:
candidate = random.choice(adjacent_nodes)
if candidate == previous_node:
adjacent_nodes.remove(candidate)
else:
next_node = candidate
error_code = 0
return next_node, error_code
def display_graph(map_image, graph, window_name="graph", line_edge=False, node_only=False, wait_for_key=False):
"""Draw nodes and edges into map image."""
map_image = cv2.cvtColor(map_image, cv2.COLOR_GRAY2BGR)
node_points = np.array([graph.nodes()[i]["o"] for i in graph.nodes()])
for pnt in node_points:
cv2.circle(
img=map_image,
center=(int(pnt[1]), int(pnt[0])),
radius=1,
color=(0, 0, 255),
thickness=-1,
)
if node_only:
pass
else:
if line_edge:
for (s, e) in graph.edges():
cv2.line(
img=map_image,
pt1=(int(graph.nodes[s]["o"][1]), int(graph.nodes[s]["o"][0])),
pt2=(int(graph.nodes[e]["o"][1]), int(graph.nodes[e]["o"][0])),
color=(255, 0, 0),
thickness=1,
)
else:
for (s, e) in graph.edges():
for pnt in graph[s][e]["pts"]:
map_image[int(pnt[0])][int(pnt[1])] = (255, 0, 0)
if graph[s][e]["pts"] == []:
cv2.line(
img=map_image,
pt1=(int(graph.nodes[s]["o"][1]), int(graph.nodes[s]["o"][0])),
pt2=(int(graph.nodes[e]["o"][1]), int(graph.nodes[e]["o"][0])),
color=(255, 0, 0),
thickness=1,
)
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, 1152, 1152)
cv2.imshow(window_name, map_image)
if wait_for_key:
cv2.waitKey()
def visualize_path(map_image, graph, node_list, window_name="path", wait_for_key=False):
"""Draw nodes and edges into map image."""
map_image = cv2.cvtColor(map_image, cv2.COLOR_GRAY2BGR)
node_points = np.array([graph.nodes()[i]["o"] for i in node_list])
for pnt in node_points:
cv2.circle(
img=map_image,
center=(int(pnt[1]), int(pnt[0])),
radius=1,
color=(0, 255, 0),
thickness=-1,
)
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, 1152, 1152)
cv2.imshow(window_name, map_image)
if wait_for_key:
cv2.waitKey()
def generate_map_image(map_image, graph, node_only=False, line_edge=False):
"""Same code with display graph method. Only excluding cv2.imshow"""
map_image = cv2.cvtColor(map_image, cv2.COLOR_GRAY2BGR)
node_points = np.array([graph.nodes()[i]["o"] for i in graph.nodes()])
for pnt in node_points:
cv2.circle(
img=map_image,
center=(int(pnt[1]), int(pnt[0])),
radius=1,
color=(0, 0, 255),
thickness=-1,
)
if node_only:
pass
else:
if line_edge:
for (s, e) in graph.edges():
cv2.line(
img=map_image,
pt1=(int(graph.nodes[s]["o"][1]), int(graph.nodes[s]["o"][0])),
pt2=(int(graph.nodes[e]["o"][1]), int(graph.nodes[e]["o"][0])),
color=(255, 0, 0),
thickness=1,
)
else:
for (s, e) in graph.edges():
for pnt in graph[s][e]["pts"]:
map_image[int(pnt[0])][int(pnt[1])] = (255, 0, 0)
if graph[s][e]["pts"] == []:
cv2.line(
img=map_image,
pt1=(int(graph.nodes[s]["o"][1]), int(graph.nodes[s]["o"][0])),
pt2=(int(graph.nodes[e]["o"][1]), int(graph.nodes[e]["o"][0])),
color=(255, 0, 0),
thickness=1,
)
return map_image
def remove_isolated_area(topdown_map, removal_threshold=1000):
"""Remove isolated small area to avoid unnecessary graph node."""
contours, _ = cv2.findContours(topdown_map, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.contourArea(contour) < removal_threshold:
cv2.fillPoly(topdown_map, [contour], 0)
return topdown_map
def topdown_map_to_graph(topdown_map, is_remove_isolated):
"""Generate graph map from topdown map."""
if is_remove_isolated:
topdown_map = remove_isolated_area(topdown_map)
binary_map = convert_to_binarymap(topdown_map)
_, graph = convert_to_dense_topology(binary_map)
return graph