Catkin OpenCV segmentation fault (core dumped)
I create a catkin workspace like:
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
After that I create a package:
cd ~/catkin_ws/src
catkin_create_pkg vp
cd ~/catkin_ws
catkin_make
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
waitKey(2000);
cout << img.rows << "; " << img.cols << endl;
return 0;
}
I edit ~/catkin_ws/src/vp/package.xml
and add:
<build_depend>opencv2</build_depend>
<run_depend>opencv2</run_depend>
I edit ~/catkin_ws/src/vp/CMakeLists.txt
so it looks like this:
cmake_minimum_required(VERSION 2.8.3)
project(vp)
find_package(catkin REQUIRED cv_bridge genmsg image_transport sensor_msgs)
find_package(OpenCV 3 REQUIRED)
catkin_package(
DEPENDS system_lib
)
include_directories(include ${OpenCV_INCLUDE_DIRS})
add_executable(vpnode src/vpnode.cpp)
target_link_libraries(vpnode
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
)
Then I perform:
cd ~/catkin_ws
catkin_make
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?
ADDITIONAL INFO
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.
EDIT
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.