Rosanswers logo

The ROS1 package cv_bridge can be downloaded here: https://github.com/ros-perception/vision_opencv.git 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?

Originally posted by bear234 on ROS Answers with karma: 71 on 2018-04-19

Post score: 2


2 Answers 2


Rosanswers logo

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.

Originally posted by Wolf with karma: 7555 on 2018-04-19

This answer was ACCEPTED on the original site

Post score: 5


Rosanswers logo

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

Originally posted by drNoob13 with karma: 36 on 2019-04-18

This answer was NOT ACCEPTED on the original site

Post score: 1


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.