I am going to be showing you how to install the OpenCV computer vision library from source. This is the best way to install this library to ensure that you get the full set of features this library has to offer. Moreover, it allows you to customize your installation. For example, you may not want any python bindings.
This post has been inspired by the tutorials at PyImageSearch and Learn OpenCV.
The bulk of this tutorial has been taken from the superb tutorial at
Install OpenCV 4 on your Raspberry Pi
Installing dependencies
The first thing you need to do is to install dependencies. I am a newcomer to Arch Linux. I come from Ubuntu. The package names are different between Ubuntu and arch Linux. You need to use a search string like
"arch linux"
in your search engine of choice to find the package names.
If you wanted to search for the package called build-essentials in Arch Linux, the search string you would use is
"Arch linux" build-essentials
The tutorial on installing OpenCV on the raspberry pi lists a series of dependencies. I have created an Arch Linux mapping to the Ubuntu dependencies and have given the equivalent commands.
Note
I am using the image created by the F123 project which as an accessible Arch Linux on the raspberry pi. I installed OpenCV on that image therefore most of my dependencies were met.
Run each of the commands and follow the prompts. If a dependency is already installed, then select not to reinstall it.
Raspbian | Arch Linux |
---|---|
sudo apt-get install build-essential cmake unzip pkg-config |
yay -S build-devel yay -S cmake |
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev |
yay -S libjpeg yay -S libtiff yay -S libpng |
sudo apt-get install libxvidcore-dev libx264-dev |
yay -S xvidcore yay -S libx264 |
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev |
yay -S ffmpeg yay -S v4l-utils |
sudo apt-get install libgtk-3-dev | yay -S gtk3 |
sudo apt-get install libcanberra-gtk* | yay -S libcanberra |
sudo apt-get install libatlas-base-dev gfortran |
yay -S lapack yay -S gcc-fortran |
sudo apt-get install python3-dev | already done because python development headers are already installed |
Setting up python version 3
I am maximizing my use of python 3 which is what I will be using here. Python3 is already installed therefore, all we need to do now is to create a python virtual environment to keep things isolated.
sudo pip3 install virtualenv virtualenvwrapper
Clean the package cache.
sudo rm -rf ~/get-pip.py ~/.cache/pip
We need to enable some convenience commands to be able to redily invoke our virtual environment. Ensure that you are in your home directory.
cd ~
and then press enter.
Run the following commands to add to your .profile file.
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.profile
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.profile
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.profile
echo "source /usr/bin/virtualenvwrapper.sh" >> ~/.profile
Once the above commands are run, you can verify that the file has been created correctly by examining its contents.
cat .profile
Activate the commands by issuing the following command.
source .profile
Download OpenCV
Ensure that you are in your home folder.
cd ~
Run the following commands to download OpenCV version 4, the latest at the time of this writing to your raspberry pi.
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip
Expand OpenCV and set up the folders
It is time to expand the zip archives you downloaded.
unzip opencv.zip
unzip opencv_contrib.zip
Rename the folders for easy reference.
mv opencv-4.0.0 opencv
mv opencv_contrib-4.0.0 opencv_contrib
Building OpenCV
Make the virtual environment
Run the following command to make your OpenCV virtual environment.
mkvirtualenv cv -p python3
You can name your virtual environment whatever you like. If you do name it differently, remember to account for that during the rest of this tutorial.
Switch to your virtual environment by executing the following command.
workon cv
The command prompt will change such that the name of your virtual environment will be displayed as a part of the prompt. You can exit the environment by using the following command.
deactivate
Install numpy
You need to install a dependency for OpenCV python.
Ensure that you are in your virtual environment.
workon cv
pip install numpy
The installation of numpy is going to take quite some time. Do not be alarmed.
Generate the build files
It is time to create the build files for OpenCV.
Switch to the opencv folder, create a build folder to hold the build files and then switch to it.
cd ~/opencv
mkdir build
cd build
Run cmake to generate the files. If you are copy pasting the below lines, do not manually try to delete the line breaks and create a single huge command. Copy paste each line and hit enter. The two fulstop characters at the end of the last line tell cmake that it should run.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D CMAKE_SIZEOF_VOID_P=4 \
-D BUILD_opencv_python3=TRUE \
-D BUILD_EXAMPLES=OFF ..
Do inspect the output and ensure that you do not see any significant errors. You may want to enable the logging facility in your terminal emulation program assuming you are running headless on your raspberry pi.
Increase the size of the swap file
The raspberry pi is a memory constrained device. You need to create a swap file to allow for the compile to complete. It is crucial that you do this else the compilation process will not complete.
In Arch Linux, you need to create a swap file first and then activate it. Issue the following commands.
sudo dd if=/dev/zero of=/swp bs=1024 count=4000000
sudo chmod 600 /swp
sudo mkswap /swp
sudo swapon /swp
See below for what these commands mean.
sudo dd if=/dev/zero of=/swp bs=1024 count=4000000
Create a file which will become our swap file. This file must be as large as the swap file we want. I have taken 4GB for safety.
sudo chmod 600 /swp
Assign the right permission to the file we have created.
sudo mkswap /swp
Convert our ordinary file to a swap file.
sudo swapon /swp
Activate the swap file.
Compile and install OpenCV
Ensure that you are in the build folder.
cd ~/opencv/build
Warning, the below command takes several hours to complete. It is the longest part of this procedure. Keep the logging in your terminal emulator active.
make
You can make things go faster by issuing the -j flag with make.
make -j
This utilizes all available CPU cores. You can specify the number of cores you want make to use.
make -j2
The above command will use only two cores. In my experience, running just make is sufficient. I have had the compilation being interrupted if I have used more than one core. Your mileage may vary.
If make completes without errors then run the following commands.
sudo make install
sudo ldconfig
OpenCV is now installed. There are however more steps to follow to ensure that the python bindings work with our virtual environment.
Turn off the swap file
It is crucial that you switch off the swap file else you risk reducing the life of the SD card in the raspberry pi due to the increased write operations that the pi will perform when it swaps memory to the SD card.
sudo swapoff /swp
You can recover space by deleting the swap file.
sudo rm /swp
Linking in the OpenCV binding into our python virtual environment
It is time to create a sim link to the OpenCV library in our python virtual environment.
Issue the following commands.
Note
I am writing this tutorial based on python 3.7. The version of python may change therefore please adjust your steps accordingly.
Run the below commands.
cd ~/.virtualenvs/cv/lib/python3.7/site-packages/
ln -s /usr/local/python/cv2/python-3.7/cv2.cpython-37m-arm-linux-gnueabihf.so cv2.so
Testing your installation
Carry out the following steps to verify if you have installed OpenCV with its python bindings correctly.
workon cv
Ensure that the virtual environment is active. If the above command fails, then do a
source .profile
to ensure that the relevant commands are loaded.
Launch python by issuing the following command
python
Then enter the following text in the interpreter that comes up.
import cv2
If the above commands without an error and your cursor moves to the next line, you can type:
cv2.__version__
You should see the version of OpenCV you have installed. Execute
quit()
to exit python.
Troubleshooting
If the make fails, ensure that your swap file is active and do not issue the -j flag.
If you get unusual errors and the compilation process goes very fast but stops in the middle, you should check the paths to your contrib folder and ensure that all other paths are right.