首页 > 编程语言 >使用python3拼接rgb.txt与depth.txt为associate.txt(适用于GCNV2_SLAM中TUM数据集的运行)

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

时间:2024-07-25 13:25:58浏览次数:14  
标签:GCNV2 associate keys list matches second file txt first

这里以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,一起聊聊。

标签:GCNV2,associate,keys,list,matches,second,file,txt,first
From: https://blog.csdn.net/2301_80467135/article/details/140685921

相关文章

  • 如何在 Mac 上运行 Python 文件来读取 txt 文件并将其写入外部硬盘?
    我目前有一个充满了我想阅读的epub的文件夹,一个我已经阅读过并想再次阅读的epub的文件夹,以及一个相应的文件,其中每个文件都有epub文件的名称。问题是,这些文件夹仅位于我的外部硬盘上。我想要做的是让我的脚本解析这些文件夹中的epub列表,并在我的下载文件夹中创建最新的副......
  • DroneVehicle数据集标签转换(.xml→.txt yolo_obb)
    1.数据集下载:目录1.数据集下载:2.数据集介绍:3.数据集标签转换1.DroneVehicle转DOTA2.DOTA转YOLO_OBB5.数据集标签可视化DroneVenicle数据集是由天津大学收集、标注的大型无人机航拍车辆数据集。DroneVenicle训练集下载地址:https://pan.baidu.com/s/1ptZCJ1mKYqFnMn......
  • 生成指定大小的TXT文件
    https://cloud.tencent.com/developer/article/2187817fsutilfilecreatenewD:\txt_1m.txt1048576#!/user/bin/envpython#-*-coding:utf-8-*-importtime#获取时间和日期defget_now_datetime(flag=0):"""flag=0为时间和日期e......
  • 021集——批量txt格式坐标转dwg——vba代码实现
    在工作中遇到txt转dwg的重复性工作,详细如下:已知若干个txt文件,坐标格式如下:要求:将每个txt格式坐标文件转为dwg,名称与原txt名称一样,即1.txt生成1.dwg。因txt文件中存在一些非坐标文本数字,我们需进行判断只提取xy坐标数据,然后在cad中输入坐标,生成一个轻量线。此项工作只需一......
  • Winform小工具:.txt档转excel档
    privatevoidbt_txt_to_excel_Click(objectsender,EventArgse){FolderBrowserDialogfolderDialog=newFolderBrowserDialog();if(folderDialog.ShowDialog()==DialogResult.OK){string......
  • 如何在 Google Colab 上打开 txt?
    我的iris.txt存储在GoogleDrive的MyDrive/ML中,我试图打开并阅读它。importcsvimportnumpyfile=open(r"/drive/ML/iris.txt")mylist=list(csv.reader(file))lines=numpy.asarray(mylist)它返回了错误,我该怎么办?IOErrorTraceback(mostrecentcalll......
  • python 文件(txt)操作
    我有一个txt文件,其中包含一列(大约2000个)单词。每个单词只用一个新行分隔。我想将所有这些单词存储在一个数组中words.txt文件的示例:applebananaorange我尝试过的代码:importrandomwithopen('E:/Code/learn/Projects/word-guessing-game/words.txt','r')a......
  • Python 实现Excel和TXT文本格式之间的相互转换
    Excel是一种具有强大的数据处理和图表制作功能的电子表格文件,而TXT则是一种简单通用、易于编辑的纯文本文件。将Excel转换为TXT可以帮助我们将复杂的数据表格以文本的形式保存,方便其他程序读取和处理。而将TXT转换为Excel则可以将文本文件中的数据导入到Excel中进行进一步的分析和......
  • 点云txt文件转pcd文件
    基于C++和pcl实现以下格式的点云txt文档转pcd格式。使用qt的console实现:#include<QCoreApplication>#include<QDir>#include<QDebug>#include<QDirIterator>#include<iostream>#include<fstream>#include<strstream>#include<vec......
  • 使用案例显示时在“.txt”文件中打印现有待办事项时出现名称错误
    我正在尝试使用以下代码中给出的案例显示将现有的待办事项保存在我的txt文件中:whileTrue:user_action=input("Typeadd,show:")user_action=user_action.strip()matchuser_action:case'add':todo=input("Enteranytodo:")+......