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;
}
相关推荐
历程里程碑34 分钟前
LeetCode热题11:盛水容器双指针妙解
c语言·数据结构·c++·经验分享·算法·leetcode·职场和发展
郝学胜-神的一滴35 分钟前
使用OpenGL绘制卡通效果的圣诞树
开发语言·c++·程序人生·游戏·图形渲染
Morwit8 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
OliverH-yishuihan8 小时前
开发linux项目-在 Windows 上 基于“适用于 Linux 的 Windows 子系统(WSL)”
linux·c++·windows
七禾页丫8 小时前
面试记录12 中级c++开发工程师
c++·面试·职场和发展
zmzb010310 小时前
C++课后习题训练记录Day56
开发语言·c++
编程小Y10 小时前
C++ Insights
开发语言·c++
王老师青少年编程10 小时前
csp信奥赛C++标准模板库STL案例应用5
c++·stl·set·集合·标准模板库·csp·信奥赛
历程里程碑10 小时前
hot 206
java·开发语言·数据结构·c++·python·算法·排序算法
Tipriest_11 小时前
C++ 的 ranges 和 Python 的 bisect 在二分查找中的应用与实现
c++·python·算法·二分法