USB摄像头运行ORB-SLAM2

一:运行的前提

1.先完成usb摄像头相关功能包的安装和标定,可参考我之前写的文章:

Ubuntu18.04下usb相机运行与标定

2.以安装好ORB-SLAM2

二:运行

1.在相机标定后生成的calibrationdata文件中,在文件最后有2个文本文件,我这里的文本文件是

ost.txt和ost.yaml,如下图所示:

USB摄像头运行ORB-SLAM2_第1张图片

1) 将ost.yaml文件复制到ORB-SLAM2文件下,将其中的参数改为ORB_SLAM2/Examples/Monocular/TUM1.yaml文件的格式

示例:TUM1.yaml文件的格式


%YAML:1.0

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

# Camera calibration and distortion parameters (OpenCV) 
Camera.fx: 517.306408
Camera.fy: 516.469215
Camera.cx: 318.643040
Camera.cy: 255.313989

Camera.k1: 0.262383
Camera.k2: -0.953104
Camera.p1: -0.005358
Camera.p2: 0.002628
Camera.k3: 1.163314

# 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: 1

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

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

# 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

ost.yaml文件格式

USB摄像头运行ORB-SLAM2_第2张图片

 将ost.yaml文件修改为TUM1.yaml文件的格式:如下

注意:1.ORB Parameters和Viewer Parameters下面的不需要修改

             2.camera_matrix:
USB摄像头运行ORB-SLAM2_第3张图片
distortion_coefficients:[k1   k2   p1   p2   k3]
参数值对应。其中ORB参数与Viewer参数不变。

修改后文件如下:

%YAML:1.0

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

# Camera calibration and distortion parameters (OpenCV) 
Camera.fx: 822.08061
Camera.fy: 823.58442
Camera.cx: 344.54658
Camera.cy: 227.65515

Camera.k1: 0.007815
Camera.k2: 0.090786
Camera.p1: -0.000757
Camera.p2: 0.001866
Camera.k3: 0.000000

# 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: 1

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

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

# 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

2)新建一个ost.cpp文件(文件代码如下:注意该代码的文件是./ost.yaml,如果你的文件名不一样,注意修改),这段代码定义了一个SLAM对象,然后会打开你⾃带的摄像头(或视频),读取图像,并交给ORB-SLAM2 处理

// 该文件将打开你电脑的摄像头,并将图像传递给ORB-SLAM2进行定位
 
// opencv
#include 
 
// ORB-SLAM的系统接口
#include "System.h"
#include 
#include    // for time stamp
#include 
 
using namespace std;
 
// 参数文件与字典文件
// 如果你系统上的路径不同,请修改它
string parameterFile = "./ost.yaml";
string vocFile = "./Vocabulary/ORBvoc.txt";
 
int main(int argc, char **argv) {
 
    // 声明 ORB-SLAM2 系统
    ORB_SLAM2::System SLAM(vocFile, parameterFile, ORB_SLAM2::System::MONOCULAR, true);
 
    // 获取相机图像代码
    cv::VideoCapture cap(0);    // change to 1 if you want to use USB camera.
 
    // 分辨率设为640x480
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);;//设置采集视频的宽度
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);//设置采集视频的高度

    // 记录系统时间
    auto start = chrono::system_clock::now();
 
    while (1) {
        cv::Mat frame;
        cap >> frame;   // 读取相机数据
        auto now = chrono::system_clock::now();
        auto timestamp = chrono::duration_cast(now - start);
        SLAM.TrackMonocular(frame, double(timestamp.count())/1000.0);
    }
 
    return 0;
}

2.这是一个Cmake工程,接下来我们需要将这个文件与ORB SLAM2进行结合。把这些代码文件放在ORB SLAM2/目录下,在ORB SLAM2对应的CMakeLists.txt文件的末尾加入一下代码:

#生成调用摄像头可执行文件
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
add_executable(ost ost.cpp)
target_link_libraries(ost ${PROJECT_NAME})

注意:如果你的相机标定后生成的calibrationdata文件中的文本文件名为xxx.yaml,一定要将上述代码里的ost改为xxx

如图所示

USB摄像头运行ORB-SLAM2_第4张图片

3.创建build编译空间

因为在下载ORB-SLAM2的时候,ORB-SLAM2目录下面很可能已经有build空间了,并且已经编译过了,所以,可以另创一个编译空间,命名为build1,在build1目录下进行编译。以上步骤做完,ORB-SLAM2的目录如下:

USB摄像头运行ORB-SLAM2_第5张图片

 在ORB-SLAM2/build1目录下打开终端:

cmake ..
make
./ost          (执行)

至此运行成功,效果如图:

USB摄像头运行ORB-SLAM2_第6张图片

 注意:当ORB SLAM2初始化的时候,请不要直接开始旋转或者运动速度过猛烈,先左右平移运动让SLAM系统成功初始化然后进行的

你可能感兴趣的:(视觉slam,slam,经验分享,视觉检测)