高翔老师《SLAM十四讲》第一讲的课后练习
1.运行一段录制好的视频作为SLAM数据输入:
这里可以是自己录制的视频,格式更改为myvideo.mp4,就可以。
脚本文件myvideo.cpp和参数配置文件myvideo.yaml,就直接贴上高翔老师的代码在下面;
运行ORB_SLAM2,假如上节测试已经安装好了,如果没有,先编译好ORB_SLAM2。
a. git clone https://github.com/raulmur/ORB_SLAM2
将ORB_SLAM2文件夹下载到本地,根据github页面的readme安装好依赖。
b. 将课程提供的myvideo.cpp、myvideo.yaml、myvideo.mp4放到Examples/myvideo文件夹下,注意修改myvideo.cpp中的文件路径。
// 参数文件与字典文件
// 如果你系统上的路径不同,请修改它
string parameterFile = "./myvideo.yaml";
string vocFile = "../../Vocabulary/ORBvoc.txt";
c. 修改ORB_SLAM2文件夹下的CMakeLists.txt文件,在最后增加如下语句
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/Examples/myvideo)
add_executable(myvideo Examples/myvideo/myvideo.cpp)
target_link_libraries(myvideo ${PROJECT_NAME})
d. 在ORB_SLAM2文件夹下打开终端
cd ORB_SLAM2
chmod +x build.sh
./build.sh
cd build
make
e. 在Examples/myvideo文件夹下找到myvideo可执行程序,./myvideo
执行
注意:这里需要鼠标点击你的运行视频画面,然后按键盘任意一个键,触发一帧一帧的运行程序,也可以直接把cv::waitKey(30);注释掉,这样就不用键盘触发,程序会一口气给你运行完视频。
运行结果:
2. USB摄像头输入实时测试
补充一点:ubuntu16.04安装USB摄像头驱动:
a.插入摄像头检查驱动
$: ls -la /dev/vid*
crw-rw----+ 1 root video 81, 0 11月 11 11:23 /dev/video0
crw-rw----+ 1 root video 81, 1 11月 12 09:30 /dev/video1
一般video0是笔记本自带,video1是usb外接摄像头。
b. 如果没有video1,需要安装驱动。
cd ~/catkin_ws/src //任意的工作空间
git clone https://github.com/bosch-ros-pkg/usb_cam.git usb_cam //下载usb_cam包
##编译
cd ..
catkin_make
##启动节点测试
roslaunch usb_cam usb_cam-test.launch
更改脚本myvideo.cpp
// 获取视频图像
//cv::VideoCapture cap(videoFile); // change to 1 if you want to use USB camera.
cv::VideoCapture cap(1);
重新编译上面1小结中的d,并运行e。
结果如下:
附上脚本代码:
//myvideo.cpp
//
// Created by xiang on 11/29/17.
//
// 该文件将打开给定的视频文件,并将图像传递给ORB-SLAM2进行定位
// 需要opencv
#include <opencv2/opencv.hpp>
// ORB-SLAM的系统接口
#include "System.h"
#include <string>
#include <chrono> // for time stamp
#include <iostream>
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
%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
参考博客:
https://blog.youkuaiyun.com/qq_33034981/article/details/98944404