Rosanswers logo


I am getting the following error in Rosbridge WebSocket server when I try to send an image to a html web page using roslibjs with cvbridge

"Tried to register topic /camera/image/compressed with type sensor_msgs/CompressedImage but it is already established with type sensor_msgs/Image"

Any ideas on what I am doing wrong? i can see the image being published to rviz and I have the option there to choose raw or compressed so I'm assuming it's something I am doing wrong on the html side

This is what I have on the html side

      var image_listener = new ROSLIB.Topic({
       ros : ros,
       name : '/camera/image/compressed',
       messageType : 'sensor_msgs/CompressedImage'
    image_listener.subscribe(function(message) {
      //document.getElementById("liveimage").innerHTML = m.data;
      console.log("image listenerr event fired");
      var imagedata = "data:image/png;base64," + message.data;
      document.getElementById("liveimage").src = imagedata;      
      //document.getElementById("liveimage").src =  m.data;      

this is what I have on the C++ side

    ros::init(argc, argv, "test image");
    ros::NodeHandle n;

    cv::Mat image2 = cv::imread("test.png" ,0);

    image_transport::ImageTransport it_(n);        
    image_transport::Publisher image_pub_ = it_.advertise("/camera/image/compressed", 1);

    cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);

    ros::Rate loop_rate(10); //10 hz

        #if 1
            ros::Time time = ros::Time::now();
            //cv_ptr->encoding = "bgr8";
            cv_ptr->encoding = "mono8";
            cv_ptr->header.stamp = time;
            cv_ptr->header.frame_id = "/camera/image/compressed";


            //cv_ptr->image = image;
            cv_ptr->image = image2;

            ROS_INFO("ImageMsg Sent.");
            ROS_INFO("Subscribers: %d", image_pub_.getNumSubscribers());


    return 0;

Originally posted by Jt3d on ROS Answers with karma: 5 on 2020-09-22

Post score: 0


1 Answer 1


Rosanswers logo

When you advertise the image topic you need to only supply the base topic name, which in your case should be like this:

image_transport::Publisher image_pub_ = it_.advertise("/camera/image", 1);

Once advertised transports will automatically be appended depending on what your have installed on your system.

Since you supplied '/camera/image/compressed' as the base topic name, your node will advertise a topic by this name of type sensor_msgs/Image. To see what image_transports you have installed on your system, run

rosrun image_transport list_transports

Make sure you see image_transport/compressed in the output.

If you don't see it, then it's probably not installed. You can install it using

sudo apt install ros-$ROS_DISTRO-compressed-image-transport

With your current node running, list the topics using 'rostopic list -v'. You should see '/camera/image/compressed' advertised as a sensor_msgs/Image topic. But, if you have image_transport/compressed installed you should also see '/camera/image/compressed/compressed' being advertised. This is the topic of type sensor_msgs/CompressedImage.

If you plan to use this node for capturing images directly from your camera and publishing them, then take a look at image_transport::ImageTransport::CameraPublisher.

Hope this helps!

Originally posted by mwenger with karma: 36 on 2020-09-22

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by Jt3d on 2020-09-23:
Removing compressed off the topic name worked - many thanks

As a matter of interest what are the benefits of using the camera publisher vs the normal image publisher? any good tutorials / examples you would recommend?

Also for anyone else who might be struggling with setting this up you need this on the html side of things i.e you need the topic with compressed at the end

var image_listener = new ROSLIB.Topic({
      ros : ros,
      name : '/camera/image/compressed',
      messageType : 'sensor_msgs/CompressedImage'

Comment by mwenger on 2020-09-23:
CameraPublisher is just a convenience class that simplifies interacting with the camera. I don't know of any good tutorials that use it. However, you could look at the usb_cam source code and see how it's used there.

Comment by Jt3d on 2020-09-23:
OK - many thanks for the help again


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.