Get OpenCV running on the Raspberry Pi (2024)

Throughout this guide, we will walk you through the process of setting up OpenCV on your Raspberry Pi.

Get OpenCV running on the Raspberry Pi (1)

Successfully installing OpenCV to your Raspberry Pi requires a couple of different steps and a fair bit of patience.

For those who do not know what OpenCV is. It is a library of different programming functions that are aimed at dealing with real-time computer vision.

Using computer vision, you can interpret images and videos in real-time. Allowing you to perform tasks such as motion detection and facial recognition with relative ease.

The Raspberry Pi is an excellent platform for starting to learn OpenCV and also doubles as an affordable and small device.

Equipment List

Here is all the equipment that we recommend for this Raspberry Pi OpenCV tutorial.

Recommended

  • Raspberry Pi
  • Micro SD Card
  • Power Supply
  • Ethernet Cable orWi-Fi
  • Raspberry Pi camera or USB Webcam

Optional

  • Raspberry Pi Case
  • USB Keyboard
  • USB Mouse

This tutorial was tested using the latest version of Raspbian Buster. If you are running an older version of Raspbian, you can use our guide to upgrade to Buster.

Installing Packages for OpenCV

In this section, we will be walking you through the process of installing all the packages you need to compile and run the OpenCV software.

As OpenCV requires so many packages on the Raspberry Pi, we will install these over a couple of steps.

1. Before proceeding, we should first update any preexisting packages.

You can update the currently installed packages by running the following two commands.

sudo apt updatesudo apt upgrade

2. Now we can start the process of installing all the packages we need for OpenCV to compile.

To start, run the command below. This command will install the packages that contain the tools needed to compile the OpenCV code.

sudo apt install cmake build-essential pkg-config git

3. Next, we are going to install the packages that will add support for different image and video formats to OpenCV.

Install these libraries to your Raspberry Pi with the following command.

sudo apt install libjpeg-dev libtiff-dev libjasper-dev libpng-dev libwebp-dev libopenexr-devsudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libdc1394-22-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

4. Our next step is to install all the packages needed for OpenCV’s interface by using the command below.

sudo apt install libgtk-3-dev libqt5gui5 libqt5webkit5 libqt5test5 python3-pyqt5

5. These next packages are crucial for OpenCV to run at a decent speed on the Raspberry Pi.

You can install these packages by running the following command.

sudo apt install libatlas-base-dev liblapacke-dev gfortran

6. The second last lot of packages thaat we need to install relate to the Hierarchical Data Format (HDF5) that OpenCV uses to manage data.

Install the HDF5 packages to your Pi by using the command below.

sudo apt install libhdf5-dev libhdf5-103

7. Finally, we can install the final few packages by using the command below.

These last few packages will allow us to compile OpenCV with support for Python on our Raspberry Pi.

sudo apt install python3-dev python3-pip python3-numpy

Before proceeding to the next section, make sure all the packages installed successfully.

Preparing your Raspberry Pi for Compiling OpenCV

1. With all the required packages to compile OpenCV on our Raspberry Pi now installed, we need to do some preparatory work before we can start the compilation process.

We will now need to temporarily increase the size of the swap space to help the process of compiling OpenCV on the Raspberry Pi.

The swap space is used by the operating system when the device has run out of physical RAM. While swap memory is a lot slower than RAM, it can still be helpful in certain situations.

Begin modifying the swap file configuration by running the following command.

sudo nano /etc/dphys-swapfile

2. While we are within this file, we need to find and replace the following line.

Find

CONF_SWAPSIZE=100

Replace With

CONF_SWAPSIZE=2048

Once changed, save the file by pressing CTRL+X followed by Y then Enter.

3. As we have made changes to the swapfile configuration, we need to restart its service by utilizing the command below.

sudo systemctl restart dphys-swapfile

By restarting the service, we are forcing it to recreate the swap file.

4. Next, let’s go ahead and clone the two OpenCV repositories we need to our Raspberry Pi.

Running these two commands will retrieve the latest available version of OpenCV from their git repository.

git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git

As these repositories are quite large, they may take some time to clone to your Raspberry Pi.

Compiling OpenCV on your Raspberry Pi

1. Let’s start by creating a directory called “build” within the cloned “opencv” folder and then changing the working directory to it.

mkdir ~/opencv/buildcd ~/opencv/build

In this folder, we will be compiling OpenCV on your Raspberry Pi.

2. Now that we are within our newly created build folder, we can now use cmake to prepare OpenCV for compilation on our Raspberry Pi.

Run the following command to generate the required makefile.

cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D CMAKE_SHARED_LINKER_FLAGS=-latomic \ -D OPENCV_PYTHON_INSTALL_PATH=lib/python3.9/dist-packages \ -D BUILD_EXAMPLES=OFF ..

3. Once the make file has successfully finished generating, we can now finally move on to compiling OpenCV by running the command below.

We use the argument -j$(nproc) to tell the compiler to run a compiler for each of the available processors.

Doing this will significantly speed up the compilation process and allow each core on the Raspberry Pi to work on compiling OpenCV.

make -j$(nproc)

Please note that the compilation process can take considerable time. On our Raspberry Pi 4, this process took about 1 hour to complete.

If the compilation process is for some reason failing to complete or appearing to get hung, there is a chance the CPU is getting locked up.

To get around this, you can try running a single make process. This should reduce the likely hood of issues but cause longer compilation times.

make

4. When the compilation process finishes, we can then move on to installing OpenCV.

Luckily for us, this is a reasonably straightforward process and requires you to run the following command.

sudo make install

This command will copy all the required files into there needed locations automatically.

5. Now we also need to regenerate the operating systems library link cache.

The Raspberry Pi won’t be able to find our OpenCV installation if we don’t run the following command.

sudo ldconfig

Cleaning up after Compilation

1. Now that we have finished compiling OpenCV, we no longer need to have such a large swap file.

Let’s again edit the swap file configuration by using the following command.

sudo nano /etc/dphys-swapfile

2. Within this file, you need to find and change the following line.

Find

CONF_SWAPSIZE=2048

Replace With

CONF_SWAPSIZE=100

When done, save the file by pressing CTRL+X followed by Y then Enter.

3. Now our final cleanup task requires us to restart the swap file service.

Restarting the service will downsize the file from 2GB to 100 MB.

sudo systemctl restart dphys-swapfile

Testing OpenCV on your Raspberry Pi

1. To test whether OpenCV is now installed to our Raspberry Pi, we will make use of our Python 3 installation.

Launch into the Python terminal by running the command below.

python3

2. While we are within Python, we can now import the OpenCV Python module using the command below.

By importing the module, we can first check to see if OpenCV will even load on our Pi.

import cv2

3. With the OpenCV module now imported, we should be able to retrieve its version.

To retrieve OpenCV’s version, use the following command.

cv2.__version__

4. If everything is now working as intended and OpenCV has been successfully installed to your Raspberry Pi, you should see text like the following appear in the command line.

'4.6.0'

Hopefully, at this point you will now have OpenCV up and running.

If you have run into any issues or have any feedback on this Raspberry Pi OpenCV tutorial, then feel free to drop a comment below.

Recommended

How to get your IP Address on Ubuntu

How to Setup Spotify Connect on the Raspberry Pi

How to Show Hidden Files in macOS Finder

Installing Home Assistant on Ubuntu

How to Get the Mac Address in macOS

How to Enable SSH on the Steam Deck

Get OpenCV running on the Raspberry Pi (2024)

FAQs

Can a Raspberry Pi run OpenCV? ›

In fact, the best method to install OpenCV on your Raspberry Pi is to compile the source code. You always have the latest version while controlling all the features you want to add. The method generates the C++ libraries and the Python bindings. You have them all in one.

How much time does it take to install OpenCV on Raspberry Pi? ›

Installing opencv on raspberry can be quite challenging and time-taking, if you do it the usual way. It takes around 3 long hours just to compile the opencv source code on raspberry pi 3 model b+. Buttt, stick with me and in this tutorial, we are going to install opencv on your raspberry pi in less than 10 minutes.

How to check if OpenCV is installed? ›

Check Your OpenCV Version Using cv2.__version__()

The simplest way to check the OpenCV version is by using the cv2.version attribute. This method returns a string representing the OpenCV version. When you run this code snippet, it will print the version of OpenCV installed on your system.

How to check OpenCV version in Raspberry Pi? ›

Check OpenCV Version
  1. import cv2.
  2. Use __version__ on cv2 to get its version. cv2.<< your code comes here >>

How to connect OpenCV to Raspberry Pi? ›

Installing OpenCV on Raspberry Pi
  1. Step 1: Preparing the Raspberry.
  2. Step 2: Install the dependencies on Raspberry Pi.
  3. Step 3: Download OpenCV 4 for Raspberry pi.
  4. Step 4: Python 3 virtual environment for OpenCV 4.
  5. Step 5: CMake and compile OpenCV 4.
  6. Step 6: Link OpenCV to python virtual environment.
  7. Step 7: Test OpenCV.
Apr 6, 2022

Can Raspberry Pi 4 run OpenCV? ›

OpenCV makes adding computer vision to your Raspberry Pi projects a straight-forward process. Using it, you could train the Raspberry Pi to classify or recognise objects and react to them. In this guide, learn to install OpenCV on the Raspberry Pi 4 in a dozen steps.

How much RAM does OpenCV need? ›

We recommend you have at least 4 GB RAM in your system.

Can Raspberry Pi Zero run OpenCV? ›

While the Pi Zero isn't quite fast enough for advanced video processing, it's still a great tool that you can use to learn the basics of computer vision and OpenCV.

Does OpenCV need internet? ›

If you have already downloaded the OpenCV source code, building OpenCV is fairly straightforward. You will only need the internet if you have any unmet dependencies (which it sounds like you're able to take care of). This may take a while so have patience.

How do I run OpenCV? ›

Build OpenCV for Python and C++ from sources
  1. Install cmake. ...
  2. Unzip both archives Run cmake-gui.
  3. choose the directory with opencv sources.
  4. specify the directory for storing building binaries and Visual Studio project. ...
  5. press 'Configure'
  6. use standard settings and proceed with Finish button.

Where should OpenCV be installed? ›

By default OpenCV will be installed to the /usr/local directory, all files will be copied to following locations: /usr/local/bin - executable files. /usr/local/lib - libraries (. so)

How to install OpenCV in Raspberry Pi 4? ›

Install & Setup OpenCV on Raspberry Pi
  1. Step 1: Install dependencies. Updating Existing Packages: ...
  2. Step 2: Installing pip (Package Management Tool) ...
  3. Step 3: Installing the Numpy Library. ...
  4. Step 4: Accessing OpenCV on Raspbian Repository. ...
  5. Step 5: Installing OpenCV. ...
  6. Step 6: Verifying OpenCV Installation.
Aug 28, 2023

How to check if OpenCV is installed in Python? ›

  1. Simply go to terminal/command prompt.
  2. Write python/python3.
  3. import cv2.
  4. if it doesn't show any error then your opencv is installed.
Feb 17, 2010

Does OpenCV work on Raspberry Pi 3? ›

I understand that installing OpenCV on single board computers like Raspberry Pi, Beagle bone black ,Asus Tinker Board etc can be a challenging task if you are doing this for the first time. You can follow these steps and you will be able to install OpenCV latest version i.e OpenCV 3.3. 0 on your Raspberry Pi 3.

Can Raspberry Pi be used for image processing? ›

Thanks to low-cost hardware platforms such as Raspberry Pi™, it is now easier than ever to prototype image processing algorithms on hardware. Most image processing algorithms are computationally intensive, and it can be challenging to run them on an embedded platform with acceptable frame rates.

Can you run facial recognition on a Raspberry Pi? ›

Once you have all the packages installed you can jump straight into training the system. Installing all the packages can take some time as the install process for Open-CV and Facial Recognition onto a Raspberry Pi requires in-depth use of the terminal. Below are the contents of this guide.

Can Raspberry Pi run object detection? ›

The fast way to get up and running with object recognition on the Raspberry Pi is to do the following. Flash a micro-SD card with a fresh version of Raspberry Pi OS. Link on how to flash micro-SD with Raspberry Pi OS found here. With the Micro-SD Card flashed you can install it into your Raspberry Pi.

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6145

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.