用视频和摄像头测试orb_slam3

用已拍摄好的视频测试

终端进入该目录下:/Workspace/src/ORB_SLAM3/Examples/Monocular

使用命令创建.cpp文件

sudo gedit myvideo.cpp

编辑myvideo.cpp

//需要opencv库
#include <opencv2/opencv.hpp>

//ORB_SLAM的系统接口
#include "System.h"

#include <string>
//计算时间
#include <chrono>   
#include <iostream>

using namespace std;

//如果系统的路径不同,需要修改路径
string parameterFile = "./myvideo.yaml";
string vocFile = "./../../Vocabulary/ORBvoc.txt";

//视频文件,该示例中视频文件存放在/Workspace/src/ORB_SLAM3/Examples/Monocular下
string videoFile = "./video.mp4";

int main(int argc, char **argv){
	//声明ORB_SLAM3系统
	ORB_SLAM3::System SLAM(vocFile, parameterFile, ORB_SLAM3::System::MONOCULAR, true);
	
	//获取视频图像
	cv::VideoCapture cap(videoFile); //如果使用的是USB相机,将该参数修改成接口名称,如:0,1
	
	//记录系统时间
	auto start = chrono::system_clock::now();
	
	while(1){
		cv::Mat frame;
		cap >> frame;  //读取相机数据
		if(frame.data == nullptr)
			break;
		cv::Mat frame_resized;
		cv::resize(frame, frame_resized, cv::Size(960,540));//运行时显示的视频的尺寸
		
		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;
}

在CmakeLists.txt末尾添加:

add_executable(myvideo Examples/Monocular/myvideo.cpp)
target_link_libraries(myvideo ${PROJECT_NAME})

myvideo.yaml配置文件

%YAML:1.0

#--------------------------------------------------------------------------------------------
# Camera Parameters. Adjust them!
#--------------------------------------------------------------------------------------------
Camera.type: "PinHole"

# Camera calibration and distortion parameters (OpenCV) 
Camera.fx: 614.3472290039062
Camera.fy: 613.3615112304688
Camera.cx: 314.36767578125
Camera.cy: 239.8182830810547

Camera.k1: 0.0
Camera.k2: 0.0
Camera.p1: 0.0
Camera.p2: 0.0
Camera.k3: 0.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: 1

# Camera resolution
Camera.width: 1920
Camera.height: 1080

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

回到Workspace目录下,执行catkin_make命令
进入到Workspace/src/ORB_SLAM3/目录下,打开终端,执行./build.sh命令,等待生成myvideo可执行文件
该路径下Workspace/src/ORB_SLAM3/Examples/Monocular,打开终端,执行./myvideo

用摄像头进行测试

1.安装usb_cam
在Workspace/src目录下载安装usb_cam

git clone https://github.com/bosch-ros-pkg/usb_cam.git usb_cam 

若是下载较慢,或是报错连接超时,可修改为:

git clone git://github.com/bosch-ros-pkg/usb_cam.git usb_cam 

下载完成后进入usb_cam目录下

mkdir build 
cd build 
cmake ..
make
  1. 修改myvideo.cpp文件
//需要opencv库
#include <opencv2/opencv.hpp>

//ORB_SLAM的系统接口
#include "System.h"

#include <string>
//计算时间
#include <chrono>   
#include <iostream>

using namespace std;

//如果系统的路径不同,需要修改路径
string parameterFile = "./myvideo.yaml";
string vocFile = "./../../Vocabulary/ORBvoc.txt";

int main(int argc, char **argv){
	//声明ORB_SLAM3系统
	ORB_SLAM3::System SLAM(vocFile, parameterFile, ORB_SLAM3::System::MONOCULAR, true);
	
	//获取视频图像
	cv::VideoCapture cap(0);
	
	//记录系统时间
	auto start = chrono::system_clock::now();
	
	while(1){
		cv::Mat frame;
		cap >> frame;  //读取相机数据
		if(frame.data == nullptr)
			break;
		cv::Mat frame_resized;
		cv::resize(frame, frame_resized, cv::Size(960,540));//运行时显示的视频的尺寸
		
		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;
}

若是不知道相机具体参数,可使用命令查看

ls /dev/video*

在这里插入图片描述
若是直接采用摄像头测试,还需对配置文件myvideo.yaml与CmakeLists.txt进行配置,具体的配置过程如上。

  1. 运行
    到Workspace目录下,执行catkin_make命令
    进入到Workspace/src/ORB_SLAM3/目录下,打开终端,执行./build.sh命令,等待生成myvideo可执行文件
    打开终端运行roscore
    重新打开另一个终端,到Workspace/src/ORB_SLAM3/Examples/Monocluar目录下,执行./myvideo
    运行结果如下:
    在这里插入图片描述
  • 1
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值