ros2 humble 华硕rgbd xtion驱动
记录一下:
1、sudo apt-get install ros-humble-openni2-camera
2、ros2 launch openni2_camera camera_with_cloud.launch.py
再启动rviz2
可以看到rgb和深度图,但没有点云数据。
/camera/depth_registered/image_raw 正常
/camera/depth_registered/points 无数据
/camera/rgb/image_raw 正常
根据rgb像素index获取对应的深度值
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import numpy as np
import message_filters
class DepthFromRGB(Node):
def __init__(self):
super().__init__('depth_from_rgb')
self.bridge = CvBridge()
# 订阅RGB图像和深度图像的话题
self.rgb_sub = message_filters.Subscriber(self, Image, '/camera/rgb/image_raw')
self.depth_sub = message_filters.Subscriber(self, Image, '/camera/depth_registered/image_raw')
# 使用消息过滤器同步RGB图像和深度图像
self.ts = message_filters.ApproximateTimeSynchronizer([self.rgb_sub, self.depth_sub], queue_size=10, slop=0.1)
self.ts.registerCallback(self.image_callback)
# 像素索引
self.pixel_x = 320 # 示例:图像中心的x坐标
self.pixel_y = 240 # 示例:图像中心的y坐标
def image_callback(self, rgb_msg, depth_msg):
try:
# 将ROS图像消息转换为OpenCV图像
rgb_image = self.bridge.imgmsg_to_cv2(rgb_msg, desired_encoding='bgr8')
depth_image = self.bridge.imgmsg_to_cv2(depth_msg, desired_encoding='passthrough')
# 获取深度值
depth_value = depth_image[self.pixel_y, self.pixel_x]
# 打印深度值
self.get_logger().info(f'Depth value at ({self.pixel_x}, {self.pixel_y}): {depth_value} meters')
except Exception as e:
self.get_logger().error(f'Error processing images: {e}')
def main(args=None):
rclpy.init(args=args)
node = DepthFromRGB()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()