Robotics StackExchange | Archived questions

How to convert ImageConstPtr to c++ string

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;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }

    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);
    cv::waitKey(3);
}

Asked by can-43811 on 2017-05-27 06:29:46 UTC

Comments

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

Asked by gvdhoorn on 2017-05-27 06:54:33 UTC

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.

Asked by can-43811 on 2017-05-28 07:48:10 UTC

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.

Asked by gvdhoorn on 2017-05-28 12:12:53 UTC

Answers