Get Started The Right Way With OpenCV On Raspberry Pi

OpenCV is often used to promote Raspberry Pi capacities. Even if the Raspberry Pi is a cheap piece of hardware, showing it with a camera, using artificial intelligence and electronics is pretty impressive. But before doing all of this, you have to set it up correctly, and it isn’t straightforward for beginners. That’s why I wrote this tutorial, to help you to get started with OpenCV.

The OpenCV library is now available in the default repository on Raspberry Pi OS and most Linux distributions. For Python, it can be installed with apt, by using the following command: sudo apt install python3-opencv.

Let’s start with a brief introduction to OpenCV and its main features, and then I’ll show you how to install it on your Raspberry Pi and create your first scripts with it.

By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only the essential concepts) required to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun parts. Get 10% off by downloading it today!

What is OpenCV?

OpenCV is an open-source library, compatible with languages like Python or C++, which allows you to build computer vision and machine learning projects.

By the way, CV stands for “Computer Vision”. In short, it’ll provide you with interesting functions to manipulate images and videos. If you are reading this, you have probably already seen some cool projects using it.

Typical examples of OpenCV use are image manipulation (I’ll give you some basic code to do this later), object detection (recognizing a specific type of object, like a face or pet), or object tracking (a robot can follow you, for example).

A classic OpenCV project will use other features from the Raspberry Pi, like the camera module and the GPIO pins. You can for example have the camera detect when your car shows up in front of the garage (object detection), find the license plate, and read the number on it (text recognition). If it matches, you can send a signal to the GPIO pins (or a smart device) to open the garage door.

Well, this is not exactly the easiest project to get started with OpenCV, but it gives you an idea. First, let’s learn how to install the software on your system.

Install OpenCV on Raspberry Pi

Get Raspberry Pi OS

The easiest way to use OpenCV on Raspberry Pi is to use the official operating system: Raspberry Pi OS. It comes with Python preinstalled, so you’ll just need to add the OpenCV library before creating your first script.

If you need some help installing Raspberry Pi OS, you can click on the link to read my tutorial where I explain everything step-by-step. A desktop version is probably better, as we’ll test it with images and the camera module, but pick whatever version makes sense for you.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!

Whether you are installing it from scratch, make sure you do the following steps before going further:

  • Connect to the Internet (via Ethernet or Wi-Fi).
  • Do the system updates:
    sudo apt update
    sudo apt upgrade
  • Enabling SSH and/or VNC is probably a good idea if you want to follow this tutorial from your computer directly.

Once ready, you can move to the OpenCV installation.

Install OpenCV

Installing OpenCV on Raspberry Pi OS can be done with apt directly. Open a terminal and type this command:
sudo apt install python3-opencv

I’ve seen many tutorials where they do the installation from the source code, but it’s pretty complicated and you don’t need to install it that way anymore. This command will install all the dependencies, and it will work directly after that.

I’m going to use Python for this tutorial, as it’s the easiest way to start (everything is ready for Python on Raspberry Pi OS), but OpenCV can also be used with other languages. For example, install libopencv-dev instead to use it with C++.

Are you a bit lost in the Linux command line? Check this article first, for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

Test OpenCV with Python

Once OpenCV is installed on your Raspberry Pi, create a quick test to make sure it’s working as expected. Create a new file and copy the following lines into it:
import cv2

When you run this script, it should display the current OpenCV version installed on your system:

If you experience any error at this point, make sure to double-check the previous steps, as you probably missed something.

I’m using Thonny as it’s preinstalled on Raspberry Pi OS, but you can do the same test in a terminal, via SSH, or on a Lite version of Raspberry Pi OS if you like. Create the file with Nano and run it with:
python3 <>

If you get the version number on your screen, you are ready to start using OpenCV. The main goal of this tutorial was to get you there with OpenCV working in a Python script, but I’ll show you 3 examples to go a bit further.

3 Examples to get started with OpenCV on Raspberry Pi

First script: manipulate images with OpenCV

Most projects using OpenCV are based on image manipulation. It could either be pictures stored on your computer or a live stream from a camera. OpenCV is pretty useful to load them in a script, change something and display or analyze them after that.

If you are entirely new to Python, I highly recommend reading this article first, where I explain the basics of Python on Raspberry Pi. I also think reading my book “Master Python on Raspberry Pi” is almost mandatory before trying any project with OpenCV if you don’t want to get lost quickly.

But anyway, if you have a minimal amount of experience with Python, here are a few methods you can try to get a sense of what OpenCV can do.

The first test you can do is to load a picture saved on your Raspberry Pi (it works with JPG and PNG files).
You can do this with:
Once the picture is loaded, you can use various functions included in the OpenCV library to get more data about the file, like its dimensions for example:

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!

You can show the picture on the screen, by using:
cv2.imshow(<window title>,<image>)
Something like:

The two lines after that are used to keep the window open until you press a key. If you press “Space” for example, it will close the picture and the script will continue.

Another useful function is the “resize” method.
You can either resize the picture to a specific size (like 200x200px in this example):
cv2.resize(image, (x, y))

Or use a coefficient, like 50% of the original size in this example:
cv2.resize(image, (0, 0), fx=X, fy=Y)

Using 0.5 means the new image will be 50% of the original size. You can use a different coefficient for width (X) and height (Y).

Finally, you can store the modified image somewhere on your Raspberry Pi, by using the “imwrite” function, like this:
cv2.imwrite(<filename>, image)

Just make sure to use the same file extension and that you have access to the specified location.

These functions are a good introduction to make sure OpenCV is working properly on your system, and get used to the features you’ll use in most projects. But OpenCV can then be used with artificial intelligence (machine learning especially), the camera module, and GPIO pins to build impressive Python projects. Let’s introduce two of them.

Face recognition: use the Raspberry Pi camera

In short, the idea for that kind of project is to constantly check the camera module stream on the Raspberry Pi, and do something when a face is detected. Once trained correctly, OpenCV can not only detect a face on the camera but also recognize who it is.

Basically, you take 10 to 20 pictures of your face with the camera from various angles and then train OpenCV, so it can recognize you in a live stream. This makes many interesting projects possible, where you can code your Raspberry Pi to do something when you show up.

That was one of the examples given with the PiCar-X robot kit I tested recently. You can find the script they use to do this on the SunFounder documentation. It will draw a rectangle around the detected face, but you can do anything from there.

The best tutorial I have found to go a bit further on a Raspberry Pi is this one from Tim on Core Electronics. You can follow the steps or download the code for the full project directly (so you can test even if not everything is clear for you).

The idea of this simple project is to start a servo motor when his face is detected, but it can be anything (playing music? Turning the lights on in your house? There is no limit).

Help me help them <3
Help me raise funds for education by donating to my Pencils of Promise campaign.
Together we could fund a school for them!

Warning: Most Python scripts using the camera module won’t work on the latest version of Raspberry Pi OS. It’s probably better to still use Raspberry Pi OS Buster (Legacy) to avoid these issues. Check this article for more details.

Object detection/identification

Another fun project you can try is object detection or tracking. It’s basically the same idea, but instead of detecting a face, you’ll try to identify the objects on a picture (or camera stream).

If your Raspberry Pi is mounted on a robot kit, you can even program it to follow this object (that is possible with the Robot Dog kit I also tested on this website). You have a red ball that you can throw, and the robot will follow it.

To get started, I find that this tutorial is pretty good to give you a sense of the possibilities. As with the previous project, you can either follow it step-by-step to try to understand everything or just run the full script to see it in action.

I have many other Python projects listed here if you want to experiment fun things with your Raspberry Pi. And let me know if you need more help with OpenCV, I can write step-by-step tutorials if you are interested.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

FAQ: OpenCV on Raspberry Pi

How to check if OpenCV is already installed on Raspberry Pi?

To check if OpenCV is installed, go to Add/Remove software and search for “OpenCV”. If some packages are installed, there will be a checkbox checked on the left. This command can also be used to list the OpenCV packages installed:
sudo dpkg -l | grep "python-opencv|python3-opencv"

Can I use OpenCV with C++?

OpenCV developers are building it with C++, so OpenCV can be used with most languages, including C++, Java, and Python. On a Raspberry Pi (with any Debian-like distribution), OpenCV can be installed for C++ with:
sudo apt install libopencv-dev.

You can then start your script with:
#include <opencv2/opencv.hpp>
To use OpenCV in a C++ script.
Read this article on my website if you need help to get started with C++ on Raspberry Pi.

How much memory does OpenCV use?

1 GB memory should be enough for most projects using OpenCV on a Raspberry Pi.

Obviously, the amount of memory you need will depend on what your program does (analyzing a high-quality video stream in real-time doesn’t need the same amount as resizing a small picture), but any Raspberry Pi model should be sufficient for most projects.

It may also vary depending on the distribution you use, and other applications

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

Patrick Fromaget

I'm the lead author and owner of My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts