Popup

Wait! Don’t Go Yet! 👋

Become a Member Today and Unlock Access to All eBooks! 😍

Thousands of eBooks at your fingertips. Read, learn, and grow anytime, anywhere ✨

Install OpenCV on a Raspberry Pi (compatible with all RPi boards)

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 to install OpenCV on Raspberry Pi; you can install it 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:

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:

Top 6

Raspberry Pi eBooks

From Zero to Professional

Raspberry Pi Projects
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 in a directory on our Desktop. Enter the following command in 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
python3 virtual environment

Activate the virtual environment:

source projectsenv/bin/activate

Your prompt should change to indicate that you are now in the virtual environment.

python3 activate 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).

python3 install openCV pip command

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 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.

python3 import opencv2 test version pip

You can exit the 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
check python version on raspberry pi

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

Testing OpenCV apt Installation

Now, let’s ensure that OpenCV has been installed successfully. In your terminal window, start Python:

python

Then, in 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.

check opencv version installation raspberry pi

You can exit the 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:

Share your love

🚀 Discover the world of electronics and innovation!

✨ Create, program, and experiment with all your creative ideas with ease.

Spotpear

Leave a Reply

Your email address will not be published. Required fields are marked *

Secure Payments
Securing online payments is a shared responsibility, and everyone can contribute.
Free Shipping
You get unlimited free shipping on eligible items with Ebokify, with no minimum spend.
24/7 Support
Sales gifts are helpful tools often used to show appreciation to clients for their purchase.
Gifts & Sales
Our customer care service is offered in the form of 1st or 2nd level support.