当前位置:网站首页>Comparison of OpenCV basic codes of ros2 foxy~galactic~humble
Comparison of OpenCV basic codes of ros2 foxy~galactic~humble
2022-06-12 00:45:00 【zhangrelay】
Reference resources :
automaticaddison.com/getting-started-with-opencv-in-ros-2-galactic-python/
automaticaddison.com/getting-started-with-opencv-in-ros-2-foxy-fitzroy-python/Recommended :YOLOX + ROS2 object detection package
You can also refer to :github.com/jeffreyttc/opencv_ros2
vision_opencv
ros2 vision_opencv Include will ROS 2 And OpenCV Interface package ,OpenCV Is a library designed for computing efficiency and real-time computer vision applications . The repository contains :
- cv_bridge:ROS 2 Image messages and OpenCV The bridge between image representation
- image_geometry: A collection of methods for processing images and pixel geometry
- opencv_tests: Integrate tests to use with opencv Features of the package
- vision_opencv: install cv_bridge and image_geometry Yuan Bao of
In order to ROS 2 And OpenCV Use it together , see also cv_bridge Details in the package .
The procedure applies to foxy/galactic/humble,windows/linux System common

In this tutorial , Will learn how to ROS 2 With the popular computer vision library OpenCV Basic knowledge of interface . These basics will provide the basis for adding vision to robot applications .
We will create an image publisher node to transfer webcam data ( I.e. video frame ) Publish to a topic , We will create an image subscriber node that subscribes to this topic .
pub Release :
| foxy | galactic |
|
|
sub subscribe :
| foxy | galactic |
|
|
humble:
pub
# Basic ROS 2 program to publish real-time streaming
# video from your built-in webcam
# Author:
# - Addison Sears-Collins
# Import the necessary libraries
import rclpy # Python Client Library for ROS 2
from rclpy.node import Node # Handles the creation of nodes
from sensor_msgs.msg import Image # Image is the message type
from cv_bridge import CvBridge # Package to convert between ROS and OpenCV Images
import cv2 # OpenCV library
class ImagePublisher(Node):
"""
Create an ImagePublisher class, which is a subclass of the Node class.
"""
def __init__(self):
"""
Class constructor to set up the node
"""
# Initiate the Node class's constructor and give it a name
super().__init__('image_publisher')
# Create the publisher. This publisher will publish an Image
# to the video_frames topic. The queue size is 10 messages.
self.publisher_ = self.create_publisher(Image, 'video_frames', 10)
# We will publish a message every 0.1 seconds
timer_period = 0.1 # seconds
# Create the timer
self.timer = self.create_timer(timer_period, self.timer_callback)
# Create a VideoCapture object
# The argument '0' gets the default webcam.
self.cap = cv2.VideoCapture(0)
# Used to convert between ROS and OpenCV images
self.br = CvBridge()
def timer_callback(self):
"""
Callback function.
This function gets called every 0.1 seconds.
"""
# Capture frame-by-frame
# This method returns True/False as well
# as the video frame.
ret, frame = self.cap.read()
if ret == True:
# Publish the image.
# The 'cv2_to_imgmsg' method converts an OpenCV
# image to a ROS 2 image message
self.publisher_.publish(self.br.cv2_to_imgmsg(frame))
# Display the message on the console
self.get_logger().info('Publishing video frame')
def main(args=None):
# Initialize the rclpy library
rclpy.init(args=args)
# Create the node
image_publisher = ImagePublisher()
# Spin the node so the callback function is called.
rclpy.spin(image_publisher)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
image_publisher.destroy_node()
# Shutdown the ROS client library for Python
rclpy.shutdown()
if __name__ == '__main__':
main()sub
# Basic ROS 2 program to subscribe to real-time streaming
# video from your built-in webcam
# Author:
# - Addison Sears-Collins
# Import the necessary libraries
import rclpy # Python library for ROS 2
from rclpy.node import Node # Handles the creation of nodes
from sensor_msgs.msg import Image # Image is the message type
from cv_bridge import CvBridge # Package to convert between ROS and OpenCV Images
import cv2 # OpenCV library
class ImageSubscriber(Node):
"""
Create an ImageSubscriber class, which is a subclass of the Node class.
"""
def __init__(self):
"""
Class constructor to set up the node
"""
# Initiate the Node class's constructor and give it a name
super().__init__('image_subscriber')
# Create the subscriber. This subscriber will receive an Image
# from the video_frames topic. The queue size is 10 messages.
self.subscription = self.create_subscription(
Image,
'video_frames',
self.listener_callback,
10)
self.subscription # prevent unused variable warning
# Used to convert between ROS and OpenCV images
self.br = CvBridge()
def listener_callback(self, data):
"""
Callback function.
"""
# Display the message on the console
self.get_logger().info('Receiving video frame')
# Convert ROS Image message to OpenCV image
current_frame = self.br.imgmsg_to_cv2(data)
# Display image
cv2.imshow("camera", current_frame)
cv2.waitKey(1)
def main(args=None):
# Initialize the rclpy library
rclpy.init(args=args)
# Create the node
image_subscriber = ImageSubscriber()
# Spin the node so the callback function is called.
rclpy.spin(image_subscriber)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
image_subscriber.destroy_node()
# Shutdown the ROS client library for Python
rclpy.shutdown()
if __name__ == '__main__':
main()边栏推荐
- Jeecgboot 3.1.0 release, enterprise low code platform based on code generator
- Kill session? This cross domain authentication solution is really elegant
- Lambda中间操作distinct
- Lambda termination operation foreach
- 2022 Tibet latest special worker (construction elevator) simulation test question bank and answers
- 2022 edition of global and Chinese high purity silicon carbide powder operation research and investment strategy analysis report
- Flutter uses local pictures
- LabVIEW Arduino电子称重系统(项目篇—1)
- How to change the font size of Apple phone WPS
- 2021 inventory: Top 10 low code development platforms at home and abroad
猜你喜欢

Seven trends in test automation that need attention

Tencent programmer roast: 1kW real estate +1kw stock +300w cash, ready to retire at the age of 35

Virtual human appears on the stage of the Winter Olympic Games, connecting elements of the meta universe

Why are the values of ordereddict not equal- Why are the values of an OrderedDict not equal?

Wechat applet Chinese English conversion

What are the advantages of Tiktok applet

Streaming Data Warehouse 存储:需求与架构

How to change the font size of Apple phone WPS

UVM: transaction level modeling (TLM) 1.0 communication standard

模块八-设计消息队列存储消息数据的 MySQL 表格
随机推荐
Global and Chinese chromatographic silica gel resin industry research and investment direction forecast report 2022 Edition
2022 edition of global and Chinese high purity silicon carbide powder operation research and investment strategy analysis report
'virtue and art' in the field of recurrent+transformer video recovery
Experiment 5 constructor and destructor
Month selector disable data after the current month to include the current month
Kill session? This cross domain authentication solution is really elegant
Is the o2o platform worth doing in 2022
2022 edition of global and Chinese sodium hydrosulfide market in-depth investigation and prospect Trend Forecast Report
关于接口测试的那些“难言之隐”
Openmmlab:ai CV framework
Seven trends in test automation that need attention
关于 MySQL 修改密码失败
组态王如何利用无线Host-Link通信模块远程采集PLC数据?
Lambda quick start
C language preprocessing instructions - learning 21
Industrial control system ICs
VsCode - 保存文件自动格式化将单引号 ‘ 变成双引号 “ 的问题
2022 Tibet latest special worker (construction elevator) simulation test question bank and answers
be based on. NETCORE development blog project starblog - (11) access statistics
MySQL basic tutorial -- MySQL transaction and storage engine