You can install OpenCV on any model of Raspberry Pi, including the Pi Zero. However, as you'll be compiling the software library before you install it, a newer Raspberry Pi model will take less time to compile the required packages (due to increased system resources).
As OpenCV is a visual analysis library, you may prefer to use it with live data. If that's the case, you'll need to connect a camera to your Raspberry Pi. This can either be the official Raspberry Pi camera add-on or a third-party USB camera. This isn't required for setting up and installing it, but you will likely need it for projects you create afterward.
If you're using a USB camera, make sure that enough power is supplied to it from the USB ports on your Raspberry Pi. You’ll also need a microSD card (or SD card for older models) with a suitable operating system installed.
This guide will assume you’re using Raspberry Pi OS (previously Raspbian) for this project, but OpenCV is supported on all Linux platforms, including Ubuntu and Debian.
You can follow these steps by using a keyboard or mouse or by connecting to your Raspberry Pi remotely via SSH if SSH is enabled. SSH isn’t enabled by default in Raspberry Pi OS, so you’ll need to enable this first before you can connect remotely (assuming you’re on the same local network).
As we’ve mentioned, we’re assuming that you already have the latest version of Raspberry Pi OS installed on your Raspberry Pi. Before you can proceed with installing OpenCV, however, you’ll need to make sure that your system is fully up-to-date.
To do this, open a terminal window (or remote SSH connection) and type the following to update your system and installed packages:
sudo apt update sudo apt upgrade
Depending on how outdated your Raspberry Pi is, this may take some time to complete. Once it’s completed, you can begin to install the necessary packages that you’ll need to compile OpenCV.
This is because OpenCV isn’t available as a set of packages that can just be ‘installed’. Instead, you’ll need to download the source code and compile it.
To do this, type the following in an open terminal window (or via SSH):
sudo apt install cmake pkg-config build-essential git
Once the various packages for compiling OpenCV have installed, you’ll need to install some additional video and image libraries, as well as the libraries to control OpenCV data in the HDF5 file format. These are used by OpenCV to analyze the video and images you capture.
To do this, type the following in a terminal or SSH window:
sudo apt install libwebp-dev libjasper-dev libopenexr-dev libv4l-dev libavformat-dev sudo apt install libxvidcore-dev libx264-dev libdc1394-22-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libavcodec-dev sudo apt install libtiff-dev libpng-dev libjpeg-dev libswscale-dev sudo apt install libhdf5-dev libhdf5-103
This may take a bit of time to complete. Once the process is done, however, you’ll be ready to install some essential components used to create and use applications with OpenCV that have a user interface. To do this, type the following in a terminal or SSH window:
sudo apt install libgtk-3-dev libqtwebkit4 libqt4-test libqtgui4 python3-pyqt5
Some additional user packages are required for improving how OpenCV works on the Raspberry Pi, as well as add Python support (which is used by many open source projects that use OpenCV). To do this, type the following in a terminal window or over SSH:
sudo apt install python3-dev python3-pip python3-testresources python3-numpy
If each of these stages completes successfully, you’ll be ready to compile OpenCV. Otherwise, troubleshoot the installation process and ensure that each of these packages has been installed before you proceed, or OpenCV may not install or work correctly.
Once the packages you need for OpenCV are ready, you can begin to compile it. To help with this, however, you’ll need to tweak the swap file on your Raspberry Pi.
The swap file is used when physical memory has been exhausted, ensuring your Raspberry Pi doesn’t crash when it runs out of RAM. Instead, it saves some data to your SD card, allowing it to prioritize other running processes.
To do this, type the following in a terminal window or over SSH:
sudo nano /etc/dphys-swapfile
In the nano file editor, replace CONF_SWAPSIZE=100 with CONF_SWAPSIZE=2048. Once the file has been edited, press Ctrl + X on your keyboard, then hit the Y + Enter keys to confirm and save.
You’ll then need to restart the swap file service. To do this, type the following in the terminal or over SSH:
sudo systemctl restart dphys-swapfile
This will recreate the swap file, giving you more memory to use while compiling OpenCV. Once that’s completed, type the following in a terminal or over SSH to download the source code and generate the necessary makefile for OpenCV on your Raspberry Pi:
git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git mkdir ~/opencv/build cd ~/opencv/build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D CMAKE_SHARED_LINKER_FLAGS=-latomic \ -D BUILD_EXAMPLES=OFF ..
If the makefile is created successfully, you can then begin to compile OpenCV. To do this, type the following in a terminal or over SSH:
argument will massively speed up the compilation process, especially if you have a newer Raspberry Pi with multiple CPU cores available. The older the Raspberry Pi, the longer the compilation process, which could take several hours to complete.
Once the compilation process has completed (and assuming it completed successfully), you can then move to install the compiled OpenCV package. To do this, type the following in a terminal or over SSH:
sudo make install sudo ldconfig
This will install OpenCV and allow it to run after it’s been installed. Once OpenCV is installed, repeat the steps above to return your swap file to a normal size by typing the following in a terminal window or over SSH:
sudo nano /etc/dphys-swapfile
Return CONF_SWAPSIZE=2048 back to the default CONF_SWAPSIZE=100, then press Ctrl + X, Y and Enter on your keyboard to save. To restart the swap file service, type the following in a terminal or over SSH:
sudo systemctl restart dphys-swapfile
This will return the swap file to normal. At this point, you’re ready to begin using OpenCV.
Once OpenCV is installed, you can check it works by importing the OpenCV module into Python. By doing this, you can then interact with it to ensure that it works correctly.
To do this, type the following to open a Python interpreter, import the OpenCV Python module, and check that OpenCV works by finding the currently installed version (the version you compiled and installed):
python3 import cv2 cv2.__version__ exit()
should return the latest version of OpenCV (for instance,
) in the Python interpreter before you exit it with the
You can then begin to use OpenCV with other projects, but you may need to install additional packages if you wish to use it with other apps and services.
With OpenCV compiled and ready to use, you’re ready to begin creating projects with OpenCV and Raspberry Pi at its core. For instance, you could build an advanced Raspberry Pi security camera that takes advantage of OpenCV’s motion detection features to alert you to potential intruders.
If you want to create a more personal project, then you could think about integrating OpenCV with other packages to create a magic mirror using your Raspberry Pi. The sky really is the limit, so if you have your own project ideas you’d like to share, feel free to share them in the comments below.