Image Processing based on OpenCv with Python on Raspbian, Raspberry Pi

First Steps with Image processing based on Rasberry Pi, OpenCVPython 3.6.1 on Debian Jessie

Python environment, libraries


Update Python 2.7

update setup tools

sudo apt-get install python-setuptools

install packets tool

sudo easy_install pip

How to install Python 3.6.1 on Raspbian Jessie

sudo echo 'deb testing main contrib non-free rpi' > /etc/apt/sources.list.d/stretch.list
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get autoremove

First of all you need to get your dependencies right. That mostly depend on what you have already installed previously. So for a vanilla fresh Raspbian (jessie), you will (approximately) need to make sure you have these:

sudo apt-get install build-essential libc6-dev
sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
sudo apt-get install libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev
sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev

The rest is simple. First download and extract…

cd $HOME
tar -zxvf Python-3.6.1.tgz

compile with

cd Python-3.6.1 ./configure --enable-optimizations 

We have 4 processors, so let’s use 4 threads or

make -j4


sudo make altinstall

Save your SD card space

cd ..
sudo rm -fr ./Python-3.6.1*

Now test with

python3.6 -V
# "Python 3.6.1"

pip3.6 list


pip (9.0.1)
setuptools (28.8.0)

 if is necessary to install pip , a Python package manager

sudo pip3.6 install --upgrade pip
sudo pip3.6 install -U pip
sudo pip3.6 install -U setuptools

Alternative for Install Python 3.6.1

A. with packages, shorter process on small device RPI ZERO W

sudo nano etc/apt/sources.list
# add
deb experimental main
deb unstable main
deb jessie main contrib non-free

sudo apt-get update
sudo apt-get install python3.6
python3.6 -V

Install with Python 2.7

OpenCV auf dem Raspberry Pi installieren



Install Raspberry Camera, 8MP

sudo pip3.6 install picamera

Before Install OpenCv

Installing Build Dependencies

To compile OpenCV, we must ensure that the required dependencies are available, including the build tools themselves. We can get the required ones using apt on Ubuntu, but first, ensure the package list is up-to-date by running apt-get update. Next, execute the following commands to get the required packages (see below for a one-line list of all):

sudo apt-get install cmake build-essential pkg-config
sudo apt-get install libgtk2.0-dev libtbb-dev
sudo apt-get install python-dev python-numpy python-scipy
sudo apt-get install libjasper-dev libjpeg-dev libpng-dev libtiff-dev 
sudo apt-get install libavcodec-dev libavutil-dev libavformat-dev libswscale-dev
sudo apt-get install libdc1394-22-dev libv4l-dev

In single line

sudo apt-get install cmake build-essential pkg-config libgtk2.0-dev libtbb-dev python-dev python-numpy python-scipy libjasper-dev libjpeg-dev libpng-dev libtiff-dev libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libdc1394-22-dev libv4l-dev

Install libraries

sudo pip3.6 install numpy

Setup Python

The first step in setting up Python for our OpenCV compile is to install pip , a Python package manager:

Using virtualenv  and virtualenvwrapper  allows you to create isolated Python environments, separate from your system install of Python. This means that you can run multiple versions of Python, with different versions of packages installed into each virtual environment — this solves the “Project A depends on version 1.x, but Project B needs 4.x” problem that often arises in software engineering.
/usr/local/bin/pip install virtualenv virtualenvwrapper
# OR
sudo pip3.6 install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip3.6
Python virtual environment where we’ll be doing our computer vision work
mkvirtualenv cv
# OR
mkvirtualenv --python=python3.6 cv
# OR
mkvirtualenv cv -p python3.6
after reboot your system, logout and log back in, or open up a new terminal, you’ll need to use the workon  command to re-access the cv  virtual environment, otherwise you’ll be using the system version of Python instead:
source ~/.profile
workon cv
python -V
on rpi ZERO

Installing OpenCV on your Raspberry Pi Zero

Camera Test

Steps to install PyGame using pip

    1. Install build dependencies (on linux):
      sudo apt-get build-dep python-pygame
    2. Install mercurial to use hg (on linux):
      sudo apt-get install mercurial
    3. Use pip to install PyGame:
      pip3.6 install hg+

      If the above gives freetype-config: not found error (on Linux), then try sudo apt-get install libfreetype6-dev and then repeat 3.

Alternative way

sudo apt-get install python3-pygame


python3.6 -m pip install pygame --user
python3.6 -m pygame.examples.aliens

Install on virtualenvironment

pip3.6 install numpy

Install OpenCv v2

from linux packages

sudo apt-get install python-opencv
sudo apt-get install python-scipy
sudo apt-get install ipython

from OpenCv repository

cd ~
wget -O

We’ll want the full install of OpenCV 3 (to have access to features such as SIFT and SURF, for instance), so we also need to grab the opencv_contrib repository as well:

wget -O


cd ~/opencv-3.1.0/
mkdir build
cd build


make clean
sudo make install
sudo ldconfig

Verifying Installation

As mentioned previously, you can verify that the cv2 module was installed correctly by executing the following in a Python shell:

import cv2

Image Processing

image processing opencv python

More info

Rpi Camera Documentation

Rpi Camera Start

Pi Camera Calibration (image undistortion)


Pi Camera with GoPro Lens Distortion Removal (for Stream and Video undistortion)


Pi Camera with OpenCv Text detection


About OpenCv

How to install OpenCV 3 on Raspbian Jessie


Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3


Tom Sapletta

Author: Tom Sapletta

I connect the expiriences with new ideas. I have been passionate about computers and programming for 10 years. My first (micro) computer was ZX-Spectrum and the programming language was Basic. Since 2010, I have been programming professionally, objectively and functionally in monolithic and micro-service architectures. I am currently creating an ecosystem architecture at API foundation.