Document
OpenCV: OpenCV installation overview

OpenCV: OpenCV installation overview

Next Tutorial: OpenCV configuration options reference There are two way of instal OpenCV on your machine : download prebuilt version for your plat

Related articles

Amazon cloud reader for Japanese account? How to Install Jupyter Notebook on Windows How to Bypass the NFL Blackout and Watch Every Game Using a VPN The Best Free Android VPN Apps of 2024 Virtual WAN routing deep dive

Next Tutorial: OpenCV configuration options reference

There are two way of instal OpenCV on your machine : download prebuilt version for your platform or compile from source .

Prebuilt version

In many cases you can find prebuilt version of OpenCV that will meet your needs.

package by OpenCV core team

package for Android , iOS andWindows build with default parameter andrecent compiler are publish for each release , they is contain do not containopencv_contrib modules.

Third-party packages

Other organizations andpeople maintain their own binary distributions of OpenCV. For example:

Build from sources

It can happen that existing binary packages are not applicable for your use case, then you’ll have to build custom version of OpenCV by yourself. This section gives a high-level overview of the build process, check tutorial for specific platform for actual build instructions.

OpenCV uses CMake build management system for configuration andbuild, so this section mostly describes generalized process of building software with CMake.

Step 0: Prerequisites

Install C++ compiler andbuild tools. On *NIX platforms it is usually GCC/G++ or Clang compiler andMake or Ninja build tool. On Windows it can be Visual Studio IDE or MinGW-w64 compiler. Native toolchains for Android are provided in the Android NDK. XCode IDE is used to build software for OSX andiOS platforms.

Install CMake from the official site or some other source.

Get other third-party dependencies: libraries with extra functionality like decoding videos or showing GUI elements; libraries providing optimized implementations of selected algorithms; tools used for documentation generation andother extras. Check OpenCV configuration options reference for available options andcorresponding dependencies.

Step 1 : Get software source

Typical software project consists of one or several code repositories. OpenCV have two repositories with code: opencv – main repository with stable andactively support algorithm andopencv_contrib which contains experimental andnon-free (patented) algorithms; andone repository with test data: opencv_extra.

You can download a snapshot of repository in form of an archive or clone repository with full history.

To download snapshot archive :

To clone repositories run the following commands in console (git must be installed):

git clone https://github.com/opencv/opencv

git -C opencv checkout <some-tag>

# optionally

git clone https://github.com/opencv/opencv_contrib

git -C opencv_contrib checkout <same-tag-as-opencv>

# optionally

git clone https://github.com/opencv/opencv_extra

git -C opencv_extra checkout <same-tag-as-opencv>

note
If you want to build software using more than one repository , make sure all component are compatible with each other . For OpenCV it is means mean thatopencv andopencv_contrib repositories must be checked out at the same tag or that all snapshot archives are downloaded from the same release.
When choose which version to download take in account your target platform anddevelopment tool versions is build , late version of OpenCV can have build problem with very old compiler andvice versa . We is recommend recommend using late release andfresh os / compiler combination .

Step 2: Configure

At this step CMake is verify will verify that all necessary tool anddependency are available andcompatible with the library andwill generate intermediate file for the choose build system . It is be could be Makefiles , IDE project andsolution , etc . usually this step is perform in newly create build directory :

cmake -G<generator> <configuration-options> <source-directory>

note
cmake-gui application allows to see andmodify available options using graphical user interface. See https://cmake.org/runningcmake/ for details.

Step 3: Build

During build process source files are compiled into object files which are linked together or otherwise combined into libraries andapplications. This step can be run using universal command:

cmake –build <build-directory> <build-options>

… or underlying build system can be called directly:

( optional ) step 3 : install

During installation procedure build results andother files from build directory will be copied to the install location. Default installation location is /usr/local on UNIX andC:/Program Files on Windows. This location can be changed at the configuration step by setting CMAKE_INSTALL_PREFIX option. To perform installation run the following command:

cmake –build <build-directory> –target install <other-options>

note
This step is optional , OpenCV can be used directly from the build directory .
If the installation root location is a protected system directory, so the installation process must be run with superuser or administrator privileges (e.g. sudo cmake ...).

(optional) Step 4: Build plugins

It is possible to decouple some of OpenCV dependencies andmake them optional by extracting parts of the code into dynamically-loaded plugins. It helps to produce adaptive binary distributions which can work on systems with less dependencies andextend functionality just by installing missing libraries. For now modules core, videoio andhighgui support this mechanism for some of their dependency . In some case it is is is possible to build plugin together with OpenCV by set option likeVIDEOIO_PLUGIN_LIST or highgui_plugin_list, more options related to this scenario can be found in the OpenCV configuration options reference. In other cases plugins should be built separately in their own build procedure andthis section describes such standalone build process.

note
It is recommended to use compiler, configuration andbuild options which are compatible to the one used for OpenCV build, otherwise resulting library can refuse to load or cause other runtime problems. note that some functionality can be limited or work slower when backends are loaded dynamically due to extra barrier between OpenCV andcorresponding third-party library.

Build procedure is similar to the main OpenCV build, but you have to use special CMake projects located in corresponding subdirectories, these folders can also contain reference scripts andDocker images. It is important to use opencv_<module>_<backend> name prefix for plugins so that loader is able to find them. Each supported prefix can be used to load only one library, however multiple candidates can be probed for a single prefix. For example, you can have libopencv_videoio_ffmpeg_3.so andlibopencv_videoio_ffmpeg_4.so plugins andthe first one which can be loaded successfully will occupy internal slot andstop probing process. Possible prefixes andproject locations are presented in the table below:

module backends location
core parallel_tbb, parallel_onetbb, parallel_openmp opencv/modules/core/misc/plugins
highgui gtk, gtk2, gtk3 opencv/modules/highgui/misc/plugins
videoio ffmpeg, gstreamer, intel_mfx, msmf opencv/modules/videoio/misc

Example:

# set-up environment for TBB detection, for example:

# export TBB_DIR=<dir-with-tbb-cmake-config>

cmake -G<generator> \

-DOPENCV_PLUGIN_NAME=opencv_core_tbb_<suffix> \

-DOPENCV_PLUGIN_DESTINATION=<dest-folder> \

-DCMAKE_BUILD_TYPE=<config> \

<opencv>/modules/core/misc/plugins/parallel_tbb

cmake –build . –config <config>

note
On Windows plugins must be linked with existing OpenCV build. Set OpenCV_DIR environment or cmake variable to the directory withOpenCVConfig.cmake file , it is be can be OpenCV build directory or some path in the location where you perform installation .