-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_extraction.py
215 lines (170 loc) · 6.92 KB
/
graph_extraction.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
import numpy as np
import torch
from torch.utils.data import Dataset
import cv2
import math
import tcod
from sklearn.neighbors import KDTree
from skimage.draw import line
import networkx as nx
from graph_utils import nms_points
IMAGE_SIZE = 2048
SAMPLE_MARGIN = 64
def read_rgb_img(path):
bgr = cv2.imread(path)
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
return rgb
# returns (x, y)
def get_points_and_scores_from_mask(mask, threshold):
rcs = np.column_stack(np.where(mask > threshold))
xys = rcs[:, ::-1]
scores = mask[mask > threshold]
return xys, scores
def draw_points_on_image(image, points, radius):
"""
Draws points on a square image using OpenCV.
Parameters:
- size: The size of the square image (width and height) in pixels.
- points: A list of tuples, where each tuple represents the (x, y) coordinates of a point in pixel coordinates.
- radius: The radius of the circles to be drawn for each point, in pixels.
Returns:
- A square image with the given points drawn as filled circles.
"""
# Iterate through the list of points
for point in points:
cv2.circle(image, point, radius, (0, 255, 0), -1)
return image
def draw_points_on_grayscale_image(image, points, radius):
"""
Draws points on a square image using OpenCV.
Parameters:
- size: The size of the square image (width and height) in pixels.
- points: A list of tuples, where each tuple represents the (x, y) coordinates of a point in pixel coordinates.
- radius: The radius of the circles to be drawn for each point, in pixels.
Returns:
- A square image with the given points drawn as filled circles.
"""
# Iterate through the list of points
for point in points:
cv2.circle(image, point, radius, 255, -1)
return image
# takes xy
def is_connected_bresenham(cost, start, end):
c0, r0 = start
c1, r1 = end
rr, cc = line(r0, c0, r1, c1)
kp_block_radius = 4
cv2.circle(cost, start, kp_block_radius, 0, -1)
cv2.circle(cost, end, kp_block_radius, 0, -1)
# mean_cost = np.mean(cost[rr, cc])
max_cost = np.max(cost[rr, cc])
cv2.circle(cost, start, kp_block_radius, 255, -1)
cv2.circle(cost, end, kp_block_radius, 255, -1)
return max_cost < 255
def is_connected_astar(pathfinder, cost, start, end, max_path_len):
# we can still modify the cost matrix after creating the pathfinder with it
# seems pathfinder uses reference
c0, r0 = start
c1, r1 = end
kp_block_radius = 6
cv2.circle(cost, start, kp_block_radius, 1, -1)
cv2.circle(cost, end, kp_block_radius, 1, -1)
path = pathfinder.get_path(r0, c0, r1, c1)
connected = (len(path) != 0) and (len(path) < max_path_len)
cv2.circle(cost, start, kp_block_radius, 0, -1)
cv2.circle(cost, end, kp_block_radius, 0, -1)
return connected
def create_cost_field(sample_pts, road_mask):
# road mask shall be uint8 normalized to 0-255
cost_field = np.zeros(road_mask.shape, dtype=np.uint8)
kp_block_radius = 4
for point in sample_pts:
cv2.circle(cost_field, point, kp_block_radius, 255, -1)
cost_field = np.maximum(cost_field, 255 - road_mask)
return cost_field
def create_cost_field_astar(sample_pts, road_mask, block_threshold=200):
# road mask shall be uint8 normalized to 0-255
# for tcod, 0 is blocked
cost_field = np.zeros(road_mask.shape, dtype=np.uint8)
kp_block_radius = 6
for point in sample_pts:
cv2.circle(cost_field, point, kp_block_radius, 255, -1)
cost_field = np.maximum(cost_field, 255 - road_mask)
cost_field[cost_field == 0] = 1
cost_field[cost_field > block_threshold] = 0
return cost_field
def extract_graph_points(keypoint_mask, road_mask, config):
kp_candidates, kp_scores = get_points_and_scores_from_mask(keypoint_mask, config.ITSC_THRESHOLD * 255)
kps_0 = nms_points(kp_candidates, kp_scores, config.ITSC_NMS_RADIUS)
kp_candidates, kp_scores = get_points_and_scores_from_mask(road_mask, config.ROAD_THRESHOLD * 255)
kps_1 = nms_points(kp_candidates, kp_scores, config.ROAD_NMS_RADIUS)
# prioritize intersection points
kp_candidates = np.concatenate([kps_0, kps_1], axis=0)
kp_scores = np.concatenate([np.ones((kps_0.shape[0])), np.zeros((kps_1.shape[0]))], axis=0)
kps = nms_points(kp_candidates, kp_scores, config.ROAD_NMS_RADIUS)
return kps
def extract_graph_astar(keypoint_mask, road_mask, config):
kps = extract_graph_points(keypoint_mask, road_mask, config)
# cost_field = create_cost_field(kps, road_mask)
cost_field = create_cost_field_astar(kps, road_mask)
viz_cost_field = np.array(cost_field)
viz_cost_field[viz_cost_field == 0] = 255
# cv2.imwrite('astar_cost_dbg.png', viz_cost_field)
pathfinder = tcod.path.AStar(cost_field)
tree = KDTree(kps)
graph = nx.Graph()
checked = set()
for p in kps:
# TODO: add radius to config
neighbor_indices = tree.query_radius(p[np.newaxis, :], r=config.NEIGHBOR_RADIUS)[0]
for n_idx in neighbor_indices:
n = kps[n_idx]
start, end = (int(p[0]), int(p[1])), (int(n[0]), int(n[1]))
if (start, end) in checked:
continue
# if is_connected_bresenham(cost_field, p, n):
if is_connected_astar(pathfinder, cost_field, p, n, max_path_len=config.NEIGHBOR_RADIUS):
graph.add_edge(start, end)
checked.add((start, end))
return graph
# takes xys
def visualize_image_and_graph(img, graph):
# Draw nodes as green squares
for node in graph.nodes():
x, y = node
cv2.rectangle(
img, (int(x) - 2, int(y) - 2), (int(x) + 2, int(y) + 2), (0, 255, 0), -1
)
# Draw edges as white lines
for start_node, end_node in graph.edges():
cv2.line(
img,
(int(start_node[0]), int(start_node[1])),
(int(end_node[0]), int(end_node[1])),
(255, 255, 255),
1,
)
return img
if __name__ == '__main__':
# cost = np.array(
# [[1, 0, 1],
# [0, 1, 0],
# [0, 0, 0]],
# dtype=np.int32
# )
# pathfinder = tcod.path.AStar(cost)
# print(pathfinder.get_path(0, 2, 0, 0))
# cost[1, 1] = 0
# print(pathfinder.get_path(0, 2, 0, 0))
# cost[1, 1] = 1
# print(pathfinder.get_path(0, 2, 0, 0))
rgb_pattern = './cityscale/20cities/region_{}_sat.png'
keypoint_mask_pattern = './cityscale/processed/keypoint_mask_{}.png'
road_mask_pattern = './cityscale/processed/road_mask_{}.png'
index = 0
rgb = read_rgb_img(rgb_pattern.format(index))
road_mask = cv2.imread(road_mask_pattern.format(index), cv2.IMREAD_GRAYSCALE)
keypoint_mask = cv2.imread(keypoint_mask_pattern.format(index), cv2.IMREAD_GRAYSCALE)
graph = extract_graph_astar(keypoint_mask, road_mask)
viz = visualize_image_and_graph(rgb, graph)
cv2.imwrite('test_graph_astar_blk6_r40_m40_inms.png', viz)