使用python3拼接rgb.txt与depth.txt为associate.txt(适用于GCNV2_SLAM中TUM数据集的运行)

这里以GCNV2_SLAM中TUM数据集的运行为例子:

安装gnv2_slam可以参考:GCNv2_SLAM-CPU详细安装教程(ubuntu18.04)-CSDN博客

首先下载数据集Computer Vision Group - Dataset Download

下载后通过该命令解压:

 tar -xvf rgbd_dataset_freiburg1_desk.tgz

打开后,你可以发现:在该数据集中,深度信息何rgb信息是分开的,而在执行数据集时,两者需要同时执行,这就要将两者按照各自时间戳进行拼接。如图所示:

然后,首先需要自己创建一个文档,将其更改为associate.py文件,然后往里面添加代码,如下为添加后的代码可以使用pyton3进行运行:

 以下是加了那两个的代码:

#!/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 = first_list.keys()
    second_keys = 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 = []
    first_keys = list(first_keys)   //此为添加的代码
    second_keys = list(second_keys)  //此为添加的代码用于执行pyton3指令
    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])))
            
        

接下来就可以运行命令:

python3 associate.py rgb.txt depth.txt > associate.txt

就会生成associate.txt文件,如下:

接下来就可以了。完成。嘿嘿嘿。有什么疑问可以来交流。鄙人在做多机器人方面。

 本人目前研究多机器人方向,有相关研究方向的欢迎交流讨论。

 有志同道合的朋友可以加+V,一起聊聊。

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用C++的文件流(fstream)来读取文件数据。具体步骤如下: 1. 打开associate.txt文件,创建输入文件流对象,将文件名作为参数传入构造函数: ``` ifstream fin(associate_file); ``` 2. 在循环读取每一行数据。可以使用while循环和getline()函数来实现。同时,为了方便起见,我们可以定义一些变量来存储每一行数据的信息: ``` string rgb_file, depth_file; double time_rgb, time_depth; while (getline(fin, line)) { istringstream iss(line); iss >> time_rgb >> rgb_file >> time_depth >> depth_file; // TODO: 处理读取到的数据 } ``` 3. 在读取到每一行数据后,您可以对数据进行处理,例如将RGB和深度图像数据进行匹配等。 完整的代码如下所示: ``` #include <iostream> #include <fstream> #include <sstream> using namespace std; int main() { // 打开associate.txt文件,创建输入文件流对象 string associate_file = "associate.txt"; ifstream fin(associate_file); if (!fin) { cerr << "无法打开文件" << associate_file << endl; return 1; } // 逐行读取数据 string line; string rgb_file, depth_file; double time_rgb, time_depth; while (getline(fin, line)) { istringstream iss(line); iss >> time_rgb >> rgb_file >> time_depth >> depth_file; // 处理读取到的数据 cout << "RGB文件:" << rgb_file << endl; cout << "深度文件:" << depth_file << endl; cout << "RGB时间戳:" << time_rgb << endl; cout << "深度时间戳:" << time_depth << endl; // TODO: 将RGB和深度图像数据进行匹配等操作 } // 关闭文件流 fin.close(); return 0; } ``` 在这个例子,我们打开了associate.txt文件,并逐行读取文件的数据。将每一行数据RGB文件名、深度文件名、RGB时间戳和深度时间戳存储在变量,并对它们进行了简单的处理和输出。最后,关闭文件流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值