ros2--图像/image

ros2中传输图像的接口

sensor_msgs/msg/Image

ros2的image接口需要配合而opencv和cv_bridge一起使用。

案例(发布,订阅和保存和显示):

发布:

复制代码
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>

class ImagePublisher : public rclcpp::Node {
public:
    ImagePublisher() : Node("image_publisher") {
        RCLCPP_INFO(this->get_logger(), "Image publisher node has been started.");
        this->declare_parameter<std::string>("image_abs_path", "");
        image_abs_path_ = this->get_parameter("image_abs_path").as_string();
        publisher_ = this->create_publisher<sensor_msgs::msg::Image>("camera/image", 10);
        timer_ = this->create_wall_timer(std::chrono::seconds(3), std::bind(&ImagePublisher::publishImage, this));
    }

private:
    void publishImage() {
        RCLCPP_INFO(this->get_logger(), "image_abs_path: %s", image_abs_path_.c_str());
        cv::Mat image = cv::imread(image_abs_path_, cv::IMREAD_COLOR); // 读取图像文件
        if (!image.empty()) {
            sensor_msgs::msg::Image msg;
            cv_bridge::CvImage(std_msgs::msg::Header(), "bgr8", image).toImageMsg(msg); // 转换为ROS Image消息类型
            publisher_->publish(msg); // 发布图像消息
            RCLCPP_INFO(this->get_logger(), "Image has been published.");
        } else {
            RCLCPP_ERROR(this->get_logger(), "Image loading failed!");
        }
    }

private:
    std::string image_abs_path_{""};
    rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher_;
    rclcpp::TimerBase::SharedPtr timer_;
};

int main(int argc, char** argv) {
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<ImagePublisher>());
    rclcpp::shutdown();
    return 0;
}

订阅:

复制代码
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <functional>

class ImageSubscriber : public rclcpp::Node {
public:
    ImageSubscriber() : Node("image_subscriber") {
        declare_parameter("image_save_path", "");
        declare_parameter("image_name", "");
        image_save_path_ = get_parameter("image_save_path").as_string();
        image_name_ = get_parameter("image_name").as_string();
        subscription_ = this->create_subscription<sensor_msgs::msg::Image>(
            "camera/image",10, std::bind(&ImageSubscriber::image_callback, this, std::placeholders::_1));
    }

    private:
    void image_callback(const sensor_msgs::msg::Image::SharedPtr msg) {
        cv_bridge::CvImagePtr cv_ptr;
        try {
            cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
        } catch (cv_bridge::Exception& e) {
            RCLCPP_ERROR(this->get_logger(), "Failed to convert image: %s", e.what());
            return;
        }
        cv::imshow("Image window", cv_ptr->image);
        cv::waitKey(2);
        if (image_save_path_!= "" && image_name_!= "") {
            cv::imwrite(image_save_path_ + image_name_ + ".jpg", cv_ptr->image);
            RCLCPP_INFO(this->get_logger(), "Image saved to %s", (image_save_path_ + image_name_ + ".jpg").c_str());
        }
    }

private:
    std::string image_save_path_{""};
    std::string image_name_{""};
    rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscription_;
};

int main(int argc, char *argv[]) {
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<ImageSubscriber>());
    rclcpp::shutdown();
    return 0;
}
相关推荐
王老师青少年编程3 分钟前
2024年信奥赛C++提高组csp-s初赛真题及答案解析(阅读程序第3题)
c++·题解·真题·csp·信奥赛·csp-s·提高组
凡人叶枫31 分钟前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
CSDN_RTKLIB32 分钟前
使用三方库头文件未使用导出符号情景
c++
rainbow68892 小时前
Linux文件描述符与重定向原理
c++
CodeSheep程序羊3 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
编程小白20263 小时前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
.小墨迹4 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
历程里程碑4 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法
郝学胜-神的一滴4 小时前
深入浅出:使用Linux系统函数构建高性能TCP服务器
linux·服务器·开发语言·网络·c++·tcp/ip·程序人生
天若有情6734 小时前
【自研实战】轻量级ASCII字符串加密算法:从设计到落地(防查岗神器版)
网络·c++·算法·安全·数据安全·加密