Over a million developers have joined DZone.

How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision

DZone's Guide to

How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision

Want to learn how to enable machine vision on your Raspberry Pi? Check out this tutorial on how to deploy the OpenCV on your Raspberry Pi.

· IoT Zone ·
Free Resource

OpenCV is an integral part of machine visionIn this tutorial, you will learn to deploy the OpenCV library on a Raspberry Pi, using Raspbian Jessie for testing. You’ll install OpenCV from source code in the Raspberry Pi board.

How to Build OpenCV From the Source on a Raspberry Pi

To start building the OpenCV library from source code on Raspberry Pi, you’ll first need to install development libraries.

How to Install Development Libraries

Type these commands on the Raspberry Pi terminal:

$ sudo apt-get update
$ sudo apt-get install build-essential git cmake pkg-config libgtk2.0-dev
$ sudo apt-get install python2.7-dev python3-dev

You also need to install the required matrix, image, and video libraries:

$ 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 libatlas-base-dev gfortran

The next step is to download the OpenCV source code via Git:

$ mkdir opencv
$ cd opencv
$ git clone https://github.com/Itseez/opencv.git
$ git clone https://github.com/Itseez/opencv_contrib.git

Using a Python virtual environment, deploy the OpenCV on Raspberry Pi with virtualenv. The benefit of this approach is that it isolates the existing Python development environment.

Check out these links below t0 learn more:

How to build a home surveillance system using Raspberry Pi and camera

How to build an IoT system using Raspberry Pi

Install and Configure Virtualenv for OpenCV

If your Raspbian hasn’t installed it yet, you can install it using pip:

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

Then, you can configure a virtualenv in your bash profile:

$ nano ~/.profile

Now, add the following scripts:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Next, save your bash profile file when finished.  To create a Python virtual environment, type this command:

$ mkvirtualenv cv

This command will create a Python virtual environment called cv. If you’re using Python 3, you can create the virtual environment with the following command:

$ mkvirtualenv cv -p python3

You should see (cv) on your terminal. If you close the terminal or call a new terminal, you’d have to activate your Python virtual environment again:

$ source ~/.profile</strong>
$ workon cv

The following screenshot shows a sample Python virtual environment form called cv:


Inside the Python virtual terminal, continue installing NumPy as the required library for OpenCV Python. You can install the library using pip:

$ pip install numpy

Now, you’re ready to build and install OpenCV from the source. After cloning the OpenCV library, you can build it by typing the following commands:

$ cd ~/opencv/
$ mkdir build
$ cd build
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules \

Next, install the OpenCV library on your internal system from Raspbian OS:

$ make -j4
$ sudo make install
$ sudo ldconfig

When you are done, you will have to configure the library so that Python can access it through Python binding. The following is a list of command steps for configuring with Python 2.7:

$ ls -l /usr/local/lib/python2.7/site-packages/
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

If you’re using Python 3.x (say, Python 3.4), perform the following steps on the terminal:

$ ls /usr/local/lib/python3.4/site-packages/
$ cd /usr/local/lib/python3.4/site-packages/
$ sudo mv cv2.cpython-34m.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so

The installation process is over.

Verifying the OpenCV Installation on Raspberry Pi

Now, you need to verify whether OpenCV has been installed correctly by checking the OpenCV version:

$ workon cv
$ python >>> import cv2
  >>> cv2.__version__

You should see the OpenCV version on the terminal. A sample program output is shown in the following screenshot:

how to install opencv on raspberry pi

How to use OpenCV to Enable Computer Vision

The next demo displays an image file using OpenCV. For this scenario, you can use the cv2.imshow() function to display a picture file. For testing, log into the Raspberry Pi desktop to execute the program. Type the following script on a text editor:

import numpy as np
import cv2
img = cv2.imread('circle.png')
cv2.imshow('My photo', img)

Here, circle.png has been used as a picture source; you can use whichever file you want. Save the script into a file called ch03_hello_opencv.py; now, open the terminal inside your Raspberry Pi desktop and type this command:

$ python ch03_hello_opencv.py

If successful, you should see a dialog that displays a picture:

opencv running on raspberry pi

The picture dialog shows up because you called cv2.waitKey(0) in the code. Press any key to close the dialog.

Lastly, close the dialog by calling the cv2.destroyAllWindows() function after receiving a click event. And, there you have it!

iot ,raspberry pi ,tutorial ,opencv ,virtualenv ,python

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}