高翔老師《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
參考博客: