Custom OpenCV 3 installation with ROS Kinetic
Before you start reading, I made a .deb
package of OpenCV 3.3.1
ready to work on Ubuntu 16 and ROS Kinetic. You can download it
here. Please note that I
do NOT actively maintain this package, and that compiling the package
yourself as explained below is a better approach security-wise.
ROS Kinetic comes with an OpenCV package that only contains its core
functionality. In order to use some advanced features (anything
contained in
opencv-contrib), you will
have to build OpenCV from source and manually include any module you
want from
opencv-contrib. But the
ROS Kinetic packages are already precompiled to work with a specific
OpenCV version. In order for this to work, version 3.3.1
is
mandatory (not even 3.3.0
will do it).
To do the source installation, first clone both
opencv and
opencv-contrib and change
to the 3.3.1
branch:
$ git clone https://github.com/opencv/opencv -b 3.3.1 --depth=1
$ git clone https://github.com/opencv/opencv_contrib -b 3.3.1 --depth=1
The --depth=1
flag indicates git not to download all the commit
history, but only the files of the specified branch. This will speed
up the download. If you were to contribute to OpenCV, you will need to
download the entire commit history. But since we are only going to
compile it, this is fine.
Next, go to the opencv
and generate the necessary build files.
$ mkdir opencv/build && cd opencv/build
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local \
-DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_OPENGL=ON \
-DOPENCV_EXTRA_MODULES_PATH=/your/path/to/opencv_contrib ..
Note that you need to specify the path to your opencv_contrib
folder
in the -DOPENCV_EXTRA_MODULES_PATH
parameter. If you want to only
build a set of modules, you can activate them with the
-DBUILD_opencv_<module>
parameters.
To build everything, simply run:
$ make -j$(nproc)
Note that if you have enough CPU cores but not enough RAM, your system may crash due to insufficient memory. This happened to me with 8 cores and 7Gb of free RAM.
Once compiled you can generate a .deb
package so the process of
installing and especially uninstalling is greatly simplified. To do
so, execute the script generated in the opencv/build
folder:
sudo checkinstall
checkinstall
will track all the changes that would take place if you
executed make install
and produce a .deb
package accordingly. You
will be prompted to fill information such as package name, maintainer
name and email, …
To use the custom OpenCV library installation instead of the ROS one,
you need to specify in every CMakeLists.txt
of every package you
want using the source installation of OpenCV by using the following code:
find_package(OpenCV 3.3.1 REQUIRED core aruco)
link_directories(${OpenCV_LIBRARY_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(your_node src/your_node.cc)
target_link_libraries(your_node ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
In my case, I wanted to use the aruco module, so I specified it in the
find_package
function.