用ORB SLAM2跑自己的图片序列数据集

demo下载链接:https://download.csdn.net/download/ykwjt/10991327
一、生成数据集
1.生成图片

搭建好ORB-SLAM后,可以跑自己的数据进行实验了。首先进行数据准备。我这里是通过摄像头采集到的视频。先要将视频处理成一帧一帧的图片,如果采集来的是图片的话就不用做这步了。为了方便并且有序,统一将图片按顺序命名为“00001.png”,“00002.png”,···,“00xxx.png”。下面附一段读视频存成指定名字的图片的python程序,程序已将生成的图片放入指定文件夹中。若你通过其他方式将名字改成上述表达的以后,将其放到’/rgb/’文件夹中:

//视频分割为图片程序 sep.py
# coding:utf-8
import os
import cv2
 
 
def getName(num):
    if num <10:
        strRes = '0000' + str(num)
    elif num <100:
        strRes = '000' + str(num)
    elif num <1000:
        strRes = '00' + str(num)
    elif num < 10000:
        strRes = '0' + str(num)
    return strRes
 
 
cap = cv2.VideoCapture('myvideo.mp4')
# 获得码率及尺寸
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
 
# 读帧
success, frame = cap.read()
idx = 1
while cap.isOpened:
    cv2.imwrite('rgb/' + getName(idx) + '.png', frame)
    success, frame = cap.read()  # 获取下一帧
    idx = idx + 1
    cv2.imshow('frame', frame)
    cv2.waitKey(100)

cd 到这个路径下 然后执行这个python:

python sep.py

2.生成索引txt

现在要生成一个ORB-SLAM2规定的一个txt文本,用来表示图像数据集内容和关系的。参照例子,使用下面python生成txt:

//file.py
import os
 
def getName(num):
    if num <10:
        strRes = '0000' + str(num)
    elif num <100:
        strRes = '000' + str(num)
    elif num <1000:
        strRes = '00' + str(num)
    elif num < 10000:
        strRes = '0' + str(num)
    return strRes
 
file_object = open('rgb.txt','w')
Ostr = ''
num = len(os.listdir('rgb'))
for i in range(1,num+1):
    name = getName(i)
    Ostr = Ostr + name + ' rgb/' + name + '.png\n'
file_object.writelines(Ostr)
file_object.close()

cd 到这个路径下 然后执行这个python:

python file.py

3.制作相机参数文件

使用opencv进行标定内参,计算出相机的内参和畸变系数,这里就不详细说了。按照例子中的参数文件(如./Examples/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

二、运行程序进行测试
  至此,就可以像使用测试用例那样,运行程序测试自己的数据集了。

./Examples/Monocular/mono_tum Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml /home/yk/桌面/my_dataset

你可能感兴趣的:(SLAM)