Skip to content

Tutorials

Raspberry Pi 3 and Opencv 3 Installation Tutorial

We’re going to see today how to install opencv on the raspberry pi 3 Model b+ (with camera).

1) Update os system:

sudo apt-get update
sudo apt-get upgrade

2) Install dependencies:

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 libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran

3) Install Python 3 and Pip3:

sudo apt-get install python3-dev
sudo apt-get install python3-pip

4) Install Opencv:

pip3 install opencv-python

5) Extra depencies for Opencv and the Camera:

sudo apt-get install libqtgui4
sudo modprobe bcm2835-v4l2
sudo apt-get install libqt4-test

34 comments

  1. HI, I tried to install open cv in my raspberry pi using your commands but I’m getting errors pls help me to install open cv and other packages open cv was installed in my pi but getting an error while importing cv2, and imutils, I trayed the code
    sudo apt-get install libqtgui4 install open cv still its showing error pls help me it install open cv and other packages imutils , requests , json , base64

  2. Hello sir ,
    I’m trying to install opencv on rpi 3 b+. while putting “pip3 install opencv-python” it shows like this
    [
    Collecting opencv-python
    Could not find a version that satisfies the requirement opencv-python (from versions: )
    No matching distribution found for opencv-python
    ]
    what should i do?

    Thanks,
    Mugil

    1. It could be that raspberry is not able to connect to the server to download an opencv version compatible.

      I found that a problem can be that the time of your raspberry pi isn’t synchronized with the real time and the server refuses the connection.

  3. Hi Sergio, thanks for this tutorial.
    I’m also installing on a Pi 3 B+, and the dependency steps took me around the same time as yours did on the video.
    I started step 4 and it has proceeded to the point that you paused the video. So I’m wondering how long your step 4 took?
    My Pi has been running for several hours with no change to the screen, and I’m wondering if I should be leaving it running? The Pi is clearly doing something as even moving the mouse cursor takes a while to respond.

    Thanks.

  4. Hi Sergio,

    Ignore that last comment. I powered the Pi off and back up, repeated the steps to make sure I hadn’t mistyped anything and this time it worked! And that step 4 only took a few minutes this time.
    I am now happily detecting faces and eyes, and just need to work out why I can’t detect smiles and I’ll be happy. Perhaps it doesn’t like my face *grin*.

    Thanks.

  5. Hi Sergio,
    I installed opencv using your tutorial and it installed correctly as I am able to import cv2 in python idle 3 as well as in terminal.
    But, my problem is little bit different. When I run below code it shows error.
    import cv2
    import numpy as np
    import imutils
    from imutils.video import WebcamVideoStream
    from imutils.video import VideoStream
    cam1 = WebcamVideoStream(src=0).start()
    cam2 = WebcamVideoStream(src=2).start()
    while (1):
    frame1 = cam1.read()
    cv2.imshow(“frame1”,frame1)
    frame2 = cam2.read()
    cv2.imshow(“frame2”,frame2)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
    break
    cam1.release()
    cam2.release()
    cv2.destroyAllWindows()

    It shows error :
    VIDIOC_QBUF: Invalid argument
    VIDIOC_QBUF: Invalid argument
    Traceback (most recent call last):
    VIDIOC_QBUF: Invalid argument
    File “camm11.py”, line 14, in
    cv2.imshow(“frame1”,frame1)
    cv2.error: OpenCV(3.4.4) /home/pi/packaging/opencv-python/opencv/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

    I am trying to run two webcams using raspberry pi 3 B+. I am using USB power hub to connect webcams.
    Please help me how to solve this error.
    Thank you.

  6. Hi, nice code! but in my raspberry pi 3 plus i recive this message when i put (pip3 install opencv-python)

    ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permiso denegado: ‘/usr/local/lib/python3.5/dist-packages/cv2’
    Consider using the `–user` option or check the permissions.

    Any body can i help me?

    Thanks

  7. Hi Sergio, thanks for this amazing tutorial. I have just one error when i run the command ” sudo modprobe bcm2835-v4l2 ” it tells me
    modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file ‘/lib/modules/4.14.98-v7+/modules.dep.bin’
    modprobe: FATAL: Module bcm2835-v4l2 not found in directory /lib/modules/4.14.98-v7+

    I’ll use OpenCV to recognize a circle taking the Raspberry Pi Camera as input. ¿Do I really need this command to my purpose? Thanks

  8. Hi Sergio.
    Few days ago I’ve finished your tutorial and I can used my Opencv. now when I try to use it again, my Opencv couldn’t be used. Can you help me?

  9. pls help, after follow all the install step, still error
    import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/local/lib/python3.7/dist-packages/cv2/__init__.py”, line 3, in
    from .cv2 import *
    ImportError: /usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8

    1. I found a workaround on github. Try to call your script that way:

      LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1 python3 your_script.py

    2. this is because the latest version of openCV doesn’t work with rasPI. Instead of pip3 install opencv-python. Try: pip3 install opencv-python==3.4.6.27
      You will need to uninstall the latest version first: pip3 uninstall opencv-python

  10. Please help me!
    Traceback (most recent call last):
    File “/home/pi/Desktop/opencv_test.py”, line 1, in
    import cv2
    File “/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py”, line 3, in
    from .cv2 import *
    ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8
    >>>

    1. I found a workaround on github. Try to call your script that way:

      LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1 python3 your_script.py

  11. i do everything in your instruction but still can import the ‘cv2’ heres the error:

    pi@raspberrypi:~ $ python3
    Python 3.7.3 (default, Apr 3 2019, 05:39:12)
    [GCC 8.2.0] 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.7/site-packages/cv2/__init__.py”, line 3, in
    from .cv2 import *
    ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8

      1. I found a workaround on github. Try to call your script that way:

        LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1 python3 your_script.py

Join the discussion