Installing OpenCV on Raspberry Pi for Image Recognition

Installing OpenCV on Raspberry Pi for Image Recognition

Sometimes we use the Raspberry Pi and camera for image recognition. The most commonly used library for image recognition on Raspberry Pi and Linux systems is OpenCV. This article introduces how to install OpenCV on Raspberry Pi and perform image recognition.

1. Raspberry Pi System

Installation will not be discussed here. Just boot up the Raspberry Pi and open the command window to install the dependencies for OpenCV, which involves several steps.

1.1 Update the System

$ sudo apt-get update
$ sudo apt-get upgrade

1.2 Install Tools for Compiling OpenCV Source Code

$ sudo apt-get install build-essential cmake pkg-config

1.3 Install packages for processing common image and video formats, allowing us to read different formats of images and videos from the hard drive

$ 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

1.4 OpenCV requires certain modules for image display functionality

$ sudo apt-get install libgtk2.0-dev
$ sudo apt-get install libatlas-base-dev gfortran

Next, we also need to install Python dev

sudo apt-get install python2.7-dev python3-dev

2. Download and Unzip OpenCV Resource Library

$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
$ unzip opencv.zip
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
$ unzip opencv_contrib.zip

3. Prepare Python Development Environment

3.1 Install Python Package Manager:

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

3.2 Install Python Virtual Environment

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

Then add a few lines at the end of the ~/.profile file

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

3.3 Next, create a Python virtual environment for OpenCV development

$ mkvirtualenv cv -p python3

Open a command window and execute the following command to confirm that our cv environment has been created

$ source ~/.profile
$ workon cv

If the text before the command window changes to (cv), it indicates that we have successfully created a Python virtual environment named cv

Installing OpenCV on Raspberry Pi for Image Recognition

3.4 Install NumPy in the cv virtual environment

(cv) -> ~ $ pip install numpy

All subsequent operations must remain in the cv environment.

4. Compile and Install OpenCV

4.1 Be sure to be in the cv environment, then use cmake to compile OpenCV

$ cd ~/opencv-3.1.0/
$ mkdir build
$ cd build
$ cmake -D ENABLE_PRECOMPILED_HEADERS=OFF \ 
-D CMAKE_BUILD_TYPE=RELEASE \ 
-D CMAKE_INSTALL_PREFIX=/usr/local \ 
-D INSTALL_PYTHON_EXAMPLES=ON \ 
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \ 
-D BUILD_EXAMPLES=ON ..

4.2 Start compiling OpenCV

$ make -j4

The compilation process may take several hours, please be patient. -j specifies how many threads to use for compilation. Using single-threaded compilation on Raspberry Pi, although it is much slower, it will not freeze. Using -j4 may cause freezing.If no number is added after -j, it defaults to no limit on the number of threads for compilation.

The compilation process is as follows

Installing OpenCV on Raspberry Pi for Image Recognition

4.3 Install OpenCV

$ sudo make install
$ sudo ldconfig

4.4 Link OpenCV module in Python virtual environment

We need to rename cv2.cpython-34m.so to cv2.so

$ cd /usr/local/lib/python3.4/site-packages/
$ sudo mv cv2.cpython-34m.so cv2.so

Then link cv2.so in the Python virtual environment to the file just renamed to cv2.so

$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so

Installing OpenCV on Raspberry Pi for Image Recognition

5. Test if OpenCV3 is Successfully Installed

$ source ~/.profile
$ workon cv
$ python>>> import cv2>>> cv2.__version__
'3.1.0'>>>

Installing OpenCV on Raspberry Pi for Image Recognition

6. After Completing the OpenCV Development Environment, You Can Run Some Simple Image Recognition DEMOs

The DEMOs are located in the /usr/local/share/OpenCV/sample/python directory

Installing OpenCV on Raspberry Pi for Image Recognition

We will copy the /usr/local/share/OpenCV/sample/ folder to Downloads/sample/ folder

Installing OpenCV on Raspberry Pi for Image Recognition

Run a few DEMOs

Edge detection algorithm:(cv) python edge.py

Installing OpenCV on Raspberry Pi for Image Recognition

Pattern recognition algorithm:(cv) python find_obj.py

Installing OpenCV on Raspberry Pi for Image Recognition

Motion direction detection:(cv) python lk_track.py

Installing OpenCV on Raspberry Pi for Image Recognition

All done, OpenCV is a common tool for image processing and recognition on Raspberry Pi. If paired with the Raspberry Pi CSI camera to capture photos and perform recognition processing, it will be even more interesting, allowing for facial recognition or tracking.

Disclaimer: Some content is sourced from the internet for the purpose of learning and communication. The copyright of the article belongs to the original author. If there is any inconvenience, please contact for deletion.

For more visual information, follow the “New Machine Vision” WeChat public account~

Installing OpenCV on Raspberry Pi for Image Recognition

Leave a Comment