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 Ubuntu 18.04.

 

Step 1: Installing dependencies of OpenCV

It’s time to install the dependencies of OpenCV. You have to execute the following commands to install all the dependencies one by one. We will also discuss the dependencies along with their installation.

1- build-essential: It is a package which contains references to other important packages which are needed for building software or compiling packages. It generally includes GCC / g++ compilers, libraries and some other utilities.

2- checkinstall: It is a program which helps to compile the code from source and creates native package for your linux distribution. After compilation, you will be able to install the package using your distribution package management system (dpkg, rpm)

3- cmake: It is cross-platform build system generator. You can build a project by using CMake to generate a build system for a native tool on your platform. It controls the software compilation process using simple platform and compiler independent configuration files.

4- pkg-config: It 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.

5- yasm: It is an assembler which supports x86 and AMD64 instruction sets. ELF32, ELF64, 32 and 64-bit Mach-O, RDOFF2, COFF, Win32 and Win64 are the object formats used to generate output binaries.

6- git: It is an open source controlling system which can handle projects to boost up the efficiency of developers.

7- gfortran: GNU Fortran is a part of GCC, the GNU Compiler Collection. It can be used to compile Fortran programs to get the desired output during execution. The GFortran will develop the Fortran compiler front end and run-time libraries for GCC.

8- libjpeg: 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.

9- libjasper: 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.

10- libpng: 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. 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.

11- libtiff: 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.

12- libavcodec: This library provides encoding and decoding framework for audio, video and subtitle streams and several bitstream filters. The popular MPlayer, xine and VLC media players use it as their main, built-in decoding engine that enables playback of many audio and video formats on all supported platforms. It also contains highly optimized implementations of common building blocks such as DCT (Discrete Cosine Transform) and Color Space conversion.

13- libavformat-dev: This library provides a general framework for multiplexing and demultiplexing audio, video and subtitle streams. It also supports several input and output protocols to access media resources.

14- libdc1394-22-dev: This library allows a program to interface with camera which conforms to IEEE 1394 standard.

15- libxine2-dev: This library provides complete infrastructure for a media player. The format supported are MPEG 1/2, some AVI and Quicktime videos. It also supports network streams, subtitles and even MP3 or Ogg files.

16- libv4l-dev: It is a collection of libraries which adds a thin abstraction layer on top of video4linux2 devices. This makes easy for application writers to support a wide variety of devices without having to write separate code for different devices in the same class. It has got other utilities such as lib4lconvert which does the job of converting the current pixel format to BGR24, RGB24, YUV420 and YVU420.

17- libgstreamer0.10-dev: It is a streaming media framework based on graphs of filters which operate on media data. You can perform real-time sound processing to playing videos with this library. It has plugin-based architecture which means that new processing capabilities can be added simply.

18- libgstreamer-plugins-base0.10-dev: This package contains development files for GStreamer libraries from the ‘base’ set, an essential exemplary set of elements.

19- qt5-default: Qt is a cross-platform C++ application framework. It has a rich set of widgets which provides standard GUI functionality. It is classified as widget toolkit. It has the capability of developing non-GUI programs such as command-line tools and consoles for servers.

20- libgtk2.0-dev: It is a free and open source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It contains set of graphical control elements and is an object oriented widget toolkit.

21- libtbb-dev: This library makes use of multi-core processor performance to do tasks in parallel manner. It represents a higher-level, task-based parallelism that abstracts platform details and threading mechanism for performance and scalability.

Note: This package only supports Pentium4 compatible and higher processors, if you are using 32-bit machine.

22- libatlas-base-dev: This library supplies optimized versions for the complete set of linear algebra kernels known as Basic Linear Algebra Subroutines (BLAS) and a subset of the linear algebra routines in the LAPACK library. ATLAS is an acronym for Automatically Tuned Linear Algebra Software.

23- libfaac-dev: This library is AAC encoder and FAAD2 decoder. It supports several MPEG-4 object types (LC, Main, LTP, HE AAC, PS) and file formats (ADTS AAC, raw AAC, MP4).

24- libmp3lame-dev: It is MP3 encoding library which is used to for sound analysis as well as convenience tools.

25- libtheora-dev: Theora Video Compression Codec. Theora is a fully open, non-proprietary, general purpose compressed video format.

26- libvorbis-dev: This library contains development files for Vorbis General Audio Compression Codec. Ogg Vorbis is a fully open, non-proprietary, general purpose compressed audio format for audio and music at fixed and variable bitrates from 16 to 128 kbps/channel.

27- libxvidcore-dev: This library contains development files for MPEG-4 video codec. Xvid is an open source MPEG-4 video codec, implementing MPEG-4 Simple Profile, Advanced Simple Profile and Advanced Video Coding standards. It is written in C with assembler optimizations for quality and speed.

28- libopencore-amrnb-dev: This library contains an implementation of the 3GPP TS 26.073 specification for the Adaptive Multi Rate (AMR) speech codec. The implementation is derived from the OpenCORE framework, part of the Google Android project.

29- libopencore-amrwb-dev: This library contains an implementation of the 3GPP TS 26.173 specification for the Adaptive Multi Rate – Wideband (AMR-WB) speech decoder. The implementation is derived from the OpenCORE framework part of the Google Android project.

30- x264: It is a free and open-source software library and a command line utility developed by VideoLAN for encoding video streams into the MPEG-4 AVC format. It is a very high quality encoder and produces remarkable quality in bitstreams.

31- v4l-utils: It is a collection of command line video4linux utilities such as decode_tm6000 which decodes tm6000 proprietary format streams. It also has a utility which can receive and decode Radio Data System (RDS) streams.

32- libprotobuf-dev: This library is responsible for serializing structured data which is similar to XML. This will help to easily write and read your structured data to and from a variety of data streams.

33- protobuf-compiler: This package contains the protocol buffer compiler that is used for translating from .proto files (containing the definitions) to the language binding for the supported languages.

34- libgoogle-glog-dev: This library provides logging APIs based on C++ style streams and helper macros

35- libgflags-dev: This library implements command line flags processing. It gives you the ability to define flags in the source file.

36- libgphoto2-dev: The gphoto2 library can be used by applications to access various digital camera models, via standard protocols such as USB Mass Storage and PTP, or vendor-specific protocols.

37- libeigen3-dev: Eigen 3 is a lightweight C++ template library for vector and matrix math, a.k.a. Linear Algebra. It is dedicated to provide optimal speed with GCC.

38- libhdf5-dev: This library contains development files for Hierarchical Data Format 5 (HDF5). HDF5 is designed to store and organize large amounts of data.

39- doxygen: This tool is used to generate an online documentation for annotated C++ sources. It can visualize the relations between the various elements by means of include dependency graphs, inheritance diagrams and collaboration diagrams which are generated automatically.

Step 2: Installing the required Python version

We will install Python 2 as well as Python 3. You can install Python as per your requirements.

40- python-dev: It contains the header files which you need to build Python 2 modules. It is a package that contains header files for the Python C API. This will install Python 2.7.15.

41- python-pip: pip is a package management system that simplifies installation and management of software packages written in Python such as those found in the Python Package Index (PyPI). Installing this library will install pip2 i.e. pip for Python for 2.7.15.

42- python3-dev: It contains the header files which you need to build Python 3 modules. It is a package that contains header files for the Python C API. This will install Python 3.6.7.

43- python3-pip: pip is a package management system that simplifies installation and management of software packages written in Python such as those found in the Python Package Index (PyPI). Installing this library will install pip3 i.e. pip for Python for 3.6.7.

Step 3: Installing the Virtual Environment

We will download virtual environment package for Python 2 and Python 3 to separate the installation of libraries for different projects.

Step 4: Modify .bashrc file

You don’t have to append the following lines in .bashrc file. You just need to execute the following 3 lines of command which will automatically modify the .bashrc file.

Step 5: Creating Virtual Environment and Installing basic Python libraries

Now we will create virtual environments in Python 2 and Python 3. Moreover, we will install basic libraries as shown in the code cells given below:

Step 6: Downloading OpenCV 4.1.0 and OpenCV Contrib:

At this stage, we have got all the dependencies and required Python version in our system. Therefore, we will download OpenCV and OpenCV_contrib from their github repositories and install them accordingly. For illustration purpose we are cloning the repositories in the Desktop.

Step 7: Using CMake to build the library

We will create a new directory ‘build’ in opencv folder where we will store our newly build binaries.

Before executing the next command, let’s discuss about the flags which are used to install the library.

1) CMAKE_BUILD_TYPE=RELEASE : This is used to build the library in Release Mode. There can be many types of mode present in a project. These modes are called as solution configurations. Firstly, let us learn about Debug Mode. In Debug Mode, you want your project to be built with the optimizer turned OFF and you want full debugging/symbol information included in your build files (i.e. in the .PDB file). In Release Mode, you want to build your project with the optimizer turned ON and you don’t want full debugging information included. Debug Mode is used during the development and testing phase of the project because errors and bugs must be figured out and should be corrected as soon as possible. However, Release Mode is used to optimize the code so that the final project can be deployed into production.

2) CMAKE_INSTALL_PREFIX=/usr/local : This flag is setting up the path where all the OpenCV file after the installation will be saved. The installed folder will contain optimized header files which you will include in your project work as per your requirements.

3) INSTALL_C_EXAMPLES=ON and INSTALL_PYTHON_EXAMPLES: This flag will allow the installation of the OpenCV examples written using either C or Python Language respectively. The examples are saved in the sample folder which is present in the extracted folder of OpenCV.

4) WITH_TBB=ON: 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.

5) WITH_V4L=ON: This flag is used to use the collection of device drivers and an API for supporting realtime video capture on Linux system. It supports many USB webcams, TV tuners and related devices, standardizing their output, so programmers can easily add video support to their applications.

6) WITH_QT=ON: 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.

7) WITH_OPENGL=ON: 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.

8) OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules: This flag specifies the installation of the extra modules for OpenCV. Some of the extra modules list is as follows:

  1. aruco: ArUco Marker Detection
  2. bgsegm: Tutorials for bgsegm module
  3. bioinspired: Discovering the human retina and its use for image processing
  4. cnn_3dobj: CNN for 3D Object Classification and Pose Estimation
  5. cvv: Interactive Visual Debugging of Computer Vision applications
  6. dnn_objdetect: Object Detection using CNNs
  7. face: Tutorial on Facial Landmark Detector API
  8. tracking: Introduction to OpenCV Tracker, Customizing the CN Tracker, Using Multitracker

Click on this link to know more about Extra modules.

9) OPENCV_GENERATE_PKGCONFIG=YES: OpenCV 4.1.0 does not generate opencv4.pc file on its own unless this flag is specified. When the installation is complete, we will modify and relocate opencv4.pc file to a specific location and add the location of the file to the PATH variable. This file will be used during the compilation of your program i.e. it will help in finding the OpenCV modules required for your program.

Now we will use CMake to build the binaries according to the flags passed in the next command. You can pass your own flags to build the library in your own way.

Step 8: Using make utility for building the library

Now it’s time to build the Library using make command. We will use nproc command to find the number of threads which can run in parallel to complete the desired task. The syntax of make command is “make -jn” where n is the number of threads that you want to use for building the library. Let us say that the output of nproc is 8 which states that your machine can execute 8 threads in parallel. It depends upon your choice i.e. you are responsible for selecting the number of threads for completing the build task. Usually people don’t want to use all the threads because if all the threads will get busy in the building task then you won’t be able to use your system. Therefore, it is advised to use one or fewer less than total number of threads so that you can use your system while building the library completes in the background.

Step 9: Modifying opencv4.pc file

It’s time to modify the opencv4.pc file.

Replace the specified line with the following line:

Save the file and exit.

The reason of modifying the specified line is the new path is changed from opencv to opencv2. If you don’t modify the line then compilation error will occur when you try to compile your program.

Step 10: Relocate the opencv4.pc file

In this step you have to move opencv4.pc file to the location specified below.

Step 11: Adding file location to PKG_CONFIG_PATH Variable and modifying .bashrc file

Add the location to the variable PKG_CONFIG_PATH by editing the .bashrc file.

Save and Exit the file and run the following command.

Step 12: Verifying the Installation

It’s time to verify the installation. We will verify the installation for C++ and Python by printing the OpenCV version along with it’s major, minor and subminor versions.

C++ Verification

In this section, we will create a new file test.cpp at your desired location. Please copy the following code into the newly created file and follow the instructions accordingly.

Save the file and exit. Now, open the terminal and jump to the directory where you have saved your test.cpp file. For illustration purpose we have saved the file in the location ~/Desktop/test_code/test.cpp .

Python2 Verification

In this section, we will create a new file test_2.py at your desired location. Please copy the following code into the newly created file and follow the instructions accordingly.

Save the file and exit. Now, open the terminal and jump to the directory where you have saved your test.cpp file. For illustration purpose we have saved the file in the location ~/Desktop/test_code/test_2.py .

Python3 Verification

In this section, we will create a new file test_3.py at your desired location. Please copy the following code into the newly created file and follow the instructions accordingly.

Save the file and exit. Now, open the terminal and jump to the directory where you have saved your test.cpp file. For illustration purpose we have saved the file in the location ~/Desktop/test_code/test_3.py .