• First Steps
  • General
  • Projects
Facebook Twitter YouTube
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
  • Hardware & GPIO
  • Sensors & Components
  • Scale
  • Cases
  • Fingerprint
  • Gas Sensor
  • Heartbeat
  • German
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
Home»Projects»Installing OpenCV on the Raspberry Pi

Installing OpenCV on the Raspberry Pi

Facebook Twitter LinkedIn Tumblr Email Reddit
Raspberry Pi OpenCV
Share
Facebook Twitter LinkedIn Email Tumblr Reddit Telegram WhatsApp

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 https://github.com/Itseez/opencv.git && 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 https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py

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

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

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

Done!

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

Python
1
2
import cv2
cv2.__version__

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

bathymetric charts Calibration Computer Vision face detection opencv 3.0.0 python Stereo Vision text detection
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleRaspberry Pi: Control Relay switch via GPIO
Next Article Measuring Soil Moisture with the Raspberry Pi

Related Posts

Control all GPIOs with the Raspberry Pi REST API via Python

Using TensorFlow Lite with Google Coral TPU on Raspberry Pi 4

Raspberry Pi Samba Server: Share Files in the Local Network

Build Live Text Recognition with the Raspberry Pi (OCR)

40 Comments

  1. abderrazek on 8. August 2018 4:20

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

    Reply
  2. Mark on 30. August 2018 20:11

    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.

    Reply
    • Ian on 31. August 2018 6:14

      Mark, put a on the end of each of the cmake lines (except the last one).

      Reply
      • Mark on 19. September 2018 12:18

        tnx

      • nico on 10. November 2018 17:15

        put a what?

      • anu on 19. April 2019 8:04

        cmake -D CMAKE_BUILD_TYPE=RELEASE
        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

      • Ian on 22. July 2021 3:33

        Put a backslash on the end of multi-line commands, except the last line.

        Sorry, it got edited out and my attempt to subscribe failed so I was blissfully unaware. (It was only three years ago…)

  3. Antonio on 10. September 2018 22:39

    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.

    Reply
    • Kiran Pai on 6. August 2021 15:27

      i am also facing same problem. Any suggestion please

      Reply
  4. Bjucha on 18. September 2018 10:26

    When doing this I get a few errors from some modules

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

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

    Reply
    • anuroop katukam on 20. January 2019 20:23

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

      Reply
      • Friday on 21. April 2019 14:42

        It still give “make: *** No targets specified and no makefile found. Stop.”

  5. Diana on 21. September 2018 8:21

    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?

    Reply
    • tony on 3. December 2019 12:50

      hi dianna, i get the same error, how did you fix it?

      Reply
  6. Frank A Gould on 26. September 2018 21:13

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

    Reply
    • Frank Gould on 3. October 2018 12:56

      Nevermind. I’m migrating to Ubuntu-Mate and hope that works.

      Reply
      • David Ford on 29. November 2018 7:30

        Did Ubuntu-Mate work?

  7. Elijah on 20. November 2018 1:13

    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
    #include_next
    ^
    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

    Reply
  8. cezar on 3. February 2019 13:41

    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.

    Reply
    • wagner_sarto on 12. March 2019 20:01

      parabens cezar, por mais pessoas como você!
      abraço

      Reply
    • Chris on 29. July 2021 9:49

      The best advice, thank you Cezar! https://pypi.org/project/opencv-python/ worked well for me on RPi 4 Model B, requiring just an upgrade of numpy to the latest version

      Reply
  9. shreyash on 1. April 2019 7:39

    E: Package ‘libtiff4-dev’ has no installation candidate
    how to solve this Error while installing : “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” this command on Rpi3B.

    Reply
  10. Peter Mason on 8. April 2019 12:28

    I cannot get the compile to work. It failed at the make as above.
    sudo apt-get install python-opencv does not work either.
    Python Test Code
    import cv2
    cv2 –version
    Error message Error: No module named ‘cv2’
    Is there anyone who can help please

    Reply
  11. Peter Mason on 8. April 2019 13:09

    https://gist.github.com/willprice/abe456f5f74aa95d7e0bb81d5a710b60#file-build-opencv-sh
    This script may be useful

    Reply
  12. RAMI SGH on 18. April 2019 1:27

    I keep getting this error anytime I try to compile OpenCV … someone please help ?
    Scanning dependencies of target pch_Generate_opencv_stitching
    [ 82%] Generating precomp.hpp
    [ 82%] Generating precomp.hpp.gch/opencv_stitching_RELEASE.gch
    In file included from /home/pi/opencv-4.0.1/build/modules/stitching/precomp.hpp:90:0:
    /home/pi/opencv-4.0.1/modules/core/include/opencv2/core/private.hpp:871:1: fatal error: can’t write PCH file: No space left on device
    } // namespace cv
    ^
    compilation terminated.
    modules/stitching/CMakeFiles/pch_Generate_opencv_stitching.dir/build.make:62: recipe for target ‘modules/stitching/precomp.hpp.gch/opencv_stitching_RELEASE.gch’ failed
    make[2]: *** [modules/stitching/precomp.hpp.gch/opencv_stitching_RELEASE.gch] Error 1
    CMakeFiles/Makefile2:23170: recipe for target ‘modules/stitching/CMakeFiles/pch_Generate_opencv_stitching.dir/all’ failed
    make[1]: *** [modules/stitching/CMakeFiles/pch_Generate_opencv_stitching.dir/all] Error 2
    Makefile:160: recipe for target ‘all’ failed
    make: *** [all] Error 2

    Reply
  13. anusha on 19. April 2019 8:05

    cmake -D CMAKE_BUILD_TYPE=RELEASE
    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

    Reply
    • Rahul Itnal on 16. August 2019 12:14

      just use
      (cmake ..)
      i got is from “https://stackoverflow.com/questions/35784700/cmake-error-when-building-opencv-cmakelists-not-match”

      Reply
      • Rahul Itnal on 16. August 2019 13:33

        cmake -D CMAKE_BUILD_TYPE=RELEASE
        -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-“$cvVersion”
        -D INSTALL_C_EXAMPLES=ON
        -D INSTALL_PYTHON_EXAMPLES=ON
        -D WITH_TBB=ON
        -D WITH_V4L=ON
        -D OPENCV_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages
        -D WITH_QT=ON
        -D WITH_OPENGL=ON
        -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules
        -D BUILD_EXAMPLES=ON ..

  14. Jasmeet Singh on 5. June 2019 23:20

    hey!! I have a raspberry pi 3 model B. I have used it a few times and it was working fine with no problems. I wanted to install opencv for a project and after I followed all the three command sudo apt-get update, sudo apt-get upgrade and sudo rpi-update and all these commands worked with no error. But I am getting a problem while rebooting. After rebooting, I get the rainbow screen and then raspberry logo and the console after which the screen goes blank with nothing but a cursor blinking at the top. The power led stays on without blinking while the act led keeps on blinking and nothing happens.
    I am using a 8 GB micro sd card. So, do you have any ideas on what the problem is and how can I solve it?

    Reply
    • arjun on 8. March 2021 14:30

      Use 16gb class 10 micro sd card if not solved try chanfing dapter atleat 12v and 2 amp.

      Reply
  15. Jeff Cacossa on 2. July 2019 21:05

    Add backslashes to the end of every line but the last one

    Reply
  16. Jeffrey Chang on 27. July 2019 12:40

    You need to keep backslashes clean without any white spaces until end of line.

    Reply
  17. Rahul Itnal on 16. August 2019 12:16

    just use
    (cmake ..)
    i got is from “https://stackoverflow.com/questions/35784700/cmake-error-when-building-opencv-cmakelists-not-match”

    Reply
  18. Dants on 30. October 2019 9:46

    What modifications do I need to make if I want to install to a virtual environment ?

    Reply
  19. DS on 22. December 2019 17:34

    when i try install libtiff4-dev it tells me there is no such thing but libtiff-dev replaces it
    will this work?

    Reply
  20. kalyan on 14. February 2020 12:24

    you are a saviour bro!!! thank you

    Reply
  21. stranger on 15. May 2021 10:20

    I get this error when i try cmake -D CMAKE_BUILD_TYPE=RELEASE :

    CMake Warning at CMakeLists.txt:1178 (message):
    The source directory is the same as binary directory. “make clean” may
    damage the source tree

    — Configuring incomplete, errors occurred!
    See also “/home/pi/opencv/CMakeFiles/CMakeOutput.log”.
    See also “/home/pi/opencv/CMakeFiles/CMakeError.log”.

    Reply
  22. Shilan on 27. July 2021 21:00

    I followed every step and got no error during the process. But still when I try to import cv2 I get
    ModuleNotFoundError

    Reply
  23. nxyzuiimii on 10. April 2025 3:45

    nmjgwxqeojszlwyeaqxjrtueslwydx

    Reply

Leave A Reply Cancel Reply

Build a digital Raspberry Pi Scale (with Weight Sensor HX711)

How to setup a Raspberry Pi Security Camera Livestream

Controlling the Raspberry Pi RTC Module – I2C Real Time Clock

Tips & Tricks (FAQ) to the official 7″ Raspberry Pi Touchscreen

Build Your Own Raspberry Pi Weather Station with OpenHAB 2

PiCar-X: Build a smart, self-driving robot car with the Raspberry Pi

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

Thank you!

We will contact you soon.

Tutorials for Raspberry Pi
Facebook Twitter YouTube
  • Contact & Disclaimer

Type above and press Enter to search. Press Esc to cancel.