Install Precompiled OpenCV 4.5 on Raspberry Pi

OpenCV is an open-source library for image processing, computer vision, and machine learning. OpenCV is widely used for object detection, gesture recognition, motion tracking, face detection and recognition, image segmentation, medical image analysis, license plate recognition, and so on.

This tutorial shows how to install precompiled OpenCV 4.5 on Raspberry Pi.

Debian package

We have created Debian package (.deb) that contains precompiled OpenCV 4.5.3 binaries for Raspberry Pi 3 Model A+/B+ and Raspberry Pi 4 Model B. Binaries are compatible with Raspberry Pi OS Buster (32-bit). We have created a release on GitHub repository and uploaded opencv.deb package.

OpenCV was built with the following features:

  • NEON optimization
  • VFPv3 optimization
  • TBB library
  • FFmpeg library
  • GStreamer library
  • Python 2 and Python 3 bindings

You can found detailed build information on GitHub repository.

We tested on Raspberry Pi 4 Model B (8 GB).

Install OpenCV

Connect to Raspberry Pi via SSH. Download the .deb package from releases page of the repository:

wget https://github.com/prepkg/opencv-raspberrypi/releases/latest/download/opencv.deb

Execute the following command to install OpenCV:

sudo apt install -y ./opencv.deb

We can run opencv_version command to check version of OpenCV:

opencv_version

The .deb package is no longer needed, you can remove it:

rm -rf opencv.deb

Testing OpenCV (C++)

Before starting, make sure you have installed GNU C++ compiler:

sudo apt install -y g++

Create a main.cpp file:

nano main.cpp

When a file is opened, add the following code:

#include <opencv2/opencv.hpp>

int main() {
    std::cout << cv::getBuildInformation() << std::endl;

    return 0;
}

Execute the following command to compile code:

g++ main.cpp -o test -lopencv_core

The core functionality of OpenCV has been used. So we need to link libopencv_core.so shared library.

Run a program:

./test

Testing OpenCV (Python)

Create a main.py file:

nano main.py

Add the following code:

import cv2

print(cv2.getBuildInformation())

Run a script using Python 3:

python3 main.py

Python 2 can be used as well:

python main.py

Uninstall OpenCV

If you wish to completely remove OpenCV and related dependencies, execute the following command:

sudo apt purge --autoremove -y opencv

Leave a Comment

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