This is a quick guide showing how to install OpenCV (Open Computer Vision Library) software on a Raspberry Pi board. We tested this installation on Raspberry Pi OS 64-bit. OpenCV is an open-source library for computer vision and machine learning that comes with tools and algorithms for image processing, video analysis, and machine learning applications.
There are many ways for installing OpenCV on Raspberry Pi, you can install with pip on a virtual environment, install it using apt install or compile the software from source. In this guide, we’ll show you two options:
a) Installing OpenCV on Raspberry Pi with pip on Virtual Environment (Recommended)
b) Installing OpenCV on Raspberry Pi with apt
Prerequisites
Before proceeding:
- You need a Raspberry Pi board.
- You should have a Raspberry Pi running Raspberry Pi OS (32-bit or 64-bit).
- You should be able to establish an SSH connection with your Raspberry Pi.
a) Installing OpenCV on Raspberry Pi with pip on Virtual Environment (Recommended)
Having an SSH connection established with your Raspberry Pi, update and upgrade your Raspberry Pi, if any updates are available. Run the following command:
sudo apt update && sudo apt upgrade -y
Run the next command which should print in your terminal a Python version 3.X:
python --version
Install pip3 and Python 3 Virtual environment:
sudo apt install -y python3-pip python3-virtualenv
Create a Virtual Environment
We’ll install the OpenCV library in a virtual environment. Creating a virtual environment will isolate the Python libraries we’re using, in this case, the OpenCV library, from the rest of the system.
We’ll create our virtual environment on a directory on our Desktop. Enter the following command on a Terminal window to move to the Desktop:
cd ~/Desktop
Create a folder for your project. This is where we’ll create the virtual environment and install the library. We’ll create a folder called projects.
mkdir projects
Move to the newly created folder:
cd ~/Desktop/projects
Create a virtual environment for this directory called myenv. This must be the same directory where we’ll install the OpenCV library. Replace myenv with the desired name for your virtual environment.
python3 -m venv projectsenv
Then, you can run the following command to check that the virtual environment is there.
ls -l
Activate the virtual environment:
source projectsenv/bin/activate
Your prompt should change to indicate that you are now in the virtual environment.
Installing the OpenCV Library
Now that we are in our virtual environment, we can install the OpenCV library. Run the following command:
pip3 install opencv-contrib-python
After a few seconds, the library will be installed (ignore any yellow warnings about deprecated packages).
You have everything ready to start writing your Python code.
Testing OpenCV pip Installation
Now let’s ensure that OpenCV has been installed successfully on your Virtual Environment. In your terminal window, start Python:
python
Then, in the Python import the OpenCV library.
import cv2
Finally, type the next command to check the OpenCV version that you installed.
cv2.__version__
If everything has been done properly, it should return your OpenCV version as illustrated in the image below.
You can exit Python prompt by typing:
exit()
b) Installing OpenCV on Raspberry Pi with apt
Having an SSH connection established with your Raspberry Pi, enter the next command to update and upgrade your packages:
sudo apt update && sudo apt upgrade -y
Run the next command which should print in your terminal a Python version 3.X:
python --version
Then, run the next command to install all the required dependencies.
sudo apt install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran libhdf5-dev libhdf5-serial-dev libhdf5-103 libqt5gui5 libqt5webkit5 libqt5test5 python3-pyqt5 python3-dev
Install OpenCV using apt install:
sudo apt install -y python3-opencv
This command can take up to 10 minutes, so be patience and wait for the command to complete.
Testing OpenCV apt Installation
Now let’s ensure that OpenCV has been installed successfully. In your terminal window, start Python:
python
Then, in the Python import the OpenCV library.
import cv2
Finally, type the next command to check the OpenCV version that you installed.
cv2.__version__
If everything has been done properly, it should return your OpenCV version as illustrated in the image below.
You can exit Python prompt by typing:
exit()
Wrapping Up
Congratulations! You successfully installed OpenCV on your Raspberry Pi. Now, you can start building your image and video processing projects.
You may also like reading:
- Set Up USB Camera for OpenCV Projects with Raspberry Pi
- Install InfluxDB on Raspberry Pi
- ESP32: Getting Started with InfluxDB
- What is MQTT and How It Works
- Install Mosquitto MQTT Broker on Raspberry Pi
Thanks for reading!
Well, there are small defects in this tutorial:
a) it does not install libopencv-dev : for C++ programming (Arduino has a subset of C++; maybe people can be happier with C++ than python); this is annoying…
b) the debian way leads to diffferent versions (4.6 -see your figure-) than the pip way (4.9). This can be annoying, too: there are very little bugfixes (AFA now) with opencv, but they add new DNNs which may be very amusing to discover. 4.9 version is therefore more appealing. (and 4.10 will be even more!)
Another way is … to build from sources (works without trouble with PI4 and PI5 using qengineering.eu/install-opencv-on-raspberry-pi.html scripts (takes < 6 hrs IIRC -I goto sleep when RPI{4,5} compile with reliable scripts…on a Pi4 and Pi5 having >= 4G RAM).
As the scripts are written, one gets both C++ and python bindings…
NB: Qengineering use the two other ways (pip and apt) but I prefer the compile from source way…
Thank you for the suggestion!