-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
181 lines (146 loc) · 4.94 KB
/
Copy pathutils.py
File metadata and controls
181 lines (146 loc) · 4.94 KB
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
import cv2
import numpy as np
import os
def read_img_gray(path, id):
return cv2.imread(get_path_img(path, id), 0)
def check_inside_image(pts1, pts2, shape):
hPts = np.where(pts2[:, 1] >= shape[0])
wPts = np.where(pts2[:, 0] >= shape[1])
outTrackPts = hPts[0].tolist() + wPts[0].tolist()
outDeletePts = list(set(outTrackPts))
if len(outDeletePts) > 0:
trackPoints1_KLT_L = np.delete(pts1, outDeletePts, axis=0)
trackPoints2_KLT_L = np.delete(pts2, outDeletePts, axis=0)
else:
trackPoints1_KLT_L = pts1
trackPoints2_KLT_L = pts2
return trackPoints1_KLT_L, trackPoints2_KLT_L
def double_check_inside_image(pts1, pts2, shape):
new_pts1 = []
new_pts2 = []
for feature_pt in zip(pts1, pts2):
if pt_within_img(feature_pt[0], shape) and pt_within_img(feature_pt[1], shape):
new_pts1.append(feature_pt[0])
new_pts2.append(feature_pt[1])
return np.array(new_pts1), np.array(new_pts2)
def pt_within_img(pt, shape):
return 0 <= pt[0] <= shape[1] and 0 <= pt[1] <= shape[0]
def get_path_img(img_file_path, id_img):
return os.path.join(img_file_path, str(id_img).zfill(6) + ".png")
def get_number_images(img_file_path):
return len(os.listdir(img_file_path))
def form_transformation(R, t):
T = np.eye(4, dtype=np.float64)
T[:3, :3] = R
T[:3, 3] = t
return T
def load_paths(data_dir, sequence_id):
pose_file_path = os.path.join(data_dir, "poses", str(sequence_id).zfill(2) + ".txt")
img_file_path = os.path.join(
data_dir, "sequences", str(sequence_id).zfill(2), "image_"
)
calib_file_path = os.path.join(
data_dir, "sequences", str(sequence_id).zfill(2), "calib.txt"
)
return pose_file_path, img_file_path, calib_file_path
def load_calib(filepath, camera_id):
parameters = {}
with open(filepath, "r") as f:
params = np.fromstring(
f.readlines()[camera_id].split(":")[1], dtype=np.float64, sep=" "
)
P = np.reshape(
params, (3, 4)
) # Projection matrix: Intrinsic + baselines with respect to reference camera
K = P[0:3, 0:3] # Intrinsic matrix
theta = 90
ku = 1
kv = 1
f = K[0][0] / ku
f = K[1][1] * np.sin(theta * np.pi / 180) / kv
u_0 = K[0][2]
v_0 = K[1][2]
parameters["focal_length"] = f
parameters["principal_point"] = (u_0, v_0)
parameters["proj_matrix"] = P
# print(parameters)
return parameters
def get_vect_from_pose(pose):
x = float(pose[3])
y = float(pose[7])
z = float(pose[11])
return np.array([[x], [y], [z]])
def show_image(img, shape=(1080, 720), img_name="fig", append=False):
img = cv2.resize(img, shape)
if append:
return img
cv2.imshow(img_name, img)
# cv2.imshow("fig", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def show_imgs(imgs):
# Display all images simultaneously
for idx, image in enumerate(imgs):
cv2.imshow(f"Image {idx+1}", image)
# Wait for any key to be pressed
cv2.waitKey(0)
# Close all OpenCV windows
cv2.destroyAllWindows()
class plotter:
def __init__(self, shape=(1440, 720), center=(100, 200)):
self.shape = shape
self.traj = np.zeros(shape=(shape[0] // 2, shape[1], 3))
self.center = center
def get_trajectory_step(self, true_x, true_z, draw_x, draw_z):
self.traj = cv2.circle(
self.traj,
(true_x + self.center[0], true_z + self.center[1]),
1,
list((0, 0, 255)),
4,
)
self.traj = cv2.circle(
self.traj,
(draw_x + self.center[0], draw_z + self.center[1]),
1,
list((0, 255, 0)),
4,
)
cv2.putText(
self.traj,
"Actual Position:",
(140, 90),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1,
)
cv2.putText(
self.traj, "Red", (270, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1
)
cv2.putText(
self.traj,
"Estimated Odometry Position:",
(30, 120),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1,
)
cv2.putText(
self.traj,
"Green",
(270, 120),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 255, 0),
1,
)
return np.asarray(self.traj, np.uint8)
def plot_step(self, current_trajectory, current_frame):
current_frame = cv2.resize(current_frame, (self.shape[1], self.shape[0] // 2))
if len(current_frame.shape) == 2: # show gray
current_frame = np.stack((current_frame,) * 3, axis=-1)
horizontal_concatenation = np.hstack([current_trajectory, current_frame])
cv2.imshow("Figure", horizontal_concatenation)
cv2.waitKey(1000 // 60)