Installing OpenCV 3 on Raspberry Pi 3

Learn how to install OpenCV 3 on your Raspberry Pi 3 running Raspbian Jessie.


Documentation

Installing OpenCV 3

This guide will help you set up the OpenCV 3 image processing library. It's goal is to keep the process short and efficient. For a detailed guide with in-depth explanations check out the instructions here. We will include the Python 3 bindings, as we need them for further projects like the "Face Detection using PyPylon" which you can find here.

Prerequisites

  1. This guide assumes that you're running Raspbian Jessie or a similar derivate (think Ubuntu) on your machine. Make sure Python 3 is already installed, which is the case if you are using Raspian Jessie.
  2. You should have enough free space on your micro-SD card as OpenCV needs a few gigabytes during the compile. An 8 GB SD card should be fine, but a minimum of 16 GB is advised.
  3. Ensure your system's software is up-to-date by running the following commands from your terminal:
    sudo apt-get update
    sudo apt-get dist-upgrade
    sudo raspi-update

Getting the installation process started

  1. Installing libraries and dependencies:

    sudo apt-get install build-essential cmake pkg-config
    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
    sudo apt-get install python2.7-dev python3-dev
  2. Download OpenCV 3.1.0: In the future you might want to change the 3.1.0 in the following code snippet to the newest version. You can check out the newest version here.

    cd ~
    wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
    unzip opencv.zip
    wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
    unzip opencv_contrib.zip
  3. Installing the Python 3 package manager:

    wget https://bootstrap.pypa.io/get-pip.py
    sudo python get-pip.py
  4. Installing NumPy:

    pip install numpy
  5. Compile and install OpenCV 3.1.0: Remember to update the version, if you used another version than specified before. Also, notice that this step can take a couple of hours.

    cd ~/opencv-3.1.0/
    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
        -D BUILD_EXAMPLES=ON ..
    make -j4
    sudo make install
    sudo ldconfig
  6. Testing OpenCV 3 installation: To test the installation it is easiest to open Python and check whether it is possible to import the bindings. As a result this step should show you the installed version which is in this case 3.1.0 or a newer version of OpenCV.

    cd ~
    python3
    import cv2
    cv2.__version__

     

 

 

Does this project pique your interest?
Login or register on Imaginghub to join or follow this project.