orb_tum源码阅读

#include<iostream>   // 输入输出
#include<algorithm>  // 泛型算法头文件
#include<fstream>     // 文件读写
#include<chrono>      // C++11 std::chrono库:时间库 三个重要概念(duration、time_point、clock)

#include<opencv2/core/core.hpp>

#include<System.h>

using namespace std;
// 函数声明:载入图像
void LoadImages(const string &strAssociationFilename, vector<string> &vstrImageFilenamesRGB,
                vector<string> &vstrImageFilenamesD, vector<double> &vTimestamps);

int main(int argc, char **argv)
{
    if(argc != 5)
    {   // 输入参数满足5个,节点+4个路径
        cerr << endl << "Usage: ./rgbd_tum path_to_vocabulary path_to_settings path_to_sequence path_to_association" << endl;
        return 1;
    }

    //  检索图像路径
    vector<string> vstrImageFilenamesRGB;
    vector<string> vstrImageFilenamesD;
    vector<double> vTimestamps;
    string strAssociationFilename = string(argv[4]);
    // 载入图像
    // 文件位置、彩色图、深度图、时间戳
    LoadImages(strAssociationFilename, vstrImageFilenamesRGB, vstrImageFilenamesD, vTimestamps);

    // 检查彩色深度图编号是否一致
    int nImages = vstrImageFilenamesRGB.size();  // 个数
    if(vstrImageFilenamesRGB.empty())
    {
        cerr << endl << "No images found in provided path." << endl;
        return 1;
    }
    else if(vstrImageFilenamesD.size()!=vstrImageFilenamesRGB.size())
    {
        cerr << endl << "Different number of images for rgb and depth." << endl;
        return 1;
    }

    // Create SLAM system. It initializes all system threads and gets ready to process frames.
    // 创建SLAM系统,初始化所有线程,准备获取帧
    // System.h:ORB_SLAM2命名空间,System类初始化
    // 词袋/ yaml参数文件/ emum枚举:RGBD传感器/ use Viewer
    ORB_SLAM2::System SLAM(argv[1],argv[2],ORB_SLAM2::System::RGBD,true);

    // 跟踪模块时间统计
    vector<float> vTimesTrack;
    vTimesTrack.resize(nImages);   // 调整容器大小

    cout << endl << "-------" << endl;
    cout << "Start processing sequence ..." << endl;
    cout << "Images in the sequence: " << nImages << endl << endl;

    // 主循环,读入图像进行处理
    cv::Mat imRGB, imD;
    for(int ni=0; ni<nImages; ni++)
    {
        // 从文件中读取图像
        // 增量的载入数据集里图片
        imRGB = cv::imread(string(argv[3])+"/"+vstrImageFilenamesRGB[ni],CV_LOAD_IMAGE_UNCHANGED);
        imD = cv::imread(string(argv[3])+"/"+vstrImageFilenamesD[ni],CV_LOAD_IMAGE_UNCHANGED);
        double tframe = vTimestamps[ni];

        if(imRGB.empty())
        {
            cerr << endl << "Failed to load image at: "
                 << string(argv[3]) << "/" << vstrImageFilenamesRGB[ni] << endl;
            return 1;
        }

#ifdef COMPILEDWITHC11    // 获取当前时间t1
        std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
#else
        std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now();
#endif

        // 图像进入slam系统处理(彩色图/深度图/时间戳)
        SLAM.TrackRGBD(imRGB,imD,tframe);

#ifdef COMPILEDWITHC11    // 获取当前时间t2
        std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
#else
        std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now();
#endif

        double ttrack= std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count();

        vTimesTrack[ni]=ttrack;    // 记录下每帧用的时间

        // 等待下一帧的载入
        double T=0;
        if(ni<nImages-1)
            T = vTimestamps[ni+1]-tframe;
        else if(ni>0)
            T = tframe-vTimestamps[ni-1];

        if(ttrack<T)
            usleep((T-ttrack)*1e6);
    }

    // 所有图像处理完,关闭所有线程
    SLAM.Shutdown();

    // 时间统计追踪
    sort(vTimesTrack.begin(),vTimesTrack.end());  // 排序
    float totaltime = 0;
    for(int ni=0; ni<nImages; ni++)
    {
        totaltime+=vTimesTrack[ni];
    }
    cout << "-------" << endl << endl;
    cout << "median tracking time: " << vTimesTrack[nImages/2] << endl;
    cout << "mean tracking time: " << totaltime/nImages << endl;

    // 保持相机轨迹/关键帧轨迹
    SLAM.SaveTrajectoryTUM("CameraTrajectory.txt");
    SLAM.SaveKeyFrameTrajectoryTUM("KeyFrameTrajectory.txt");   

    return 0;
}
// 载入图像函数定义
void LoadImages(const string &strAssociationFilename, vector<string> &vstrImageFilenamesRGB,
                vector<string> &vstrImageFilenamesD, vector<double> &vTimestamps)
{
    ifstream fAssociation;  // 输入流
    fAssociation.open(strAssociationFilename.c_str());  // 打开指定文件
    while(!fAssociation.eof())    // 检测末尾
    {
        string s;
        getline(fAssociation,s);    // 将fAssociation流中1行读取内容到string对象s中去
        if(!s.empty())
        {
            stringstream ss;
            ss << s;
            // 关联文件中的1行:1305031453.359684【t】 rgb/1305031453.359684.png【sRGB】 
            // 1305031453.374112【t】 depth/1305031453.374112.png【sD】

            double t;
            string sRGB, sD;
            ss >> t;    // 写到t
            vTimestamps.push_back(t);   // 载入时间戳
            ss >> sRGB;
            vstrImageFilenamesRGB.push_back(sRGB);  // 载入彩色图像
            ss >> t;
            ss >> sD;
            vstrImageFilenamesD.push_back(sD);  // 载入深度图像

        }
    }
}
转自https://blog.youkuaiyun.com/LIZHONGPING00/article/details/64132677
### 使用 ORB_SLAM3 运行 TUM 数据集并用 evo 工具进行轨迹评估 #### 安装 ORB_SLAM3 和配置环境 为了运行 ORB_SLAM3 并处理 TUM 数据集,首先需要确保安装了必要的依赖项以及正确编译了 ORB_SLAM3 的源码。以下是具体操作: 1. **克隆 ORB_SLAM3 源码库** 需要从官方仓库获取最新版本的 ORB_SLAM3 源码[^3]。 ```bash git clone https://github.com/UZI-CVLab/ORB_SLAM3.git cd ORB_SLAM3 ``` 2. **设置 ROS 环境(如果适用)** 如果计划在 ROS 中集成 ORB_SLAM3,则需将其作为 catkin 包的一部分构建。如果没有使用 ROS,可以直接通过 CMake 构建。 3. **编译项目** 编辑 `CMakeLists.txt` 文件以适配本地路径,并执行以下命令完成编译: ```bash mkdir build && cd build cmake .. make -j$(nproc) ``` 4. **准备 TUM 数据集** 下载 TUM RGB-D 基准测试数据集,并解压到指定目录。例如,假设文件存储于 `/path/to/tum_dataset`。 5. **生成相机参数文件** 创建一个 YAML 格式的相机参数文件用于描述传感器特性。可以参考 TUM 提供的标准模板调整参数[^4]。 #### 执行 ORB_SLAM3 处理 TUM 数据集 一旦上述准备工作就绪,可以通过如下方式启动 ORB_SLAM3 来跟踪 TUM 序列帧序列: ```bash ./Examples/Monocular/mono_tum vocab.bin ./tum.yaml /path/to/tum_dataset associations_file.txt ``` 其中: - `vocab.bin`: 单词表文件路径; - `tum.yaml`: 相机校准参数文件; - `/path/to/tum_dataset`: 存储实际图像和时间戳的位置; - `associations_file.txt`: 关联 rgb 图像与 depth 图像的时间戳列表。 成功运行后,在默认情况下会在当前工作目录下生成名为 `CameraTrajectory.txt` 或类似的轨迹记录文件[^5]。 #### 利用 Evo 对轨迹进行定量分析 对于生成的结果文件 (如前述提到的 Keyframe 轨迹),可采用 evo 工具进一步计算 ATE/RPE 指标来衡量定位精度: 1. **安装 Evo 工具链** 参考官网指南安装 Python 版本或者独立二进制形式的应用程序[^6]: ```bash pip install evo --upgrade ``` 2. **加载真值 GT 数据** 获取对应场景的真实地面实况轨迹 (`ground_truth.txt`) ,通常由 TUM 官方提供。 3. **比较预测轨迹 vs 地面实况** 使用下面脚本来对比两组坐标系下的差异: ```bash evo_ape tum ground_truth.txt CameraTrajectory.txt --plot --save_plot output.png ``` 此外还可以针对局部片段估计 RPE 性能指标: ```bash evo_rpe tum ground_truth.txt CameraTrajectory.txt -r trans_part --delta 1 --delta_unit s ``` 以上流程涵盖了从初始化实验条件直至最终获得量化评价结果的整体过程。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值