Overview
In this guide, we will learn how to install & setup OpenCV on Raspberry Pi 4 computer.
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It contains over 2500 optimized algorithms that span a wide range of areas in vision, including but not limited to:
- Image and Video Capture
- Preprocessing (filtering, graying, thresholding, etc.)
- Face & Eyes Detection
- Object Detection
- Edge Detection & Motion Sensing
- Image Segmentation
- Machine Learning
- Age & Gender Identification
- 3D Reconstruction
- Motion Analysis and Object Tracking
- Augmented Reality
OpenCV and Raspberry Pi together offer a powerful platform for a wide range of computer vision projects. Whether you’re building a smart doorbell with face recognition, a robot that can navigate its surroundings, or simply learning the basics of computer vision, this combo is both affordable and versatile.
Requirements
For the Hardware parts, we need the following components:
| S.N. | Components | Quantity | Purchase Link |
|---|---|---|---|
| 1 | Raspberry Pi 4 | 1 | Amazon | SunFounder |
| 2 | SD Card 16/32 GB | 1 | Amazon | SunFounder |
| 3 | 5V, 3A DC Adapter for RPi | 1 | Amazon | SunFounder |
| 4 | LCD Display (Optional) | 1 | Amazon | SunFounder |
| 5 | Mouse & Keyboard (Optional) | 1 | Amazon | SunFounder |
You may use the HDMI Monitor or a VNC Viewer for the setup part.
Install & Setup OpenCV on Raspberry Pi
Before learning how to install & setup OpenCV on the Raspberry Pi Computer, you need to setup your Raspberry Pi first. To understand how to setup the Raspberry Pi first, follow this article: Setting up Raspberry Pi.
In this guide, we have used a Raspberry Pi 4 with the latest Raspbian Buster. The OpenCV installation process is divided into multiple steps as follows:
Step 1: Install dependencies
1. Updating Existing Packages:
Execute the following command to update and upgrade your system’s packages:
|
1 2 |
sudo apt-get update && sudo apt-get upgrade |
2. Installing Image I/O Packages:
For support with various image file formats, install the necessary packages using:
|
1 2 |
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev |
3. Setting Up Video I/O Packages:
To handle different video file formats and work with video streams, use the commands below:
|
1 2 3 |
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev |
4. Installing the GTK Development Library:
To compile the highgui module (used for displaying images and creating basic GUIs), install the GTK development library:
|
1 2 |
sudo apt-get install libgtk2.0-dev |
5. Additional Dependencies for OpenCV Optimization:
For enhanced OpenCV operation optimization, install these extra dependencies:
|
1 2 |
sudo apt-get install libatlas-base-dev gfortran |
Step 2: Installing pip (Package Management Tool)
If you haven’t installed pip for Python 3 yet, execute the command below:
|
1 2 |
sudo apt-get install python3-pip |
Step 3: Installing the Numpy Library
Numpy provides essential mathematical and numerical capabilities useful for OpenCV. If you haven’t installed it yet, use the command:
|
1 2 |
pip install numpy |
Step 4: Accessing OpenCV on Raspbian Repository
To locate OpenCV in the default Raspbian Buster repository, use the command:
|
1 2 |
apt list python*opencv* |
Step 5: Installing OpenCV
Execute the following command to install OpenCV on Raspberry Pi.
|
1 2 |
sudo apt install python3-opencv |
Step 6: Verifying OpenCV Installation
To confirm the installation of OpenCV, use:
|
1 2 |
apt show python3-opencv |
After executing, you should see that the latest version is successfully installed!
Conclusion
We’ve walked through the systematic process of setting up and confirming the installation of OpenCV on a Raspbian system. This foundational step is crucial for delving into computer vision projects using a Raspberry Pi. By ensuring that the correct version is installed, you’re setting the stage for a seamless experience with OpenCV. As you embark on your computer vision journey, always ensure that your software tools are up-to-date and correctly configured for optimal performance.






