ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to change ImageConstPtr data?

asked 2012-02-21 22:04:33 -0500

Konstantin Cherenkov gravatar image

Hi all!

Please explain me how could I change raw data in ImageConstPtr& object? I have a function like:

void filterImage (const sensor_msgs::ImageConstPtr& rgb_msg)
  // something like rgb_msg->data[100] = 0;

So, I should change image data, and then pass rgb_msg to other functions.

I see deprecated functions get_data_vec and set_data_vec in Image.h file, but looks like I could not use them cause I have const modifier on object, and set_data_vec is non-const function.

Thank you.

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2012-02-21 22:46:11 -0500

alfa_80 gravatar image

You might want to try something like below:

void filterImage (const sensor_msgs::ImageConstPtr& rgb_msg)
    sensor_msgs::Image current_rgb_msg;
    current_rgb_msg = *rgb_msg;   

You can further manipulate the value from current_rgb_msg variable.

edit flag offensive delete link more


It's better to put the declaration of "current_rgb_msg" in class scope.

alfa_80 gravatar image alfa_80  ( 2012-02-21 22:49:53 -0500 )edit

As I understood, "current_rgb_msg" is a copy of object, referenced by "rgb_msg". Ok, that means, that all changes would be actually for "current_rgb_msg" object. Could you please explain, how to convert "current_rgb_msg" to "const sensor_msgs::ImageConstPtr&" after all changes? Thanks.

Konstantin Cherenkov gravatar image Konstantin Cherenkov  ( 2012-02-22 20:18:57 -0500 )edit

You cannot convert/overwrite the incoming "rgb_msg", because it is a constant value(it's read-only variable). I'm not so into openCV, I think, you can try what dlaz and Eddit suggested, that's by using straightaway cv_bridge for further processing.

alfa_80 gravatar image alfa_80  ( 2012-02-22 20:44:25 -0500 )edit

answered 2012-02-21 22:16:19 -0500

TomTUM gravatar image

You should probably copy the data to another sensor_msgs::Image and then modify it, as you are supposed to read only from const data.

edit flag offensive delete link more

answered 2012-02-22 04:27:18 -0500

If you're doing any real image processing, you should use cv_bridge to convert it to an OpenCV type first.

edit flag offensive delete link more

answered 2012-02-22 17:02:10 -0500

Eddit gravatar image

updated 2012-02-22 17:04:47 -0500

Here is the example how to copy your ImageConstPtr& object to opencv obj:

void filterImage (const sensor_msgs::ImageConstPtr& rgb_msg)



cv_bridge::CvImagePtr cv_ptr;

try {

cv_ptr = cv_bridge::toCvCopy(rgb_msg, enc::BGR8);


catch (cv_bridge::Exception& e)


ROS_ERROR("cv_bridge exception: %s", e.what()); return; }

cv::imshow(WINDOW, cv_ptr->image);



edit flag offensive delete link more

Question Tools


Asked: 2012-02-21 22:04:33 -0500

Seen: 3,367 times

Last updated: Feb 22 '12