Rosanswers logo

How do I convert an image I have opened through opencv into sensor_msgs/image??

Originally posted by ayush_dewan on ROS Answers with karma: 1610 on 2012-04-28

Post score: 1

Original comments

Comment by Kevin on 2012-04-30:
If either of these answers answered your question, please mark one as "correct" so this questions gets closed out ... thanks!


2 Answers 2


Rosanswers logo

I recommend the cv_bridge tutorials.

Originally posted by Mac with karma: 4119 on 2012-04-28

This answer was ACCEPTED on the original site

Post score: 1


Rosanswers logo

Here is a snippet of code I have used to do this and the full code is here. It is a package that also uses opencv to grab images and publish them. Some of the variables are:

image_transport::ImageTransport transport;
image_transport::CameraPublisher image_pub;
camera_info_manager::CameraInfoManager info_mgr;

And the code:

        // if subscribers, pub images
        if(image_pub.getNumSubscribers() >  0) {

            ros::Time time = ros::Time::now();

            // convert OpenCV image to ROS message
            cv_bridge::CvImage cvi;
            cvi.header.stamp = time;
            cvi.header.frame_id = "image";
            cvi.encoding = "bgr8";
            cvi.image = cv_image;

            // get camera parameters 
            sensor_msgs::CameraInfo info = info_mgr.getCameraInfo();
            info.header.stamp = cvi.header.stamp;
            info.header.frame_id = cvi.header.frame_id; // whatever it is called
            info.width = cv_image.cols;
            info.height = cv_image.rows;

            sensor_msgs::Image im;

Originally posted by Kevin with karma: 2962 on 2012-04-28

This answer was NOT ACCEPTED on the original site

Post score: 3


Your Answer

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