【只谈干货】ROS图像和OpenCV图像的相互转换

    xiaoxiao2022-07-03  120

    1. sensor_msgs::Image转Mat

    sub节点的回调函数

    void chatterCallback(const sensor_msgs::Image::ConstPtr& msg) { cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); Mat img = cv_ptr -> image; cv::imshow("image", img); cv::waitKey(1); }

    2. Mat转sensor_msgs::Image

    pub节点的while循环

    while (ros::ok()) { //替换为需要转换的图像 Mat image; //将Mat类型转为sensor_msgs::Image sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg(); //发布消息 chatter_pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); }

    Reference: http://wiki.ros.org/image_transport/Tutorials/PublishingImages

    最新回复(0)