OpenCV is an integral part of machine vision. This tutorial by Agus Kurniawan, the author of Smart Internet of Things Projects, shows you how to deploy OpenCV on Raspberry Pi.
In this tutorial, you will learn to deploy the OpenCV library on Raspberry Pi, using Raspbian Jessie for testing. You’ll install OpenCV from source code in Raspberry Pi board.
How to build OpenCV from source on 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:[bash] $ 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:[bash]$ 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:[bash]$ 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 OpenCV on Raspberry Pi with virtualenv. The benefit of this approach is that it isolates the existing Python development environment.
Install and configure Virtualenv for OpenCV
If your Raspbian hasn’t installed it yet, you can install it using pip:[bash]$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
Then, configure virtualenv in your bash profile:[bash]$ nano ~/.profile[/bash]
Now, add the following scripts:[bash] export WORKON_HOME=$HOME/.virtualenvs
Save your bash profile file when finished.
To create a Python virtual environment, type this command:[bash]$ mkvirtualenv cv[/bash]
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:[bash]$ mkvirtualenv cv -p python3[/bash]
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:[bash]$ source ~/.profile</strong>
$ workon cv[/bash]
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:[bash]$ pip install numpy[/bash]
You’re now ready to build and install OpenCV from source. After cloning the OpenCV library, you can build it by typing the following commands:[bash]$ cd ~/opencv/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Next, install the OpenCV library on your internal system from Raspbian OS:[bash]$ make -j4
$ sudo make install
$ sudo ldconfig
When done, you’d 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:[bash]$ 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:[bash]$ 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:[bash]$ workon cv
$ python >>> import cv2
You should see the OpenCV version on the terminal. A sample program output is shown in the following screenshot:
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:[python] import numpy as np
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:
If successful, you should see a dialog that displays a picture:
The picture dialog shows up because you called
cv2.waitKey(0) in the code. Press any key to close the dialog.
Close the dialg by calling the
cv2.destroyAllWindows() function after receiving a click event.
If you found this tutorial interesting and want to explore more about IoT and Raspberry Pi, you can explore the book, Smart Internet of Things Projects by Agus Kurniawan. The book is replete with comprehensive, easy-to-understand examples and follows a project-based approach to building smart IoT projects.