ROS使用python发布Image topic

ROS下使用python发布Image消息

博主去官网看了很久,都没有python端发布Image的例子。特地来补充一下。
首先Image类型是源自于sensor_msgs.msg里面的。其类型包括以下几个成员。这里写图片描述
(摘自于ros官网)


首先我们要先初始化header,因为Image不需要fix_frame等东西的定义,所以发布的时候只需要给一个时间戳就可以了。
接下来就是我们的图片的大小和编码了。编码有很多种,这里博主使用的rbg格式的,所以编码也是rbg8格式。

接下来就是is_bigendian翻译过来就是是否为大端模式,大端模式为一种数据排列方式。例如你传输一个整数,1024。因为在系统中都是以byte传输了。大端模式发送就是0x04,0x00。小端模式就是0x00,0x04。但是对于我这个8bit的数值来说,不存在大端小端的。所以可以忽略。

step是值每一行的数据有多少byte,我的一行为1241个点,每个点有三个byte,所以,我的step应该为1241×3。

data就是我们的传输的数据了。不能直接的把cv2读出来的数值赋过去,因为此data接受的是一个string,而不是array。所以我们要先转一下。


接下来就是所有的程序了。(你可以从博主github上面下载本样例程序。git@github.com:lucky-ing/ROS_Publish_Image.git)


import rospy
from std_msgs.msg import Header
from sensor_msgs.msg import Image
import os
import cv2
import numpy as np
import time
IMAGE_WIDTH=1241
IMAGE_HEIGHT=376

rospy.init_node("listener", anonymous=True)
image_pubulish=rospy.Publisher('/camera/image_raw',Image,queue_size=1)
def publish_image(imgdata):
    image_temp=Image()
    header = Header(stamp=rospy.Time.now())
    header.frame_id = 'map'
    image_temp.height=IMAGE_HEIGHT
    image_temp.width=IMAGE_WIDTH
    image_temp.encoding='rgb8'
    image_temp.data=np.array(imgdata).tostring()
    #print(imgdata)
    #image_temp.is_bigendian=True
    image_temp.header=header
    image_temp.step=1241*3
    image_pubulish.publish(image_temp)
file_list=os.listdir('/home/lucky/open/data/kitti/00/image_0')
file_list.sort()
for i in file_list:
    img=cv2.imread('/home/lucky/open/data/kitti/00/image_0/'+i)
    publish_image(img)
    #time.sleep(1)
    cv2.imshow('123',img)
    key=cv2.waitKey(1000)
    if key==ord('q'):
        break
    print("pubulish")

any question you can contact lucky, by email @lucky_lsq@163.com

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Apriltag is a popular library for detecting and identifying visual fiducial markers called tags. The `apriltag_ros` package is a ROS wrapper for the Apriltag library, allowing you to use Apriltag functionalities within a ROS environment. It provides ROS nodes that can subscribe to image topics, detect Apriltags in the images, and publish the detected tag poses. To use `apriltag_ros` in Python, you can follow these steps: 1. Install the `apriltag_ros` package: ```bash sudo apt-get install ros-<distro>-apriltag-ros ``` Replace `<distro>` with your ROS distribution (e.g., melodic, noetic). 2. Create a ROS package (if you haven't already) where you'll write your Python code: ```bash cd ~/catkin_ws/src catkin_create_pkg my_apriltag_pkg rospy apriltag_ros ``` Replace `my_apriltag_pkg` with your desired package name. 3. In your Python script, import the necessary modules: ```python #!/usr/bin/env python import rospy from sensor_msgs.msg import Image from cv_bridge import CvBridge from apriltag_ros.msg import AprilTagDetectionArray ``` 4. Initialize the ROS node and create a subscriber to the image topic: ```python rospy.init_node('apriltag_detector') bridge = CvBridge() def image_callback(msg): cv_image = bridge.imgmsg_to_cv2(msg, desired_encoding='passthrough') # Perform Apriltag detection using cv_image image_sub = rospy.Subscriber('/camera/image_raw', Image, image_callback) ``` Replace `/camera/image_raw` with the appropriate image topic for your setup. 5. Process the received image in the callback function and publish the detected tag poses: ```python def image_callback(msg): cv_image = bridge.imgmsg_to_cv2(msg, desired_encoding='passthrough') # Perform Apriltag detection using cv_image # Publish the detected tag poses detection_array = AprilTagDetectionArray() # Populate detection_array with detected tag poses pub = rospy.Publisher('/apriltag/detections', AprilTagDetectionArray, queue_size=10) pub.publish(detection_array) ``` Replace `/apriltag/detections` with the desired topic to publish the detected tag poses. 6. Finally, run your ROS node: ```bash rosrun my_apriltag_pkg my_apriltag_node.py ``` Remember to replace `my_apriltag_pkg` and `my_apriltag_node.py` with your actual package and node names. This is a basic example to get you started with `apriltag_ros` in Python. You can find more information about the package and its functionalities in the official ROS documentation and the `apriltag_ros` GitHub repository.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值