请用python写一段ros程序,读取深度相机的深度图和彩色图,并分别保存至/home/hudi/pic路径下
时间: 2023-12-23 11:04:45 浏览: 169
人脸检测深度图像和彩色图像并保存
好的,以下是一个简单的ROS程序,读取深度相机的深度图和彩色图,并将它们分别保存在/home/hudi/pic路径下:
```python
#!/usr/bin/env python
import rospy
import cv2
from cv_bridge import CvBridge
from sensor_msgs.msg import Image, CameraInfo
import os
class DepthCameraSubscriber:
def __init__(self):
self.bridge = CvBridge()
self.depth_sub = rospy.Subscriber('/camera/depth/image_raw', Image, self.depth_callback)
self.color_sub = rospy.Subscriber('/camera/color/image_raw', Image, self.color_callback)
self.depth_image = None
self.color_image = None
self.depth_info = None
self.color_info = None
def depth_callback(self, msg):
self.depth_image = self.bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
def color_callback(self, msg):
self.color_image = self.bridge.imgmsg_to_cv2(msg, desired_encoding="bgr8")
def depth_info_callback(self, msg):
self.depth_info = msg
def color_info_callback(self, msg):
self.color_info = msg
def save_images(self):
if self.depth_image is not None and self.color_image is not None:
depth_path = os.path.join(os.environ['HOME'], 'pic', 'depth.png')
color_path = os.path.join(os.environ['HOME'], 'pic', 'color.png')
cv2.imwrite(depth_path, self.depth_image)
cv2.imwrite(color_path, self.color_image)
if __name__ == '__main__':
rospy.init_node('depth_camera_subscriber')
depth_subscriber = DepthCameraSubscriber()
rate = rospy.Rate(10)
while not rospy.is_shutdown():
depth_subscriber.save_images()
rate.sleep()
```
注意:在运行此程序之前,需要先启动深度相机的ROS节点。并且需要注意文件夹/home/hudi/pic必须存在,否则程序会报错。
阅读全文