sensor_msgs §
Image §
- 注意图像信息和OpenCV使用的Numpy不直接兼容,必须经过
imgmsg_to_cv2或cv2_to_imgmsg的转换
- 获取时:
def get_rgb(self):
try:
msg = rospy.wait_for_message("/front_cam/color/raw", Image, timeout=1)
self.color_raw = self.bridge.imgmsg_to_cv2(msg, "bgr8")
except rospy.ROSException:
print("Timeout! No image received.")
exit()
或
def callback(self, msg):
try:
self.color_raw = self.bridge.imgmsg_to_cv2(msg, desired_encoding="bgr8")
except Exception as e:
rospy.logerr(f"CV_BRIDGE EXCEPTION: {e}")
return
- 发布时
def pub_img(self):
if self.color_raw is not None:
img_msg = self.bridge.cv2_to_imgmsg(self.color_raw)
self.img_pub.publish(img_msg)
PointCloud §
PointCloud2 §
- 可以设定长宽(Organized,如
height=240,width=320),或不设定长宽(默认height=1宽度为点云总数,如width=240*320)
- 需要将
header.frame_id设置为特定TF(如world)
- 调用
point_cloud2(header, fields, points)创建点云信息
header包含参考坐标系,fields为描述符,points为具体数据
- 代码示例Python参考,C++参考
import struct
from sensor_msgs import point_cloud2
from sensor_msgs.msg import PointCloud2, PointField
from std_msgs.msg import Header