How To Install OpenCV On Raspberry Pi?

We need to perform different operations on an image to extract useful information from it. So, this process of applying different algorithms on an image to obtain the desired output is called Image Processing. Sometimes, the image at the input is blurred and we want to acquire data from it. For example. When the robbers come to snatch the bike or car they mostly come on the bike and overhead cameras are installed on the roads which capture the footage of the incident. We need to know the registration number of that vehicle on which the robbers come and it can be done easily using some algorithms of image processing. To perform image processing on certain images, we need to install some libraries on the hardware that we are using. Among those libraries the most important is OpenCV. OpenCV can be installed on PCs and microprocessors as well. Raspberry Pi is a microprocessor and it is used in various electronic projects. After installing the Operating System on Raspberry Pi we can perform various image processing tasks on it. Installing the OpenCV on Raspberry Pi is a lengthy and hectic task. In this article, we will learn how to install OpenCV on Raspberry Pi for performing different image processing operations on it.

Face Detection With OpenCV Installed On Raspberry Pi

How To Setup Raspberry Pi And Configure OpenCV On It?

Now, let’s move towards setting up Pi and doing operations mentioned as under step by step to install OpenCV on it. Installing OpenCV on Pi is a lengthy process and it takes around 4 hours to complete so if you have a shortage of time don’t start the installation, refer to this tutorial when you are free. Along with this Pi is heated up when it has been turned ON for a long time and time taking operations are performed on it so, keep it in a cool place when you are working on it.

Step 1: Components Used

Step 2: Selecting the Raspberry Pi Model

Several models of raspberry pi are available in the market. Except for raspberry pi zero, any model can be preferred. This is because on Pi zero setting up a network is a very tiring job. The latest models like 3A+, 3B+ or 4 can be purchased. The new Raspberry Pi 3 is the quickest and most dominant gadget the Raspberry Pi Foundation has released to date. So, in this project, we will use the Raspberry Pi 3B+.

Raspberry Pi 3B+

Step 3: Connecting The Peripherals

After choosing Raspberry Pi we will connect keyboard and mouse to the Raspberry Pi. After connecting them use the HDMI cable to connect Pi with the Television. After making these connections we are ready to proceed further.

Step 4: Choosing The Operating System

Firstly, we will need an SD card with an appropriate operating system. When picking the OS, nowadays there are various alternatives, from “conventional” Raspbian to devoted media working frameworks, and even Windows 10 IoT. There is no need for lots of applications hence, we should leave the Central Processing Unit (CPU) and Random Access Memory (RAM) as much as we can for the media streaming application. One problem is that Arch Linux is recommended for people who have quite a lot of Linux knowledge. They’re very front line and we’re bound to keep running into issues when introducing third-party applications and libraries. Thus, if this is your first establishment of a home theater, we suggest picking Raspbian Lite. It’s command-line driven, and can without much of a stretch designed to keep running in “headless” mode, i.e. accessed entirely remotely over the system with no requirement for a console or screen.

Raspbian Lite

Step 5: Make Sure That Raspberry Pi Is Up To Date

Keep the sources of your Pi up to date otherwise, the outdated software will cause some problems. Enable the Virtual Network Computing (VNC) viewer on your Pi, then connect your Raspberry Pi with the VNC viewer. The link is provided below for downloading VNC and then connecting it with Pi.

VNC Viewer

Now, open the terminal and run the following command :

sudo apt-get update

Then,

sudo apt-get upgrade

Numerous packages will be installed and if asked press Y and then Enter to install them properly.

Step 6: Login To Raspberry Pi

The default username of the Raspberry Pi is pi, and the default password is raspberry. These are the default login details and on your first login use these details to log in to pi. You can change these details too whenever you want.

Login To Raspberry Pi

Step 7: Creating Enough Space On Raspbian For OpenCV

OpenCV acquires large memory so we need to expand the file system and allocate all of the space to the memory card. We will go to the command prompt of raspberry and type the following command:

sudo raspi-config

A window will appear and it will look like this:

Configuration Tool

Now, we will click on Advanced options and there we will find an option “Expand Filesystem”. Select that option.

Expand Filesystem

We will press the Enter button and then hit the Finish button. At this stage, our Raspberry Pi needs to be rebooted for the changes to take effect. Type the following command to reboot it:

sudo reboot

After rebooting we will check whether our file system has expanded and all of the space is included in the SD card or not. By executing df -h command we can verify that our disk has expanded:

The one who is using an 8GB micro SD card may be using 50% of the available space, so deleting Wolfram Engine and LibreOffice can free up around 1GB of space. (Remember that this step is optional).

sudo apt-get purge wolfram-engine

sudo apt-get purge libreoffice*

sudo apt-get clean

sudo apt-get autoremove

Step 8: Installing Dependencies

Before going for and dependancies we need to update and upgrade the existing packages that are installed on Pi:

sudo apt-get update

Then,

sudo apt-get upgrade

Now, we will install some developing tools which will help us in configuring OpenCV build:

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

To perform the different operations on images we need to load the several image formats from the hard drive. Those formats include JPEG, PNG, etc. For loading these image formats we will install some I/O packages:

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

Along with these images I/O packages we will also install video I/O packages. After installing these video packages we will be able to load various video file formats.

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

sudo apt-get install libxvidcore-dev libx264-dev

The OpenCV library accompanies a sub-module named highgui which is utilized to show pictures to our screen and manufacture essential GUIs. Before compiling that sub-module we need to install the GTK development library:

sudo apt-get install libgtk2.0-dev libgtk-3-dev

Several matrix operations can be performed on an image by checking the size of the image and then reading pixel values. We can also convert those pixel values into binary form and then modify that binary digits to regenerate an image. In raspberry pi, we have some limitations when providing input hence these libraries are important and need to be installed. Therefore, those results can be made better by installing some extra dependencies:

sudo apt-get install libatlas-base-dev gfortran

Some people will be working on Python 2.7 and some will be working on Python 3. The header files of Python 2.7 and Python 3 need to be installed for compiling OpenCV along with Python bindings:

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

In the new version of Raspbian Python 3 is already installed and a msg may appear at the Lx Terminal stating that “Python 3 is already the latest version”. This step is important because we can face error regarding the header file named as Python.h while running the command make to compile OpenCV.

Step 9: Downloading OpenCV Source Code

As we are finished with installing the dependencies we will look for the archive folder of OpenCV version 3.3.0 from the official directory of OpenCV.

cd ~

wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip

unzip opencv.zip

We are installing the whole package of OpenCV so we need to include opencv_contrib as well. Download it from the official site and then unzip it.

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip

unzip opencv_contrib.zip

While downloading these directories keep one thing in mind that the version of OpenCV and opencv_contrib should be the same i.e they should be 3.3.0 otherwise there will be compiling errors during installation.

Step 10: Python 2.7 or Python 3?

Performance-wise python 2.7 is better than python 3 but in OpenCV, there is not much difference. We need to install pip on Raspberry before compiling OpenCV. It is a package management system that is used to install the software packages that are used in Python. These packages may be present in the latest raspbian by default but it is better to verify it by using the following commands.

wget https://bootstrap.pypa.io/get-pip.py

sudo python get-pip.py

sudo python3 get-pip.py

After installing pip two packages are highly recommended and need to be installed while working on OpenCV. The first one is virtualenv and the second one virtualenvwrapper. We can’t import OpenCV directly in Python so we will create a virtual environment and then work in that environment. A virtual environment is an exceptional tool that is used to keep the conditions required by various projects in discrete places by creating separate Python environments for every one of them.

sudo pip install virtualenv virtualenvwrapper

sudo rm -rf ~/.cache/pip

After installing these packages we need to update our ~/.profile file which is the hidden file in our home directory to include the following lines at the end of it. Type the following command to enter the directory:

nano ~/.profile

When the directory is opened scroll down and includes the following lines:

# virtualenv and virtualenvwrapper

export WORKON_HOME=$HOME/.virtualenvs

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

source /usr/local/bin/virtualenvwrapper.sh

After including these lines press ctrl+x, save it by pressing Y and exit.

Home Directory

Anytime we launch the terminal and we log into our Pi this dot file will be automatically loaded for us. As we are already logged in we would manually type source ~/.profile to load the contents of the file.

Creating Python Virtual Environment: We need to create a virtual environment using python 2.7 and python 3.

mkvirtualenv cv -p python2

It will create an environment named cv on Python 2.7. Anyone who wants to create an environment on Python 3 should type the command mentioned below:

mkvirtualenv cv -p python3

Verifying That We Are In The Virtual Environment Named As “cv”: When we reboot the pi we will not remain in a virtual environment and we need to type in two commands mentioned below to go into virtual environment mode.

source ~/.profile

workon cv

The picture below indicates that we are not in the virtual environment mode:

LxTerminal

So, by typing the two commands mentioned above we will be able to access our virtual environment. If we want to leave the virtual environment we will type deactivate:

Working In Virtual Environment

Installing NumPy On Raspbian: The only dependency that we need to install OpenCV on Raspberry is Numpy. Type the command mentioned below to install Numpy on Raspberry Pi. It will approximately 10 minutes to install:

pip install numpy

Step 11: Compiling And Installing OpenCV

We will compile and install OpenCV in the Virtual Environment so make sure that you are working into the CV virtual environment. If we are not in the virtual environment the OpenCV will fail to compile. Now, change the directory to home directory, sub-directory open cv 3.3 and then make the build directory. After making the build directory paste the last five lines in the CMake directory. It will check for certain libraries set paths, python versions, etc.

cd ~/opencv-3.3.0/

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \

-D CMAKE_INSTALL_PREFIX=/usr/local \

-D INSTALL_PYTHON_EXAMPLES=ON \

-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \

-D BUILD_EXAMPLES=ON ..

For those who are using Python 2.7, they will have to scroll to CMake output and will look for Python 2.7 section and see whether Numpy and paths of the packages are configured properly. For those who are using Python 3 will check the python 3 section right beneath Python 2 section:

Checking Python 2.7 Section

Now, we are finally ready to compile OpenCV. Type make command and it will start the compile process. It will take approximately four hours to compile hence, it is preferred to start compilation before you sleep at night so that when you wake up in the morning the OpenCV is compiled. Typing one command “make” will compile using only one core. Although it a bit time taking process but it has less probability of errors. Using the command of make -j4 and make -j2 could result in overheating of the Raspberry Pi and it could also result in compilation errors:

make
Compilation Complete

We will install OpenCV 3 on Raspberry Pi by using the following command. Running this command will copy the respective files into their locations:

sudo make install

Our installation will be completed by running this final command:

sudo ldconfig

A couple of steps are left now when we are using Python 2.7 or Python 3.

Step 12: Finishing The Installation

Return to the home directory by typing cd~.

Python 3: We will sym-link the OpenCV bindings into our cv in python 3 directory because we compiled OpenCV and python bindings for python 3.

cd ~/.virtualenvs/cv/lib/python3.5/site-packages/

ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

That’s it!. Now, we have installed OpenCV onto Raspberry Pi. We will now check for it in the virtual environment.

Step 13: Testing OpenCV

Open the LxTerminal and write the source command followed by the workon command. As we have entered the virtual environment mode we will import OpenCV bindings by typing python and then import cv2. If there is no error msg it means that it has been imported successfully.

source ~/.profile

workon cv

python

>>import cv2

After that, we will check our OpenCV version by typing the following command:

cv2.__version__
Testing]

We have installed OpenCV on Raspberry 3B+. Now we can perform numerous image processing operations in Python like Digit Detection, Face Recognition, etc.

ABOUT THE AUTHOR

Hamza Iqbal


Hey! I am Hamza. I am an Electrical Engineer who has a very innovative approach towards daily life stuff. I tend to make life easier by making circuits and designs to automate things around me. I mainly work with printed circuit boards on proteus to bring life to my inventions.