[Documentation] [TitleIndex] [WordIndex


Indigo and Jade

Since Indigo, there is a package for OpenCV3. It contains the opencv and opencv_contrib repos from https://github.com/Itseez. Some modules might not get included because the dependencies are hard to package for all platforms (e.g. the optical character recognition module that needs tesseract).

Kinetic and above

Since Kinetic, OpenCV3 is the default. Discussion happened on https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/ros-sig-perception/K5__71SX7eU/mxWwn3AeAwAJ.

The reasons to choose OpenCV3 are:

The reasons to package it ourselves are:


You would use OpenCV3 as you would usually outside of ROS but let's explain a few corner cases:

A consequence of all that is that you have OpenCV3 installed in Indigo or Jade and compile from source, you need to have any OpenCV dependent package be part of your source workspace.


Switching code to OpenCV3 can be done following the official guide at http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html.

The rosdep key is opencv3.

Dual Compatibility

Now, as a maintainer, you might want to not duplicate branches and keep you code compatibility with OpenCV2. It is actually easy to write code compatible with OpenCV 2.4.9+ and 3+.


   1 import cv2
   2 from distutils.version import LooseVersion
   3 if LooseVersion(cv2.__version__).version[0] == 2:
   4     # Whatever OpenCV2 code
   5 else:
   6     # Whatever OpenCV3 code


#include "opencv2/core/version.hpp"
// do opencv 2 code
// do opencv 3 code

Also, if you write OpenCV3 code, chances are high that it will also compile with OpenCV 2.4.9+. E.g., in color conversion, when replacing CV_BGR2GRAY by cv::COLOR_RGB2GRAY, your code will also compile in OpenCV2 as this API has been backported (and not documented ...).


# use 2.4 modules
# use 3.x modules


Instead of depending on opencv3, you should depend on cv_bridge or image_geometry. Depending on one of those two keys transitively makes you depend on libopencv-dev on Jade and below, or opencv3 on Kinetic and above.

image_pipeline will only pull in sensor_msgs as an extra dependency while cv_bridge will also pull in boost, python and rosconsole so it depends on whether you go for something small or something you need.

Building the Package

The package is slightly patched from upstream to not build certain things like tests and examples. It is released through bloom using a tar file as source. In case you ever need to build your own package, this tar file is obtained with the following:

   1 #!/bin/bash
   2 mkdir tmp
   3 cd tmp
   4 # the two lines below will fail in case it's not the first time we clone
   5 git clone https://github.com/Itseez/opencv.git
   6 git clone https://github.com/Itseez/opencv_contrib.git
   8 # revert the main CMakeLists.txt file in case it's not the first time
   9 cd opencv
  10 git reset --hard HEAD
  11 git checkout master
  12 git clean -dxf
  13 git reset --hard HEAD
  14 git pull --rebase
  15 # Use that date because of: https://github.com/ros-perception/vision_opencv/issues/170
  16 git checkout `git rev-list -n 1 --before="2017-04-01 00:00" master`
  17 #git checkout 3.2.0
  19 cd ../opencv_contrib
  20 git checkout master
  21 git reset --hard HEAD
  22 git pull --rebase
  23 git checkout `git rev-list -n 1 --before="2017-04-01 00:00" master`                                                                  
  24 #git checkout 3.2.0                                                                                                                  
  25 cd ../                                                                                                                               
  27 cp -fr ./opencv_contrib/modules ./opencv/opencv_contrib                                                                              
  29 # revert the main CMakeLists.txt file in case it's not the first time                                                                
  30 cd opencv                                                                                                                            
  31 # Disable dnn for now.                                                                                                               
  32 rm -fr ./opencv_contrib/dnn                                                                                                          
  33 git checkout CMakeLists.txt                                                                                                          
  34 sed -i 's/set(OPENCV_EXTRA_MODULES_PATH "" CACHE PATH "Where to look for additional OpenCV modules")/set(OPENCV_EXTRA_MODULES_PATH "${CMAKE_CURRENT_SOURCE_DIR}\/opencv_contrib\/" CACHE PATH "Where to look for additional OpenCV modules")/' ./CMakeLists.txt           
  35 # Disable tests                                                                                                                      
  36 sed -i 's/OCV_OPTION(BUILD_PERF_TESTS         "Build performance tests"                     ON  IF (NOT APPLE_FRAMEWORK) )/OCV_OPTION(BUILD_PERF_TESTS         "Build performance tests"                     OFF)/' ./CMakeLists.txt                                      
  37 sed -i 's/OCV_OPTION(BUILD_TESTS              "Build accuracy \& regression tests"           ON  IF (NOT APPLE_FRAMEWORK) )/OCV_OPTION(BUILD_TESTS              "Build accuracy \& regression tests"           OFF)/' ./CMakeLists.txt                                    
  38 # Enable Qt                                                                                                                          
  39 sed -i 's/OCV_OPTION(WITH_QT             "Build with Qt Backend support"               OFF  IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )/OCV_OPTION(WITH_QT             "Build with Qt Backend support"               ON  IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )/' ./CMakeLists.txt                                                                                                                      
  40 # Enable mangled path to have two OpenCV installable side by side                                                                    
  41 sed -i 's/OCV_OPTION(INSTALL_TO_MANGLED_PATHS "Enables mangled install paths, that help with side by side installs." OFF IF (UNIX AND NOT ANDROID AND NOT APPLE_FRAMEWORK AND BUILD_SHARED_LIBS) )/OCV_OPTION(INSTALL_TO_MANGLED_PATHS "Enables mangled install paths, that help with side by side installs." ON)/' ./CMakeLists.txt                                                                           
  42 #sed -i 's/OCV_OPTION(WITH_IPP            "Include Intel IPP support"                   NOT MINGW IF (X86_64 OR X86) AND NOT WINRT )/OCV_OPTION(WITH_IPP            "Include Intel IPP support"                   OFF)/' ./CMakeLists.txt                                 
  43 sed -i 's/set(OPENCV_DLLVERSION "")/set(OPENCV_DLLVERSION "3")/' ./CMakeLists.txt                                                    
  44 echo "install(FILES package.xml DESTINATION share/opencv3)" >> ./CMakeLists.txt                                                      
  46 cd ../                                                                                                                               
  48 # when configuring bloom the first time, use the tar file created above                                                              
  49 rm ./opencv.tar.gz                                                                                                                   
  50 tar --exclude-vcs -zcf ./opencv.tar.gz ./opencv/*

We need to use a .tar archive and not an upstream repo because we are packaging opencv_contrib too and it cannot be packaged independently: the official way of compiling it is by incorporating it into the OpenCV sources.

Then, just download the opencv3-release repo at https://github.com/ros-gbp/opencv3-release and:

git-bloom-release kinetic


OpenCV3 still evolves a lot, though the API is stable. You can expect more GSOC projects to be integrated and more CMake modularity.

2024-07-20 13:24