Installing OpenCV on the Raspberry Pi

Anyone who has dealt with image processing in relation to the Raspberry Pi will sooner or later come across the OpenCV library. It provides many very useful features such as face recognition, the creation of depth maps (stereo vision, optical flow), text recognition or even for machine learning. In addition, OpenCV (Open Source Computer Vision) can be integrated into both its C ++ files and its Python scripts.
Especially in terms of feature recognition in images taken by the Raspberry Pi, OpenCV is very helpful.

This advanced tutorial shows how to install OpenCV on the Raspberry Pi and how to integrate it into Python.


For the time being, I would like to recommend everyone a Raspberry Pi Model B to use, because this is a lot more powerful than its predecessor. Even on a Raspberry Pi B + compiling takes about 6 times as long, with a Pi 2 in total “only” takes about an hour.


First of all, we update the package lists:

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

A reboot is necessary if it has been updated.

sudo reboot

Then you can install all the important tools and libraries needed for OpenCV (installation takes a few minutes).

sudo apt-get install build-essential git cmake pkg-config libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk2.0-dev libatlas-base-dev gfortran

If everything worked, we could clone OpenCV from git. This step also takes a few minutes.

git clone && cd opencv &&git checkout 3.0.0

Whether version 3.0 or 2.4 of OpenCV is taken is up to you. Depending on the application, one of the versions may be better suited.

Afterwards, OpenCV can be compiled. You can either use Python 2.7 or Python 3+. There are some differences between the versions, especially as some libraries are not (yet) executable with Python 3+. However, this mainly affects smaller libraries, as common libraries (NumPy, SciPy, etc.) usually provide the respective files for both versions.

In this tutorial, I use Python 2.7. If you already have Python installed and want to know which version is installed, you can simply enter python into the console and get the exact version at the beginning (the command for Python 3+ is python3). If you do not have a Python installed, you can install it by following the steps below:

sudo apt-get install python2.7-dev

We also need the package management tool pip, which installs NumPy right away:

cd ~ && wget && sudo python

Now we can simply install via pip NumPy. NumPy is a library that makes it very easy to perform array operations in Python.

pip install numpy


But now to compile OpenCV. For this purpose, a build folder must be created in which the compiled files land:

cd ~/opencv && mkdir build && cd build

 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules 

Now you can finally compile. This step takes (depending on Raspberry Pi model) quite a long time (on my Pi 2 about an hour). To use all four cores to compile on the Raspberry Pi 2, type in the following:

make -j4

If the compilation has worked without problems, we can install OpenCV:

sudo make install && sudo ldconfig


To check if everything worked, you can open the Python console and import the library:

Now you can use all OpenCV functions in your projects. A look at the OpenCV documentation is certainly helpful.

15 Responses

  1. thnks for all this information that help me so much but can you tell me the source of this documentation

  2. I got stuck at the compilation phase.
    when I write cmake -D CMAKE_BUILD_TYPE=RELEASE i get the following error:
    CMake Error: The source directory “/home/pi/opencv/build/CMAKE_BUILD_TYPE=RELEASE” does not exist.
    Specify –help for usage, or press the help button on the CMake GUI.

    please help.

  3. Mark
    did putting the fix your problem ? I have the same problem . I tried with at the end and i dont get a fault but goes to the next line and just get this >
    please help.

  4. When doing this I get a few errors from some modules

    -D CMAKE_INSTALL_PREFIX=/usr/local
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

    also after when running make -j4, I get “make *** no targets specified and no makefile found. stop.”
    What I’m doing wrong?

    • install cmake file using the command “sudo apt-get install build-essential cmake unzip pkg-config” to install the make file

  5. When running the install line for the important tools and libraries for OpenCV, I get an error about that libtiff4-devis not available, but has been replaced by libtiff5-dev. So, I made that substitution and then got the errors that libgtk2.0-dev has unmet dependencies and lists 3 libraries, then states that libtiff5-dev also has an unmet dependency on libjpeg-dev. All followed by the line: “E: Unable to correct problems, you have held broken packages.” Will this cause problems with the later steps?

  6. Where can we get help with the OpenCV compilation for Raspbian? I cannot get these instructions to compile with my Raspberry Pi 3B.

  7. I keep getting this error anytime I try to compile OpenCV … someone please help ?

    Generating precomp.hpp.gch/opencv_imgcodecs_RELEASE.gch
    In file included from /home/pi/opencv/modules/hal/include/opencv2/hal/defs.h:329:0,
    from /home/pi/opencv/modules/core/include/opencv2/core/cvdef.h:59,
    from /home/pi/opencv/modules/core/include/opencv2/core.hpp:52,
    from /home/pi/opencv/modules/imgcodecs/include/opencv2/imgcodecs.hpp:46,
    from /home/pi/opencv/build/modules/imgcodecs/precomp.hpp:45:
    /usr/include/c++/6/cmath:45:23: fatal error: math.h: No such file or directory
    compilation terminated.
    modules/imgcodecs/CMakeFiles/pch_Generate_opencv_imgcodecs.dir/build.make:62: recipe for target ‘modules/imgcodecs/precomp.hpp.gch/opencv_imgcodecs_RELEASE.gch’ failed
    make[2]: *** [modules/imgcodecs/precomp.hpp.gch/opencv_imgcodecs_RELEASE.gch] Error 1
    CMakeFiles/Makefile2:5803: recipe for target ‘modules/imgcodecs/CMakeFiles/pch_Generate_opencv_imgcodecs.dir/all’ failed
    make[1]: *** [modules/imgcodecs/CMakeFiles/pch_Generate_opencv_imgcodecs.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….
    [ 20%] Linking CXX static library ../../lib/libopencv_stitching_pch_dephelp.a
    [ 20%] Linking CXX static library ../../lib/libopencv_test_stitching_pch_dephelp.a
    [ 20%] Linking CXX static library ../../lib/libopencv_perf_stitching_pch_dephelp.a
    [ 20%] Built target opencv_stitching_pch_dephelp
    [ 20%] Built target opencv_test_stitching_pch_dephelp
    [ 20%] Built target opencv_perf_stitching_pch_dephelp
    Makefile:149: recipe for target ‘all’ failed
    make: *** [all] Error 2

  8. I see all tutorials explaining how to install OpenCV by compiling the sources without ever mentioning there is the easy and natural way of sudo apt-get install python-opencv

    Not against compiling unless it takes >20 hours on a PiZero, and all top results in google “install opencv on raspberry pi” do not mention the easy way.

    Beginners get in a tedious compiling process without ever knowing there is an already compiled package.


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!