opencv 如何在ROS中订阅和发布图片

j5fpnvbx  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(244)

我试图订阅“/camera/image_color”主题,这是来自我的相机的数据,然后我想在opencv中对这些图像做一些voodoo操作,并以特定的频率发布它们,这样我就可以用另一个节点订阅它们。
到目前为止,我已经尝试了下面的代码及其许多变体。在这一点上,代码什么也没做。没有图像被发布到“/imagetimer”主题。我得到了一个输出“Timing images”,然后什么也没发生。

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import os
import numpy as np

class Nodo(object):
    def __init__(self):
        # Params
        self.image = None
        self.br = CvBridge()
        # Node cycle rate (in Hz).
        self.loop_rate = rospy.Rate(1)

        # Publishers
        self.pub = rospy.Publisher('imagetimer', Image,queue_size=10)

        # Subscribers
        rospy.Subscriber("/camera/image_color",Image,self.callback)

    def callback(self, msg):
        self.image = self.br.imgmsg_to_cv2(msg)

    def start(self):
        rospy.loginfo("Timing images")
        rospy.spin()
        br = CvBridge()
        while not rospy.is_shutdown():
            rospy.loginfo('publishing image')
            #br = CvBridge()
            self.pub.publish(br.cv2_to_imgmsg(self.image))
            rate.sleep()

if __name__ == '__main__':
    rospy.init_node("imagetimer111", anonymous=True)
    my_node = Nodo()
    my_node.start()
oug3syen

oug3syen1#

一旦你运行了rospy.spin(),代码就不会继续运行了。在rospy中,一旦你有了rospy.Subsriber()行,它就会启动另一个线程进行回调。rospy.spin()本质上保持了节点的活跃,所以回调和保持chugging。这里你使用了一个while循环来保持节点的活跃,所以不需要rospy.spin()。这个版本应该可以工作:

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import os
import numpy as np

class Nodo(object):
    def __init__(self):
        # Params
        self.image = None
        self.br = CvBridge()
        # Node cycle rate (in Hz).
        self.loop_rate = rospy.Rate(1)

        # Publishers
        self.pub = rospy.Publisher('imagetimer', Image,queue_size=10)

        # Subscribers
        rospy.Subscriber("/camera/image_color",Image,self.callback)

    def callback(self, msg):
        rospy.loginfo('Image received...')
        self.image = self.br.imgmsg_to_cv2(msg)

    def start(self):
        rospy.loginfo("Timing images")
        #rospy.spin()
        while not rospy.is_shutdown():
            rospy.loginfo('publishing image')
            #br = CvBridge()
            if self.image is not None:
                self.pub.publish(self.br.cv2_to_imgmsg(self.image))
            self.loop_rate.sleep()
            
if __name__ == '__main__':
    rospy.init_node("imagetimer111", anonymous=True)
    my_node = Nodo()
    my_node.start()
zlhcx6iw

zlhcx6iw2#

在我的例子中,我发现图像主题有压缩的图像。请验证您是否是这种情况。
我使用了下面的代码来读取.bag文件。如果你发现你的ROS主题中有压缩图像,你可以使用这段代码的一部分来执行转换

bag = rosbag.Bag(os.path.join(path_to_bags, bag_list[index]))
        for topic, msg, t in bag.read_messages(topics=['/conti115/image_raw/compressed']):
            try:
                img = bridge.compressed_imgmsg_to_cv2(msg)
            except CvBridgeError, e:
                print e
            txt = str(datetime.datetime.fromtimestamp(t.to_sec()))
            cv2.rectangle(img, (0, 0), (500, 40), (0,0,0), -1)
            cv2.putText(img, txt, (0, 30), 1, 2, (255, 255, 255), 2)
            cv2.imshow("win",img)

            wtr.write(img)
            if cv2.waitKey(5)==27:
                cv2.destroyAllWindows()
                break
        bag.close()

相关问题