当前位置:网站首页>从.bag文件中读取并保存.jpg图片和.pcd点云
从.bag文件中读取并保存.jpg图片和.pcd点云
2022-07-02 07:06:00 【SmileAtlas】
从.bag文件中读取并保存.jpg图片和.pcd点云
#!/usr/bin/env python
#coding:utf-8
import os
import rosbag
import cv2
from cv_bridge import CvBridge
from tqdm import tqdm
import time
class ExtractBagData(object):
def __init__(self, bagfile_path, camera_topic, pointcloud_topic, root):
self.bagfile_path = bagfile_path
self.camera_topic = camera_topic
self.pointcloud_topic = pointcloud_topic
self.root = root
self.image_dir = os.path.join(root, "images")
self.pointcloud_dir = os.path.join(root, "pointcloud")
#创建提取图片和点云的目录 ./root/images root/pointcloud
if not os.path.exists(self.image_dir):
os.makedirs(self.image_dir)
if not os.path.exists(self.pointcloud_dir):
os.makedirs(self.pointcloud_dir)
def extract_camera_topic(self):
bag = rosbag.Bag(self.bagfile_path, "r")
bridge = CvBridge()
bag_data_imgs = bag.read_messages(self.camera_topic)
index = 0
# for topic, msg, t in bag_data_imgs:
# for topic, msg, t in tqdm(bag_data_imgs):
pbar = tqdm(bag_data_imgs)
for topic, msg, t in pbar:
pbar.set_description("Processing extract image id: %s" % (index+1))
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")
# print('\033[31m=\033[0m'*120)
# print(topic) # /usb_cam/image_raw
# print(msg)
# print(t) # 1616554905461126311
#print(type(cv_image)) # <type 'numpy.ndarray'>
# cv2.imshow("Image window", cv_image)
# cv2.waitKey(3)
# 如果你需要使用时间戳对提取的图片命名,可以使用msg.header.stamp.to_sec()获取时间戳
# timestr = "%.6f" % msg.header.stamp.to_sec()
cv2.imwrite(os.path.join(self.image_dir, str(index) + ".jpg"), cv_image)
index += 1
def extract_pointcloud_topic(self):
''' # 提取点云数据为pcd后缀文件,默认提取以为时间戳命名 # 提取命令:rosrun pcl_ros bag_to_pcd result.bag /velodyne_points ./pointcloud # 提取点云以时间戳命令:1616554905.476288682.pcd :return: '''
cmd = "rosrun pcl_ros bag_to_pcd %s /velodyne_points %s" % (self.bagfile_path, self.pointcloud_dir)
os.system(cmd)
# 再读取提取的pcd点云数据,把文件名修改为按照顺序索引名
pcd_files_list = os.listdir(self.pointcloud_dir)
# 因为提取的pcd是以时间戳命令的,但是存放到列表中并不是按照时间戳从小到大排列,这里对时间戳进行重新排序
pcd_files_list_sorted = sorted(pcd_files_list)
# print(zip(pcd_files_list, pcd_files_list_sorted))
index = 0
pbar = tqdm(pcd_files_list_sorted)
for pcd_file in pbar:
pbar.set_description("Processing extract poindcloud id: %s" % (index + 1))
os.rename(os.path.join(self.pointcloud_dir, pcd_file),
os.path.join(self.pointcloud_dir, str(index) + ".pcd"))
print("pcd_file name: ", pcd_file)
index += 1
if __name__ == '__main__':
bagfile_path = '/home/cyp/WorkSpace/lcf_ws/src/lidar_camera_fusion/scripts/zed_lidar_calibration.bag'
camera_topic = "/zed/zed_node/left_raw/image_raw_color"
pointcloud_topic = "/velodyne_points"
extract_bag = ExtractBagData(bagfile_path, camera_topic, pointcloud_topic, "/home/cyp/WorkSpace/lcf_ws/src/data")
extract_bag.extract_camera_topic()
extract_bag.extract_pointcloud_topic()
pip install rospkg
ModuleNotFoundError:No module named ‘Cryptodome’
pip install pycryptodomex
ModuleNotFoundError: No module named ‘gnupg’
pip install gnupg
pip install opencv-python==3.2.0.6
边栏推荐
- (五)APA场景搭建之挡位控制设置
- 2021-10-04
- Easyexcel, a concise, fast and memory saving excel processing tool
- Convert yv12 to rgb565 image conversion, with YUV to RGB test
- 13.信号量临界区保护
- 2021-09-12
- [unity3d] nested use layout group to make scroll view with dynamic sub object height
- Allure -- common configuration items
- 长投学堂上面的账户安全吗?
- Use WinDbg to statically analyze dump files (summary of practical experience)
猜你喜欢
[TS] 1368 seconds understand typescript generic tool types!
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
MPLS experiment
(5) Gear control setting of APA scene construction
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
Webui automated learning
nodejs+express+mysql简单博客搭建
flink 提交程序
AI技术产业热点分析
Use WinDbg to statically analyze dump files (summary of practical experience)
随机推荐
Leetcode+ 76 - 80 storm search topic
Thanos Receiver
Network communication learning
js promise.all
快速做出原型
UVM learning - object attribute of UVM phase
UVM - usage of common TLM port
Transport Optimization abstraction
Lunix reallocates root and home space memory
Session cookies and tokens
Pywin32打开指定窗口
axis设备的rtsp setup头中的url不能带参
618再次霸榜的秘密何在?耐克最新财报给出答案
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
Win11 arm系统配置.net core环境变量
14.信号量的代码实现
Flink实时计算topN热榜
[SUCTF2018]followme
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
2021-10-04