ORB-SLAM2源码学习:System.h 头文件

兄弟姐妹们我放假回来了,继续学习ORB-SLAM2的代码吧。

前言

头文件的标注放到了最后一部分,可以跳过前边的逻辑直接看后边。

如果对大家的学习有帮助请点赞收藏(谢谢)。

有什么问题错误请在评论区批评指正(我会及时修改)。

一、代码逻辑

1.头文件和命名空间

a.引入必要的标准库和OpenCV库头文件。

b.使用命名空间ORB-SLAM2来组织代码。

c.创建的头文件要用“”。

#include "Tracking.h"
#include "FrameDrawer.h"
#include "MapDrawer.h"
#include "Map.h"
#include "LocalMapping.h"
#include "LoopClosing.h"
#include "KeyFrameDatabase.h"
#include "ORBVocabulary.h"
#include "Viewer.h"
namespace ORB_SLAM2
{}

2. 类的声明

a.仅声明类而不定义,允许在system类中使用这些类作为指针或引用。

class Viewer;
class FrameDrawer;
class Map;
class Tracking;
class LocalMapping;
class LoopClosing;

b.定义system类,作为整个SLAM系统的核心。 

class System
{
public:
....
private:
....
    };

3.构造函数

a.定义构造函数,接收ORB词汇文件、设置文件、传感器类型和是否使用可视化的布尔值。

b.在构造函数内初始化成员变量,并加载配置文件。

System(const string &strVocFile, const string &strSettingsFile, const eSensor sensor, const bool bUseViewer = true);

4.处理帧的方法 

a.定义处理不同类型图像帧的方法。

b.每个方法返回相机的位姿,如果跟踪失败则返回空矩阵。

cv::Mat TrackStereo(const cv::Mat &imLeft, const cv::Mat &imRight, const double &timestamp);
cv::Mat TrackRGBD(const cv::Mat &im, const cv::Mat &depthmap, const double &timestamp);
cv::Mat TrackMonocular(const cv::Mat &im, const double &timestamp);

 5.控制线程的方法

void ActivateLocalizationMode();
void DeactivateLocalizationMode();

6.地图管理 

a.MapChanged():检查地图是否发生了重大变化。

b.Reset():重置系统,清除地图数据。

bool MapChanged();
void Reset();

 7.线程管理

  请求所有线程完成并等待其结束。

void Shutdown();

 8.轨迹保存

按特定数据集格式保存轨迹和关键帧。

void SaveTrajectoryTUM(const string &filename);
void SaveKeyFrameTrajectoryTUM(const string &filename);
void SaveTrajectoryKITTI(const string &filename);

9.获取跟踪状态 

std::vector<MapPoint*> GetTrackedMapPoints();
std::vector<cv::KeyPoint> GetTrackedKeyPointsUn();

10.定义私有成员变量 

eSensor mSensor;
ORBVocabulary* mpVocabulary;
KeyFrameDatabase* mpKeyFrameDatabase;
Map* mpMap;
Tracking* mpTracker;
LocalMapping* mpLocalMapper;
LoopClosing* mpLoopCloser;
Viewer* mpViewer;
...

11.互斥锁和状态管理 

  定义互斥锁以确保多线程访问时的安全。

std::mutex mMutexReset;
bool mbReset;
std::mutex mMutexMode;
bool mbActivateLocalizationMode;
bool mbDeactivateLocalizationMode;

 12.线程与状态管理

std::vector<MapPoint*> mTrackedMapPoints;
std::vector<cv::KeyPoint> mTrackedKeyPointsUn;
std::mutex mMutexState;

注意:每个部分的实现都应关注系统的整体逻辑,并确保线程安全和模块化设计,以便于后续维护和扩展。 

二.已标注的头文件

请忽略标注的风格,专注于理解标注的内容

#ifndef SYSTEM_H
#define SYSTEM_H

#include<string>
#include<thread>
#include<opencv2/core/core.hpp>

#include "Tracking.h"
#include "FrameDrawer.h"
#include "MapDrawer.h"
#include "Map.h"
#include "LocalMapping.h"
#include "LoopClosing.h"
#include "KeyFrameDatabase.h"
#include "ORBVocabulary.h"
#include "Viewer.h"

namespace ORB_SLAM2
{

class Viewer;
class FrameDrawer;
class Map;
class Tracking;
class LocalMapping;
class LoopClosing;

class System
{
public:
    // Input sensor
    enum eSensor{
        MONOCULAR=0,
        STEREO=1,
        RGBD=2
    };

public:

    // Initialize the SLAM system. It launches the Local Mapping, Loop Closing and Viewer threads.
    // SLAM 系统的初始化包括启动局部建图(Local Mapping)、回环检测(Loop Closing)和可视化(Viewer)线程。
    /*
      1.strVocFile  ORB 词汇表(用于特征描述和匹配)的文件路径。
      2.strSettingsFile  配置文件路径,包含相机参数和其他系统设置。
      3.sensor  枚举类型 eSensor,表示传感器类型 对应system类声明的公共接口处。
      4.bUseViewer  布尔值,是否启用可视化界面来查看 SLAM 结果。
    */
    System(const string &strVocFile, const string &strSettingsFile, const eSensor sensor, const bool bUseViewer = true);

    // Proccess the given stereo frame. Images must be synchronized and rectified.
    // 处理给定的双目的图像帧。入的立体图像(左图和右图)必须是同步的并且经过了校正。
    // Input images: RGB (CV_8UC3) or grayscale (CV_8U). RGB is converted to grayscale.
    // 输入图像的格式:RGB图像的格式为 CV_8UC3 灰度图像的格式为 CV_8U 如果输入的是RGB图像,函数内部会将其转换为灰度图像进行处理
    // Returns the camera pose (empty if tracking fails).
    // 函数的返回值是一个 cv::Mat 类型的对象,表示相机的位姿(位置和方向)。如果跟踪失败,返回的结果将是一个空的矩阵。
    cv::Mat TrackStereo(const cv::Mat &imLeft, const cv::Mat &imRight, const double &timestamp);

    // Process the given rgbd frame. Depthmap must be registered to the RGB frame.
    // Input image: RGB (CV_8UC3) or grayscale (CV_8U). RGB is converted to grayscale.
    // Input depthmap: Float (CV_32F).
    // Returns the camera pose (empty if tracking fails).
    cv::Mat TrackRGBD(const cv::Mat &im, const cv::Mat &depthmap, const double &timestamp);

    // Proccess the given monocular frame
    // Input images: RGB (CV_8UC3) or grayscale (CV_8U). RGB is converted to grayscale.
    // Returns the camera pose (empty if tracking fails).
    cv::Mat TrackMonocular(const cv::Mat &im, const double &timestamp);

    // This stops local mapping thread (map building) and performs only camera tracking.
    // 停止本地映射线程(即地图构建),并仅执行相机跟踪。
    void ActivateLocalizationMode();

    // This resumes local mapping thread and performs SLAM again.
    // 重新开始 SLAM 过程,这个函数可以恢复原来的地图构建线程。
    void DeactivateLocalizationMode();

    // Returns true if there have been a big map change (loop closure, global BA)
    //这个函数返回一个布尔值,指示自上次调用该函数以来,是否发生了重大地图变化(如循环闭合或全局BA)。
    // since last call to this function
    bool MapChanged();

    // Reset the system (clear map)
    // 重置系统,清除地图。
    void Reset();

    // All threads will be requested to finish.
    // It waits until all threads have finished.
    // This function must be called before saving the trajectory.
    // 请求系统中的所有线程完成它们的工作,并等待这些线程结束,保存轨迹之前必须要调用这个函数。
    void Shutdown();

    // Save camera trajectory in the TUM RGB-D dataset format.
    //
    // Only for stereo and RGB-D. This method does not work for monocular.
    // 该方法只支持双目相机和 RGB-D 传感器,不支持单目相机(monocular)。
    // Call first Shutdown() 要先调用Shutdown()函数。
    void SaveTrajectoryTUM(const string &filename);

    // Save keyframe poses in the TUM RGB-D dataset format.
    // 将所有关键帧的位姿以 TUM RGB-D 数据集格式保存。
    // This method works for all sensor input.
    // 此方法支持所有类型的传感器输入,包括单目、立体和 RGB-D
    // Call first Shutdown() 要先调用Shutdown()函数。
    void SaveKeyFrameTrajectoryTUM(const string &filename);

    // Save camera trajectory in the KITTI dataset format.
    // 将相机的轨迹保存为 KITTI 数据集格式。
    // Only for stereo and RGB-D. This method does not work for monocular.
    // 该方法只支持立体相机和 RGB-D 传感器,不支持单目相机。
    // Call first Shutdown() 要先调用Shutdown()函数。
    void SaveTrajectoryKITTI(const string &filename);

    // TODO: Save/Load functions 这两个函数的实现尚未完成(TODO)
    // SaveMap(const string &filename);//用于将当前的地图数据保存到指定的文件中。
    // LoadMap(const string &filename);//用于从指定的文件中加载地图数据。

    // Information from most recent processed frame
    // You can call this right after TrackMonocular (or stereo or RGBD)
    int GetTrackingState();
    std::vector<MapPoint*> GetTrackedMapPoints();// 获取被跟踪的地图点: GetTrackedMapPoints()返回一个指向当前被跟踪地图点的指针向量。
    std::vector<cv::KeyPoint> GetTrackedKeyPointsUn();
    // 获取未尺度化的跟踪关键点:“未尺度化”特指这些特征点的原始坐标,没有经过任何缩放或归一化处理,通常保留了其在原始图像中的位置和大小信息。

private:

    // Input sensor 输入传感器
    eSensor mSensor;

    // ORB vocabulary used for place recognition and feature matching.
    // ORB词汇用于特征匹配和位置识别。
    ORBVocabulary* mpVocabulary;

    // KeyFrame database for place recognition (relocalization and loop detection).
    // 存储所有关键帧的信息,用于重定位和循环检测,确保系统能够识别先前的关键帧位置。
    KeyFrameDatabase* mpKeyFrameDatabase;

    // Map structure that stores the pointers to all KeyFrames and MapPoints.
    // 包含所有的关键帧和地图点
    Map* mpMap;

    // Tracker. It receives a frame and computes the associated camera pose.
    // It also decides when to insert a new keyframe, create some new MapPoints and
    // performs relocalization if tracking fails.
    // 负责跟踪相机的运动,决定何时插入新关键帧、创建新的地图点,以及在跟踪失败时进行重新定位。
    Tracking* mpTracker;

    // Local Mapper. It manages the local map and performs local bundle adjustment.
    // 管理局部地图并执行局部束调整
    LocalMapping* mpLocalMapper;

    // Loop Closer. It searches loops with every new keyframe. If there is a loop it performs
    // a pose graph optimization and full bundle adjustment (in a new thread) afterwards.
    // 闭环检测器。它会在每一个新的关键帧中搜索闭环。如果检测到闭环,
    // 它会进行位姿图优化,并在之后的一个新线程中执行全局束调整。
    LoopClosing* mpLoopCloser;

    // The viewer draws the map and the current camera pose. It uses Pangolin.
    // 将系统中的地图和当前相机位姿可视化。它使用 Pangolin 作为图形库进行绘制。
    // Pangolin是一个图形库。
    Viewer* mpViewer;

    FrameDrawer* mpFrameDrawer;
    MapDrawer* mpMapDrawer;

    // System threads: Local Mapping, Loop Closing, Viewer.
    // The Tracking thread "lives" in the main execution thread that creates the System object.
    // 系统线程:局部建图(Local Mapping)、闭环检测(Loop Closing)、可视化(Viewer)。
    // 跟踪线程(Tracking thread)运行在创建 System 对象的主执行线程中。
    /*多线程编程,多个线程共享资源,因此可能会出现竞争条件(race condition)。
    为避免这种情况,程序中需要使用锁(mutex)等同步机制来确保数据的一致性和安全性。*/
    std::thread* mptLocalMapping;
    std::thread* mptLoopClosing;
    std::thread* mptViewer;

    // Reset flag
    // 多个线程可能会同时访问和修改某些共享资源。为了避免线程竞争,我们需要使用互斥锁来确保同一时间只有一个线程可以访问这些资源。
    std::mutex mMutexReset;//互斥锁
    bool mbReset;// 是否重置系统

    // Change mode flags
    std::mutex mMutexMode;
    bool mbActivateLocalizationMode;// 为true时,仅进行相机的位姿跟踪。
    bool mbDeactivateLocalizationMode;// 恢复原本的 SLAM 操作.

    // Tracking state
    int mTrackingState;// 表示当前的跟踪状态。
    std::vector<MapPoint*> mTrackedMapPoints;// 存储当前跟踪帧所关联的地图点。
    std::vector<cv::KeyPoint> mTrackedKeyPointsUn;// 存储当前帧中未去除重复的关键点。
    std::mutex mMutexState;
};

}// namespace ORB_SLAM

#endif // SYSTEM_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值