视觉slam十四讲—利用编译好的orbslam2来运行自己的video(这里是高翔博士作业给出的参考代码)

利用编译好的orbslam2来运行自己的video

一、首先在orbslam2的目录下,也就是在build的同一级目录上创建一个myvideo.cpp文件,具体的代码如下:

对里面用到的几个文件进行指定路径的修改就可以了。
其中yaml是配置文件,下面会给出。
ORBvoc.txt文件是 编译好orbslam2就会有的。
video文件自己录制就可以了,参数可以在下面的yaml文件中进行修改。

//
// Created by xiang on 11/29/17.
//

// 该文件将打开给定的视频文件,并将图像传递给ORB-SLAM2进行定位

// 需要opencv
#include 

// ORB-SLAM的系统接口
#include "System.h"

#include 
#include    // for time stamp
#include 

using namespace std;

// 参数文件与字典文件
// 如果你系统上的路径不同,请修改它
string parameterFile = "./myvideo.yaml";
string vocFile = "./Vocabulary/ORBvoc.txt";

// 视频文件
string videoFile = "./myvideo.mp4";

int main(int argc, char **argv) {

    // 声明 ORB-SLAM2 系统
    ORB_SLAM2::System SLAM(vocFile, parameterFile, ORB_SLAM2::System::MONOCULAR, true);

    // 获取视频图像
    cv::VideoCapture cap(videoFile);    // change to 1 if you want to use USB camera.

    // 记录系统时间
    auto start = chrono::system_clock::now();

    while (1) {
        cv::Mat frame;
        cap >> frame;   // 读取相机数据
        if ( frame.data == nullptr )
            break;

        // rescale because image is too large
        cv::Mat frame_resized;
        cv::resize(frame, frame_resized, cv::Size(640,360));

        auto now = chrono::system_clock::now();
        auto timestamp = chrono::duration_cast<chrono::milliseconds>(now - start);
        SLAM.TrackMonocular(frame_resized, double(timestamp.count())/1000.0);
        cv::waitKey(30);
    }

    SLAM.Shutdown();
    return 0;
}

二、创建一个myvideo.yaml文件,这个文件的位置具体上面的myvideo.cpp中的程序文件定下来,编译的时候用不到这个文件。

%YAML:1.0

#--------------------------------------------------------------------------------------------
# Camera Parameters. Adjust them!
#--------------------------------------------------------------------------------------------

# Camera calibration and distortion parameters (OpenCV) 
Camera.fx: 500.0
Camera.fy: 500.0
Camera.cx: 320.0
Camera.cy: 180.0

Camera.k1: 0
Camera.k2: 0
Camera.p1: 0
Camera.p2: 0
Camera.k3: 0

# Camera frames per second 
Camera.fps: 30.0

# Color order of the images (0: BGR, 1: RGB. It is ignored if images are grayscale)
Camera.RGB: 0

#--------------------------------------------------------------------------------------------
# ORB Parameters
#--------------------------------------------------------------------------------------------

# ORB Extractor: Number of features per image
ORBextractor.nFeatures: 2000

# ORB Extractor: Scale factor between levels in the scale pyramid 	
ORBextractor.scaleFactor: 1.2

# ORB Extractor: Number of levels in the scale pyramid	
ORBextractor.nLevels: 8

# ORB Extractor: Fast threshold
# Image is divided in a grid. At each cell FAST are extracted imposing a minimum response.
# Firstly we impose iniThFAST. If no corners are detected we impose a lower value minThFAST
# You can lower these values if your images have low contrast			
ORBextractor.iniThFAST: 20
ORBextractor.minThFAST: 7

#--------------------------------------------------------------------------------------------
# Viewer Parameters
#--------------------------------------------------------------------------------------------
Viewer.KeyFrameSize: 0.05
Viewer.KeyFrameLineWidth: 1
Viewer.GraphLineWidth: 0.9
Viewer.PointSize: 2
Viewer.CameraSize: 0.08
Viewer.CameraLineWidth: 3
Viewer.ViewpointX: 0
Viewer.ViewpointY: -0.7
Viewer.ViewpointZ: -1.8
Viewer.ViewpointF: 500

三、最重要的一步!记得修改orbslam2下的CmakeList.txt文件,在最后一行加上

add_executable(myvideo myvideo.cpp)
target_link_libraries(myvideo ${PROJECT_NAME})

然后

cd build
cmake ..
make -j8

最后生成的文件在

slam/ORB_SLAM2-master/Examples/Monocular/myvideo

是个可执行文件,那个配置文件yaml文件根据里面的程序的路径放置就可以了。

你可能感兴趣的:(slam的学习)