
This tutorial explains how to build and setup OpenCV 4.12
Software package versions assumed by this guide are:
- opencv 4.12.0
- opencv_contrib 4.12.0
- A build environment with all required dependencies
Preparing OpenCV sources for building
First we need to download OpenCV sources. The sources are split in two, the base OpenCV and a separate extra module source that contains experimental or unstable modules.
Download and extract OpenCV sources
wget -O opencv-4.12.0.tar.gz https://github.com/opencv/opencv/archive/4.12.0.tar.gz
tar xf opencv-4.12.0.tar.gz
Optional: Download and extract OpenCV Extra modules
If you would like to use OpenCV features from the OpenCV Extra modules, then download and extract the parallel version release of opencv_contrib. You can skip this step if you don't intend to use them.
wget -O opencv_contrib-4.12.0.tar.gz https://github.com/opencv/opencv_contrib/archive/4.12.0.tar.gz
tar xf opencv_contrib-4.12.0.tar.gz
Prepare building OpenCV
Prepare a build directory for OpenCV by creating a directory under the source tree, you can use pretty much any name you like, but a good one is build, or for example if you are building both 32-bit and 64-bit versions you might use build32 and build64.
cd opencv-4.12.0
mkdir build
cd build
Configure the OpenCV build
Configure OpenCV build
Configure the OpenCV build with the following parameters to cmake.
cmake .. \
-DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release \
-DWITH_OPENCL=ON -DWITH_TBB=ON \
-DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF
Optional: OpenCV extra modules
opencv_contrib is a collection of experimental and specialized modules that are not part of the official core OpenCV distribution. These modules:
- Introduce new functionality or cutting-edge algorithms.
- May depend on additional libraries.
- Are less mature than core modules (APIs might change, less testing).
- Experimental algorithms and new computer vision techniques appear here first.
Include opencv_contrib if you need advanced, experimental, or specialized modules beyond the standard OpenCV feature set.
If you downloaded the extra modules and would like to have them built, then add the following configuration option to the above list of cmake options:
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.12.0/modules
Optional: NVIDIA CUDA Support
If you would like to have CUDA support in the OpenCV DNN module then make sure you have the NVIDIA CUDA SDK installed and add the following cmake option:
-DWITH_CUDA=ON -DOPENCV_DNN_CUDA=ON
Build and install OpenCV
As we are only interested in native libraries, we skip building of any android specific projects and also enable shared libraries. If all goes well you should now have the build configured and ready to go.
Just run make and the OpenCV should build for you:
cmake --build .
cmake --install .
The default location, or install prefix, is /usr/local/, if that is not suitable for you, then you can specify the install location with the prefix option, for example --prefix=/opt/OpenCV-4.12.0 or give -DCMAKE_INSTALL_PREFIX=/opt/OpenCV-4.12.0 option to cmake when configuring.
cmake --install . --prefix=/opt/OpenCV-4.12.0
The Android install target will not install anything to your system, instead it will make directory structure with headers and libraries under a install/sdk directory. From here you can then copy the libraries and headers to a suitable location.
Reference the libraries & headers from your Qt project file
Next you need to reference the Android ARM libraries for linking and packaging from your project.
Add a section with the following contents, adjusted for your needs. In my application I only needed a couple of libraries but depending on what features of OpenCV you are using you need to add more of them.
You also need add the INCLUDEPATH setting that points to the OpenCV C++ header files. Adjust any example paths as needed for you installation.