Ask Your Question

cv_bridge/OpenCV segmentation fault

asked 2015-07-03 09:09:47 -0500

prokto gravatar image

Hi, I'm getting a segmentation fault whenever I'm trying to convert an image from subscribed topic to:

cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvCopy(image)

while this works:


I'm getting the same error when I call later:

cv::Mat newImage = cv_ptr->image.clone();

I'm using ROS Indigo, Ubuntu 14.04 and opencv 2.4.8 . I've checked my code on another computer with the same ROS/Ubuntu/OpenCV versions and it works fine...

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-08-18 10:13:05 -0500

roach gravatar image

Hi, it may be because the image is empty. Before sending the image, you should check if it is not empty, and recheck its size before using it.

This callback works for me (Ubuntu 16.04, ROS kinetic, OpenCV 3) :

cv::Mat cameraFeed;
// Callback function. Serves to receive images from ROS camera's images topic
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
        cameraFeed = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8)->image;
    catch (cv_bridge::Exception& e)
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
edit flag offensive delete link more

answered 2016-08-18 02:38:03 -0500

Hinas gravatar image

updated 2016-08-20 16:33:48 -0500

I am also having same problem. Any solutions.Please

I'm using:

Ubuntu 16.04
ROS Kinetic
OpenCV 2

The same code worked earlier with Ubuntu 14.04, ROS Indigo and OpencCV 2

Edit 1: I do not have 1 opencv 3 installed only have opencv 2. I did some dirty hecks and my programs are working now. I don't think this heck is a persistent solution. Here is the heck. 1. I downloaded latest cv_bridge sources from git and compiled using catkin. 2. Removed the cv_bridge installed by package manager 3. Manually copied the compiled libcv_bridge file and corresponding header into respective ROS installation folders.

Edit 2: Unfortunately my above heck has some side effect. I could not run rqt_image_view which is depend on the old libcv_brige file. Therefore, I compiled cv_bridge library in a different name libmycv_bridge and linked my ros node to libmycv_bridge. Now my rosnode work as a stand alone program without setting the ROS_PACKAGE_PATH variable. But if I set the variable ROS_PACKAGE_PATH to the corresponding ros node folder, again segmentation fault. But It seems from a different place of a program than earlier error.

I know I can use make install rather than copying files manually. If I use make install, will the package manager recognize it or Will it break the package manager?

edit flag offensive delete link more


Are you sure that you are explicitly linking to OpenCV 2 (and cmake actually uses OpenCV 2.x.x.)? As ROS Kinetic uses OpenCV 3.1 by default

takahashi gravatar image takahashi  ( 2016-08-18 02:48:45 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-07-03 09:09:47 -0500

Seen: 1,385 times

Last updated: Aug 20 '16