Install Precompiled OpenCV 4.9 on Raspberry Pi

Install Precompiled OpenCV 4.9 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.9 on Raspberry Pi.

Debian package

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

OpenCV was built with the following features:

  • NEON optimization
  • VFPv3 optimization
  • TBB library
  • FFmpeg library
  • GStreamer library
  • 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_64.deb

Execute the following command to install OpenCV:

sudo apt install -y ./opencv_64.deb

We can run the opencv_version command to check version of OpenCV:

opencv_version

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

rm -rf opencv_64.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

Uninstall OpenCV

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

sudo apt purge --autoremove -y opencv

The 27 Comments Found

  1. Avatar
    James Hodds Reply

    I have loaded this onto my Raspberry Pi 3b+ what do I need to do now how do I run Opencv

    • Avatar
      lindevs Reply

      Hi, James
      First of all, try to run simple C++ and Python examples provided in this post to verify if OpenCV is working fine in your Raspberry Pi. If examples are working fine then you can read posts provided in OpenCV category.

  2. Avatar
    Robert Reply

    I am pretty new to OpenCV,
    All I get is:

    "terminate called after throwing an instance of 'cv::Exception'
    what():  OpenCV(4.5.5) /home/pi/opencv-src/modules/highgui/src/window.cpp:1268: error: (-2:Unspecified error)
    The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support.
    If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'"
      • Avatar
        huangganggui Reply

        Hi,lindevs

        It's a nice package for me. But I want to know why "built without GUI support". It is very useful for python like "cv2.imshow()",(cv2.imshow not work without GUI support)

      • Avatar
        lindevs Reply

        The OpenCV binaries without GUI support can be useful for Raspberry Pi OS Lite with no desktop environment. But there is a plan to build a separate Debian package that contains precompiled OpenCV binaries with GUI support.

        • Avatar
          Robert S Reply

          Hi,

          Nice Package. Do you have the package available, yet, with GUI Support?

          Thanks,
          Robert

        • Avatar
          lindevs Reply

          Hi,
          Unfortunately, we don't provide a package with GUI support yet.

  3. Avatar
    Terry Jones Reply

    I am still not there yet, but I have to say that after struggling with both Buster and Bullseye and every flavour of OpenCV I tried and every install tutorial for openCV (I am like 12 hours into this task...) yours is the very first "install" that actually got me to the point where "import cv2" worked in python3

    So for now, huge thanks for the one small step towards my motion tracking project...

  4. Avatar
    jay scott Reply
    apt -y install ./opencv.deb 
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    Note, selecting 'opencv' instead of './opencv.deb'
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:
    
    The following packages have unmet dependencies:
     opencv : Depends: libgstreamer1.0-0 (>= 1.18.4) but 1.14.4-1 is to be installed
              Depends: libgstreamer-plugins-base1.0-0 (>= 1.18.4) but 1.14.4-2+deb10u1 is to be installed
    E: Unable to correct problems, you have held broken packages.

    what do i do?

    • Avatar
      lindevs Reply

      Hi, Jay
      Perhaps you have installed Raspberry Pi OS Buster. Please update OS to the latest version.

  5. Avatar
    Tac Tacelosky Reply

    I upgraded to the 64-bit version of Pi OS, following the commands above everything worked exactly as expected. Thanks!

  6. Avatar
    Nathan Sinclair Reply

    Is it possible to install it to another python3 installation on the pi?
    Running it installed on the system version of Python (3.9). I would like to install it in Python3.11. Any help is appreciated!

    • Avatar
      lindevs Reply

      Hi,
      Currently, the OpenCV binaries provided in GitHub repository are built using Python 3.9 which is installed by default on Raspberry Pi OS Bullseye. Binaries incompatible with other Python versions, such as 3.11.

  7. Avatar
    Arczi Reply

    Great job! a package with mediapipe would be nice - they have the fastest face detection and face landmarks on the Raspberry...

    • Avatar
      lindevs Reply

      Hi,
      OpenCV can be fully uninstalled by executing the command provided in the post. After that, start all over again. Download the latest opencv.deb package and perform the installation.

  8. Avatar
    Supra Reply

    I am using Bookworm on rpi with 8gb.

    sudo apt install -y ./opencv_64.deb

    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    Note, selecting 'opencv' instead of './opencv_64.deb'
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:
    
    The following packages have unmet dependencies:
     libavformat58 : Depends: libavcodec58 (= 8:4.3.6-0+deb11u1+rpt5)
                     Depends: libsrt1.4-gnutls (>= 1.4.2) but it is not installable
     opencv : Depends: libavcodec58 (>= 4.3.2)
    E: Unable to correct problems, you have held broken packages.

    I need opencv 4.8.1 on latest Bookworm Not Bulseye.

    • Avatar
      lindevs Reply

      Hi,
      Currently, OpenCV binaries are compatible with Raspberry Pi OS Bullseye. I'm awaiting Cross-Compiler Toolchain for Raspberry Pi OS Bookworm. Once the toolchain becomes available, I will precompile OpenCV binaries for the new OS version.

  9. Avatar
    Supra Reply

    I got error problem:
    cv2.imshow('NContours', objects)
    cv2.error: OpenCV(4.9.0) /home/pi/opencv-src/modules/highgui/src/window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

    • Avatar
      lindevs Reply

      Hi,
      Currently, the OpenCV binaries available in the GitHub repository are built without GUI support.

    • Avatar
      lindevs Reply

      Hi,
      The cross-compilation of OpenCV binaries for the 32-bit version of Raspberry Pi OS has been discontinued.

  10. Avatar
    Happi_Hours Reply

    Hi! I'm having this same problem trying to install openCV for my raspberry pi 3. I updated my OS version, and I am getting this result when I attempt to run sudo apt install -y ./opencv_64.deb:

    Note, selecting 'opencv:arm64' instead of './opencv_64.deb'
    Some packages could not be installed, This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The folloiwng information may help to resolve the situation:
    
    The following packages have ummet dependencies:
     opencv:arm64 : Depends: libgstreamer1.0-0:arm64 (>= 1.22.0) but it is not installable
                    Depends: libgstreamer-plugins-base1.0-0:arm64 (>= 1.22.0) but it is not installable
                    Depends: libgvcodec59:arm64 (>=5.1.3) but it is not installable
                    Depends: libgvformat59:arm64 (>=5.1.3) but it is not installable
                    Depends: libgavutil57:arm64 (>=5.1.3) but it is not installable
                    Depends: libswscale6: arm64 (>=5.1.3) but it is not installable
                    Depends: python3-numpy:arm64 (>= 1.24.2) but it is not installable
    E: Unable to correct problems, you may have broken packages.
    • Avatar
      lindevs Reply

      Hi,
      Ensure your operating system is upgraded to the most recent version, Raspberry Pi OS Bookworm. Additionally, confirm that you're utilizing the 64-bit version of Raspberry Pi OS.

Leave a Comment

Cancel reply

Your email address will not be published.