The sensor_msgs/CompressedImage message type is converted to the sensor_msgs/Image message type

The sensor_msgs/CompressedImage message type is converted to the sensor_msgs/Image message type

Code implemented in Python

The sensor_msgs/CompressedImage message type can be converted to the sensor_msgs/Image message type:

import rospy
from sensor_msgs.msg import CompressedImage, Image
from cv_bridge import CvBridge, CvBridgeError

bridge = CvBridge()

def compressed_image_callback(msg):
    try:
        cv_image = bridge.compressed_imgmsg_to_cv2(msg)
    except CvBridgeError as e:
        rospy.logerr("CvBridgeError: %s", e)
        return

    try:
        image_msg = bridge.cv2_to_imgmsg(cv_image, encoding="bgr8")
    except CvBridgeError as e:
        rospy.logerr("CvBridgeError: %s", e)
        return

    image_msg.header = msg.header

    pub.publish(image_msg)

if __name__ == '__main__':
    rospy.init_node("compressed_to_image")
    sub = rospy.Subscriber("compressed_image", CompressedImage, compressed_image_callback, queue_size=1)
    pub = rospy.Publisher("image", Image, queue_size=1)
    rospy.spin()

In this code, we first import the necessary ROS message types and the cv_bridge library. Then, we define a CvBridge instance called "bridge", which allows us to convert ROS messages to OpenCV images. Next, we define a callback function called "compressed_image_callback", which converts the received compressed image message to an OpenCV image, and uses cv_bridge to convert it to the sensor_msgs/Image message type. Finally, we publish the converted image message. In the main() function, we create a ROS node named "compressed_to_image" and subscribe to compressed image messages named "compressed_image". We also created a publisher called "image" to publish transformed image messages. Finally, we keep the node alive by calling rospy.spin().

Code implemented in C++

The sensor_msgs/CompressedImage message type can be converted to the sensor_msgs/Image message type:

#include <ros/ros.h>
#include <sensor_msgs/CompressedImage.h>
#include <sensor_msgs/Image.h>
#include <cv_bridge/cv_bridge.h>

void compressedImageCallback(const sensor_msgs::CompressedImageConstPtr& msg)
{
    
    
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
    
    
        cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
    
    
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }

    sensor_msgs::ImagePtr image_msg = cv_ptr->toImageMsg();
    image_msg->header = msg->header;

    pub.publish(image_msg);
}

int main(int argc, char** argv)
{
    
    
    ros::init(argc, argv, "compressed_to_image");
    ros::NodeHandle nh;

    ros::Subscriber sub = nh.subscribe("compressed_image", 1, compressedImageCallback);
    pub = nh.advertise<sensor_msgs::Image>("image", 1);

    ros::spin();

    return 0;
}

In this code, we first include the necessary ROS message types and the cv_bridge library. Then, we define a callback function called "compressedImageCallback", which converts the received compressed image message to an OpenCV image, and uses cv_bridge to convert it to the sensor_msgs/Image message type. Finally, we publish the converted image message. In the main() function, we create a ROS node named "compressed_to_image" and subscribe to compressed image messages named "compressed_image". We also created a publisher called "image" to publish transformed image messages. Finally, we keep the node alive by calling ros::spin().

Guess you like

Origin blog.csdn.net/gezongbo/article/details/130166776