TUM数据集groundtruth轨迹与估计轨迹统一参考系

TUM数据集groundtruth轨迹与估计轨迹(ORBSLAM2运行的结果)统一参考系

参考https://blog.csdn.net/luo870604851/article/details/85006243
阅读ORBSLAM的相关论文并且在UBuntu系统上编译好ORB-SLAM2后,准备运行下程序,获得估计的轨迹后,评估下轨迹的误差。这里运行的是rgbd_dataset_freiburg1_desk数据集,关于深度图和RGB图、估计轨迹和真实轨迹的时间戳的对齐,使用的是associate.py文件,具体可以参考高翔大佬的一起做RGB-D SLAM 第二季 (一)。绘制两者的轨迹后,发现两者完全没有对上,然后看了groudtruth.txtKeyFrameTrajectory.txt文件后,发现估计轨迹是以第一帧坐标系作为参考,而真值轨迹应该是以某个固定的坐标系作为参考。为了能评估轨迹误差,必须要知道两个参考系之间的变换矩阵T。
不在相同参考系下的轨迹

首先考虑的是,在时间戳对齐后,求取第一帧groundtruth轨迹的逆作为T,下图是运行的结果。在论文中给出的是绝对位移误差为0.046m,而这里计算的结果为0.053m,误差比较大。(这个差的还算比较小的了,其它的数据集也试过了,误差更大。)
在这里插入图片描述
在这里插入图片描述

从上面的结果可以看出,直接取第一帧groundtruth轨迹的逆作为T,最后求得的误差要比论文里给出的要大。分析原因,上述两个参考系之间的变换矩阵T只由一帧计算得,存在比较大的误差。因此,可以把所有帧均考虑进来,构建最小二乘问题来估计T,这就是一个ICP问题,在视觉SLAM十四讲的第七章有给出求解方式。这可以使用SVD方法求解,也可以用非线性优化的方式求解。从下面的图中可以看出,估计的绝对平移误差为0.043m,现在比较符合论文中的结果。

在这里插入图片描述
在这里插入图片描述
下面给出读取轨迹文件并统一参考系,显示轨迹,计算轨迹误差的代码。

#include <pangolin/pangolin.h>
#include <Eigen/Core>
#include <unistd.h> 
#include <thread>
#include <sophus/se3.hpp>

class Trajectory{
    public:
        void showAssociateTrajectory();
        vector<double> calTrajectoryError();
        void readAssociateTrajectory(const string &strAssociateTrajectoryFile);
        vector<double> mvimageTimeStamps, mvposeTimeStamps, mvtruthTimeStamps;
        vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> mvpose;
        vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> mvgroundTruthPose;
};


vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> pose;
vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> groundTruthPose;

// Input -> The path of associated trcjectory file.
void Trajectory::readAssociateTrajectory( const string &strAssociateTrajectoryFile){

        ifstream fin(strAssociateTrajectoryFile);
        string str;
        if(!fin){
            cerr << "Failed to open trajectory file at:" << strAssociateTrajectoryFile << endl;
            exit(-1);
        }

        
        // Read trajectory file
        double data[7];
        // bool isFirstFrame = true;
        // orbslam保存的轨迹是以第一帧为世界坐标系,需要将groundtruth轨迹的世界坐标变换到估计轨迹的固定坐标
        // Eigen::Isometry3d Tinv;  
        while(!fin.eof()){ 
            double realTimeStamp, truthTimeStamp;
            Eigen::Vector3d v;
            fin >> realTimeStamp;
            if(fin.fail()) {  // 在读数据后判断,避免多读一行数据!!
                break;
            }
            mvposeTimeStamps.push_back(realTimeStamp);


            //读取实际轨迹
            for(size_t i = 0; i < 7; i++){
                fin >> data[i];
            }
            Eigen::Quaterniond q(data[6],data[3],data[4],data[5]);
            Eigen::Isometry3d T(q);
            T.pretranslate(Eigen::Vector3d(data[0],data[1],data[2])); //左乘,相对与旋转前的坐标
            pose.push_back(T);

            fin >> truthTimeStamp;
            //读取轨迹真值
            for(size_t i = 0; i < 7; i++){
                fin >> data[i];
            }
            Eigen::Quaterniond q1(data[6],data[3],data[4],data[5]);
            Eigen::Isometry3d T1(q1);
            T1.pretranslate(Eigen::Vector3d(data[0],data[1],data[2]));
            // if(isFirstFrame){
            //     Tinv = T1.inverse();
            //     cout << "两组轨迹之间的变换矩阵:" << endl;
            //     cout << "T:" << Tinv.matrix() << endl;
            //     isFirstFrame  = false; 
            // }
            mvtruthTimeStamps.push_back(truthTimeStamp);
            // groundTruthPose.push_back(Tinv*T1); 
            groundTruthPose.push_back(T1); 
        }

        // // 将groundtruth轨迹的世界坐标系转换到估计轨迹的世界坐标系下
        Eigen::Vector3d estiMid, truthMid; // 两组点的质心
        vector<Eigen::Vector3d> vEsti, vTruth; // 两组点的去质心坐标
        int N = pose.size();
        for(size_t i = 0; i < N ; i++){
            estiMid += pose[i].translation();
            truthMid += groundTruthPose[i].translation();
        }
        estiMid /= N;
        truthMid /=N;

        vEsti.resize(N);  vTruth.resize(N);
        for(int i = 0; i < N; i++){
            vEsti[i] =  pose[i].translation() - estiMid;
            vTruth[i] = groundTruthPose[i].translation() - truthMid;
        }
        
        Eigen::Matrix3d W = Eigen::Matrix3d::Zero();
        for(size_t i = 0 ; i < N ; i++){
            W += vEsti[i]*(vTruth[i].transpose());
        }

        Eigen::JacobiSVD<Eigen::Matrix3d> svd (W, Eigen::ComputeFullU|Eigen::ComputeFullV);
        Eigen::Matrix3d u = svd.matrixU();
        Eigen::Matrix3d v = svd.matrixV();

        Eigen::Matrix3d R = u * (v.transpose());
        Eigen::Vector3d t = estiMid - R * truthMid;

        Eigen::Isometry3d T = Eigen::Isometry3d::Identity();
        cout << "两组轨迹之间的变换矩阵:" << endl;
        cout << "R: " << R << endl;   //这两行输出注释掉,结果会出错?!(迷~~)
        cout << "t: " << t << endl;
        // // 方式1 (方式一和方式二T必须初始化成单位矩阵)
        T.translate(t);
        T.rotate(R);
        // // 方式2
        // T.rotate(R);        // 旋转后,translate()是相对旋转后的坐标,pretranslate()是相对于旋转前的坐标
        // T.pretranslate(t);  // 带pre的函数表示左乘,不带的表示右乘
        // // 方式3
        // // T.matrix().block(0, 0, 3, 3) = R;
        // // T.matrix().col(3) = t.homogeneous(); // 将平移向量变成对应的齐次坐标
        // // T.matrix().row(3) = Eigen::Vector4d(0, 0, 0, 1);

        cout << "T:" << T.matrix() << endl;
        for(auto &p: groundTruthPose){
            p = T*p;
        }

        mvpose = pose;
        mvgroundTruthPose = groundTruthPose;
}

void Trajectory::showAssociateTrajectory(){
    assert(pose.size() == groundTruthPose.size()); 
    assert(!pose.empty() && !groundTruthPose.empty());

    pangolin::CreateWindowAndBind("Trajectory Viewer", 1024, 768); //创建一个窗口
    glEnable(GL_DEPTH_TEST); //启动深度测试,OpenGL只绘制最前面的一层,绘制时检查当前像素前面是否有别的像素,如果别的像素挡住了它,那它就不会绘制
    glEnable(GL_BLEND); //在openGL中使用颜色混合
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //选择混合选项

    // 1. 定义相机投影模型:ProjectionMatrix(w, h, fu, fv, u0, v0, zNear, zFar)
    // 2. 定义观测方位向量:
    // 观测点位置:(mViewpointX mViewpointY mViewpointZ)
    // 观测目标位置:(0, 0, 0)
    // 观测的方位向量:(0.0,-1.0, 0.0)
    pangolin::OpenGlRenderState s_cam(
        pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 289, 0.1, 1000),
        pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0)
        );

    // 定义地图面板
    // 前两个参数(0.0, 1.0)表明宽度和面板纵向宽度和窗口大小相同
    // 中间两个参数(pangolin::Attach::Pix(175), 1.0)表明右边所有部分用于显示图形
    // 最后一个参数(-1024.0f/768.0f)为显示长宽比
    pangolin::View &d_cam = pangolin::CreateDisplay().SetBounds(0.0, 1.0 ,0.0 , 1.0, -1024.0f/768.0f)
        .SetHandler(new pangolin::Handler3D(s_cam));

    while(pangolin::ShouldQuit() == false){
        //消除缓冲区中的当前可写的颜色缓冲和深度缓冲
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // |和& -> 总是要计算两个数
        d_cam.Activate(s_cam);
        // 设置为白色,glClearColor(red, green, blue, alpha),数值范围(0, 1)
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glLineWidth(2);

        //画出连线
        for(size_t i = 0; i < pose.size(); i++){
            glColor3f(0.0, 0.0, 0.0);
            glBegin(GL_LINES);
            auto p1 = pose[i], p2 = pose[i+1];
            glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
            glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
            glEnd();
            glColor3f(1.0, 0.0, 0.0); //红色是参考轨迹
            glBegin(GL_LINES);
            p1 = groundTruthPose[i];
            p2 = groundTruthPose[i+1];
            glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
            glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
            glEnd();
        }
        pangolin::FinishFrame();
        usleep(5000);   // sleep 5 ms

    }
}

vector<double> Trajectory::calTrajectoryError(){
    assert(pose.size() == groundTruthPose.size()); 
    assert(!pose.empty() && !groundTruthPose.empty());
    double ATE_all = 0, ATE_trans = 0;
    vector<double> ATE;
    for(size_t i = 0; i < pose.size() ; i++){
        Sophus::SE3d p1(pose[i].rotation(),pose[i].translation());
        Sophus::SE3d p2(groundTruthPose[i].rotation(),groundTruthPose[i].translation());
        double error = ((p2.inverse() * p1).log()).norm();
        ATE_all += error * error;
        error = (groundTruthPose[i].translation() - pose[i].translation()).norm();
        ATE_trans += error*error;
    }
    ATE_all = ATE_all / double(pose.size());
    ATE_all = sqrt(ATE_all);
    ATE_trans = ATE_trans / double(pose.size());
    ATE_trans = sqrt(ATE_trans);
    ATE.push_back(ATE_all);
    ATE.push_back(ATE_trans);
    return ATE;
}
  • 9
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用ORB-SLAM3对TUM数据集进行轨迹对齐的C++代码示例: ```c++ #include <iostream> #include <fstream> #include <vector> #include <string> #include <cmath> using namespace std; // 读取轨迹文件 bool readTrajectory(const string& filename, vector<double>& timestamps, vector<vector<double> >& poses) { ifstream file(filename); if (!file.is_open()) { cerr << "Failed to open file: " << filename << endl; return false; } while (!file.eof()) { string line; getline(file, line); if (!line.empty() && line.at(0) != '#') { stringstream ss(line); double timestamp; ss >> timestamp; vector<double> pose(7); for (int i = 0; i < 7; ++i) { ss >> pose[i]; } timestamps.push_back(timestamp); poses.push_back(pose); } } file.close(); return true; } // 对齐两个轨迹 void alignTrajectories(const vector<double>& timestamps1, const vector<vector<double> >& poses1, const vector<double>& timestamps2, const vector<vector<double> >& poses2, vector<vector<double> >& aligned_poses2) { aligned_poses2.clear(); // 计算两个轨迹的平均旋转和平移 double mean_rotation1 = 0.0; vector<double> mean_translation1(3, 0.0); for (size_t i = 0; i < poses1.size(); ++i) { double qw = poses1[i][0], qx = poses1[i][1], qy = poses1[i][2], qz = poses1[i][3]; double tx = poses1[i][4], ty = poses1[i][5], tz = poses1[i][6]; double roll = atan2(2.0 * (qw * qx + qy * qz), 1.0 - 2.0 * (qx * qx + qy * qy)); double pitch = asin(2.0 * (qw * qy - qx * qz)); double yaw = atan2(2.0 * (qw * qz + qx * qy), 1.0 - 2.0 * (qy * qy + qz * qz)); mean_rotation1 += yaw; mean_translation1[0] += tx; mean_translation1[1] += ty; mean_translation1[2] += tz; } mean_rotation1 /= poses1.size(); mean_translation1[0] /= poses1.size(); mean_translation1[1] /= poses1.size(); mean_translation1[2] /= poses1.size(); double mean_rotation2 = 0.0; vector<double> mean_translation2(3, 0.0); for (size_t i = 0; i < poses2.size(); ++i) { double qw = poses2[i][0], qx = poses2[i][1], qy = poses2[i][2], qz = poses2[i][3]; double tx = poses2[i][4], ty = poses2[i][5], tz = poses2[i][6]; double roll = atan2(2.0 * (qw * qx + qy * qz), 1.0 - 2.0 * (qx * qx + qy * qy)); double pitch = asin(2.0 * (qw * qy - qx * qz)); double yaw = atan2(2.0 * (qw * qz + qx * qy), 1.0 - 2.0 * (qy * qy + qz * qz)); mean_rotation2 += yaw; mean_translation2[0] += tx; mean_translation2[1] += ty; mean_translation2[2] += tz; } mean_rotation2 /= poses2.size(); mean_translation2[0] /= poses2.size(); mean_translation2[1] /= poses2.size(); mean_translation2[2] /= poses2.size(); // 对齐轨迹2 for (size_t i = 0; i < poses2.size(); ++i) { double qw = poses2[i][0], qx = poses2[i][1], qy = poses2[i][2], qz = poses2[i][3]; double tx = poses2[i][4], ty = poses2[i][5], tz = poses2[i][6]; double roll = atan2(2.0 * (qw * qx + qy * qz), 1.0 - 2.0 * (qx * qx + qy * qy)); double pitch = asin(2.0 * (qw * qy - qx * qz)); double yaw = atan2(2.0 * (qw * qz + qx * qy), 1.0 - 2.0 * (qy * qy + qz * qz)); // 将姿态变换到轨迹1的参考系下 double aligned_yaw = yaw - mean_rotation2 + mean_rotation1; double aligned_tx = tx - mean_translation2[0] + mean_translation1[0]; double aligned_ty = ty - mean_translation2[1] + mean_translation1[1]; double aligned_tz = tz - mean_translation2[2] + mean_translation1[2]; double aligned_qw = cos(aligned_yaw / 2.0); double aligned_qx = 0.0; double aligned_qy = 0.0; double aligned_qz = sin(aligned_yaw / 2.0); vector<double> aligned_pose(7); aligned_pose[0] = aligned_qw; aligned_pose[1] = aligned_qx; aligned_pose[2] = aligned_qy; aligned_pose[3] = aligned_qz; aligned_pose[4] = aligned_tx; aligned_pose[5] = aligned_ty; aligned_pose[6] = aligned_tz; aligned_poses2.push_back(aligned_pose); } } // 计算轨迹误差 void computeTrajectoryError(const vector<vector<double> >& poses1, const vector<vector<double> >& poses2, double& rmse_translation, double& rmse_rotation) { rmse_translation = 0.0; rmse_rotation = 0.0; for (size_t i = 0; i < poses1.size(); ++i) { double qw1 = poses1[i][0], qx1 = poses1[i][1], qy1 = poses1[i][2], qz1 = poses1[i][3]; double tx1 = poses1[i][4], ty1 = poses1[i][5], tz1 = poses1[i][6]; double qw2 = poses2[i][0], qx2 = poses2[i][1], qy2 = poses2[i][2], qz2 = poses2[i][3]; double tx2 = poses2[i][4], ty2 = poses2[i][5], tz2 = poses2[i][6]; // 计算平移误差 double dx = tx1 - tx2; double dy = ty1 - ty2; double dz = tz1 - tz2; double translation_error = sqrt(dx * dx + dy * dy + dz * dz); rmse_translation += translation_error * translation_error; // 计算旋转误差 double dot_product = qw1 * qw2 + qx1 * qx2 + qy1 * qy2 + qz1 * qz2; double rotation_error = acos(2.0 * dot_product * dot_product - 1.0); rmse_rotation += rotation_error * rotation_error; } rmse_translation = sqrt(rmse_translation / poses1.size()); rmse_rotation = sqrt(rmse_rotation / poses1.size()); } int main(int argc, char** argv) { if (argc < 3) { cerr << "Usage: " << argv[0] << " trajectory_file1 trajectory_file2" << endl; return 1; } // 读取轨迹文件 string filename1(argv[1]); vector<double> timestamps1; vector<vector<double> > poses1; if (!readTrajectory(filename1, timestamps1, poses1)) { return 1; } string filename2(argv[2]); vector<double> timestamps2; vector<vector<double> > poses2; if (!readTrajectory(filename2, timestamps2, poses2)) { return 1; } // 对齐轨迹2 vector<vector<double> > aligned_poses2; alignTrajectories(timestamps1, poses1, timestamps2, poses2, aligned_poses2); // 计算轨迹误差 double rmse_translation, rmse_rotation; computeTrajectoryError(poses1, poses2, rmse_translation, rmse_rotation); cout << "Before alignment:" << endl; cout << "RMSE translation: " << rmse_translation << " m" << endl; cout << "RMSE rotation: " << rmse_rotation << " rad" << endl; computeTrajectoryError(poses1, aligned_poses2, rmse_translation, rmse_rotation); cout << "After alignment:" << endl; cout << "RMSE translation: " << rmse_translation << " m" << endl; cout << "RMSE rotation: " << rmse_rotation << " rad" << endl; return 0; } ``` 该代码读取两个轨迹文件(TUM数据集格式),并计算它们之间的平移和旋转误差。然后,它使用平均旋转和平移对第二个轨迹进行对齐,并重新计算误差。注意,该代码使用欧拉角(yaw-pitch-roll)来表示姿态,而不是四元数,因为这更容易理解和调试。如果您需要更高的精度,请改用四元数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值