Installing OpenCV with Contrib Library for Linux

This post is for you, if you don’t know how to install a library from it’s source

As I mentioned in this blog post I’m about to explain how to install OpenCV library with contrib library from it’s source. It’s also a way to install all libraries from newbies. For fresh installed Linux distro like Ubuntu you need to gear up your computer first

Firstly, let’s update our computer for being sure it’s up to date

sudo apt update -y
sudo apt upgrade -y
sudo apt dist-upgrade -y

-y flag to install automatically our steps, without -y mark it will save this package is about that size do you want to install with yes and no options.

sudo apt install build-essential cmake cmake-qt-gui -y

build-essential is a metapackage (a package that installs many other packages, like g++ and gcc: the GNU C & C++ compilers). It’s required if you want to compile anything from source, and if you want to work with almost any programming language.

https://superuser.com/questions/151557/what-are-build-essential-build-dep

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.

https://cmake.org/

cmake-qt-gui is a gui made with QT framework to customize our build settings with a graphical user interface.

I like to use a lot of options of OpenCV library to get full power like VTK, contrib, highgui support with QT and etc. Here is a set of command lines that will be install some libraries from remote repository by using apt again.

sudo apt install zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libopenexr-dev libgdal-dev -y &&
sudo apt install libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev -y && sudo apt install libgtk2.0-0 libgtk2.0-dev libjpeg8 libjpeg8-dev yasm -y &&
sudo apt install libatlas-base-dev -y &&
sudo apt install libatlas-cpp-0.6-dev -y &&
sudo apt install libavdevice-dev libavresample-dev libass-dev libfdk-aac-dev libfreetype6-dev libmp3lame-dev libopus-dev libtheora-dev libvorbis-dev libvpx-dev libx264-dev libx265-dev -y  &&
sudo apt install x264 x265 v4l-utils -y &&
sudo apt install libprotobuf-dev protobuf-compiler -y &&
sudo apt install mesa-utils -y &&
sudo apt install freeglut3-dev -y &&
sudo apt install libgoogle-glog-dev libgflags-dev -y &&
sudo apt install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen -y &&
sudo apt install libtbb-dev libeigen3-dev -y &&
sudo apt install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler -y &&
sudo apt install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy -y &&
sudo apt install libgflags-dev libgoogle-glog-dev liblmdb-dev -y &&
sudo apt install --no-install-recommends libboost-all-dev -y &&
sudo apt install libgstreamer1.0-dev -y &&
sudo apt install git gfortran qt5-default -y &&
sudo apt install libvtk7-dev -y

We are ready to install OpenCV. You can download libraries source file from it’s own website which also downloads source zip file from it’s own GitHub page.

Time to time I need to download from GitHub to install different versions of library. Right now the most recent version of the library is 4.1.2. To check library’s releases here is the link. Another link for contrib library’s releases. Let’s download tar.gz files of 4.1.2 version for OpenCV. If you love your terminal, you can get and unarchieve by typing:

wget -O opencv.tar.gz https://github.com/opencv/opencv/archive/4.1.2.tar.gz
wget -O opencv_contrib.tar.gz https://github.com/opencv/opencv_contrib/archive/4.1.2.tar.gz
tar -xvf opencv.tar.gz
tar -xvf opencv_contrib.tar.gz
cd opencv-4.1.2/
mkdir build

If you’re not terminal friendly, it can be simply downloaded from your web browser and unzip from file explorer. Please create a build directory under your OpenCV root folder. I’m suggesting this to store make files under this folder. Time to time you can be in the need of different build configurations. It makes it easy to store different configurations.

Anyway, time to kick CMake with cmake-gui from our terminal to create CMake configuration.

cmake-gui
  • Set OpenCV's root path and your build path then click "configure" button
  • Run First configuration with default settings
  • Type extra in your search bar to set contrib library's path
  • click "..." to set path
  • set your path under modules directory of your contrib library
  • press configure button to reconfigure last settings. It will download some files and display contrib's settings too
  • press configure for a second time to get rid of red lines
  • after you got rid of red lines, now click generate to get your make file
  • generation is down open up your terminal and go to your build path
  • after you get your make file under build directory call make with number of threads that your cpu can support
  • grab a cup of tea or coffee, it takes some time to install whole library 🙂

After getting %100 of your make installation now you got to install it under your file system with one line of command.

sudo make install

Congratulations!!! You managed to install your OpenCV with Contrib library. If you want to check, it’s installed under /usr/local/lib and /usr/local/include

cd /usr/local/lib
ls -l

You may also like...

Leave a Reply

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