associate.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. 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.
  38. 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.
  39. """
  40. import argparse
  41. import sys
  42. import os
  43. import numpy
  44. def read_file_list(filename,remove_bounds):
  45. """
  46. Reads a trajectory from a text file.
  47. File format:
  48. The file format is "stamp d1 d2 d3 ...", where stamp denotes the time stamp (to be matched)
  49. and "d1 d2 d3.." is arbitary data (e.g., a 3D position and 3D orientation) associated to this timestamp.
  50. Input:
  51. filename -- File name
  52. Output:
  53. dict -- dictionary of (stamp,data) tuples
  54. """
  55. file = open(filename)
  56. data = file.read()
  57. lines = data.replace(","," ").replace("\t"," ").split("\n")
  58. if remove_bounds:
  59. lines = lines[100:-100]
  60. list = [[v.strip() for v in line.split(" ") if v.strip()!=""] for line in lines if len(line)>0 and line[0]!="#"]
  61. list = [(float(l[0]),l[1:]) for l in list if len(l)>1]
  62. return dict(list)
  63. def associate(first_list, second_list,offset,max_difference):
  64. """
  65. Associate two dictionaries of (stamp,data). As the time stamps never match exactly, we aim
  66. to find the closest match for every input tuple.
  67. Input:
  68. first_list -- first dictionary of (stamp,data) tuples
  69. second_list -- second dictionary of (stamp,data) tuples
  70. offset -- time offset between both dictionaries (e.g., to model the delay between the sensors)
  71. max_difference -- search radius for candidate generation
  72. Output:
  73. matches -- list of matched tuples ((stamp1,data1),(stamp2,data2))
  74. """
  75. first_keys = first_list.keys()
  76. second_keys = second_list.keys()
  77. potential_matches = [(abs(a - (b + offset)), a, b)
  78. for a in first_keys
  79. for b in second_keys
  80. if abs(a - (b + offset)) < max_difference]
  81. potential_matches.sort()
  82. matches = []
  83. for diff, a, b in potential_matches:
  84. if a in first_keys and b in second_keys:
  85. first_keys.remove(a)
  86. second_keys.remove(b)
  87. matches.append((a, b))
  88. matches.sort()
  89. return matches
  90. if __name__ == '__main__':
  91. # parse command line
  92. parser = argparse.ArgumentParser(description='''
  93. This script takes two data files with timestamps and associates them
  94. ''')
  95. parser.add_argument('first_file', help='first text file (format: timestamp data)')
  96. parser.add_argument('second_file', help='second text file (format: timestamp data)')
  97. parser.add_argument('--first_only', help='only output associated lines from first file', action='store_true')
  98. parser.add_argument('--offset', help='time offset added to the timestamps of the second file (default: 0.0)',default=0.0)
  99. parser.add_argument('--max_difference', help='maximally allowed time difference for matching entries (default: 0.02)',default=0.02)
  100. args = parser.parse_args()
  101. first_list = read_file_list(args.first_file)
  102. second_list = read_file_list(args.second_file)
  103. matches = associate(first_list, second_list,float(args.offset),float(args.max_difference))
  104. if args.first_only:
  105. for a,b in matches:
  106. print("%f %s"%(a," ".join(first_list[a])))
  107. else:
  108. for a,b in matches:
  109. print("%f %s %f %s"%(a," ".join(first_list[a]),b-float(args.offset)," ".join(second_list[b])))