How to Install OpenCV in Raspberry Pi

I’ll show you how to install Open CV on your Raspberry Pi board in this guide. So let’s get started.

For those unfamiliar with OpenCV, installing it on your Raspberry Pi entails a series of steps and a lot of patience.

What is OpenCV?

OpenCV is an excellent tool for image processing and computer vision. It’s an open-source library for tasks including face detection, objection tracking, landmark detection, and more. Python, Java, and C++ are among the languages supported.

The library is equipped with hundreds of useful functions and algorithms, which are all freely available to us. Some of these functions are quite widespread, appearing in nearly every computer vision work. Many of the functions, on the other hand, are still unknown and haven’t gotten much attention. Intel was the first to develop it.Intel was the first to develop it. 

Things required

Installing Packages for OpenCV

We’ll walk you through the process of installing all the packages you’ll need to assemble and execute the OpenCV software in this part.

We’ll install OpenCV one step at a time because it requires so many packages on the Raspberry Pi.

1. We should first update any pre-existing packages before proceeding. LXterminal is required for all commands (default Raspbian Terminal).

You can update the currently installed packages by running the following two commands.

sudo apt updatesudo apt upgrade

2. We can now begin the process of installing all of the packages that OpenCV requires to compile.

Run the command below to get started. This programme will download and install the packages that contain the tools required to compile OpenCV code.

sudo apt install cmake build-essential pkg-config git

3. Next, we’ll install the packages that will enable OpenCV to support a variety of image and video formats.

With the following command, you may install these libraries on your Raspberry Pi.

sudo apt install libjpeg-dev libtiff-dev libjasper-dev libpng-dev libwebp-dev libopenexr-devsudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libdc1394-22-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

4. Our next step is to install all the packages needed for OpenCV’s interface by using the command below.

sudo apt install libgtk-3-dev libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5

5. The following packages are required for OpenCV to run smoothly on the Raspberry Pi.

The following command can be used to install these packages.

sudo apt install libatlas-base-dev liblapacke-dev gfortran

6. The last set of programmes to install is for the Hierarchical Data Format (HDF5), which is used by OpenCV to organise data.

Use the command below to install the HDF5 packages on your Pi.

sudo apt install libhdf5-dev libhdf5-103

7. Finally, we may run the command below to install the last few items.

These last few packages will allow us to compile OpenCV on our Raspberry Pi with Python support.

sudo apt install python3-dev python3-pip python3-numpy

Before proceeding to the next section, make sure all the packages installed successfully.

Preparing your Raspberry Pi for Compiling OpenCV

1. With all the required packages to compile OpenCV on our Raspberry Pi now installed, we need to do some preparatory work before we can start the compilation process.

We will now need to temporarily increase the size of the swap space to help the process of compiling OpenCV on the Raspberry Pi.

The swap space is used by the operating system when the device has run out of physical RAM. While swap memory is a lot slower than RAM, it can still be helpful in certain situations.

Begin modifying the swap file configuration by running the following command.

sudo nano /etc/dphys-swapfile

2. While we are within this file, we need to find and replace the following line.



Replace With


Once changed, save the file by pressing CTRL+X followed by Y then Enter.

3. As we have made changes to the swapfile configuration, we need to restart its service by utilizing the command below.

sudo systemctl restart dphys-swapfile

By restarting the service, we are forcing it to recreate the swap file.

4. Next, let’s go ahead and clone the two OpenCV repositories we need to our Raspberry Pi.

Running these two commands will retrieve the latest available version of OpenCV from their git repository.

git clone clone

As these repositories are quite large, they may take some time to clone to your Raspberry Pi.

Compiling OpenCV on your Raspberry Pi

1. Let’s start by creating a directory called “build” within the cloned “opencv” folder and then changing the working directory to it.

mkdir ~/opencv/buildcd ~/opencv/build

In this folder, we will be compiling OpenCV on your Raspberry Pi.

2. Now that we are within our newly created build folder, we can now use cmake to prepare OpenCV for compilation on our Raspberry Pi.

Run the following command to generate the required makefile.


3. Once the make file has successfully finished generating, we can now finally move on to compiling OpenCV by running the command below.

We use the argument -j$(nproc) to tell the compiler to run a compiler for each of the available processors.

Doing this will significantly speed up the compilation process and allow each core on the Raspberry Pi to work on compiling OpenCV.

make -j$(nproc)

Please note that the compilation process can take considerable time. On our Raspberry Pi 4, this process took about 1 hour to complete.

4. When the compilation process finishes, we can then move on to installing OpenCV.

Luckily for us, this is a reasonably straightforward process and requires you to run the following command.

sudo make install

This command will copy all the required files into there needed locations automatically.

5. Now we also need to regenerate the operating systems library link cache.

The Raspberry Pi won’t be able to find our OpenCV installation if we don’t run the following command.

sudo ldconfig

Cleaning up after Compilation

1. Now that we have finished compiling OpenCV, we no longer need to have such a large swap file.

Let’s again edit the swap file configuration by using the following command.

sudo nano /etc/dphys-swapfile

2. Within this file, you need to find and change the following line.



Replace With


When done, save the file by pressing CTRL+X followed by Y then Enter.

3. Now our final cleanup task requires us to restart the swap file service.

Restarting the service will downsize the file from 2GB to 100 MB.

sudo systemctl restart dphys-swapfile

Testing OpenCV on your Raspberry Pi

1. To test whether OpenCV is now installed to our Raspberry Pi, we will make use of our Python 3 installation.

Launch into the Python terminal by running the command below.


2. While we are within Python, we can now import the OpenCV Python module using the command below.

By importing the module, we can first check to see if OpenCV will even load on our Pi.

import cv2

3. With the OpenCV module now imported, we should be able to retrieve its version.

To retrieve OpenCV’s version, use the following command.


4. If everything is now working as intended and OpenCV has been successfully installed to your Raspberry Pi, you should see text like the following appear in the command line.


Hopefully, at this point you will now have OpenCV up and running.Hope this blog helps you to understand  how you can install OpenCV to your Raspberry Pi.. We ,MATHA ELECTRONICS  will come back with more informative blogs.

Leave a Reply

Your email address will not be published.