Open Source for Computer Vision, OpenCV, is the mother lode for image processing experiments. For hobbyists, it affords an accelerated entry computer graphics algorithms without the burden of going solo or maverick. it is therefore only natural that literature abounds with the installation and porting of the OpenCV suite on SoC boards. The Raspberry Pi is no exception. Robert Castle's seminal work has been emulated and verified by thousands of users. Yours truly used the documentation as reference guide for the deployment on over a dozen RPi boards. This article describes one such deployment on the Raspberry Pi Zero W board.
InstallationThere are three phase for the installation:
- Pre-requisites
- OpenCV Source
- Python environment
This section is not mandatory but recommended for the practical reason that an updated platform is best for the installation of the OpenCV program:
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
sudo reboot
Pre-requisitesThe following commands install the pre-requisite packages:
sudo apt-get install build-essential git cmake pkg-config
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 libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python2.7-dev python3-dev
OpenCV sourceThe following commands install the source code files:
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip
unzip opencv_contrib.zip
Python environmentThe following commands prepare the platform for the use of the Python packages:
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo pip3 install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip
Virtual environmentThe following command prepares the initial virtual environment for the building and testing of OpenCV:
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source ~/.profile
mkvirtualenv cv
workon cv
pip3 install numpy
Compile and buildChange the virtual environment (necessary for subsequent build operations):
workon cv
cd ~/opencv-3.2.0/
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_contrib-3.2.0/modules \
-D BUILD_EXAMPLES=ON ..
Another variation is the following:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_WITH_DEBUG_INFO=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_PERF_TESTS=OFF \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules \
-D ENABLE_NEON=ON \
-D WITH_LIBV4L=ON \
Validate the Cmake logEnsure that the virtual environment is using the right links to Python files:
You may already be in the build sub-folder owing the operations in the previous set of commands. Run the make command to start the compilation of the OpenCV source code:
Logon to the Raspberry Pi Zero computer using SSH (i.e. do not use Microsoft Remote Desktop Connection with the xrdp package if the model is the Raspberry Pi Zero W). Ensure that no other services that are not needed for OpenCV build are running and then enter the following commands:
cd ~/opencv-3.2.0/build
make
After a few hours, the last few lines on the terminal screen should be similar to that below based on the build options selected:
After the make operation is complete, enter the following command to proceed with the build:
sudo ldconfig
There will no messages on the screen after the successful conclusion of this operation.
Verify the packages
Use the following command to confirm the version of the installation:
pkg-config --modversion opencv
The newly built files are in the following sub-folders:
/usr/local/lib/libcv*
/usr/local/lib/python2.7/dist-packages/cv*
/usr/local/lib/python3.4/dist-packages/cv*
/usr/local/include/opencv2/
/usr/local/bin/opencv_*
/usr/local/share/OpenCV/
The bindings output file has to be renamed in the current version with the following command:
ls /usr/local/lib/python3.4/site-packages/
cd /usr/local/lib/python3.4/site-packages/
sudo mv cv2.cpython-34m.so cv2.so
Python interpreter reportCreate a symbolic link to the bindings file using the following command:
The program:
- Reads an image file
- Default filename is "default.png"
- Other filename can be passed as a parameter value from the command line
- Displays the image, as shown below, in a window
- Waits for any keystroke to end program
The source code for this very basic example is posted at GitHub.
Python exampleDeactivateTo exit from a virtual environment, simply enter the following command:
deactivate
Subsequently, if there is a pressing need to remove the files specific for the virtual environment, enter the following command:
ReferencesOpen Source for Computer Vision
Installing OpenCV on a Raspberry Pi
Comments