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

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

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

Installing the Python 3 package manager:

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

Installing NumPy:

pip install numpy

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

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__

 

 

 

Info

Project State

Public Project

Licences

Software Licence: Project has no software
Hardware Licence: Project has no hardware

Project Tags

Admins

StudentCV

Does this project pique your interest?

Login or register to join or follow this project.

Comments
matt-amacker report abuse
These worked great! Thanks! One edit: raspi-update should be rpi-update... I think? Thanks!
berkcanacikgoz report abuse
if you are getting stdlib.h not included error, try adding: -D ENABLE_PRECOMPILED_HEADERS=OFF
ahmed-samir report abuse
can any one upload finished image (.img)to Raspberry b+ with opencv3
prateek-aranha report abuse
sudo apt-get update isn't working
WeepingAngel report abuse
prateek-aranha, that is suprising because it is a standard of the updating system. Do you have a more specific error message you can post? (remember many firewalls of companies stop apt-get working properly).
tomas-lapinskas report abuse
In file included from /usr/include/c++/6/bits/stl_algo.h:59:0, from /usr/include/c++/6/algorithm:62, from /home/pi/opencv-3.1.0/modules/core/include/opencv2/core/base.hpp:53, from /home/pi/opencv-3.1.0/modules/core/include/opencv2/core.hpp:54, from /home/pi/opencv-3.1.0/modules/core/include/opencv2/core/utility.hpp:52, from /home/pi/opencv-3.1.0/build/modules/core/precomp.hpp:49: /usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory #include_next
tomas-lapinskas report abuse
pi@raspberrypi:~ $ python3 Python 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 Traceback (most recent call last): File "", line 1, in File "/home/pi/.local/lib/python3.5/site-packages/cv2/__init__.py", line 3, in from .cv2 import * ImportError: libjasper.so.1: cannot open shared object file: No such file or directory >>>
Back to top

Ready to join the project?

You'd like to participate ... Show more