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

How to specify my own OpenCV3 for cv_bridge

asked 2018-04-19 03:05:50 -0500

bear234 gravatar image

The ROS1 package cv_bridge can be downloaded here: and be found at vision_opencv/cv_bridge.

I just checked its CMakeLists.txt and I found OpenCV3:

find_package(OpenCV 3 REQUIRED

  INCLUDE_DIRS include
  CATKIN_DEPENDS rosconsole sensor_msgs
  CFG_EXTRAS cv_bridge-extras.cmake
include_directories(include ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})

The OpenCV3 by default is very huge so I want to use a custom one, which is smaller.

I put my own OpenCV3 at /opt/opencv3/ and there are only two directories: include and lib in it.

Now I want to tell the package cv_bridge to look for my own specific OpenCV3 so that I can remove the original OpenCV3 from the source of ROS.

Is it possible? How can I modify the CMakeLists.txt of cv_bridge?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2018-04-19 20:09:23 -0500

Wolf gravatar image

You do not need to modify cv_bridge source code for this. You can point cmake to your own opencv version by defining the OpenCV_DIR variable when running catkin_make.

For example:

If you compile and install your own OpenCV version like:

cd opencv
mkdir build
cd build
cmake ..
sudo make install

It will be installed to /usr/local

In this case you can force catkin_make to build your workspace against this OpenCV version by running:

catkin_make -DOpenCV_DIR=/usr/local/share/OpenCV

Note that OpenCV_DIR must point to a folder with opencv-conifg.cmake file.

This will compile and link all packages in your workspace against the modified opencv version, rather than the opencv version shipped with ROS.

Alternatively, if you do not want to polute your system directories you can run

make DESTDIR=$HOME/opencv_installation install

when building and installing opencv. This will place the opencv installation in your home folder (or whereever you like) so you can comfortably remove it.

In this case you run

catkin_make -DOpenCV_DIR=$HOME/opencv_installation install/usr/local/share/OpenCV

for building the workspace.

Important note:

All libraries that are linked in an executable have to use the same opencv version, otherwise attempting to run them will cause segfaults. Packages that are installed via sudo apt-get install ros-kinetic-whatever do link against the OpenCV version shipped with ROS. If your exec uses any lib package of ROS (frequently image_transport, image_geometry, image_pipeline, cv_bridge etc) you have to check them out from github and place them in your catkin workspace alongide your own packages so these are also compiled against your modified OpenCV version.

edit flag offensive delete link more

answered 2019-04-18 06:53:24 -0500

drNoob13 gravatar image

What you should do is to tell CMake that you want to use your user-built OpenCV instead of the pre-built version bundled with ROS. It can be done by editting the PATHS in find_package

To do that, you need to edit all the CMakeLists of all packages you are using. For example:

# This assumes that you have your OpenCV built and installed to /usr/local/share/OpenCV
find_package(OpenCV 3.4.2 REQUIRED
  PATHS /usr/local

Then proceed to apply similar change to all other ROS packages being used by your code and rebuild them.

Also have a look at this answer

edit flag offensive delete link more

Question Tools



Asked: 2018-04-19 03:05:50 -0500

Seen: 6,933 times

Last updated: Apr 19 '18