cv_bridge in a class leads to segfault when cv_ptr->image is accessed

asked 2016-12-12 20:51:21 -0500

luc gravatar image

updated 2016-12-12 20:53:38 -0500

Hi ROS friends,

I'm having an issue where when I get my const sensor_msgs::ImageConstPtr &msg in my callback function, and do a cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8); where cv_ptr is a cv_bridge::CvImagePtr, and try to do anything with the cv_ptr->image, the line where I do toCvCopy segfaults. When I leave the pointer alone and maybe access its contents or something elsewhere in my class, it's seemingly fine... I'm extremely confused.

Here's some of my real code:

void CNNLocalizer::imageCB(const sensor_msgs::ImageConstPtr &msg)
{
  cv_bridge::CvImagePtr cv_ptr;

  try
  {
    cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);

    ROS_INFO("Attempt!");
    cv::Mat image = cv_ptr->image.clone();
    ROS_INFO("Success!");

    image.convertTo(g_most_recent_image_, CV_32FC3);

    g_img_height_ = msg->height;
    g_img_width_ = msg->width;

    g_got_image_ = true;
  }
  catch (cv_bridge::Exception &e)
  {
    ROS_ERROR("Failed converting the received message: %s", e.what());
    return;
  }
}

If I get rid of the business where I try to clone the cv_ptr->image, there is no segfault... Any tips or tricks? Am I misunderstanding how things are working here...?

Edit: I am running my camera data through a looping rosbag with --clock enabled.

edit retag flag offensive close merge delete

Comments

When I put the contents of your try block in the cv_bridge tutorial script and subscribe to a topic from a bag file I don't get any errors. Could it be an encoding issue, or a bad bag file?

Steven_Daniluk gravatar image Steven_Daniluk  ( 2016-12-14 13:29:59 -0500 )edit
2

Also, if all you need is the cv::Mat image you can copy it in one line:

cv::Mat image = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8)->image;
Steven_Daniluk gravatar image Steven_Daniluk  ( 2016-12-14 13:31:16 -0500 )edit

can someone point me to documenation for ->image? I am using it for drawing a line on an image but not sure what it does. I am assuming it outputs a Mat image when given a pointer to the image or something on those lines?

Thanks!

Abhinavgandhi09 gravatar image Abhinavgandhi09  ( 2020-11-30 13:37:43 -0500 )edit