OpenCV is an awesome Python module for playing with images or videos. It has got a lot of features for image processing such as reading, writing and capturing image from a camera. It has functions for almost all the image processing algorithms. Here we will be installing OpenCV for Python 2.7 on Raspberry Pi 3 B+
First of all, we update the package lists:
#Step-1 sudo apt-get update sudo apt-get upgrade sudo rpi-update
A reboot is necessary if it has been updated.
#Step-2 sudo reboot
Then you can install all the important tools and libraries needed for OpenCV (installation takes a few minutes).
#Step-3 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.
#Step-4 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:
#Step-5 sudo apt-get install python2.7-dev
We also need the package management tool pip, which installs NumPy right away:
#Step-6 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.
#Step-7 pip install numpy
But now to compile OpenCV. For this purpose, a build folder must be created in which the compiled files land:
#Step-8 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:
#Step-9 make -j4
If the compilation has worked without problems, we can install OpenCV:
#Step-10 sudo make install && sudo ldconfig
Want to convey anything? Drop a Message.