The Ultimate Guide to Installing OpenCV on Raspberry Pi

A step-by-step guide to installing OpenCV on your Raspberry Pi with ease. OpenCV is a powerful library for computer vision and image processing, making it an essential tool for any Raspberry Pi projec …


Updated August 15, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


A step-by-step guide to installing OpenCV on your Raspberry Pi with ease. OpenCV is a powerful library for computer vision and image processing, making it an essential tool for any Raspberry Pi project. Follow this tutorial to get started!

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is a popular library used for computer vision and image processing tasks. It provides a wide range of functions to manipulate, analyze, and process images and videos. It has gained immense popularity in the field of robotics, drones, and self-driving cars due to its ability to perform real-time object detection, recognition, and tracking.

Why Install OpenCV on Raspberry Pi?

Raspberry Pi is a low-cost, high-performance computer that has gained popularity among hobbyists, educators, and developers for its versatility and ease of use. Installing OpenCV on your Raspberry Pi gives you access to a powerful toolkit for working with images and videos, enabling you to build all sorts of interesting projects such as:

  • Object detection in security systems
  • Facial recognition software
  • Image processing applications like image stitching, filtering, or color manipulation
  • Video surveillance systems
  • Motion tracking and object tracking for autonomous robots
  • And much more!

Requirements

Before you begin, make sure your Raspberry Pi is running the latest version of Raspbian OS. You can update it by running the following command in terminal:

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

Also, ensure that you have sufficient disk space available on your Raspberry Pi to install OpenCV and its dependencies. At least 1GB of free space is recommended.

Step 1: Install Dependencies

OpenCV requires several dependencies to be installed before it can run properly on the Raspberry Pi. Run the following commands in terminal to install them:

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran

Step 2: Download OpenCV

OpenCV can be downloaded from its official GitHub repository using Git. Run the following command to clone the repository:

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

This will download the latest version of OpenCV into a directory named opencv in your current working directory.

Step 3: Build OpenCV from Source

Navigate to the opencv directory:

cd opencv

Create a new build directory and move into it:

mkdir build && cd build

Run CMake to configure the build process, specifying that you want to build OpenCV for Python 3:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

If you’re using Python 2, replace PYTHON3 with PYTHON2.

Now run make to build OpenCV:

make -j4

The -j4 flag tells the compiler to use 4 threads for parallel compilation. You can increase or decrease this number depending on your Raspberry Pi’s processing power and available memory.

Once the build process is complete, install OpenCV using:

sudo make install && sudo ldconfig

Step 4: Verify the Installation

To verify that OpenCV has been installed correctly, run the following command in terminal:

python3 -c 'import cv2; print(cv2.__version__)'

If you see the version number of your installed OpenCV package, then congratulations! You have successfully installed OpenCV on your Raspberry Pi.

Conclusion

Installing OpenCV on Raspberry Pi is a simple and straightforward process. With this powerful library at your disposal, you can create all sorts of interesting projects that involve computer vision or image processing. Whether you’re working on autonomous robots, security systems, or image-based applications, OpenCV has you covered!