1.先去dai_ws工作区(OAK相机工作区)下发布双目信息
roslaunch depthai_examples stereo_node.launch
2.录制指令
rosbag record -O [bag_name] #不加中括号
3.查看录制的bag文件
rosbag play [bag_file] #不加中括号
rostopic list 查看话题名称,打开rviz添加话题即可查看录制好的bag文件
4.录制的视频放在工作目录下方便之后转成png(可选)
5.可选择剪取bag文件(可选)
rosbag filter stereo.bag stereo_2.bag "t.to_sec() >= 1721830322.719510 and t.to_sec() <= 1721830334.645970"
6.注意修改路径
python bag2png.py
import rosbag
import cv2
from cv_bridge import CvBridge
import osbridge = CvBridge()
bag = rosbag.Bag('/home/lzd/oak_stereo_kitti_dataset/stereo_2.bag', 'r')#写自己bag文件的存放路径
left_image_dir = '/home/lzd/oak_stereo_kitti_dataset/left_image' #生成的图片存放路径,下同
right_image_dir = '/home/lzd/oak_stereo_kitti_dataset/right_image'os.makedirs(left_image_dir, exist_ok=True)
os.makedirs(right_image_dir, exist_ok=True)for topic, msg, t in bag.read_messages():
if topic == '/stereo_publisher/left/image': #使用rostopic list查看,确保正确
try:
cv_image = bridge.imgmsg_to_cv2(msg, "mono8")
timestamp = msg.header.stamp.to_sec()
file_name = f"{timestamp:.6f}.png"
save_path = os.path.join(left_image_dir, file_name)
cv2.imwrite(save_path, cv_image)
print(f"Saved left image: {file_name}")
except cv2.error as e:
print(f"Error converting left image: {e}")
elif topic == '/stereo_publisher/right/image': #使用rostopic list查看,确保正确
try:
cv_image = bridge.imgmsg_to_cv2(msg, "mono8")
timestamp = msg.header.stamp.to_sec()
file_name = f"{timestamp:.6f}.png"
save_path = os.path.join(right_image_dir, file_name)
cv2.imwrite(save_path, cv_image)
print(f"Saved right image: {file_name}")
except cv2.error as e:
print(f"Error converting right image: {e}")bag.close()
7.在生成的图片文件夹中注意对比左右图图片数量,根据时间戳选择删除,保证左右照片文件夹数量相等
8.对所有照片重命名(分别对左右图片文件夹进行操作)
python3 rename_picture.py
import os
# 指定要处理的文件夹路径
folder_path = "/home/lzd/oak_stereo_kitti_dataset/right_image"#folder_path = "/home/lzd/oak_stereo_kitti_dataset/left_image"
# 遍历文件夹中的所有文件
for i, filename in enumerate(os.listdir(folder_path)):
# 检查是否为图片文件
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
# 生成新的文件名
new_filename = f"{str(i).zfill(6)}.png"
# 构建源文件和目标文件的完整路径
src_file = os.path.join(folder_path, filename)
dst_file = os.path.join(folder_path, new_filename)
# 重命名文件
os.rename(src_file, dst_file)
print(f"File renamed: {filename} -> {new_filename}")
9.生成time.txt文件
python3 to_time.py
import os
def getName(num):
strTmp = []
strRes = ''while(num / 10):
strTmp.append(num % 10)
num = num / 10
strTmp.append(num)
n = len(strTmp)
for i in range(0,5-n):
strRes = strRes + '0'
for i in range(n-1,-1,-1):
strRes = strRes + str(strTmp[i])
return strResfile_object = open('times.txt','w') #w:打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
Ostr = ''
num = len(os.listdir('/home/lzd/oak_stereo_kitti_dataset/left_image/'))#选择原文件路径
for i in range(0,num):
name = str(i).zfill(6)
Ostr = Ostr + name +' '+ name + '.png\n'
file_object.writelines(Ostr)
file_object.close()
10.根据kitti文件夹组织形式
其中dataset_params.yaml是数据集配置文件,包括以下内容:
cam0:
cam_cx: 666.816075048802 #根据自己的相机标定结果填写
cam_cy: 365.13825670241545 #根据自己的相机标定结果填写
cam_d0: 0.0
cam_d1: 0.0
cam_d2: 0.0
cam_d3: 0.0
cam_fx: 805.9707277371266 #根据自己的相机标定结果填写
cam_fy: 804.9165005674341 #根据自己的相机标定结果填写
cam_bl: 0.07383359 #根据自己的相机标定结果填写
cam_height: 720 #根据自己的相机标定结果填写
cam_model: Pinhole #根据自己的相机标定结果填写
cam_width: 2104 #根据自己的相机标定结果填写
rx: 0.0
ry: 0.0
rz: 0.0
tx: 0.0
ty: 0.0
tz: 0.0
images_subfolder_l: image_2/
images_subfolder_r: image_3/