How to Install OpenCV on a Raspberry Pi (2024)

Last Update: Apr 4, 2023

How to Install OpenCV on a Raspberry Pi (1)

OpenCV is an incredible computer vision library, and the Raspberry Pi is an awesome piece of hardware. You can bring them together for your projects. In fact, I am writing this article as a reference so you folks can set up your Pi with OpenCV for a series of tutorials I’m doing.

We’re going to install OpenCV on the Raspberry Pi for Python projects. Note: There are two ways to install this:

  • Pip install (30 seconds)
  • Compile from source (hours)

We are covering the Pip install here because it’s fast and easy. It will work for most OpenCV projects, and it’s an easy solution. However, there will be libraries and Non-Free algorithms that are not installed with Pip. So if you want the FULL library, you’ll need to do build OpenCV from source. For most people, the Pip install should be just fine.

You may also notice we’re installing OpenCV with the contrib modules included. You can do a smaller opencv-python installation if you choose.

This is just one of many Raspberry Pi Projects you can do to learn more about development and IoT.

Let’s get started.

Step 1: Set Up Your Raspberry Pi

The first thing you should do is make sure your Pi is using the full SD card. It’s easy enough to do.

sudo raspi-config

How to Install OpenCV on a Raspberry Pi (2)

How to Install OpenCV on a Raspberry Pi (3)

Reboot the machine

sudo reboot

Now you can check to make sure you’ve got the space:

df -h

How to Install OpenCV on a Raspberry Pi (4)

Cool, we’re ready. Let’s update the system:

sudo apt-get update && sudo apt-get upgrade

This is all you’ll need to do the Pi for now to get it ready.

Step 2: Get Pip and Install OpenCV

Now, we’ll get pip and use that to install OpenCV.

mkdir ~/src && cd ~/srcwget https://bootstrap.pypa.io/get-pip.py$ sudo python3 get-pip.py

There are two ways to install OpenCV. You may want to install it for the whole system or use a Python Virtual Environment.

I suggest using the Python Virtual Environment method because it’s more portable, and you can use different libraries with different projects on the same machine. There isn’t a lot of overhead to using the virtual environment method, but I’ll show both ways:

Option 1: Install OpenCV for the Whole System:

sudo pip install opencv-contrib-python

And you’re done. You

Or,

Option 2: Install OpenCV in a Python Virtual Environment:

sudo pip install virtualenv virtualenvwrapper
vim ~/.bashrc

How to Install OpenCV on a Raspberry Pi (5)

add the following lines to the bottom:

export WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3source /usr/local/bin/virtualenvwrapper.sh

Save the file, and run source on your .bashrc to reload the changes:

source ~/.bashrc

Now, you can create a virtual environment named pistats (or whatever you like)

mkvirtualenv pistats -p python3

Then install OpenCV:

pip install opencv-contrib-python

How to Install OpenCV on a Raspberry Pi (6)

Ok, now we’re up and running.

What Can I Do With This?

OpenCV is an amazing image analysis and processing package. It’s used for some serious computer vision projects.

The Raspberry Pi is a complete Linux computer in a small package. So the possibilities are endless with this:

The list can go on forever. Here’s a good list of OpenCV projects you can check out.

Want to learn more about the Raspberry Pi? Check out the Raspberry Pi for Developers course!


Published: Nov 7, 2020 by Jeremy Morgan. Contact me before republishing this content.

How to Install OpenCV on a Raspberry Pi (2024)

FAQs

How to Install OpenCV on a 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 do I install OpenCV on my Raspberry Pi? ›

Install & Setup OpenCV on Raspberry Pi
  1. Step 1: Install dependencies.
  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 step by step? ›

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.

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 install OpenCV 4.2 0? ›

OpenCV-4.2. 0 for Python
  1. Step 1: Create a conda virtual environment for OpenCV. conda create --name opencv-env python=3.6. ...
  2. Step2: Install OpenCV and the required packages. To use OpenCV you have to install some important packages that go alongside: ...
  3. Step3: Test your installation.

How to install OpenCV in python3 in Raspberry Pi? ›

What make this messy is at Step 1 or possibly Step 2.
  1. Step 1: Dependencies *** This will be different between installation guide. ...
  2. Step 2: Python 3 Numpy. $ pip3 install numpy. ...
  3. Step 3: Download OpenCV. Just download and unzip. ...
  4. Step 4: Cmake. $ cd ~/opencv-3.4.1/ ...
  5. Step 5: Compile. make -j4. ...
  6. Step 6: Done. $ python3.
Mar 22, 2020

Can you run OpenCV on 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.

Which command is used to install OpenCV? ›

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

How to install OpenCV Python manually? ›

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 to install OpenCV into Python? ›

Building OpenCV from source
  1. Download and install Visual Studio and CMake. ...
  2. Download and install necessary Python packages to their default locations. ...
  3. Make sure Python and Numpy are working fine.
  4. Download OpenCV source. ...
  5. Extract it to a folder, opencv and create a new folder build in it.

Which Raspberry Pi for OpenCV? ›

To install OpenCV on Raspberry Pi, you need Raspberry Pi ( Pi2 preferably) , Pi camera, and internet connection to Raspberry Pi. Now since your SD card is ready, Boot your Pi. First thing you want to do is Enable Camera.

How do I know if OpenCV is installed on my 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.
Sep 30, 2022

How to install OpenCV in Raspberry Pi Zero? ›

How to install opencv in Raspberry Pi Zero W
  1. Update your Raspberry Pi: sudo apt-get update ,sudo apt-get upgrade.
  2. Install dependencies required for building OpenCV: sudo apt-get install build-essential cmake pkg-config. ...
  3. Download and install OpenCV: cd ~ ...
  4. Build and install OpenCV: ...
  5. To verify the installation: ...
  6. Another way:
Jun 19, 2023

How do I install a new version of OpenCV? ›

Downloading and Installing OpenCV:
  1. Type the command in the Terminal and proceed:
  2. Collecting Information and downloading data:
  3. Installing Packages:
  4. Finished Installation:
Jan 18, 2023

Where is my OpenCV installed? ›

By default OpenCV will be installed to the /usr/local directory, all files will be copied to following locations:
  1. /usr/local/bin - executable files.
  2. /usr/local/lib - libraries (. ...
  3. /usr/local/cmake/opencv4 - cmake package.
  4. /usr/local/include/opencv4 - headers.

Does OpenCV work on Raspberry Pi 4? ›

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.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5738

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.