Installing OpenCV on Raspberry Pi 2 (32-bit Model): A Step-by-Step Guide

Gayan Tharuka
3 min readSep 4, 2024

--

The Raspberry Pi is a powerful, affordable microcomputer that’s perfect for learning about programming and electronics. By installing OpenCV (Open Source Computer Vision Library), you can add advanced computer vision capabilities to your projects. This guide will walk you through the steps to install OpenCV on a Raspberry Pi 2 running a 32-bit OS.

Step 1: Update and Upgrade Your Raspberry Pi

Before installing OpenCV, it’s crucial to update your Raspberry Pi to ensure all software packages are up-to-date. Open the terminal and run the following commands:

sudo apt-get update
sudo apt-get upgrade

This step updates the list of available packages and upgrades the installed packages to the latest versions.

Step 2: Install the Required Dependencies

OpenCV relies on various libraries to function correctly. Install these dependencies by running the following commands:

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libfontconfig1-dev libcairo2-dev
sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
sudo apt-get install python3-dev

These commands install essential tools like CMake, image and video processing libraries, and GUI packages that are necessary for OpenCV.

Step 3: Install OpenCV Using APT

Instead of using pip, you can install OpenCV directly from the Raspberry Pi’s package repository using APT. This method ensures compatibility with your system and simplifies the installation process:

sudo apt install python3-opencv

This command installs a pre-built version of OpenCV that is optimized for your Raspberry Pi, ensuring a smooth setup with fewer compatibility issues.

Step 4: Verify the Installation

After the installation, it’s good practice to verify that OpenCV has been installed correctly. You can do this by checking the version of OpenCV installed using the following command:

python3
import cv2
print(cv2.__version__)

If the installation was successful, this command will print the OpenCV version installed on your system.

Notes on Custom Builds

The method outlined above installs a pre-built version of OpenCV, which should work for most standard use cases. However, if you need specific features, optimizations, or support for particular hardware like the Raspberry Pi Camera Module, you might need to build OpenCV from source. This process is more complex and time-consuming but allows for greater customization.

Conclusion

Installing OpenCV on a Raspberry Pi 2 is straightforward if you follow these steps. This setup enables you to explore various computer vision projects, from simple object detection to more complex machine learning applications. Whether you’re working on personal projects, education, or prototyping, OpenCV on Raspberry Pi is a powerful tool for computer vision applications.

--

--

Gayan Tharuka
0 Followers

Web dev & future robot engineer. Unpacking tech mysteries for devs & curious minds.