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)

We will explore a couple of ways to install OpenCV on your Raspberry Pi. The first and easiest way is to install it directly from the package repository. This gets youan older version, but it means you will be up and running in a few minutes.

The second and slightly more complicated way is to compile it directly from its source code. Using this method, you will have the latest version of OpenCV on your Pi, but you mustbe preparedto wait an hour for the library to compile.

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 setting open OpenCV on the Raspberry Pi.

Recommended

  • Raspberry Pi ( Amazon | SunFounder )
  • Micro SD Card ( Amazon | SunFounder )
  • Power Supply ( Amazon | SunFounder )
  • Ethernet Cable ( Amazon ) orWi-Fi ( Amazon | SunFounder )
  • Raspberry Pi camera ( Amazon | Sunfounder ) or USB Webcam ( Amazon )

Optional

  • Raspberry Pi Case ( Amazon | SunFounder )
  • USB Keyboard ( Amazon | SunFounder )
  • USB Mouse ( Amazon | SunFounder )

This tutorial was tested using the latest version of Raspberry Pi OS Bookworm on a Raspberry Pi 5.

Installing OpenCV on your Raspberry Pi from the Repository

If you aren’t worried about running the latest version of OpenCV on your Raspberry Pi, you can install this library directly from the official package repository.

This method is significantly faster to get up and running, as you don’t have to wait for the library to compile. The disadvantage, of course, is that you could be running quite an old version of OpenCV.

If you want to run the latest version of OpenCV, follow our section on compiling this library from its source code.

1. Before we can install OpenCV we must ensure that our package list and packages are up to date.

The first command will update the package list cache. The second will upgrade any out-of-date packages it finds.

sudo apt updatesudo apt upgrade

2. Once your system is up to date, all you need to do to install OpenCV to your Raspberry Pi is run the following command.

At the time of publishing, this will install OpenCV 4.6 to your Pi.

sudo apt install python3-opencv

3. Now that you have OpenCV installed, you can skip down to the testing OpenCV section.

Compiling OpenCV on the Raspberry Pi

Over the following sections, we will be walking you through the process of compiling the latest version of OpenCV on your Raspberry Pi.

While compiling from the source code is a slow process, it will give you access to all of OpenCV’s latest features on your Pi.

Alternatively, OpenCV is available through the Pi’s package repository. However, this version is typically significantly out of date.

Installing the Packages Required to Compile OpenCV on your Pi

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 ccache build-essential pkg-config git -y

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 ffmpeg libjpeg-dev libtiff-dev libpng-dev libwebp-dev libopenexr-dev -ysudo apt install libopenjp2-7-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libdc1394-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev -y

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 -y

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 -y

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 -y

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 -y

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

Increasing your Pi’s Swap Size

8. 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

9. While we are within this file, we need to find the following line that defines the size of the swap.

CONF_SWAPSIZE=100

Once you have found the line above, adjust the “100” value to “2048“. This will create a swap file on your Pi’s SD card that is 2GB in size.

CONF_SWAPSIZE=2048

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

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

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

sudo systemctl restart dphys-swapfile

Cloning the OpenCV Source Code to your Raspberry Pi

12. 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

13. 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

14. 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 WITH_OPENMP=ON \ -D ENABLE_VFPV3=OFF \ -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=/usr/lib/python3/dist-packages \ -D BUILD_EXAMPLES=OFF ..

15. 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 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

16. When the compilation process finishes, we can then move on to installing OpenCV to our Raspberry Pi.

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.

17. 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

18. Now that we have finished compiling OpenCV on our Raspberry Pi, 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

19. Within this file, find the following line.

CONF_SWAPSIZE=2048

After finding the swap size line, change the value back to its default which at the time of publishing is “100“.

CONF_SWAPSIZE=100

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

21. 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.9.0'

Conclusion

Hopefully, at this point you will now have OpenCV up and running on your Raspberry Pi.

This library gives you access to a wealth of free and open-source tools you can use to implement computer vision on your Raspberry Pi.

Please feel free to drop a comment below if you have you have run into any issues or have any feedback on this tutorial.

If you like this guide, we highly recommend that you check out our many other Raspberry Pi projects.

Recommended

Raspberry Pi Chromecast Alternative using Raspicast

Basic Guide to Voltage Dividers

Installing Nagios on the Raspberry Pi

Raspberry Pi Accelerometer using the ADXL345

Why Not to Use chmod 777

How to Display your Robot Vacuum in Home Assistant

Get OpenCV running on the Raspberry Pi (2024)

FAQs

Get OpenCV running on the Raspberry Pi? ›

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 to check OpenCV on Raspberry Pi? ›

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. ...
  2. While we are within Python, we can now import the OpenCV Python module using the command below. ...
  3. With the OpenCV module now imported, we should be able to retrieve its version.
May 15, 2024

How to check if OpenCV is installed? ›

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 install OpenCV in Raspberry Pi 4 terminal? ›

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 install OpenCV Python in Raspberry Pi 3? ›

  1. Step 1: make sure you have the latest version of OS. ...
  2. Step 2: configure SSH and utilities. ...
  3. Step 3: free up 1GB+ by ditching Wolfram and Libreoffice. ...
  4. Step 5: install Python 2.7 & 3. ...
  5. Step 6: get the latest OpenCV source code. ...
  6. Step 7: install pip and virtualenv. ...
  7. Step 9: install Numpy, Scipy. ...
  8. Step 10: finally, install OpenCV.

How do I see what's running on my Raspberry Pi? ›

1 Answer
  1. Viewing running programs: In the terminal type: ps aux | grep programname. ps = display currently running processes. a = show processes for all users. u = display the process' user/owner. x = show processes not attached to a terminal. ...
  2. Viewing the currently open terminal session over SSH. Use tmux or screen.
Aug 25, 2016

Does OpenCV run on Raspberry Pi? ›

There are many ways for installing OpenCV on Raspberry Pi, you can install with pip on a virtual environment, install it using apt install or compile the software from source.

Where is OpenCV located in Linux? ›

We do not recommend system-wide installation to regular users due to possible conflicts with system packages. By default OpenCV will be installed to the /usr/local directory, all files will be copied to following locations: /usr/local/bin - executable files.

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 is OpenCV DLL located? ›

dlls should be available under the install\bin folder of the build directory (e.g C:\path\to\opencv-2.4. 5\build\install\bin ). As noted earlier, the . dlls in this folder need to placed in the same folder as TITARL.exe .

How long 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.

Which command is used to install OpenCV? ›

pip install --no-binary opencv-python opencv-python. pip install --no-binary :all: opencv-python.

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 install OpenCV Python in python3? ›

OpenCV-Python Installation
  1. Download and install anaconda environment Python 3.7: Download: https://www.anaconda.com/download/#windows. ...
  2. Open Anaconda Prompt. Start Menu / Anaconda3 / Anaconda Prompt.
  3. In Anaconda Prompt, type commands to install necessary libraries: pip install opencv-python==3.4.2.17. ...
  4. Run your python program.

How do I check my Raspberry Pi RAM terminal? ›

Find how much RAM from the Raspberry Pi terminal

If you don't have access to the desktop (or maybe you just want to SSH into your Pi), open a terminal and run the “top” or “htop” command. If you run “top”, you'll see “MiB Mem :” with a value, that will be the total amount of RAM in MB.

How do I know if VNC is running on Raspberry Pi? ›

RealVNC Server is included with Raspberry Pi OS (formerly Raspbian) but you still have to enable it. If not, and you're already booted into the graphical desktop, select Menu > Preferences > Raspberry Pi Configuration > Interfaces and make sure VNC is set to Enabled.

What is the command to check Raspberry Pi model? ›

If you already have your Raspberry Pi running, you can find out the model of your Raspberry Pi by going to the terminal and issuing cat /proc/cpuinfo and looking up the last four digits in the table here. This will tell you which board, board revision, RAM and manufacturer all in one fell swoop.

How do I read OpenCV? ›

The steps to read and display an image in OpenCV are:
  1. Read an image using imread() function.
  2. Create a GUI window and display image using imshow() function.
Mar 29, 2023

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6007

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.