associate.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2013, Juergen Sturm, TUM
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of TUM nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 # Requirements:
35 # sudo apt-get install python-argparse
36 
37 """
38 The Kinect provides the color and depth images in an un-synchronized way. This means that the set of time stamps from the color images do not intersect with those of the depth images. Therefore, we need some way of associating color images to depth images.
39 
40 For this purpose, you can use the ''associate.py'' script. It reads the time stamps from the rgb.txt file and the depth.txt file, and joins them by finding the best matches.
41 """
42 
43 import argparse
44 import sys
45 import os
46 import numpy
47 
48 
49 def read_file_list(filename):
50  """
51  Reads a trajectory from a text file.
52 
53  File format:
54  The file format is "stamp d1 d2 d3 ...", where stamp denotes the time stamp (to be matched)
55  and "d1 d2 d3.." is arbitary data (e.g., a 3D position and 3D orientation) associated to this timestamp.
56 
57  Input:
58  filename -- File name
59 
60  Output:
61  dict -- dictionary of (stamp,data) tuples
62 
63  """
64  file = open(filename)
65  data = file.read()
66  lines = data.replace(","," ").replace("\t"," ").split("\n")
67  list = [[v.strip() for v in line.split(" ") if v.strip()!=""] for line in lines if len(line)>0 and line[0]!="#"]
68  list = [(float(l[0]),l[1:]) for l in list if len(l)>1]
69  return dict(list)
70 
71 def associate(first_list, second_list,offset,max_difference):
72  """
73  Associate two dictionaries of (stamp,data). As the time stamps never match exactly, we aim
74  to find the closest match for every input tuple.
75 
76  Input:
77  first_list -- first dictionary of (stamp,data) tuples
78  second_list -- second dictionary of (stamp,data) tuples
79  offset -- time offset between both dictionaries (e.g., to model the delay between the sensors)
80  max_difference -- search radius for candidate generation
81 
82  Output:
83  matches -- list of matched tuples ((stamp1,data1),(stamp2,data2))
84 
85  """
86  first_keys = first_list.keys()
87  second_keys = second_list.keys()
88  potential_matches = [(abs(a - (b + offset)), a, b)
89  for a in first_keys
90  for b in second_keys
91  if abs(a - (b + offset)) < max_difference]
92  potential_matches.sort()
93  matches = []
94  for diff, a, b in potential_matches:
95  if a in first_keys and b in second_keys:
96  first_keys.remove(a)
97  second_keys.remove(b)
98  matches.append((a, b))
99 
100  matches.sort()
101  return matches
102 
103 if __name__ == '__main__':
104 
105  # parse command line
106  parser = argparse.ArgumentParser(description='''
107  This script takes two data files with timestamps and associates them
108  ''')
109  parser.add_argument('first_file', help='first text file (format: timestamp data)')
110  parser.add_argument('second_file', help='second text file (format: timestamp data)')
111  parser.add_argument('--first_only', help='only output associated lines from first file', action='store_true')
112  parser.add_argument('--offset', help='time offset added to the timestamps of the second file (default: 0.0)',default=0.0)
113  parser.add_argument('--max_difference', help='maximally allowed time difference for matching entries (default: 0.02)',default=0.02)
114  args = parser.parse_args()
115 
116  first_list = read_file_list(args.first_file)
117  second_list = read_file_list(args.second_file)
118 
119  matches = associate(first_list, second_list,float(args.offset),float(args.max_difference))
120 
121  if args.first_only:
122  for a,b in matches:
123  print("%f %s"%(a," ".join(first_list[a])))
124  else:
125  for a,b in matches:
126  print("%f %s %f %s"%(a," ".join(first_list[a]),b-float(args.offset)," ".join(second_list[b])))
127 
128 
def read_file_list(filename)
Definition: associate.py:49
def associate(first_list, second_list, offset, max_difference)
Definition: associate.py:71
GLM_FUNC_DECL genType abs(genType const &x)


rtabmap_ros
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:42:18