OpenCV & ML (Deep Learning) 01 – Building library from Source

Building OpenCV 3.3 library via source codes cloned from github.com. The library is build with contribute modules of OpenCV. CMake is used for configuring the build options. The default install target is modified in order to provide the possibility of  using different build configurations in one system. The CUDA and OpenNI modules are skipped among build options for this tutorials and postponed for latter ones.

With the release of OpenCV 3.3 there have been a lot of improvements in machine learning section of OpenCV especially those related to deep learning modules. Now dnn is part of main repository rather than contribute one. Since, I have n’t found any good tutorial describing examples in detail, I have decided to post my own tutorial. The tutorials is mostly based on practical and simple examples instead of mathematical and statistical concepts. I believe this is the best way of practicing machine learning with OpenCV. This tutorial series will also address new features brought to OpenCV by DNN. The entire series are tested and performed in a Debian based Linux. However, If you succeed installing library in your operating system, you should be able to run the provided examples in your systems as well. I will try to start from the scratch but a basic knowledge of C++ programming and Linux environment would help alot. If you are not good at those, I recommend you, keep reading the tutorial and whenever you face a problem you could Google forums like StackOverflow for extra information.

To be able to program OpenCV with its native language (C++) in your Linux you have two options:

  1. (The easy way) Install OpenCV library built available from Debian official repositories.
  2. (The good but harder way) Build your own OpenCV directly from the source code.

Apparently, we will use the second option but before justifying why I pick the harder one, let’s summarize the easy way in a few lines. You could install OpenCV library using the following commands in Linux:

terminal

  • sudo apt-get update
  • sudo apt-get install libopencv-dev

Please don’t do this and if you have already installed it via the commend, I recommend you to remove it by the following command, so that we could make sure that our program will be built with linking to new OpenCV objects, otherwise it is probable that you may face incompatible linking problems.

terminal

  • sudo apt-get remove –purge libopencv-dev

Reasons to build the OpenCV from source:

  • The first reason, is that the available version of OpenCV, even from unstable repo is far behind the recent version. As posting this tutorial it is 2.4.9. You could check it from this link and we need to use the latest version for the all cool stuff available there for deep learning and … .
  • The second reason, is that we need some other modules from contribute repo which is not available thru developmental package in the official repository.
  • The third reason, is that if you want to enable especial optimizations such as GPU for modules like deep learning you need to build your own shared objects
  • The forth reason, I mentioned shared objects what if you need to build your software or program out of static objects, again you need to compile the library by yourself. And there are some other reasons but I think for now we are all satisfied to build the library ourselves.

Let’s back to our main subject. Before starting to build process make sure you have the following packages installed:

terminal

  • sudo apt-get update
  • sudo apt-get install build-essential git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

You could use the following commands to clone the required sources to your local disk

terminal

  • mkdir opencv
  • git clone http://github.com/opencv/opencv
  • git clone http://github.com/opencv/opencv_contrib

We are going to use CMake to configure OpenCV Makefile. I prefer using GUI CMake to make the overall process easier. Install the CMake GUI in your systems as well. We will configure the CMake for the generating OpenCV Makefiles.

terminal

  • sudo apt-get update
  • sudo apt-get install cmake cmake-qt-gui

Open the CMake GUI and edit “Where is the source code:” field to point the directory of OpenCV source you have cloned from internet. You need to create another empty directory where ever you want in your hard disk and edit “Where to build the binaries” field to point to the new directory you have made.


Now hit the configure button which will brigs up “CMake Setup Dialog”, we will use this dialog chose our preferred compiler and Makefile formats. The default settings are the one we are looking for: Native compiler with Unix Makefiles.

As soon as you accept the setup dialog the configuration process will start. Attention: you need to be connected to the internet while CMake configures the OpenCV Makefile. Some packages like IPPICV: ippicv_2017u3_lnx_intel64_general_20170822.tgz should be downloaded from internet in the configuration process and they are not included by default in the OpenCV source package.

Now you will notice that all the rows in the Name-Value table of the CMake is colored with red background. we will apply our configuration to the value fields.

you could use the search box to filter the names and find the fields faster.

Disable WITH_CUDA by unchecking the checkbox value. This option is for building optimized OpenCV using GPU. We are not interested in this option for now since we want to make the overall process simple and straight, so for now we disabled the option and later we will back to build another version with CUDA support.
Next we enable these options by marking the relating checkboxes.

  • ENABLE_FAST_MATCH
  • OPENCV_ENABLE_NONFREE

The non-free module package contains a lot of cool stuff like SIFT feature descriptors. So, it is better for us to have it.

I recommend you to enable the following options as well but they are not mandatory.

  • BUILD_EXAMPLES
  • INSTALL_C_EXAMPLES
  • INSTALL_PYTHON_EXAMPLES
  • WITH_QT
  • WITH_OPENGL

Be alarmed you need to have the package qt-default installed before enabling WITH_QT. Using Qt will give you the opportunity of interacting with fairly reach window interface with some basic tools like PAN, ZOOM, Mouse Coordinate and …. in the window toolbar.

The most important configurations you should apply are the followings:

  • OPENCV_EXTRA_MODULES_PATH to “../../opencv_contrib/modules”

Since we git opencv and opencv_contrib to the same location (both are inside opencv folder) the hierarchy of folders now should look like the following figure

We use the conrtib modules location (*) as the value for extra modules path and this path should be relative path from OpenCV main modules folder (#). that is why it looks two parent folder ahead before enetering opencv_contrib

  • CMAKE_INSTALL_PREFIX “/usr/local/opencv3.3-cpu”

We change the default install directory since you may be interested to build other versions with different configurations in the future like OpenCV with CUDA support or with OpenNI. In those cases if we install them in the same folder (/usr/local/lib) our built objects would be overwritten and in the best case scenario they will interfere with each other while linking. But if you are not interested in building other configuration installing in the default location (/usr/local/lib) will make your life easier making your program binaries. Otherwise, we have to mention the library location specific while using make with -L switch (we will see on the examples ahead). And finally change the CMAKE_BUILD_TYPE value to Release since we will use release objects.

  • CMAKE_BUILD_TYPE Release

summery of changes we have made so far:

  • CMAKE_INSTALL_PREFIX => “/usr/local/opencv3.3-cpu”
  • OPENCV_EXTRA_MODULES_PATH => “../../opencv_contrib/modules”
  • CMAKE_BUILD_TYPE => Release
  • ENABLE_FAST_MATCH (check)
  • OPENCV_ENABLE_NONFREE (check)
  • BUILD_EXAMPLES (check)
  • INSTALL_C_EXAMPLES (check)
  • INSTALL_PYTHON_EXAMPLES (check)
  • WITH_QT (check)
  • WITH_OPENGL (check)
  • WITH_CUDA (uncheck)

Now that we have done with the configuration we could hit the generate button. Attention to the logs window and check them to make sure that generate process does not encounters any error. The errors and warnings will be demonstrated by red coloured font. Warnings are not subject of concern and you could overlook them. Errors means you are missing some packages for the generation of Makefile and the build process is likely to fail. You may check the Name-Value table to see if any rows still is in red background colour, that row might be the source of problem. Sometimes hitting the generate button for two or three times would solve the problem and you don’t need to apply any special configuration or install a package. But if the problem still persist you could solve the problem by uncheckning or changing the value of that coloured row. Then, hit the generate button again until you have no red rows left. If the errors do not get solved Google the errors in the StackOverflow forum and again if you couldn’t find the problem leave me a comment with a description of the error you got and I’ll try to assist you.

No we could enter to our build directory and run the following command:

terminal

  • make -j8

This will run the make to build the binaries in 8 parallel tasks if your CPU has 8 cores, this will save you a lot of time. If it fails for example after 19% built it ‘s most probable because of dependency of objects on each other and thus, there is nothing wrong with your configuration. You may try it with less parallel tasks like 4 and when you pass the percentage where the error took place (19%) you could cancel the build with Ctrl+C shortcut and re-execute the make -j8. The make process will continue to built the objects that are not exist and it would not start the process from the scratch (unless performing a “make clean” command). If you have time try pure “make” with peace.

Now when you reach the end of the make process with the 100% progress and get the “Build Successful” message, we could continue to the next tutorial to write our first program. If you are still doubted about the built process just perform another make command and it will check for the objects if they are built correctly with much more clear log on the terminal interface showing you the progress reached to the 100%.

Before, jumping to next tutorial I would like to mention about the available documentation. You can reach the OpenCV online documents from the following links:

There is also another tutorial (the official one) published by OpenCV on installing the library on a Linux. And you could use this one for installing on Windows. You could easily figure out the changes you need to apply on this guides to build OpenCV for windows. If you want to have the official OpenCV document available offline you could enter the doc folder in the build directory and execute the following commands:

terminal

  • cd doc
  • make doxygen

You don’t need the switch -j4 since it won’t take much time. After the build has done, you could access the html document with in the folder “./doc/doxygen/html/index.html” relative to your build directory.

Cite this article as: Amir Mehrafsa, "OpenCV & ML (Deep Learning) 01 – Building library from Source," in MEXUAZ, September 9, 2017, http://mexuaz.com/opencv-machine-learning-01/.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spelling error report

The following text will be sent to our editors: