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

Problems integrating OpenCV with ROS Indigo

asked 2016-01-19 15:43:45 -0500

Raiden gravatar image

updated 2016-01-19 15:53:17 -0500

I am trying to build a simple program to start working with OpenCV with ROS Indigo; however, whenever I try to build the code (catkin_make) the build fails and returns " undefined reference to 'cv::imread(cv::String const&, int)' " and so on for all the other OpenCV functions. This makes it impossible for me to build and run my test program for ROS and I'm at a loss for how to continue. I've quoted the program and CMakeLists.txt below.

OpenCV 3.1 is installed and has been successfully compiled using g++ in the terminal. The OS is LinuxMint 17.3

Program imgloader.cpp:

#include <ros/ros.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv; using namespace std;

int main(int argc, const char** argv){  
Mat img = imread("img.jpeg", CV_LOAD_IMAGE_UNCHANGED);
       cout << "Image cannot be found or opened..." << endl;
       return -1;
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
imshow("MyWindow", img);
return 0;

The following has been added to the CMakeLists.txt file for the package "opencvtest"

find_package(OpenCV REQUIRED) 
include_directories(include ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS}) 
add_executable(imgloader src/imgloader.cpp)
target_link_libraries(imgloader ${catkin_LIBRARIES} ${OpenCV_LIBRARIES}) 
add_dependencies(imgloader opencvtest)

And the following dependency has been added:


What do I need to add or do to make my package build?

edit retag flag offensive close merge delete


Please use the "Preformatted Text" button (marked "101010") to format your code sample so that it is readable.

ahendrix gravatar image ahendrix  ( 2016-01-19 15:46:37 -0500 )edit

First post, it's been edited now.

Raiden gravatar image Raiden  ( 2016-01-19 15:49:56 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-01-19 16:08:39 -0500

lucasw gravatar image

updated 2016-01-19 16:14:11 -0500

If you really want opencv 3 see

Otherwise uninstall your opencv 3 and use which comes with opencv 2.4- it probably is already on your system. Just have build/run_depends on cv_bridge and catkin will bring in the headers and libraries, you won't need a find_package for opencv.

edit flag offensive delete link more


If you use OpenCV directly, you should directly find_pacakge() it. Do not rely on transitive dependencies; the upstream package may change.

ahendrix gravatar image ahendrix  ( 2016-01-19 16:23:04 -0500 )edit

I uninstalled my OpenCV 3 and ran catkin_make afterwards (I did not change any of the code/dependencies). The code built and ran. Thank you for your answer.

Raiden gravatar image Raiden  ( 2016-01-19 16:23:58 -0500 )edit

cv_bridge package.xml has <build_depend>libopencv-dev</build_depend> - and a few other image processing packages use it, so that could be used without transitivity and without a find_package.

lucasw gravatar image lucasw  ( 2016-01-19 16:26:35 -0500 )edit

I just said that relying on the dependency through cv_bridge is not a good idea. If your package uses a library, you should depend on it directly. (just because it works another way doesn't make it a good idea).

ahendrix gravatar image ahendrix  ( 2016-01-19 16:30:58 -0500 )edit

Looking through the rosdep database, it looks like a <build_depend> on opencv2 is deprecated or wrong. The dependency in the package.xml should be <build_depend>libopencv-dev</build_depend> .

ahendrix gravatar image ahendrix  ( 2016-01-19 16:38:56 -0500 )edit

According to cv_bridge is the the right dependency, though doesn't really back that up.

lucasw gravatar image lucasw  ( 2016-01-19 17:05:15 -0500 )edit

Hrm... yeah. It looks like the dependency through cv_bridge is the only option that is backwards-compatible with Hydro.

ahendrix gravatar image ahendrix  ( 2016-01-19 17:52:19 -0500 )edit

Yes, I remember having to do that to maintain common source between Hydro and Indigo.

joq gravatar image joq  ( 2016-01-19 21:13:33 -0500 )edit

Question Tools

1 follower


Asked: 2016-01-19 15:38:24 -0500

Seen: 1,443 times

Last updated: Jan 19 '16