-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathros_utilities.py
executable file
·74 lines (52 loc) · 2.26 KB
/
ros_utilities.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
"""This module contains utilities around ROS
"""
from datetime import datetime
import xml.etree.ElementTree as et
import numpy as np
def get_time_of_file(mtime):
"""Returns the datetime instance associated to an mtime from our ROS setup
the mtimes are usually encoded in the file names.
File names are encoded in mtime with nanoseconds precision"""
return datetime.fromtimestamp(mtime / 1000000000.0)
def get_extrinsic_param_from_file(path):
'''
Takes path of the xml file as input argument and returns the inverse of camera extrinsic parameters
in world homogeneous coordinates
'''
import tf
# parse the file and get the tree
tree = et.parse(path)
# get the root
root = tree.getroot()
# get the position of the camera (indices are hardcoded for this tree structure)
pos = np.array([float(root[1][0][0][0].text),
float(root[1][0][0][1].text),
float(root[1][0][0][2].text)])
# get the orientation of the camera in quaternion (indices are harcoded for this tree structure)
orient_quat = np.array([float(root[1][0][1][0].text),
float(root[1][0][1][1].text),
float(root[1][0][1][2].text),
float(root[1][0][1][3].text)])
# covariance matrix
cov_mat = np.loadtxt(root[1][1].text[1:-1].split(','))
cov_mat = cov_mat.reshape(6,6)
# import ipdb;ipdb.set_trace()
# convert quaternion to homogeneous rotation matrix (4X4)
orient = tf.transformations.quaternion_matrix(orient_quat)
ext = orient
# merge the rotation and translation
ext[0:3, -1] = pos
# return the inverse of this camera extrinsic matrix
return np.linalg.inv(ext), ext, cov_mat
def get_intrinsic_param_from_file(path):
'''
Takes the path of the xml file as argument and returns the camera intrinsic parameters
'''
# parse the file and get the tree
tree = et.parse(path)
# get the root
root = tree.getroot()
# get the intrinsic parameters (K from the ROS CameraInfo message) (indices are hardcoded for this tree structure)
split_txt = root[5].text[1:-1].split(',')
intrinsic = np.reshape(np.array([float(elem) for elem in split_txt]), [3, 3])
return intrinsic