利用ICP把自己跑出的数据与开源数据集对齐并显示(一.时间戳对齐)

 我们采集的数据集时间戳通常与给定的groundtruth的时间戳不对齐,下面以EuRoC数据集data.csv和我们采集的CameraTrajectory.txt为例介绍两者之间的对齐。

一.data.csv转ground.txt

    //改写成txt文件
    ifstream ifile("data.csv");
    string line,s;
    vector<vector<double>> timestamp;
    while(getline(ifile,line))
    {
        stringstream record(line);
        record>>s;
        if(s[0]=='#')//跳过注释
            continue;
        vector<double> time;
        double ti;
        string str;
        for(auto &s:line)//把每一行的逗号都替换成空格
            if(s==',')
                s=' ';
        stringstream rd(line);
        for(auto i=0;i<8;i++)
        {
            rd>>ti;
            if(i==0)
            {
                setprecision(8);//使得时间戳与自己数据时间戳格式一致
                ti=ti/(10e8);
            }
            time.push_back(ti);
        }
        timestamp.push_back(time);
        record.clear();
    }
    ifile.close();
    ofstream ofile("../ground.txt");
    for(auto time:timestamp) {
        for (auto ti:time)
            ofile <<fixed<< ti << " ";
        ofile<<endl;
    }
    ofile.close();

二.对齐ground.txt与CameraTrajectory.txt

    ifstream fileGround("../ground.txt");
    ifstream fileEstimated("../CameraTrajectory.txt");
    string lineNew;
    vector<vector<double>> vTraGround,vTraEstimated,vTraGroundAligned,vTraEstimatedAligned;
    while(getline(fileGround,lineNew))//读取ground并保存成矩阵
    {
        stringstream record(lineNew);
        vector<double> vTmpGround;
        double tmp;
        for(auto i=0;i<8;i++)
        {
            record>>tmp;
            vTmpGround.push_back(tmp) ;
        }
        vTraGround.push_back(vTmpGround);
        record.clear();
    }
    while(getline(fileEstimated,lineNew))//读取CaneraTrajectory并保存成矩阵
    {
        stringstream record(lineNew);
        vector<double> vTmpEstimated;
        double tmp;
        for(auto i=0;i<8;i++)
        {
            record>>tmp;
            vTmpEstimated.push_back(tmp);
        }
        vTraEstimated.push_back(vTmpEstimated);
        record.clear();
    }
    auto it = vTraGround.begin();
    for(auto tmpEst:vTraEstimated)//对齐时间戳
    {
        double timeStampEst = tmpEst[0];
        for (; it != vTraGround.end(); it++)
        {
            double timeStampGrd = (*it)[0];
            if(abs(timeStampGrd-timeStampEst)<=0.0004)//允许一定的容差
            {
                vTraGroundAligned.push_back(*it);
                vTraEstimatedAligned.push_back(tmpEst);
            }
            else if(timeStampGrd < timeStampEst)
                continue;
            else if(timeStampGrd > timeStampEst)
                break;
        }
    }
    ofstream ofileGrd("../GroundAligned.txt");//写入对齐后的ground数据
    for(auto vTra:vTraGroundAligned) {
        for (auto ele:vTra)
            ofileGrd << fixed << ele << " ";
        ofileGrd << endl;
    }
    ofstream ofileEst("../EstimatedAligned.txt");//写入对齐后的CameraTrajectory数据
    for(auto vTra:vTraEstimatedAligned) {
        for (auto ele:vTra)
            ofileEst << fixed << ele << " ";
        ofileEst << endl;
    }
    return 0;

 

以下是一个简单的示例代码,将ORBSLAM的轨迹输到一个txt文件中,并与真实轨迹对齐。假设你已经有了真实轨迹的数据集和ORBSLAM的轨迹数据集,其中每个数据点包含了一个时间戳、位置和方向信息。 ```C++ #include <iostream> #include <fstream> #include <vector> struct Pose { double timestamp; double x, y, z; double qx, qy, qz, qw; }; int main(int argc, char** argv) { // 读取真实轨迹数据 std::vector<Pose> ground_truth_poses; std::ifstream ground_truth_file("ground_truth.txt"); double timestamp, x, y, z, qx, qy, qz, qw; while (ground_truth_file >> timestamp >> x >> y >> z >> qx >> qy >> qz >> qw) { Pose pose; pose.timestamp = timestamp; pose.x = x; pose.y = y; pose.z = z; pose.qx = qx; pose.qy = qy; pose.qz = qz; pose.qw = qw; ground_truth_poses.push_back(pose); } ground_truth_file.close(); // 读取ORBSLAM的轨迹数据 std::vector<Pose> orbslam_poses; std::ifstream orbslam_file("orbslam_trajectory.txt"); while (orbslam_file >> timestamp >> x >> y >> z >> qx >> qy >> qz >> qw) { Pose pose; pose.timestamp = timestamp; pose.x = x; pose.y = y; pose.z = z; pose.qx = qx; pose.qy = qy; pose.qz = qz; pose.qw = qw; orbslam_poses.push_back(pose); } orbslam_file.close(); // 对齐ORBSLAM的轨迹和真实轨迹 std::ofstream aligned_file("aligned_trajectory.txt"); int i = 0, j = 0; while (i < ground_truth_poses.size() && j < orbslam_poses.size()) { double gt_timestamp = ground_truth_poses[i].timestamp; double orbslam_timestamp = orbslam_poses[j].timestamp; if (orbslam_timestamp < gt_timestamp) { j++; } else if (orbslam_timestamp > gt_timestamp) { i++; } else { // 找到了时间戳匹配的点,将ORBSLAM的位置和方向信息与真实轨迹的时间戳一起输 Pose aligned_pose = orbslam_poses[j]; aligned_file << aligned_pose.timestamp << " " << ground_truth_poses[i].x << " " << ground_truth_poses[i].y << " " << ground_truth_poses[i].z << " " << aligned_pose.qx << " " << aligned_pose.qy << " " << aligned_pose.qz << " " << aligned_pose.qw << std::endl; i++; j++; } } aligned_file.close(); return 0; } ``` 这个示例代码中,我们首先读取真实轨迹和ORBSLAM的轨迹数据,然后对齐这两个轨迹。对齐的方法是,我们先按照时间戳排序,然后在真实轨迹和ORBSLAM轨迹中同时扫描,并将时间戳匹配的点输到一个新的文件中。输的格式为:时间戳(来自ORBSLAM),位置(来自真实轨迹),方向(来自ORBSLAM)。 你可以根据需要对这个示例代码进行修改,以适应你自己的数据集和需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值