OpenCV 3.0 & Python – Installation on Raspberry (Raspbian)



If you come from the world C ++ and you had to deal with the analysis of the images, you certainly know the OpenCV library. Recently, due to the increasing use of the Python programming language, many libraries, which were originally developed in C / C ++,  can also be used with this language through the Python Bindings. The OpenCV library is one of them, and its last versions (3.1) which has been released the last year, can be used within Python code.

In this article you will see how to compile the source code and install lOpenCV 3.0 of Raspbian (Raspberry). So this article is an introduction to a series of articles that will cover in depth the topic. If you want to work with OpenCV, you must first know how to compile and install on your system: in this case Raspbian.

The OpenCV library and Python

OpenCV  is a library written in C ++, specialized for Computer Vision and Image Analysis. This powerful library, designed by Gary Bradsky, was born as Intel project was released the first time in 2000. Then with the passage of time, it was released under an open source license, and since then has gradually becoming more widespread, reaching the version 3.1. At this time, OpenCV supports many algorithms related to Computer Vision and Machine Learning and is expanding day by day.

Its usefulness and spread is due precisely to his antagonist: Matlab. In fact, for those who need to work with the Image Analysis can follow only two ways: purchase Matlab packages or compile and install the open source version of OpenCV. Well, it’s easy to see why many have opted for the second choice.


Installing OpenCV on Raspberry

I was looking for a lot of tutorials on the network and I must say there are many. But after 5 or 6 attempts, went wrong to be honest, I have finally found an interesting article written by Adrian Rosebrock that concerns the process of compiling and installation of OpenCV 3.0 of Raspberry. I followed in detail and I have not had any problems (except for make -j4 that you will see later).

Prerequisites for installing

First you need to upgrade any existing package of Raspbian, then update the package manager apt-get. Moreover, it would be appropriate to update the firmware Raspberry and you can do this with rpi-update.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo rpi-update

Now that you’ve updated virtually any system, run the reboot.

$ sudo reboot

Once Raspbian has started, you will need to install all of the developer tools, in case these are not present on the system.

$ sudo apt-get install build-essential cmake git pkg-config

OpenCV is a graphics library that works only on images, many of which are in compressed format. It will therefore be necessary to install the libraries that allow you to work on this kind of formats like JPEG, PNG and TIFF. Then install the following packages.

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

OpenCV but also works on video and then even these are often in compressed formats. In the same way you did with the pictures, you will need to import the specific packages for video stream.

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev 
$ sudo apt-get libv4l-dev libxvidcore-dev libx264-dev

Finally, now that you have the ability to manage many video formats and image, install the GTK development library. This library will allow you to work with highgui, an OpenCV module, with which you can create graphical user interfaces (GUI).

$ sudo apt-get install libgtk2.0-dev

In addition, there are some additional packages that help OpenCV perform matrix calculations in an optimized way.

$ sudo apt-get install libatlas-base-dev gfortran

Finally install the Python development modules, in order to use the header files during compilation of OpenCV Python Bindings.

$ sudo apt-get install python2.7-dev python3-dev

The source code for OpenCV

Now that all prerequisites for the installation are completed, it is time to download the 3.0 version of the OpenCV source code. Will you do download the source code directly from the OpenCV repository. If you want to install versions after 3.0, simply replace the 3.0.0 version you choose.

In addition also you will install the source code of some extra modules that are not officially incorporated into OpenCV but still provide many useful functions. The package is opencv_contrib. It must be installed on the version corresponding to OpenCV, so if you used a different version 3.0.0, also replace the version number here.

$ cd ~
$ wget -O
$ unzip
$ wget -O
$ unzip

Downloading the source code should not take more than a few minutes of time.

Python setup

Before you start compiling OpenCV, do a number of useful steps to set the system in the proper use of Python packages.

All Python packages are typically installed and operated by the command pip. If for example you have not installed on your Raspbian, you can do it with the following commands:

$ wget
$ sudo python

Another interesting aspect for those who work in Python is the use of virtual environments (virtualenv). The installation and activation of virtualenv is not mandatory, but is highly recommended for anyone who wants to work with many applications in Python that require the installation of several packages. Often you want to work with both Python 2.7 to Python 3.x.

So if you want to work with OpenCV, and even more if you want to compile it from source code, apply many changes to the system, and also many modules in Python will be installed, updated, and so on. The dependencies between them is often linked to specific versions of each of them and then often likely to create conflicts. Python applications already installed may not work properly, same thing for OpenCV Python if new application will be installed later.

So it seems appropriate to install virtualenv and virtualenvwrrapper on their Raspbian. These two packages allow you to create separate Python environments for each project you’re working on.

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

In order to make the installation of these two packages, you will need to update the ~/.bashrc file and add the following lines to the end

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

This fast update will ensure that you load both virtualenv virtualenvwrapper that every time you log in.

Now that you have updated the file, you must load all the changes made so that they are loaded by the system. To reload, run the following command.

$ source ~/.profile

It is likely that each time you restart your system, and you open a terminal session, you will need to re-launch this command, to keep updated the session.

Now create the new virtual environment that you will use exclusively for OpenCV.

$ mkvirtualenv cv

Writing this command, you’ll create a virtual environment in which Python2.7 will be active. If I wanted to create a virtual environment where you Python3.x to be active, you must write:

$ mkvirtualenv cv -p python3

Launching the new virtual environment, you will notice next to prompt the name of the active virtualenv enclosed by brackets

(cv) $

In the future, any time that you reboot the system, you can reactivate this virtualenv, typing

$ source ~/.profile
$ workon cv

Now that you’ve turned on the virtual environment, it is time to install the first library in Python, called NumPy. This library is perhaps one of the most important libraries for scientific and engineering calculations and contains inside many computing tools. Many libraries and scientific analysis are based on NumPy and OpenCV is one of them.


The installation of this library of Raspbian requires quite a bit of time. Expect a time between 15 and 20 minutes. So do not be alarmed and wait for the installation to finish.

(cv)$ pip install numpy

Compiling and installing OpenCV

Now that you have completed the prerequisites is that the requirements, you can finally deal with the compilation and installation real OpenCV.

I remember that we are still within the virtualenv cv.

(cv) $ cd ~/opencv-3.0.0/
(cv) $ mkdir build
(cv) $ cd build
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \

Now you can compile OpenCV, using all four of your compiler core.

(cv)$ make -j4

The compilation, even with all four cores active, takes more than an hour and a half. Check whether the installation is successful.

For me, twice the installation gave me strange errors. Thing that I solved using a single core. So if you find yourself in the same my case, you can restart the compilation using the following two commands:

(cv) $ make clean
(cv) $ make

After compilation, you can finally begin to install.

(cv)$ sudo make install
(cv)$ sudo ldconfig

If you have chosen Python 2.7, OpenCV should be installed on /usr/local/lib/python2.7/site-packages and since you’re working virtual environment cv located in the home directory, you would do the sym-link

(cv)$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
(cv)$ ln -s /usr/local/lib/python2.7/site-packages/

If you chose Python 3.x then OpenCV should be present on of /usr/local/lib/python3.x/site-packages. Again you’re working virtual environment cv located in the home directory, and then you will have to do the sym-link

(cv)$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
(cv)$ ln -s /usr/local/lib/python3.x/site-packages/

Installation is complete.

Verification Test

Now do not you have left to check if the compilation and installation went correctly.

Close the terminal session and open an existing a new one. Try if it works workon the command to activate the virtual environment.

$ workon cv

Now the only thing that’s left to do is check if the compilation and installation went correctly.

Close the terminal session and open an existing a new one. Try if it works workon the command to activate the virtual environment.

$ python
import cv2


In this article you’ve seen how to compile and install the library OpenCV graphics for Python through a series of instructions. In future articles you will see in detail how this library, to understand its use and test its functionality based on a series of practical examples. See you soon then…[:]

Leave a Reply

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