Anyone who has dealt with image processing in relation to the Raspberry Pi will sooner or later come across the OpenCV library. It provides many very useful features such as face recognition, the creation of depth maps (stereo vision, optical flow), text recognition or even for machine learning. In addition, OpenCV (Open Source Computer Vision) can be integrated into both its C ++ files and its Python scripts.
Especially in terms of feature recognition in images taken by the Raspberry Pi, OpenCV is very helpful.
This advanced tutorial shows how to install OpenCV on the Raspberry Pi and how to integrate it into Python.
For the time being, I would like to recommend everyone a Raspberry Pi Model B to use, because this is a lot more powerful than its predecessor. Even on a Raspberry Pi B + compiling takes about 6 times as long, with a Pi 2 in total “only” takes about an hour.
First of all, we update the package lists:
sudo apt-get update && sudo apt-get upgrade && sudo rpi-update
A reboot is necessary if it has been updated.
Then you can install all the important tools and libraries needed for OpenCV (installation takes a few minutes).
sudo apt-get install build-essential git cmake pkg-config libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk2.0-dev libatlas-base-dev gfortran
If everything worked, we could clone OpenCV from git. This step also takes a few minutes.
git clone https://github.com/Itseez/opencv.git && cd opencv &&git checkout 3.0.0
Whether version 3.0 or 2.4 of OpenCV is taken is up to you. Depending on the application, one of the versions may be better suited.
Afterwards, OpenCV can be compiled. You can either use Python 2.7 or Python 3+. There are some differences between the versions, especially as some libraries are not (yet) executable with Python 3+. However, this mainly affects smaller libraries, as common libraries (NumPy, SciPy, etc.) usually provide the respective files for both versions.
In this tutorial, I use Python 2.7. If you already have Python installed and want to know which version is installed, you can simply enter
python into the console and get the exact version at the beginning (the command for Python 3+ is
python3). If you do not have a Python installed, you can install it by following the steps below:
sudo apt-get install python2.7-dev
We also need the package management tool pip, which installs NumPy right away:
cd ~ && wget https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py
Now we can simply install via pip NumPy. NumPy is a library that makes it very easy to perform array operations in Python.
pip install numpy
But now to compile OpenCV. For this purpose, a build folder must be created in which the compiled files land:
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=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..
Now you can finally compile. This step takes (depending on Raspberry Pi model) quite a long time (on my Pi 2 about an hour). To use all four cores to compile on the Raspberry Pi 2, type in the following:
If the compilation has worked without problems, we can install OpenCV:
sudo make install && sudo ldconfig
To check if everything worked, you can open the Python console and import the library:
Now you can use all OpenCV functions in your projects. A look at the OpenCV documentation is certainly helpful.