一起学ros之发送图片和接收图片

图片发送:

mkdir image_sender

mkdir image_sender/src

cd image_sender/src/

ros2 pkg create image_sender_py --build-type ament_python --dependencies rclpy --node-name image_sender

vim image_sender_py/image_sender_py/image_sender.py

代码:

#encoding:utf-8
  
import rclpy
from rclpy.node import Node
import cv2
from cv_bridge import CvBridge, CvBridgeError
import numpy as np
import time
from sensor_msgs.msg import Image
import time
from sensor_msgs.msg import Image


class NodePublisher(Node):
    def __init__(self, name):
        super().__init__(name)
        self.get_logger().info("Hi, I am %s!" %name)


def main(args=None):
    image_path = "/tmp/bus.jpeg"
    image = cv2.imread(image_path)

    rclpy.init()
    node = NodePublisher('Camera_image')
    image_pub = node.create_publisher(Image, "image_data", 10)
    bridge = CvBridge()

    while True:
        data = bridge.cv2_to_imgmsg(image, encoding="bgr8")
        image_pub.publish(data)
        time.sleep(0.04)


cd ..

colcon build

. install/setup.sh

ros2 run image_sender_py image_sender


显示:
[INFO] [1703339500.883404231] [Camera_image]: Hi, I am Camera_image!

-----------------------------------------------------------------------------------------------------------------------

接收图片:

你可能感兴趣的:(python,opencv,ros)