Installing OpenCV on Raspberry Pi 3 B+ for Python 2.7

Dec. 30, 2018, 8:06 p.m.

OpenCV is an awesome Python module for playing with images or videos. It has got a lot of features for image processing such as reading, writing and capturing image from a camera. It has functions for almost all the image processing algorithms. Here we will be installing OpenCV for Python 2.7 on Raspberry Pi 3 B+

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

 -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


New York

Embedded Systems

Dec. 30, 2018, 11:18 a.m.

New York

Internet of Things

Dec. 30, 2018, 11:19 a.m.

New York

ML in Computer Vision

Dec. 30, 2018, 11:20 a.m.



Need help?


Want to convey anything? Drop a Message.

Mumbai, India
Phone: +91 9555551613
Email: ayamamit@gmail.com

.iframeclass{ position: absolute; top: 0; width: 100%; } .iframecontainer{ position: relative; width: 100%; height: auto; padding-top: 61%; }