Ask Your Question
2

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: https://github.com/ros-perception/vis... and be found at vision_opencv/cv_bridge.

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

find_package(OpenCV 3 REQUIRED
  COMPONENTS
    opencv_core
    opencv_imgproc
    opencv_imgcodecs
  CONFIG
)

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS rosconsole sensor_msgs
  DEPENDS OpenCV
  CFG_EXTRAS cv_bridge-extras.cmake
)
catkin_python_setup()
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
4

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 ..
make 
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
0

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
  NO_DEFAULT_PATH
)

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

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

Seen: 2,019 times

Last updated: Apr 19 '18