This tutorial describes the steps necessary to Install OpenCV On Raspberry Pi 3/4. The process is simple and only requires a few minutes. Once installed, you will have access to a wide variety of example programs that demonstrate the power of this toolkit.
Installing OpenCV on a Raspberry Pi is a relatively easy method. In this guide, we’ll show you how to install OpenCV 4 on your Raspberry Pi 4.
A great way to get started with computer vision is to use the OpenCV library on a Raspberry Pi. OpenCV is a powerful library that can be used to develop computer vision applications
Required Material
In this tutorial, you will learn how to install OpenCV on your Raspberry Pi. You will need the following items to complete this tutorial: –
- Raspberry Pi
- MicroSD card
- USB power supply
- Keyboard
- Mouse First
First, make sure you have a good power supply and a reliable SD card. We recommend using a 16GB SD card or larger.
Check the Raspberry Pi OS version on Raspberry PI
1 |
cat /etc/os-release |
Here is what mine:
1 2 3 4 5 6 7 8 9 10 |
PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)" NAME="Raspbian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs" |
See I am using “Raspbian GNU/Linux version 11”.
Set Up Your Raspberry Pi
First, we need to make sure that our Raspberry Pi is up to date. We can do this by running the following commands: Cool, we’re ready. Let’s update the Pi:
First, update your system’s packages by running this command:
1 |
sudo apt-get update && sudo apt-get upgrade |
Now that our Raspberry Pi is up to date.
Install Python
Once we have updated Raspberry Pi OS, we go to the terminal and type python3
and as you will notice, python is already installed, this time in version 3.9.2. If you don’t have Python then use the following command will download Python directly onto your machine.
1 |
<span class="enlighter-text">sudo apt-get install python3</span> |
Step 1: Install Dependencies
Next, install packages for working with image files:
1 |
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev |
Install packages for handling video files and streams:
1 2 |
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev |
Install the GTK development library for creating basic graphical user interfaces:
1 |
sudo apt-get install libgtk2.0-dev |
Install additional dependencies for optimizing OpenCV:
1 |
sudo apt-get install libatlas-base-dev gfortran |
Install pip (Package Management Tool)
Python package manager (Pip) is a package manager for Python that allows you to install and manage software packages written in Python.
If you haven’t installed pip for Python 3 yet, use the below command:
1 |
sudo apt-get install python3-pip |
This will install the latest version of pip. Once pip is installed, you can use it to install packages from the Python Package Index, a repository of software written in Python.
Install the Numpy Library
Numpy provides mathematical and numerical capabilities for OpenCV. Install it with this command:
1 |
pip install numpy |
Access OpenCV in the Raspbian Repository
To find OpenCV in the default Raspbian Buster repository, use this command:
1 |
apt list python*opencv* |
Install OpenCV
To install OpenCV on Raspberry Pi, use this command: We are going to install OpenCV version 4.5.1
1 |
sudo apt install python3-opencv |
verify OpenCV Installation
To confirm that OpenCV is installed, use this command:
1 |
apt show python3-opencv |
The installation is complete
To verify the installation, import the cv2
module and print the OpenCV version:
1 2 |
import cv2 print(cv2.__version__) |
After following these steps, you will have OpenCV installed on your Raspberry Pi.