How to convert ImageConstPtr to c++ string

asked 2017-05-27 06:29:46 -0500

can-43811 gravatar image

updated 2017-05-28 10:08:10 -0500

Hi ,

I want to convert ImageConstPtr type msg to c++ string so that same can be converted in to byte array (vector <unsigned char>). can anybody suggest any approach for the same?

below is callback for ros image subscriber where publish image is handled.

 void imageCb(const sensor_msgs::ImageConstPtr& msg)

//conversion of ImageConstPtr  msg type in string
string img_string = API to convert   sensor_msgs::ImageConstPtr& in to c++ string type;
vector <unsigned char> vec(img_string.begin().img_string.end());


is there anyway to convert cv_ImagePtr to string type?

In below code snippet cv_ptr->image will specify the image which is populated from toCvCopy API.

now i want to convert cv_ptr->image into c++ string so that i can sore the same in vector in form of unsigned char.

void imageCb(const sensor_msgs::ImageConstPtr& msg)
    cv_bridge::CvImagePtr cv_ptr;
      cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
    catch (cv_bridge::Exception& e)
      ROS_ERROR("cv_bridge exception: %s", e.what());

    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    cv::imshow(WINDOW, cv_ptr->image);
edit retag flag offensive close merge delete


This reads like an xy-problem. Can you explain why you want to do that?

gvdhoorn gravatar image gvdhoorn  ( 2017-05-27 06:54:33 -0500 )edit

After subscribing image based on topic , i need to convert the same in byte array so that it can be inserted in activemqcpp producer as other end activemqjava subscriber is expecting the same in bytestream format.

can-43811 gravatar image can-43811  ( 2017-05-28 07:48:10 -0500 )edit

More generally, this is called serialisation and deserialisation of ROS messages. All ROS messages have methods generated that do this. You could probably make use of those. They need in and out streams though, but you could wrap an in-memory buffer with one and pass that.

gvdhoorn gravatar image gvdhoorn  ( 2017-05-28 12:12:53 -0500 )edit