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

Catkin OpenCV segmentation fault (core dumped)

asked 2016-05-21 04:12:54 -0500

acajic gravatar image

updated 2016-05-21 08:02:14 -0500

I create a catkin workspace like:

mkdir -p ~/catkin_ws/src 
cd ~/catkin_ws/src

After that I create a package:

cd ~/catkin_ws/src 
catkin_create_pkg vp 
cd ~/catkin_ws 

After that I create ~/catkin_ws/src/vp/src/vpnode.cpp:

#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;

int main(int argc, char* argv[]) {

    Mat img = imread("/home/andrija/Documents/image.jpg");
    imshow("img", img); // program crashes on this line. when this line is commented the code runs fine, printing out the correct dimensions of image.jpg

    cout << img.rows << "; " << img.cols << endl;

    return 0;

I edit ~/catkin_ws/src/vp/package.xml and add:


I edit ~/catkin_ws/src/vp/CMakeLists.txt so it looks like this:

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED cv_bridge genmsg image_transport sensor_msgs)

find_package(OpenCV 3 REQUIRED)
  DEPENDS system_lib

include_directories(include ${OpenCV_INCLUDE_DIRS})

add_executable(vpnode src/vpnode.cpp)


Then I perform:

cd ~/catkin_ws

And the target builds without errors. After that:

source devel/setup.bash

roscore is running the entire time. When I perform rosrun vp vpnode, I get the output:

Segmentation fault (core dumped)

In the comments inside the vpnode.cpp are details about where the crash occurs. I cannot figure out what am I doing wrong. It's like the wrong OpenCV is getting linked, and I don't know why.

Any help?


In CMakeLists.txt, if I don't explicitly put

find_package(OpenCV 3 REQUIRED)

but, instead I do this

find_package(OpenCV REQUIRED)

catkin_make cannot build the target because of the

#include "opencv2/videoio.hpp"

and after I comment that out, catkin_make goes through, but rosrun vp vpnode still reports segmentation fault. So, I'm guessing that there is OpenCV 2 version that is getting mixed up with OpenCV 3.

Im not expert on Linux systems, so I don't know how to fix that. Prior to installing OpenCV3, I deleted everything that had to do with OpenCV2, but now it seems that this isn't true.


Thanks for the response, ROSKinect.

I ended up uninstalling both OpenCV and ROS. But, from my recollection, I can respond to your suggestions, since I tried some of them.

Using #include <opencv2/highgui/highgui.hpp> did not help. Neither did getting rid of videoio. I have not tried including cv.hpp. Your suggested code would most likely crash on the line namedWindow in my problem.

I was 95% sure that some config file or something like that was left undeleted from the Opencv2.4, i had installed previously, and my program was trying to access some libraries which weren't available anymore.

After proper uninstallation of both OpenCV and ROS and re-installation, I got rid of this problem. But encounted a new one.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-21 06:20:12 -0500

ROSkinect gravatar image

updated 2016-05-21 07:15:00 -0500

I don't know if you need videoio later but for now you don't, so comment it till you solve segmentation fault (core dumped) problem.

Edit your CMakeLists:

find_package(OpenCV REQUIRED)


Edit your sourcefile as follow:

#include <iostream>
#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char* argv[])  {
    cv::Mat img = cv::imread("/home/andrija/Documents/image.jpg"); //Make sure that the image is there

    std::cout << img.rows << "; " << img.cols <<  std::endl;

    cv::imshow("img", img);

    return 0; 

You don't have to edit package.xml leave as it is, This file defines properties about the package such as the package name, version numbers, authors, maintainers, and dependencies on other catkin packages. here

edit flag offensive delete link more

Question Tools


Asked: 2016-05-21 04:12:54 -0500

Seen: 1,698 times

Last updated: May 21 '16