Using OpenCV in a CamiTK extension project

A step by step on how to use OpenCV in an action or component.

Although CamiTK Community Edition does not depends on OpenCV, if your extension requires OpenCV, this page presents the correct way of setting up, install and use OpenCV for a flawless integration.

Setup and install OpenCV

On Linux

On Ubuntu, Debian or derivatives, just install the dev package (typically using sudo apt install libopencv-dev).

For other Linux distribution, please check that a package is available. Otherwise just refer to the OpenCV documentation or get inspired by the windows setup below. As OpenCV is also using CMake, it is a easy step.

On Windows

If you followed the Windows Setup guide, just fire up a command shell and type:

%CAMITK_INSTALL_DRIVE%
cd %CAMITK_INSTALL_ROOT%\

:: set the OpenCV target version
set opencv_ver=4.5.2
echo %opencv_ver%

:: download OpenCV
curl -L https://github.com/opencv/opencv/archive/%opencv_ver%.zip --output %CAMITK_INSTALL_ROOT%\downloads\opencv-%opencv_ver%.zip

:: extract the source
7z x %CAMITK_INSTALL_ROOT%\downloads\opencv-%opencv_ver%.zip -o%CAMITK_INSTALL_ROOT%\src

:: configure
mkdir %CAMITK_INSTALL_ROOT%\build\opencv
cd %CAMITK_INSTALL_ROOT%\build\opencv
cmake ..\..\src\opencv-%opencv_ver%^
      -DCMAKE_INSTALL_PREFIX=%CAMITK_INSTALL_ROOT%\install^
      -DCMAKE_DEBUG_POSTFIX="-debug"^
      -DOPENCV_LIB_INSTALL_PATH="lib"^
      -DOPENCV_BIN_INSTALL_PATH="bin"^
      -DBUILD_SHARED_LIBS=TRUE^
      -G"NMake Makefiles JOM"^
      -DCMAKE_BUILD_TYPE="Debug"

:: build and install the debug version
:: (the parallel argument should match the number of core of your computer)
cmake --build . --target install

:: Do the same for the release version
cmake ..\..\src\opencv-%opencv_ver%^
      -DCMAKE_INSTALL_PREFIX=%CAMITK_INSTALL_ROOT%\install^
      -DOPENCV_LIB_INSTALL_PATH="lib"^
      -DOPENCV_BIN_INSTALL_PATH="bin"^
      -DBUILD_SHARED_LIBS=TRUE^
      -G"NMake Makefiles JOM"^
      -DCMAKE_BUILD_TYPE="Release"

cmake --build . --target install

Configure your CEP to use OpenCV

On Linux, once installed, you need to provide the default installation directory when you run CMake configuration:

# 1. Find the directory that contains OpenCV CMake configuration file (i.e. OpenCVConfig.cmake)
OPENCV_DIR=$(dirname $(find /usr/lib -path "*cmake/*/OpenCVConfig.cmake"))
# 2. Provide this to CMake
cmake path/to/src -DOpenCV_DIR=$OPENCV_DIR

On Windows, OpenCV should be found by CMake as it should be installed in %CAMITK_INSTALL_DRIVE%\%CAMITK_INSTALL_ROOT% (if you followed the above setup).

Declare the dependency of your extension

Do not forget to check or add the NEEDS_OPENCV in your extension CMakeLists.txt:

camitk_extension( ...
  NEEDS_OPENCV
)

CamiTK CMake macro camitk_extension will automatically do the configuration magic to include and link with OpenCV.

→ Now you are ready to use OpenCV C++ API in your extension!

If you want a more portable solution across Windows and Linux, you might need to add INCLUDE_DIRECTORIES ${OpenCV_INCLUDE_DIRS}/opencv4 in your camitk_extension options. This way `#include <opencv2/opencv.hpp>" will work independently on the OpenCV version and operating system.

If you have already written some code using OpenCV in python, just extract the processing part and translate it to C++ (it is usually a direct process). This will probably improve the performance, user experience, long-term support and reproducibility of your code.