forked from haeyeoni/lidar_slam_evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bag2kittiformat.py
executable file
·48 lines (42 loc) · 1.93 KB
/
bag2kittiformat.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
import rosbag
import tf.transformations
import numpy as np
def quaternion_to_matrix(quaternion):
"""Convert a quaternion to a full 4x4 transformation matrix."""
return tf.transformations.quaternion_matrix(quaternion)
def read_rosbag(bag_file, topic):
"""Reads a ROS bag file and extracts pose data from the specified topic."""
bag = rosbag.Bag(bag_file, 'r')
poses = []
for _, msg, _ in bag.read_messages(topics=[topic]):
for pose_stamped in msg.poses:
position = pose_stamped.pose.position
orientation = pose_stamped.pose.orientation
quaternion = (orientation.x, orientation.y, orientation.z, orientation.w)
matrix = quaternion_to_matrix(quaternion)
matrix[0:3, 3] = [position.x, position.y, position.z] # Set the translation part
poses.append(matrix)
bag.close()
return poses
def compute_transforms(poses):
"""Compute transformations relative to the first frame."""
base_pose_inv = np.linalg.inv(poses[0])
transformations = [base_pose_inv @ pose for pose in poses]
return transformations
def save_to_kitti_format(transformations, output_file):
"""Save the transformation matrices to a file in KITTI format."""
with open(output_file, 'w') as f:
for trans in transformations:
# Only use the top 3 rows and first 4 columns for KITTI format
for row in trans[:3]:
f.write(' '.join(f'{v:.9f}' for v in row) + '\n')
def main(bag_file, topic, output_file):
poses = read_rosbag(bag_file, topic)
transformations = compute_transforms(poses)
save_to_kitti_format(transformations, output_file)
if __name__ == '__main__':
# Set the path to your ROS bag file, the relevant topic, and the desired output file.
bag_file = '/home/dronomy/Downloads/kiss_icp_path.bag'
topic = 'kiss_icp_path'
output_file = 'output_kitti_format.txt'
main(bag_file, topic, output_file)