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

Revision history [back]

If you have OpenCV2 and OpenCV3 installed, find_package(OpenCV) in CMake will first find OpenCV3.

If you want to explicitly link to OpenCV2, use find_package(OpenCV 2).

if you have OpenCV2 and OpenCV3 installed, OpenCV3 is not on your path: libraries and headers are renamed to only be visible through CMake. Python is the only exception but you can use the guide below to get your code to work with both. just like with any library, you have to be careful that your binary does not link/includes directly or indirectly (through a dependency) to both OpenCV2 and OpenCV3.

Python:

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

C++:

   #include "opencv2/core/version.hpp"
   #if CV_MAJOR_VERSION == 2
   // do opencv 2 code
   #elif CV_MAJOR_VERSION == 3
  // do opencv 3 code
  #endif

CMake:

if(OpenCV_VERSION VERSION_LESS "3.0")
# use 2.4 modules
else()
# use 3.x modules
endif()

Package.xml:

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 OpenCV package

See this wiki.

Adapted from this wiki:

If you have OpenCV2 and OpenCV3 installed, find_package(OpenCV) in CMake will first find OpenCV3.

If you want to explicitly link to OpenCV2, use find_package(OpenCV 2).

if If you have OpenCV2 and OpenCV3 installed, OpenCV3 is not on your path: libraries and headers are renamed to only be visible through CMake. Python is the only exception but you can use the guide below to get your code to work with both. just Just like with any library, you have to be careful that your binary does not link/includes directly or indirectly (through a dependency) to both OpenCV2 and OpenCV3.

Python:

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

C++:

   #include "opencv2/core/version.hpp"
   #if CV_MAJOR_VERSION == 2
   // do opencv 2 code
   #elif CV_MAJOR_VERSION == 3
  // do opencv 3 code
  #endif

CMake:

if(OpenCV_VERSION VERSION_LESS "3.0")
# use 2.4 modules
else()
# use 3.x modules
endif()

Package.xml:

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 OpenCV package

See this wiki.