Installing OpenCV 3.3.0 on Ubuntu 16.04 LTS

  1. Get Ubuntu update.

First of starting this we need to make sure and update Ubuntu before doing any thing else, which is:

$ sudo apt-get update && sudo apt-get upgrade && sudo apt-get autoremove

2. Install all the recommended packages.

Compilers

$ sudo apt-get install build-essential

Required:

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

And recommended optional package:

$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install python2.7-dev python3.5-dev

3. Setup you enviroment

We are going to install pip and virtual environment manager packages in python:

$ sudo apt-get install python-pip && pip install --upgrade pip

We install virtualenv and virtualenwrapper :

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

Then wee need to add some line to .bashr file:

$ cd ~
$ nano .bashrc

add the following lines to the bottom of the content of the file:

...
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Then save the file, go back to terminal. Run this line:

$ source ~/.bashrc

This line is to activate/apply what we have done to the .bashrcfile, which finalizes the installation of virtualenv and virtualenvwrapper.

Next, we need to create a virtual enviroment for OpenCV, that we are going to call it cv:

$ mkvirtualenv cv -p python3

If you restart or accidentally close the terminal, type the command below:

$ workon cv

the last package to install is numpy.

$ pip install numpy

4. Download OpenCV source

We need to clone the latest release of OpenCV.

$ cd ~
$ git clone https://github.com/opencv/opencv/
$ git clone https://github.com/opencv/opencv_contrib

5. Configuring and compiling

We need to build using cmake:

$ cd ~/opencv/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D INSTALL_C_EXAMPLES=OFF \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
      -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
      -D BUILD_EXAMPLES=ON ..

Then we need to compile OpenCV while you are in the build folder:

$ make -j4
  • where jx where x are the number of process you want to dedicate to this operation.

Finally we need to install openCV:

$ sudo make install
$ sudo ldconfig

6. Finishing OpenCV installation

There are some work remaining so everything is done. Lets look at the ouput of our work so far:

$ cd /usr/local/lib/python3.5/site-packages/
$ ls -l

At the output you will see a file that start with the name of cv2.cpython. We need to move it back to site-packages and rename it to cv2.so:

$ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

To have OpenCV working in our virtual environment, we create a sym-link of the file to our site-package folder in the virtual environment.

$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

Finally we need to download

$ pip install imutils

Last updated