How To Install OpenCV On Raspberry Pi: 7 Easy Steps

James J. Davis
5 min readDec 11, 2020

Guys, let’s talk about my true passion — OpenCV!

OpenCV is a popular library of machine vision functions that allow robots to recognize objects in the world around them. OpenCV is used for navigation, obstacle detection, face recognition, and gesture recognition.

This article will offer step-by-step instructions on how to install OpenCV on a Raspberry Pi 3+ single-board computer. This article also precedes a series of tutorials aimed at mastering the basic functions of OpenCV.

The below steps will help you create a virtual environment for OpenCV. If you’re not sure how to do this, don’t worry! The process is fairly straightforward and can be completed in less than 10 minutes with minimal effort on your part. And once the environment has been created, it only takes one command to get started installing OpenCV inside of it. So what are you waiting for? Let’s get things rolling!

If you don’t want to read, you can skip to the video lesson

Step 1: Free Space

OpenCV and the various auxiliary packages together take up quite a lot of space. It is highly recommended that you use an SD card of at least 16 GB.

The first thing to do before installing OpenCV is to expand the file system to the full capacity of the SD card. This is done through the Raspbian setup menu. Go to the terminal and enter the command:

$ sudo raspi-config

This will open a menu, where you should select the topmost item:

Press Enter and then <Finish> button. After that, restart the system with the command:

$ sudo reboot

If you have only an 8 GB SD card, you can remove something unnecessary like a wolfram-engine package.

$ sudo apt-get purge wolfram-engine

This will free up an extra 700 MB of space.

Step 2: Installing Dependencies

We need to upgrade existing packages and install a number of new ones to make OpenCV work properly. Let’s start with an update.

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

Depending on your internet speed these operations will take about 5–10 minutes. Next, install CMake and some other useful stuff into the system:

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

Next, packages for working with known image formats:

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

Packages for working with video:

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

Packages for making simple screen forms. You’ll need all of these later, too.

$ sudo apt-get install libgtk2.0-dev

Special accelerated matrix operations.

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

Python language header files versions 2.7 and 3

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

Step 3: Downloading OpenCV from the repository

First, let’s download the archive with OpenCV itself. To do that go to the /home/pi/Downloads folder:

$ cd ~/Downloads

Use wget to download the archive and unzip it:

$ wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
$ unzip opencv.zip

Next, download the package with additional stuff — opencv_contrib.

$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip

Step 4: Setup virtual environment

Before further installation of OpenCV, we’re going to clean up a bit. Create a virtual environment that will keep our further experiments away from the main part of the operating system. This is useful in case we need to remove the current version of OpenCV and install the new one.

OpenCV is a powerful tool to help us process images. It’s easy to mess up your operating system if you install OpenCV incorrectly, so we’re going create our own virtual environment before installing the program.

We will start by installing the pip package manager:

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

Then let’s install the virtual environment manager:

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

Let’s add a couple of lines to the user profile, which is stored in ~/.profile:

$ echo -e “\n# virtualenv and virtualenvwrapper” >> ~/.profile
$ echo “export WORKON_HOME=$HOME/.virtualenvs” >> ~/.profile
$ echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.profile

Now you need to do the following three steps:

  • close all terminal windows
  • log out
  • log in again

Open a terminal and enter the command:


$ source ~/.profile

Note. In general, it is now recommended to enter this command every time you log in to the system.

Next, create a virtual environment named “cv”:

$ mkvirtualenv cv -p python3

To verify that the virtual environment is installed correctly, reboot the Raspberry Pi:

$ sudo reboot

Open a terminal and go to the environment:

$ source ~/.profile
$ workon cv

The indicator of the fact that we are in a virtual environment is a prefix (cv) at the beginning of the command line.

The last thing to do before building OpenCV is to install the NumPy math package:

$ pip install numpy

Step 5. Compiling and installing OpenCV

In the previously created virtual environment, go to the folder with OpenCV source code and run the commands:

$ cd ~/Downloads/opencv-master
$ 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=~/Downloads/opencv_contrib-master/modules
-D BUILD_EXAMPLES=ON .

At the end of the procedure, a list of OpenCV components will appear, ready to install.

And now for the fun part — building the binaries. Without leaving the current folder, run the make command:

$ make -j4

j4 means we will build using all 4 cores of the Raspberry Pi. This will speed up the procedure a lot but even with these conditions, it will take about an hour and a half to build.

The build should end with a report like this:

Installing OpenCV 3 on the Raspberry Pi 3. Completing the build
Note. Personally, during the build, the system froze up a couple of times. That is, even the mouse didn’t move. Switching the power off/on and restarting the system with the -j2 switch helped.

The last thing to do is install the built binary to the python folder:

$ sudo make install
$ sudo ldconfig

Step 6: Complete the installation

After installation, a file cv2.cpython-34m.so will appear in the python working folder, which you need to rename to something more euphonious:

$ cd /usr/local/lib/python3.4/site-packages/
$ sudo mv cv2.cpython-34m.so cv2.so

So we can use OpenCV in virtual environment, we will link to our cv2.so there:

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

Step 7: Check how it works

To check if it is installed correctly, we go into the virtual environment, run a python shell, and try to import the cv2 module:

$ source ~/.profile 
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
‘3.1.0’
>>>

That’s it — OpenCV installed successfully!

Read more:

--

--