赞
踩
运行前提:
标定摄像头(如果只是想跑通程序,这步可以跳过,等程序跑通了回头在标定也行的)
ros1 noetic摄像头标定_JT_BOT的博客-CSDN博客
已经安装好orb_slam3,没有安装好可以参考我以前的安装过程
想学ORB_SLAM3,先从编译开始,编译通不过,一切都是浮云_JT_BOT的博客-CSDN博客
一 编辑yaml文件(此步骤如果只是为跑通程序可以跳过,用程序自带的yaml文件)
找到相机标定后生成的calibrationdata文件,在文件最后有2个文本文件,我这里的文本文件是
ost.txt和ost.yaml,如下图所示:
orb-slam3目录结构,在主目录内
在ORB_SLAM3内新建文件夹cam_slam3,把标定文件ost.yaml复制进此文件夹。
参考TUM1.yaml修改ost.yaml格式。
注意:ORB Parameters和Viewer Parameters下面的不需要修改
2.camera_matrix:
对应:
# Camera calibration and distortion parameters (OpenCV)
Camera1.fx:
Camera1.fy:
Camera1.cx:
Camera1.cy:
distortion_coefficients:[k1 k2 p1 p2 k3]
参数值对应。其中ORB参数与Viewer参数不变。
修改后的 ost.yaml
- %YAML:1.0
-
- #--------------------------------------------------------------------------------------------
- # Camera Parameters. Adjust them!
- #--------------------------------------------------------------------------------------------
- File.version: "1.0"
-
- Camera.type: "PinHole"
-
- # Camera calibration and distortion parameters (OpenCV)
- Camera1.fx: 621.19783
- Camera1.fy: 624.29785
- Camera1.cx: 327.35984
- Camera1.cy: 238.12506
-
- Camera1.k1: 0.112341
- Camera1.k2: -0.170053
- Camera1.p1: -0.004971
- Camera1.p2: 0.003109
- Camera1.k3: 0.000000
-
- # Camera frames per second
- Camera.fps: 30
-
- # Color order of the images (0: BGR, 1: RGB. It is ignored if images are grayscale)
- Camera.RGB: 1
-
- # Camera resolution
- Camera.width: 640
- Camera.height: 480
-
- #--------------------------------------------------------------------------------------------
- # 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.0
- Viewer.GraphLineWidth: 0.9
- Viewer.PointSize: 2.0
- Viewer.CameraSize: 0.08
- Viewer.CameraLineWidth: 3.0
- Viewer.ViewpointX: 0.0
- Viewer.ViewpointY: -0.7
- Viewer.ViewpointZ: -1.8
- Viewer.ViewpointF: 500.0
-
在cam_slam3内新建cam_slam3.cpp
- // cam_slam3.cpp
- // 该文件将打开你电脑的摄像头,并将图像传递给ORB-SLAM3进行定位
-
- // opencv
- #include <opencv2/opencv.hpp>
-
- // ORB-SLAM的系统接口
- #include "System.h"
- #include <string>
- #include <chrono> // for time stamp
- #include <iostream>
-
- using namespace std;
-
- // 参数文件与字典文件
- // 如果你系统上的路径不同,请修改它
- string parameterFile = "../ost.yaml";//这个文件如果没有可以用程序里面的yaml文件代替,比如TUM1.yaml
- string vocFile = "../../Vocabulary/ORBvoc.txt";
-
- int main(int argc, char **argv) {
-
- // 声明 ORB-SLAM2 系统
- ORB_SLAM3::System SLAM(vocFile, parameterFile, ORB_SLAM3::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<chrono::milliseconds>(now - start);
- SLAM.TrackMonocular(frame, double(timestamp.count())/1000.0);
- }
-
- return 0;
- }
CMakeLists.txt
- # CMakeLists.txt
- cmake_minimum_required(VERSION 2.8)
- project(cam_slam3)
- find_package(OpenCV 4 REQUIRED) #opencv根据版本填写版本号
- #包含头文件
- include_directories(${OPENCV_INCLUDE_DIRS}
- #非标准头文件包含写法,根据编译报错用: sudo find / -name System.h 搜每一个缺少的头文件路径添加在下面
- ~/ORB_SLAM3/include
- ~/ORB_SLAM3
- /usr/include/eigen3
- ~/ORB_SLAM3/include/CameraModels
- )
- #生成可执行文件
- add_executable(cam_slam3 cam_slam3.cpp)
- #链接库文件
- target_link_libraries(cam_slam3 ${OpenCV_LIBS}
- #非标准的链接库文件写法,根据编译报错用:sudo find / -name libpangolin.so 搜缺少的库文件
- #按绝对路径添加缺少的库文件
- /usr/local/lib/libpangolin.so
- /lib/x86_64-linux-gnu/libOpenGL.so.0
- /lib/x86_64-linux-gnu/libGLEW.so.2.1
- ~/ORB_SLAM3/lib/libORB_SLAM3.so
-
- )
-
文件结构
编译:
- cd ORB_SLAM3/cam_slam3
- mkdir build
- cd build
- cmake ..
- make
编译完成 ./cam_slam3 运行单目程序
看到这个图像说明运行成功。
此实现难点在于编译,需要通过编译报错用 sudo find / -name 文件名 搜索缺少的头文件和库文件,通过不断调整CMakeLists.txt直至成功。
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。