如何使用ROS2的OpenCV处理图像消息

发布于 2025-02-09 05:23:59 字数 1163 浏览 3 评论 0原文

我正在尝试使用ROS2的OPENCV来处理一条图像消息,

我尝试使用bridge.imgmsg_to_cv2()将ROS2映像转换为OpENCV,但它不起作用。 这是我的代码:

import rclpy
from rclpy.node import Node

from std_msgs.msg import String
from sensor_msgs.msg import Image

import numpy as np
import cv2 as cv
from cv_bridge import CvBridge
bridge = CvBridge()

class MinimalSubscriber(Node):

    def __init__(self):
        ...

    def listener_callback(self, msg):
        self.get_logger().info('1. I heard: "%s"' % msg.width)
        self.get_logger().info('2. I heard: "%s"' % msg.encoding)
        self.cv_image = bridge.imgmsg_to_cv2(msg, 'bgra8')
        self.get_logger().info('3. I heard: "%s"' % self.cv_image)


def main(args=None):
    ...

if __name__ == '__main__':
    main()

输出:

[INFO] [1655741136.342080392] [minimal_subscriber]: 1. I heard: "512"
[INFO] [1655741136.342407086] [minimal_subscriber]: 2. I heard: "bgra8"
[INFO] [1655741136.343235582] [minimal_subscriber]: 3. I heard: "[[[180 130  70 255]
  [180 130  70 255]
  [180 130  70 255]
  ...
  [180 130  70 255]
  [180 130  70 255]
  [180 130  70 255]]
.....

有人可以帮助我吗?

i am trying to work a image message with OpenCV from ROS2

i tried to convert the ROS2 Image to OpenCV with bridge.imgmsg_to_cv2() but it did not work.
here is my code:

import rclpy
from rclpy.node import Node

from std_msgs.msg import String
from sensor_msgs.msg import Image

import numpy as np
import cv2 as cv
from cv_bridge import CvBridge
bridge = CvBridge()

class MinimalSubscriber(Node):

    def __init__(self):
        ...

    def listener_callback(self, msg):
        self.get_logger().info('1. I heard: "%s"' % msg.width)
        self.get_logger().info('2. I heard: "%s"' % msg.encoding)
        self.cv_image = bridge.imgmsg_to_cv2(msg, 'bgra8')
        self.get_logger().info('3. I heard: "%s"' % self.cv_image)


def main(args=None):
    ...

if __name__ == '__main__':
    main()

with the output:

[INFO] [1655741136.342080392] [minimal_subscriber]: 1. I heard: "512"
[INFO] [1655741136.342407086] [minimal_subscriber]: 2. I heard: "bgra8"
[INFO] [1655741136.343235582] [minimal_subscriber]: 3. I heard: "[[[180 130  70 255]
  [180 130  70 255]
  [180 130  70 255]
  ...
  [180 130  70 255]
  [180 130  70 255]
  [180 130  70 255]]
.....

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2025-02-16 05:23:59

您尝试将图像数据记录到只能显示文本的ROS节点控制台。
如果您想实际显示所需的图像:

  1. 将CVimage转换回ROS消息。
image_message = bridge.cv2_to_imgmsg(cv_image, encoding="passthrough")
  1. 将图像消息发布到您选择的主题中。
    try:
      self.image_pub.publish(image_message)
    except CvBridgeError as e:
      print(e)
  1. 收听您的主题,并使用RQT或Image_viewer主题显示图像:

    rosrun image_view image_view映像:=/您的/主题/路径

You try to log the image data to the ROS Node Console, which can only display text.
If you want to actually display the image you need to:

  1. Convert the CvImage back to a ROS Message.
image_message = bridge.cv2_to_imgmsg(cv_image, encoding="passthrough")
  1. Publish the Image Message to a topic of your choice.
    try:
      self.image_pub.publish(image_message)
    except CvBridgeError as e:
      print(e)
  1. Listen to your topic and display the Image with either RQT or the image_viewer topic:

    rosrun image_view image_view image:=/your/topic/path

荒人说梦 2025-02-16 05:23:59

ROS2 OPENCV-PYTHON DEMO

#! /usr/bin/env python3

import rclpy
from rclpy.node import Node 
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2 
    
class SimplePubSub(Node):
    def __init__(self):
        super().__init__('simple_pub_sub')

        topic_name= 'video_frames'

        self.publisher_ = self.create_publisher(Image, topic_name , 10)
        self.timer = self.create_timer(0.1, self.timer_callback)

        self.cap = cv2.VideoCapture(0)
        self.br = CvBridge()

        self.subscription = self.create_subscription(Image, topic_name, self.img_callback, 10)
        self.subscription 
        self.br = CvBridge()


    def timer_callback(self):
        ret, frame = self.cap.read()     
        if ret == True:
            self.publisher_.publish(self.br.cv2_to_imgmsg(frame))
        self.get_logger().info('Publishing video frame')


    def img_callback(self, data):
        self.get_logger().info('Receiving video frame')
        current_frame = self.br.imgmsg_to_cv2(data)
        cv2.imshow("camera", current_frame)   
        cv2.waitKey(1)


def main(args=None):
    rclpy.init(args=args)
    simple_pub_sub = SimplePubSub()
    rclpy.spin(simple_pub_sub)
    simple_pub_sub.destroy_node()
    rclpy.shutdown()

  
if __name__ == '__main__':
  main()

ros2 opencv-python demo

#! /usr/bin/env python3

import rclpy
from rclpy.node import Node 
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2 
    
class SimplePubSub(Node):
    def __init__(self):
        super().__init__('simple_pub_sub')

        topic_name= 'video_frames'

        self.publisher_ = self.create_publisher(Image, topic_name , 10)
        self.timer = self.create_timer(0.1, self.timer_callback)

        self.cap = cv2.VideoCapture(0)
        self.br = CvBridge()

        self.subscription = self.create_subscription(Image, topic_name, self.img_callback, 10)
        self.subscription 
        self.br = CvBridge()


    def timer_callback(self):
        ret, frame = self.cap.read()     
        if ret == True:
            self.publisher_.publish(self.br.cv2_to_imgmsg(frame))
        self.get_logger().info('Publishing video frame')


    def img_callback(self, data):
        self.get_logger().info('Receiving video frame')
        current_frame = self.br.imgmsg_to_cv2(data)
        cv2.imshow("camera", current_frame)   
        cv2.waitKey(1)


def main(args=None):
    rclpy.init(args=args)
    simple_pub_sub = SimplePubSub()
    rclpy.spin(simple_pub_sub)
    simple_pub_sub.destroy_node()
    rclpy.shutdown()

  
if __name__ == '__main__':
  main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文