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.
sudo apt-get update -y # Update the list of packages sudo apt-get remove -y x264 libx264-dev # Remove the older version of libx264-dev and x264
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.
sudo apt-get install -y build-essential checkinstall cmake pkg-config yasm
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.
sudo apt-get install -y git gfortran # The following command will add the link of the repository to install the libraries which does not support # Ubuntu 18.04 sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu xenial-security main"
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.
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev
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.
sudo apt-get install -y libtiff5-dev
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.
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
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.
sudo apt-get install -y libxine2-dev libv4l-dev
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.
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
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.
# This might say that libpng12-dev will be removed but it will automatically install libpng-dev which is # supported in Ubuntu 18.04 sudo apt-get install -y qt5-default libgtk2.0-dev libtbb-dev
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.
sudo apt-get install -y libatlas-base-dev
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.
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev
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.
sudo apt-get install -y libvorbis-dev libxvidcore-dev
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.
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev
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.
sudo apt-get install -y x264 v4l-utils
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.
# Some Optional Dependencies sudo apt-get install -y libprotobuf-dev protobuf-compiler sudo apt-get install -y libgoogle-glog-dev libgflags-dev sudo apt-get install -y libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
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.
# Installing Python and Pip # To install Python 2 sudo apt-get install -y python-dev python-pip # To install Python 3 sudo apt-get install -y python3-dev python3-pip # To install both sudo apt-get install -y python-dev python-pip python3-dev python3-pip # Install numpy for python 2 sudo -H pip2 install -U pip numpy # Install numpy for python 3 sudo -H pip3 install -U pip numpy
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.
# Downloads Virtual Environment for python2 sudo pip2 install virtualenv virtualenvwrapper # Downloads Virtual Environment for python3 sudo pip3 install virtualenv virtualenvwrapper
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.
# Run the following commands in the terminal one by one echo "# Virtual Environment Wrapper" >> ~/.bashrc echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc source ~/.bashrc
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:
# Run the following commands in the terminal # For Python 2 # Create a virtual environment opencv_source_2 mkvirtualenv opencv_source_2 -p python2 # Activate the environment workon opencv_source_2 # Install the basic libraries pip install numpy scipy matplotlib scikit-image scikit-learn ipython # For Python 3 # Create a virtual environment opencv_source_3 mkvirtualenv opencv_source_3 -p python3 # Activate the environment workon opencv_source_3 # Install the basic libraries pip install numpy scipy matplotlib scikit-image scikit-learn ipython
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.
cd ~/Desktop/ # Download OpenCV from Github git clone https://github.com/opencv/opencv.git cd opencv git checkout 4.1.0 cd .. # Download OpenCV_contrib from Github git clone https://github.com/opencv/opencv_contrib.git cd opencv_contrib git checkout 4.1.0 cd ..
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.
cd ~/Desktop/opencv mkdir build cd build
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:
- aruco: ArUco Marker Detection
- bgsegm: Tutorials for bgsegm module
- bioinspired: Discovering the human retina and its use for image processing
- cnn_3dobj: CNN for 3D Object Classification and Pose Estimation
- cvv: Interactive Visual Debugging of Computer Vision applications
- dnn_objdetect: Object Detection using CNNs
- face: Tutorial on Facial Landmark Detector API
- 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.
# Make sure that you have activated your virtual environment workon opencv_source_2 # For Python 2 workon opencv_source_3 # For Python 3 cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D WITH_TBB=ON \ -D WITH_V4L=ON \ -D WITH_QT=ON \ -D WITH_OPENGL=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D BUILD_EXAMPLES=ON \ -D OPENCV_GENERATE_PKGCONFIG=YES ..
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.
# Compile the library and install # We will specify the number of jobs that will be used to build the library. # To find the number of threads compatible in your machine run the following command. nproc # The following command specifies the number of jobs with make utility make -j8 # Install the library by running the following commands. sudo make install sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf' sudo ldconfig
Step 9: Modifying opencv4.pc file
It’s time to modify the opencv4.pc file.
cd ~/Desktop/opencv/build/unix-install/ vi opencv4.pc The file will look something like this: prefix=/usr/local exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir_old=${prefix}/include/opencv4/opencv <= You have to modify this line includedir_new=${prefix}/include/opencv4 Name: OpenCV Description: Open Source Computer Vision Library Version: 4.1.0 Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core Libs.private: -ldl -lm -lpthread -lrt -L/usr/lib/x86_64-linux-gnu -lGL -lGLU Cflags: -I${includedir_old} -I${includedir_new}
Replace the specified line with the following line:
includedir_old=${prefix}/include/opencv4/opencv2
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.
# Create a new directory ‘pkgconfig’ in the location /usr/local/lib/ cd /usr/local/lib/ mkdir pkgconfig # Move or Copy the opencv4.pc file to the newly created directory sudo cp ~/Desktop/opencv/build/unix-install/opencv4.pc /usr/local/lib/pkgconfig/
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.
# Open .bashrc file sudo vi ~/.bashrc # Add the following 2 lines at the end of the file i.e. copy the following lines at the end of .bashrc file. PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
Save and Exit the file and run the following command.
source ~/.bashrc # You can verify that the path is added or not by executing the following command echo $PKG_CONFIG_PATH
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.
#include "opencv.hpp" using namespace cv; using namespace std; int main( int argc, char** argv ) { cout << "OpenCV version : " << CV_VERSION << endl; cout << "Major version : " << CV_MAJOR_VERSION << endl; cout << "Minor version : " << CV_MINOR_VERSION << endl; cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl; }
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 .
# C++ Verification cd ~/Desktop/test_code/ #Compile the program to generate the executable file g++ -std=c++11 test.cpp `pkg-config --libs --cflags opencv4` -o result #Execute the program. /.result
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.
import cv2 print "OpenCV version : {0}".format(cv2.__version__) major_ver, minor_ver, subminor_ver = (cv2.__version__).split('.') print "Major version : {0}".format(major_ver) print "Minor version : {0}".format(minor_ver) print "Subminor version : {0}".format(subminor_ver)
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 .
# Python2 Verification cd ~/Desktop/test_code/ # Switch to your virtual environment workon opencv_source_2 # For Python 2 # For Python 2 python 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.
import cv2 print("OpenCV version : {0}".format(cv2.__version__)) major_ver, minor_ver, subminor_ver = (cv2.__version__).split('.') print("Major version : {0}".format(major_ver)) print("Minor version : {0}".format(minor_ver)) print("Subminor version : {0}".format(subminor_ver))
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 .
# Python 3 Verification cd ~/Desktop/test_code/ # Switch to your virtual environment workon opencv_source_3 # For Python 3 # For Python 3 python test_3.py