ROS调试求助:如何从自定义话题/tracker_topic显示source_image图像
解决方法
方法1:终端用rostopic + Python命令行快速提取显示
不用编写完整节点,直接在终端通过一行命令提取source_image字段并转发为可查看的图像话题,再用工具查看:
rostopic echo -p /tracker_topic/source_image | python3 -c " import sys import rospy from sensor_msgs.msg import Image rospy.init_node('temp_image_relay') pub = rospy.Publisher('/temp_image_topic', Image, queue_size=10) for line in sys.stdin: if line.startswith('%'): continue fields = line.strip().split(',') if len(fields) < 10: continue img = Image() img.header.stamp = rospy.Time(float(fields[0]), float(fields[1])) img.header.frame_id = fields[2] img.height = int(fields[3]) img.width = int(fields[4]) img.encoding = fields[5] img.is_bigendian = int(fields[6]) img.step = int(fields[7]) img.data = bytes.fromhex(fields[8].replace(' ', '')) pub.publish(img) "
新开终端运行图像查看工具:
rqt_image_view /temp_image_topic
或者用image_view:
rosrun image_view image_view image:=/temp_image_topic
方法2:修复topic_tools relay_field的Python3兼容问题
如果报错是因为relay_field脚本默认用Python2,手动修改脚本即可:
- 找到脚本路径(一般在
/opt/ros/<你的ROS版本>/lib/topic_tools/relay_field) - 把开头的
#!/usr/bin/env python改成#!/usr/bin/env python3 - 检查脚本内语法,把
print语句加括号、xrange替换成range - 重新运行命令:
rosrun topic_tools relay_field /tracker_topic source_image /temp_image_topic
之后同样用rqt_image_view查看图像。
方法3:极简Python脚本(轻量化替代完整节点)
写一个仅提取图像字段的小脚本,比完整节点更轻便:
#!/usr/bin/env python3 import rospy # 替换成你的自定义消息包名和消息类名 from your_custom_msg_pkg.msg import TrackerMsg from sensor_msgs.msg import Image def msg_callback(msg): pub.publish(msg.source_image) if __name__ == '__main__': rospy.init_node('image_extractor') pub = rospy.Publisher('/extracted_image', Image, queue_size=10) rospy.Subscriber('/tracker_topic', TrackerMsg, msg_callback) rospy.spin()
保存为extract_image.py,赋予执行权限后运行:
chmod +x extract_image.py rosrun your_package_name extract_image.py
最后用rqt_image_view /extracted_image查看图像。
内容的提问来源于stack exchange,提问作者Bart
相关产品推荐
相关产品推荐

