associate.py

35 篇文章 0 订阅

associate.py

引言

将相机姿势,深度图像列表和彩色图像列表合并到一个文件.groundtruth.txt+depth.txt+rgb.txt=associations.txt

associate.py

#!/usr/bin/python
# Software License Agreement (BSD License)
#
# Copyright (c) 2013, Juergen Sturm, TUM
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of TUM nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Requirements: 
# sudo apt-get install python-argparse

"""
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.

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.
"""

import argparse
import sys
import os
import numpy


def read_file_list(filename):
    """
    Reads a trajectory from a text file. 
    
    File format:
    The file format is "stamp d1 d2 d3 ...", where stamp denotes the time stamp (to be matched)
    and "d1 d2 d3.." is arbitary data (e.g., a 3D position and 3D orientation) associated to this timestamp. 
    
    Input:
    filename -- File name
    
    Output:
    dict -- dictionary of (stamp,data) tuples
    
    """
    file = open(filename)
    data = file.read()
    lines = data.replace(","," ").replace("\t"," ").split("\n") 
    list = [[v.strip() for v in line.split(" ") if v.strip()!=""] for line in lines if len(line)>0 and line[0]!="#"]
    list = [(float(l[0]),l[1:]) for l in list if len(l)>1]
    return dict(list)

def associate(first_list, second_list,offset,max_difference):
    """
    Associate two dictionaries of (stamp,data). As the time stamps never match exactly, we aim 
    to find the closest match for every input tuple.
    
    Input:
    first_list -- first dictionary of (stamp,data) tuples
    second_list -- second dictionary of (stamp,data) tuples
    offset -- time offset between both dictionaries (e.g., to model the delay between the sensors)
    max_difference -- search radius for candidate generation

    Output:
    matches -- list of matched tuples ((stamp1,data1),(stamp2,data2))
    
    """
	first_keys = list(first_list.keys())
    second_keys = list(second_list.keys())
    potential_matches = [(abs(a - (b + offset)), a, b) 
                         for a in first_keys 
                         for b in second_keys 
                         if abs(a - (b + offset)) < max_difference]
    potential_matches.sort()
    matches = []
    for diff, a, b in potential_matches:
        if a in first_keys and b in second_keys:
            first_keys.remove(a)
            second_keys.remove(b)
            matches.append((a, b))
    
    matches.sort()
    return matches

if __name__ == '__main__':
    
    # parse command line
    parser = argparse.ArgumentParser(description='''
    This script takes two data files with timestamps and associates them   
    ''')
    parser.add_argument('first_file', help='first text file (format: timestamp data)')
    parser.add_argument('second_file', help='second text file (format: timestamp data)')
    parser.add_argument('--first_only', help='only output associated lines from first file', action='store_true')
    parser.add_argument('--offset', help='time offset added to the timestamps of the second file (default: 0.0)',default=0.0)
    parser.add_argument('--max_difference', help='maximally allowed time difference for matching entries (default: 0.02)',default=0.02)
    args = parser.parse_args()

    first_list = read_file_list(args.first_file)
    second_list = read_file_list(args.second_file)

    matches = associate(first_list, second_list,float(args.offset),float(args.max_difference))    

    if args.first_only:
        for a,b in matches:
            print("%f %s"%(a," ".join(first_list[a])))
    else:
        for a,b in matches:
            print("%f %s %f %s"%(a," ".join(first_list[a]),b-float(args.offset)," ".join(second_list[b])))
            
        

生成的文本文件应如下所示:~/Datasets/TUM/freiburg3/rgbd_dataset_freiburg3_long_office_household/associations.txt

$ ./associate.py ~/Datasets/TUM/freiburg3/rgbd_dataset_freiburg3_long_office_household/groundtruth.txt ~/Datasets/TUM/freiburg3/rgbd_dataset_freiburg3_long_office_household/depth.txt > tmp.txt
$ ./associate.py tmp.txt ~/Datasets/TUM/freiburg3/rgbd_dataset_freiburg3_long_office_household/rgb.txt > ~/Datasets/TUM/freiburg3/rgbd_dataset_freiburg3_long_office_household/associations.txt

rgb.txt+depth.txt,其中对官网源文件有更改部分,参考自博客。报错:TabError: inconsistent use of tabs and spaces in indentation是空格的问题,检查检查。

制作:

./associate.py rgb.txt depth.txt > fr1_360.txt
./associate.py rgb.txt depth.txt > fr1_desk.txt
./associate.py rgb.txt depth.txt > fr1_desk2.txt
./associate.py rgb.txt depth.txt > fr1_room.txt
./associate.py rgb.txt depth.txt > fr2_desk.txt
./associate.py rgb.txt depth.txt > fr1_floor.txt
./associate.py rgb.txt depth.txt > fr2_360_hemisphere.txt
./associate.py rgb.txt depth.txt > fr2_360_kidnap.txt
./associate.py rgb.txt depth.txt > fr2_large_no_loop.txt
./associate.py rgb.txt depth.txt > fr2_large_with_loop.txt
./associate.py rgb.txt depth.txt > fr3_long_office_household.txt

原来可以索引到win盘下面的文件:

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_far/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_far/depth.txt > fr3_nostructure_notexture_far.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_near_withloop/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_near_withloop/depth.txt > fr3_nostructure_notexture_near_withloop.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_far/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_far/depth.txt > fr3_nostructure_texture_far.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_near_withloop/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_near_withloop/depth.txt > fr3_nostructure_texture_near_withloop.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_far/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_far/depth.txt > fr3_structure_notexture_far.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_near/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_near/depth.txt > fr3_structure_notexture_near.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_far/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_far/depth.txt > fr3_structure_texture_far.txt

./associate.py /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_near/rgb.txt /media/fb/软件/dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_near/depth.txt > fr3_structure_texture_near.txt

路径:

~/Dataset/EuRoC/MH_01_easy/mav0/cam0/data
~/Dataset/EuRoC/MH_02_easy/mav0/cam0/data
~/Dataset/EuRoC/MH_03_medium/mav0/cam0/data
~/Dataset/EuRoC/MH_04_difficult/mav0/cam0/data
~/Dataset/EuRoC/MH_05_difficult/mav0/cam0/data
~/Dataset/EuRoC/V1_01_easy/mav0/cam0/data
~/Dataset/EuRoC/V1_02_medium/mav0/cam0/data
~/Dataset/EuRoC/V1_03_difficult/mav0/cam0/data
~/Dataset/EuRoC/V2_01_easy/mav0/cam0/data
~/Dataset/EuRoC/V2_02_medium/mav0/cam0/data
~/Dataset/EuRoC/V2_03_difficult/mav0/cam0/data




~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg1_360
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg1_desk
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg1_desk2
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg1_floor
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg1_room
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg2_360_hemisphere
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg2_360_kidnap
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg2_desk
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg2_large_no_loop
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg2_large_with_loop
~/Dataset/TUM/HandheldSLAM/rgbd_dataset_freiburg3_long_office_household



~/Dataset/TUM/associations/HandheldSLAM/fr1_360.txt
~/Dataset/TUM/associations/HandheldSLAM/fr1_desk.txt
~/Dataset/TUM/associations/HandheldSLAM/fr1_desk2.txt
~/Dataset/TUM/associations/HandheldSLAM/fr1_room.txt
~/Dataset/TUM/associations/HandheldSLAM/fr2_desk.txt
~/Dataset/TUM/associations/HandheldSLAM/fr1_floor.txt
~/Dataset/TUM/associations/HandheldSLAM/fr2_360_hemisphere.txt
~/Dataset/TUM/associations/HandheldSLAM/fr2_360_kidnap.txt
~/Dataset/TUM/associations/HandheldSLAM/fr2_large_no_loop.txt
~/Dataset/TUM/associations/HandheldSLAM/fr2_large_with_loop.txt
~/Dataset/TUM/associations/HandheldSLAM/fr3_long_office_household.txt


/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_far
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_near_withloop
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_far
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_near_withloop
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_far
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_near
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_far
/home/fb/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_near



~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_far
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_notexture_near_withloop
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_far
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_nostructure_texture_near_withloop
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_far
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_notexture_near
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_far
~/Dataset/TUM/StructureVsTexture/rgbd_dataset_freiburg3_structure_texture_near



~/Dataset/TUM/associations/StructureVsTexture/fr3_nostructure_notexture_far.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_nostructure_notexture_near_withloop.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_nostructure_texture_far.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_nostructure_texture_near_withloop.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_structure_notexture_far.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_structure_notexture_near.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_structure_texture_far.txt
~/Dataset/TUM/associations/StructureVsTexture/fr3_structure_texture_near.txt






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值