RealSense .bag文件彩色图,深度图提取
RealSense .bag文件彩色图,深度图提取
代码
import roslib
import rosbag
import rospy
import cv2
import os
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError
rgb_path = './rgb/'# absolute path of extracted rgb images
depth_path = './depth/'# absolute path of extracted depth images
bridge = CvBridge()
with rosbag.Bag('20230308_194638.bag', 'r') as bag: # path for the bag
for topic,msg,t in bag.read_messages():
if topic == "/device_0/sensor_0/Depth_0/image/data": #topic different from devices
cv_image = bridge.imgmsg_to_cv2(msg, "mono16") #Z16对应 mono16
timestr = "%.8f" % msg.header.stamp.to_sec()
image_name = timestr + '.png'
cv2.imwrite(depth_path + image_name, cv_image)
# print(depth_path + image_name)
if topic == "/device_0/sensor_1/Color_0/image/data": #topic different from devices
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") # 尤其注意这个编码
timestr = "%.8f" % msg.header.stamp.to_sec()
image_name = timestr + '.png'
cv2.imwrite(rgb_path + image_name, cv_image)
需要注意的是topic,这个不同的设备可能有所区别;还有rgb的编码应该是 bgr8,深度图的编码 mono16
相关库的安装
rosbag
pip install --extra-index-url https://rospypi.github.io/simple/ rosbag
pip install roslz4 --extra-index-url https://rospypi.github.io/simple/
cv_bridge
-
下载源码https://codeload.github.com/ros-perception/vision_opencv/zip/refs/heads/noetic
-
cd至cv_bridge文件夹
-
然后命令行安装
python setup.py install
sensor_image and geometry_msgs
pip install sensor_msgs --extra-index-url https://rospypi.github.io/simple/
pip install geometry_msgs --extra-index-url https://rospypi.github.io/simple/
rospy
pip install -i https://pypi.douban.com/simple rospy
cv_bridge.boost
在这里下载 https://github.com/rospypi/simple/raw/any/cv-bridge/cv_bridge-1.13.0.post0-py2.py3-none-any.whl
pip install cv_bridge-1.13.0.post0-py2.py3-none-any.whl
参考
本文来自博客园,作者:CuriosityWang,转载请注明原文链接:https://www.cnblogs.com/curiositywang/p/17197090.html