[英]How to process a Image message with OpenCV from ROS2
我正在嘗試使用來自 ROS2 的 OpenCV 處理圖像消息
我嘗試使用bridge.imgmsg_to_cv2()
將 ROS2 Image 轉換為 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()
使用 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]]
.....
誰能幫我?
您嘗試將圖像數據記錄到 ROS 節點控制台,該控制台只能顯示文本。 如果要實際顯示圖像,則需要:
image_message = bridge.cv2_to_imgmsg(cv_image, encoding="passthrough")
try:
self.image_pub.publish(image_message)
except CvBridgeError as e:
print(e)
聆聽您的主題並使用 RQT 或 image_viewer 主題顯示圖像:
rosrun image_view image_view image:=/your/topic/path
ros2 opencv-python演示
#! /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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.