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 imageSteven_Daniluk ( 2016-12-14 13:29:59 -0500 )edit

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 imageSteven_Daniluk ( 2016-12-14 13:31:16 -0500 )edit