OpenCV is an open source computer vision library which is very popular for performing basic image processing tasks such as blurring, image blending, enhancing image as well as video quality, thresholding etc. In addition to image processing, it provides various pre-trained deep learning models which can be directly used to solve simple tasks at hand. The programmers have to download and load the model using OpenCV instructions in order to do the task of inference on their own dataset.

Firstly, you need to install OpenCV library in your system prior to using it for your own dataset. At this stage, there can be two pathways of installing OpenCV in your system namely – (a) Using pip (b) Source Installation. pip is the package manager which is used to install the packages written in python. The difference between installing a python package from source and through pip are given in the following table:

Note: All the points in the table are written with respect to OpenCV but they can also be generalized for other libraries and packages.

Installation using pip Installation from Source
This will install the features which are already build from source i.e., installation of general features and parameters will be carried out. This will allow you to select your own features and parameters to be installed.
Installation is done at the default location where all the python packages resides. Installation location is provided by the programmer.
In terms of performance, the packages installed might run slower because of the hidden conflicts between features. The programmer can select the optimization flags during the compilation of packages which are responsible for the fast performance of library.
The programmers can neither add nor remove features provided in the installation done by pip. Here, the programmer has all the rights to add or remove the features during the installation of library.
The programmers are not expected to have knowledge about the library. This installation is well suited for beginners and those who want to complete their job at the earliest. The programmers must have strong knowledge about packages present in the library because fast performance can be achieved through the fine selection of features during installation.
The package manager will do the work on behalf of programmer. Package Manager is also responsible for taking care of library updation. The programmers are responsible for  feature selection and updation of library. They must be aware of new package updates, latest security patches etc, to keep themselves updated about  the library.

In this article, we will give priority to the installation of OpenCV from source so that developers can modify the installation with respect to their task. We will be explaining the installation from source for Windows 10.

Step 1: Download OpenCV

Click on the link to redirect to the latest release web page of OpenCV.

This website contains all the information and links about all the OpenCV library versions.

OpenCV Release Webpage

Step 2: Download OpenCV-contrib

As you can see in the image above, Click on Sources button to download OpenCV – 4.1.0 archive files into your system. Once the download is complete, unzip the files at your desired location. For illustration purpose, I am going to create a folder named as ‘opencv’ in my Desktop and I will unzip the downloaded archive inside the same folder.

In order to download OpenCV_contrib you must open the command line tool and clone the repository by executing the following command:

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

If you do not have git installed in your system then you can click on the above link and clone it from the website. Make sure that you have cloned the repository next to the unzipped OpenCV folder.

Note: There are other versions of OpenCV as well. If you are interested to download them then you can scroll down to find out what you are looking for.

After cloning, downloading and unzipping the archive, it will be something like the screenshot shown below:

These two folders contain the files related to OpenCV.

OpenCV folder contains OpenCV contrib and OpenCV 4.1.0 folder

The opencv-4.1.0 folder contains all the important header files and code related to the modules of OpenCV. The opencv_contrib folder contains extra modules which you will install along with OpenCV. Installing OpenCV_contrib is not a mandatory step. In the final step of this tutorial, we will use one of the modules of OpenCV to run a sample code which will verify the installation.

Step 3: Download and Install Visual Studio 2019 Community

It’s time to hold up OpenCV for a little because we will now install Visual Studio 2019 community. Visual Studio is a powerful IDE which helps developers to write and debug the large amount of code smartly as well as quickly as possible. In this tutorial, we are not going to write code but we will use Visual Studio to demonstrate the compilation of OpenCV in Release mode. Click on this link to jump to Microsoft Visual Studio and download the Visual Studio 2019.

Step 4: Install Python and C++ Development Environments in Visual Studio 2019

Click on the setup file downloaded to start the installation of Visual Studio Installer. The installer will be downloaded and finally a window will pop up on the screen which will want you to select the working environments to be installed along with the installation of Visual Studio. You can select multiple workloads as per your requirements to be installed into your system. We are going to select two workloads for the illustration of OpenCV installation which are as follows:

(1) Python Development

(2) Desktop Development with C++

Python and C++ development environment selection

Selecting the Python and C++ development environments for installation.

Please refer to the screenshot to select the required packages.

Note: However, you can change the location, if there is not enough space left in the system for the installation of workloads.

After selecting the workloads, click on the install button to get the installation job done. This will take around 10 minutes to 1 hour (depending upon the internet speed).

Step 5: Install CMake

The next step is to install CMake in your system. CMake is an open-source platform which is designed to build and test the packages manually. During the installation of OpenCV from source, CMake will help us to control the compilation process and it will generate native makefiles and workspaces that can be used in the compiler environment of your choice. For more information about CMake, you can refer to this link.

Our goal is to install CMake into our system so please click on this link which will redirect you to the latest release page of CMake. Please refer to screenshot to install the highlighted version of CMake.

Follow the simple steps to install the CMake. There is a point in installation where you will be asked to add CMake to the PATH variable. Since, we are going to install OpenCV using the CMake GUI, therefore, we don’t need to set the CMake as a PATH variable. If you want to install OpenCV using command line in windows then you must add CMake to PATH variable. We are not going to discuss the installation of OpenCV using command line but if you want to follow the command line instructions then I would like to recommend you to check the next section which is OpenCV installation in Ubuntu. The commands are similar for both Windows and Ubuntu, if command line tool is used for installation.

CMake Installer webpage image

Downloading the appropriate CMake Installer

Step 6: General CMake overview

Type cmake in the search box and click on ‘CMake (cmake-gui)’ to start CMake GUI. The general description of the CMake GUI fields is given below in the screenshot.

Describing the CMake GUI text fields

Description of CMake GUI

Step 7: Setting up CMake Inputs

In the source code field, you have to browse to the folder where we have unzipped the OpenCV files. In my case the path is  C:/Users/Utkarsh/Desktop/opencv/opencv-4.1.0.

In the second text box, you have to provide a path where the newly build binaries along with Visual Studio Project file will be saved. In my case, the path is C:/Users/Utkarsh/Desktop/opencv/build.

Note: It is not necessary that the newly build binaries folder should exist in advance. If the folder doesn’t exist, CMake will create the folder on your behalf and continue to build the binaries. In my case, ‘build’ folder doesn’t exist in advance.

Filling Source and Destination Path of Build.

Filling the Source Code and Build Binaries locations.

Step 8: Overview of Flags for installation of OpenCV

After providing the path, click on the Configure button. If the build directory doesn’t exist then CMake will ask you to give permission to create a new folder. After that, you will be asked to select the compiler for the compilation. I would recommend you to select the default options as shown in the screenshot.

Click Next to continue with the compilation of files. During this process, you can see the status bar which will provide you the information about the configuration step. You can go through the messages and get a deeper understanding about the dependencies.

Select Visual Studio 16 2019 as the Generator

Selecting the Generator for configuration and generation.

When you are done with the configuration step then all the flags will be displayed to you as shown in the screenshot.

CMake Configuration Result

CMake Configuration Result

It’s time to talk about the two columns which you can see on your screen. The Name column is the flag/feature which you want to install in your system during the installation of OpenCV. The Value column confirms the selection of feature to be installed in the system. We will discuss about some of the flags as follows:

1- BUILD_CUDA_STUBS: This flag is going to build CUDA stubs if there is no CUDA SDK present in the system.

2- BUILD_DOCS: This flag is used to create build rules for the OpenCV documentation.

3- BUILD_EXAMPLES: This flag is used to build all the examples present in the OpenCV library. You can find those examples under the samples folder of the extracted zip on OpenCV.

4- BUILD_ZLIB: This will build the ZLIB from source. The zlib compression utility is a general purpose utility that is useful for various image formats other than PNG, therefore, it can be used without having libpng.

5- BUILD_TIFF: This flag is going to build libtiff from source. It is a library for reading and writing Tagged Image File Format files. It is similar to other libraries namely libpng and libjpeg which is used to work with .tiff format.

6- BUILD_JASPER: This will build the library libjasper from source. It is a project to create a reference implementation of the encoding and decoding specified in the JPEG-2000 Part-1. In other words, it helps in coding and manipulation of images. This library can handle image data in a variety of formats.

7- BUILD_JPEG: This will build the library libjpeg from source. This is a free library for handling JPEG Image Data format. It has got various utilities such as cjpeg and djpeg to convert the JPEG data into other image file formats. It also has rdjpgcom and wrjpgcom which helps to insert and extract textual comments in JPEG files.

8- BUILD_PNG: This will build libpng from source. This library is used to handle PNG format images. It is platform independent and is written in C language. It uses zlib for compression and decompression of PNG files.

9- BUILD_WITH_DEBUG_INFO: This flag option in going to include debug info into the release binaries.

10- BUILD_WITH_STATIC_CRT: This flag enables the use of statically linked C-Run Time libraries for statically linked OpenCV. This flag can be enabled in windows system if Microsoft Visual C++ is used for the compilation.

11- BUILD_ANDROID_SERVICE: This flag allows to build OpenCV manager for Google Play.

12- BUILD_JAVA: This flag will enable the Java Support for OpenCV.

13- BUILD_FAT_JAVA_LIB: This flag will create Java wrapper function i.e. exporting all functions of OpenCV library but it also requires static build of OpenCV modules.

14- INSTALL_C_EXAMPLES, INSTALL_PYTHON_EXAMPLES, INSTALL_ANDROID_EXAMPLES : This flag will enables the installation of the C, Python and Android examples present in the sample folder created during the extraction of the OpenCV zip.

15- INSTALL_TESTS: This will install accuracy and performance test binaries and test data.

16- ENABLE_CCACHE: This flag is known as Compiler Cache flag. This will store the output of the compiler after the compilation of your program. If the file remains unchanged and recompilation is done then the output of compilation will be directly taken from the ccache.

17- ENABLE_PRECOMPILED_HEADERS: This flag is used to enable the use of precompiled headers. Precompiled headers are the header files that are compiled into an intermediate form which are faster to process for the compiler.

18- ENABLE_SOLUTION_FOLDER: This flag enables the use of Solution Folder in Visual Studio IDE. This folder is the container of the projects which you want to do using OpenCV.

19- ENABLE_PROFILING: This flag enables the profiling in the GCC compiler i.e. it add flags -g, -pg. Profiling is an aspect of software programming where a programmer can determine the parts in the program code that are time consuming and need to be re-written.

20- OPENCV_GENERATE_PKGCONFIG: This will generate .pc file for pkg-config build tools. pkg-config is a tool used during the compilation of application and libraries. It will help you to provide correct compiler options on the command line i.e. it will find installed libraries which are required for the compilation of software.

21- CV_ENABLE_INTRINSICS: This will enable the compiler to use intrinsic functions which are provided in the programming language to optimize the code. This optimization will be done when the program requests explicit optimization i.e. when the flag is enabled.

22- ENABLE_LTO: This flag will be used to optimize the code during the linking of libraries. The linker will pull all the object files and combine them to optimize the code.

23- ENABLE_THIN_LTO: This flag will be used for optimization of program in incremental and scalable manner. It is similar to regular LTO as clang will emit LLVM bitcode after the compilation. The ThinLTO bitcode is augmented with a compact summary of the module. During the link step, only the summaries are read and merged into a combined summary index, which includes an index of function locations for later cross-module function importing. Fast and efficient whole-program analysis is then performed on the combined summary index.

24- LAPACK_LIBRARIES: LAPACK (Linear Algebra PACKage) is written in Fortran 90 and provides routines for solving systems of simultaneous linear equations, least-squares solutions of linear systems of equations, eigenvalue problems, and singular value problems. The original goal of the LAPACK project was to make the widely used EISPACK and LINPACK libraries run efficiently on shared-memory vector and parallel processors.

25- MKL_ROOT_DIR: Intel® Math Kernel Library (Intel® MKL) optimizes code with minimal effort for future generations of Intel® processors. It is compatible with your choice of compilers, languages, operating systems, and linking and threading models. It uses industry-standard C and Fortran APIs for compatibility with popular BLAS, LAPACK, and FFTW functions for its working.

26- OPENCV_EXTRA_MODULES_PATH: This flag is used to specify the path of the extra modules which will be installed along with the installation of OpenCV. Note that, we have downloaded opencv_contrib in step 2 which is nothing but the extra modules in OpenCV. You can set the path on this flag to the opencv_contrib downloaded folder.

27- OPENCV_GENERATE_PKGCONFIG: This flag is used to generate the opencv4.pc file. The pkg-config provides command line options for compiling the programs i.e. pkg-config finds the path of installed libraries automatically which are going to be used in your program. OpenCV 4.1.0 doesn’t generate the opencv4.pc file unless this flag is specified.

28- WITH_CUDA: This option allows the CUDA support in the OpenCV. You need to make sure that you have CUDA enabled GPU in your machine in order to use the CUDA library. CUDA is generally used to make the computations faster with the help of GPU.

29- WITH_DIRECTX: This option allows the Microsoft DirectX support for OpenCV. It is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video on Microsoft platforms.

30- WITH_EIGEN: This flag enables the Eigen library support for OpenCV. It is a high-level C++ library of template headers for linear algebra matrix and vector operations, geometrical transformations, numerical solvers and related algorithms.

31- WITH_GDAL: The Geospatial Data Abstraction Library (GDAL) is a computer software library for reading and writing raster and vector geospatial data formats, and is released under the permissive X/MIT style free software license by the Open Source Geospatial Foundation. As a library, it presents a single abstract data model to the calling application for all supported formats. It may also be built with a variety of useful command line interface utilities for data translation and processing.

32- WITH_GSTREAMER: GStreamer is a library for constructing graphs of media-handling components. The applications it supports range from simple Ogg/Vorbis playback, audio/video streaming to complex audio (mixing) and video (non-linear editing) processing.

33- WITH_HALIDE: Halide is an open-source project that let us write image processing algorithms in well-readable format, schedule computations according to specific device and evaluate it with a quite good efficiency.

34- WITH_HPX: HPX (High Performance ParalleX) is a general purpose C++ runtime system for parallel and distributed applications of any scale. This library strictly adheres to the C++11 Standard and leverages the Boost C++ Libraries which makes HPX easy to use, highly optimized, and very portable.  HPX is developed for conventional architectures including Linux-based systems, Windows, Mac, and the BlueGene/Q, as well as accelerators such as the Xeon Phi.

35- WITH_OPENGL: This flag will allow the OpenGL support is OpenCV for building application related to OpenGL. OpenGL is a library which is used to render 3D scenes. These 3D scenes can be later processed using OpenCV library in the form of images and videos.

36- WITH_TBB: The TBB flag makes use of Intel’s Parallel Programming and Heterogeneous Computing library i.e. Thread Building Block library. The library provides a wide range of features for parallel programming, including generic parallel algorithms, concurrent containers, a scalable memory allocator, work-stealing task scheduler, and low-level synchronization primitives. If you want to know more about this library click on this link.

37- WITH_OPENCL: OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms consisting of central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), field-programmable gate arrays (FPGAs) and other processors or hardware accelerators

38- WITH_QT: Qt is a cross-platform application development framework for desktop, embedded and mobile. Qt is not a programming language on its own. It is a framework written in C++. With Qt, GUIs can be written directly in C++ using its Widgets modules. Qt also comes with an interactive graphical tool called Qt Designer with functions as a code generator for Widgets based GUIs.

Step 9: Setting Up the Flags for the installation of OpenCV

Now we will choose some of the flags to install OpenCV in our way. Moreover, we will also provide the path of the opencv_contrib as shown below in the following snapshots:

a) Selecting the flag for installation of C and Python examples

Python and C examples installation flag selection

Selecting the flags for the installation of Python and C examples

b) Selection WITH_QT, WITH_OPENGL flags for Qt and OpenGL support in OpenCV

Selection of Qt and OpenGL flag

Selecting WITH_QT and WITH_OPENGL flag for QT and OpenGL support.

c) Providing the path of opencv_contrib as shown below.

OpenCV_contrib extra modules path

Providing Extra Modules path i.e. modules present in opencv_contrib folder

Step 10: Generating the Project Files for Visual Studio 2019

Now we will click on the Generate button to generate the solution file of the Visual Studio Project. After the generation is complete, you will reach a stage which looks like the snapshot as shown below:

Generation of Project Files

Generating the Visual Studio Project Files i.e. solution files

The build folder will look something like the snapshot shown below:

Build Folder

Build Folder contains important files e.g. solution file.

You can see that there are a lot of files being generated which are essential for OpenCV source installation. In the next step we will go through the project file i.e. OpenCV.sln to finally build the library in our system.

Step 11: Overview of Visual Studio 2019

Click on the OpenCV.sln file to start the Project in Microsoft Visual Studio 2019 which we have installed in previous steps. You will see a window as shown in the following snapshot.

Overview of Visual Studio 2019

Overview of Visual Studio 2019

There are two things, we need to keep in our mind:

I) We have to change the mode from Debug mode to Release

II) We have to scroll down the Solution Explorer Dialog Window to find the ALL_BUILD

Step 12: Building the Library

In the next snapshot, you can figure out that we have changed the mode from Debug Mode to Release Mode and we have right clicked on ALL_BUILD file which results in a Dialog Box. Click on the Build option to finally build the library. Remember, this will use all the CPU threads and it will take some time to complete. Please have patience while this process completes.

As we are building the library in Release Mode, the Optimizations flag are turned ON, therefore it will remove all those steps which are related to Debug Mode. This will also consume a little time to complete. Well, it is automatically being done for you. You might see sequence of numbers in the Output window which are out of order. It is about different threads which are completing their job as soon as they can.

Visual Studio Building

Finding ALL_BUILD under CMakeTargets and Click on the Build.

When the build will complete, the output will look something like the snapshot shown below:

Output of Building

Output of completion of Building the Library

Step 13: Installing the Library

It’s time to install the library. In the Solution Explorer, you have to find the INSTALL under the CMake Targets. You have to right click on the INSTALL and then you have to click on the Build as shown in the next screenshots.

Finding Install under CMake Targets to install the OpenCV library.

Find INSTALL under CMake Targets

Clicking on Build will finally install the OpenCV libray.

Right Clicking on INSTALL and Click on Build to install the library

This will finally install the library. This step is not going to take too long to complete. The time of completion will depend upon the speed of the machine. When the installation is complete, the output will look something like the next screenshot.

State after the successful installation of OpenCV

OpenCV successfully installed

Step 14: Verification of Installation of OpenCV

We will verify the installation for Python by printing the version of OpenCV.

Step 14.1- Open up the Command Prompt and run the python interpreter as shown in the screenshot below.

Starting the Python Interpreter

Opening Command Prompt and starting Python Interpreter

Step 14.2- Import the opencv library and print the version of opencv as shown in the next screenshot.

import opencv version and print the version

Printing the OpenCV version

Congratulations! OpenCV is now installed on your computer. Have fun building something cool with it.