Installing OpenCV on Raspberry Pi

Victor Fernandez
10 min readApr 7, 2022

--

Photo by Vishnu Mohanan on Unsplash

I found several posts related to OpenCV running in Raspberry pi, however, I run into several issues following them, after reading and asking around, I finally make it work, and here are the steps I follow to do so.

Some of this steps will take hours, so prepare for that.

Step 1: Preparing the Raspberry

Expanding filesystem on the Raspberry Pi

The First error I found, was that I used a really small MicroSD card, so expanding the available space was the first thing to address.

The Raspberry won’t use all the space available by default, you need to expand the filesystem to make use of it.

Access the Raspberry configuration menu
To access the configuration: On the terminal type the following command.

sudo raspi-config

After entering the command, a configuration menu appears on the screen.
This menu allows us to make changes in some configurations.

To expand the filesystem navigate to Advance Options.

Next, select the option Expand filesystem.

Finally, <Finish> and reboot the pi.

sudo reboot

After the reboot, the available space will expand.

To verify it, I use the command df-h.

❌Deleting software unused to release more space.

Depending on the OS installed, there will be extra software installed.

It might be better to download and install a lighter version of the Rapsbian OS. The official raspberry pi site has several options.

But I had the wrong option, so I needed to remove those extra applications installed by default. Some of these programs are LibreOffice and Wolfram-engine.

sudo apt-get purge wolfram-engine
sudo apt-get purge libreoffice*
sudo apt-get clean
sudo apt-get autoremove

dpkg-query -l will provide a list all the programs installed. This process is not necessary with othe Raspbian OS.

Modify memory assigned to GPU

The Raspberry pi shares the RAM with CPU and GPU, for the early models like pi2 and pi3, the memory assigned to GPU is 64Mbytes ( for pi4 is 76MBytes).

I’m going to use this Raspberry for a project that might include images and videos, thus increasing the memory assigned to the GPU to 128Mbytes will be helpful in the future.

To make this modification, follow the steps:

  1. Got to menu> preference> Raspberry Pi configuration.
  2. Navigate to the Performance tab.
  3. Change value assigned to the GPU.
  4. Reboot.

verify EEPROM is up-to-date (just for Raspberry pi 4).

This happens to me Once, and it wont be necessary in the future. I will use the OpenCV in Pi3 instead of the Pi4, but initially i use the Pi4 to test, so I thought this can be useful for some people out there.

For Pi4 using a newer OS version might not need to follow these steps.

  1. Check if the EEPROM is up-to-date with sudo rpi-eeprom-update.
  2. Update the EEPROM firmware if need it rpi-eeprom-update -a.
sudo rpi-eeprom-update
sudo rpi-eeprom-update -a
sudo reboot

Verify OS version.

One more verification before installing the dependencies, This is important for Pi4 but is not a bad idea to make this verification.

To verify what OS is installed on the Pi I use uname -a.

  • aarch64 -> 64-bit OS.
  • armv7l -> 32-bit OS.

The next steps assume 32-bit.

Step 2: Installing the dependencies on Raspberry pi

Photo by israel palacio on Unsplash

I always start by updating and upgrading the system, an old habit, but I think it is a good one.

sudo apt-get update && sudo apt-get upgrade

Install libraries to handle video and images.

Install libraries to work with videos and images.

sudo apt-get install cmake gfortran
sudo apt-get install libjpeg-dev libtiff-dev libgif-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgtk2.0-dev libcanberra-gtk*
sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
sudo apt-get install libtbb2 libtbb-dev libdc1394–22-dev libv4l-dev
sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
sudo apt-get install libjasper-dev liblapack-dev libhdf5-dev
sudo apt-get install protobuf-compiler

Development toolkits

Development toolkits, It is a big topic, with a lot of discussions, I won’t go deeper into this topic, and I won’t use them during the compilation, but I thought it worth mentioning them.

These developer toolkits are used to create the structure and how the app looks and feels. Things such as buttons, toolbars, and menus used in the apps. These toolkits are a time-saver for developers since they don’t need to waste time designing and writing code for every shape, size, and look of every button, the toolkit takes care of it.

There are two developer toolkits GTK and Qt

Install GTK

GTK is a UI tool kit used to render the different components for a UI.

To install GTK:

sudo apt-get install libgtk-3-dev
sudo apt-get install libcanberra-gtk*

Install Qt

Qt is the fastest and smartest war to produce industrial-leading software that user love.

To install Qt

sudo apt-get install qt5-default

Step 3: Download OpenCV 4 for Raspberry pi

There are two things to download, the opencv and the opencv_contrib.

cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.3.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.3.zip

I ran into some issue using “wget” , so I had two options, download the zip from github directly, or use "curl”. I end using "curl".

sudo curl -L — output opencv.zip https://github.com/opencv/opencv/archive/4.5.3.zip
sudo curl -L — output opencv_contrib https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.3.zip

In case anyone wants to download the zip directly you can find them here:

Once the download is finished, unzip the files and rename them. I change the names to something without the numbers (release versions) maybe not be a good practice, but it made it easier to work with those files.

unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.5.3 opencv
mv opencv_contrib-4.5.3 opencv_contrib

opencv_contrib is a repository with additional or extra modules to increase the functionality.

Step 4: Python 3 virtual environment for OpenCV 4

Photo by Kelvin Han on Unsplash

To avoid conflicts with other versions, I use virtual environments, there might be better ways but this is the way I feel more comfortable.

Before downloading the virtual environment libraries I follow these steps:

  1. Check the version of Python, use python 3. (do not use python 2).
  2. Locate the python files on your filesystem.
  3. Add the location at the end of the file ~/.bashrc
  4. Reload the profile.
#check python version
python3 --version
> Python 3.7.3
#python3 location
which python 3.7
>/usr/bin/python
# merge VIRTUALENVWRAPPER_PYTHON=location/version
echo “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.7” >> ~/.bashrc
# reload profile
source ~/.bashrc

Install pip

I will install pip to make the installation of python packages easier.

sudo apt-get install python3-pip

Install the virtual environment

I will use pip it to install the virtual environment libraries.

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

I need to do some admin work on the ~/.bashrc file, instead of using nano to open the file, I can use echo to append the lines to the end of the file.

echo “export WORKON_HOME=$HOME/.virtualenvs” >> ~/.bashrc
echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.bashrc
source ~/.bashrc

Finally, I can create a virtual environment. I will use the release version as part of the name (cv453) just to keep some records.

mkvirtualenv cv453

If there are no errors, the screen will display something like this.

In one of my first attempts to install the OpenCV, I ran into some errors pointing to some missing libraries. after some search, I found out the library missing was Numpy, so the solution was to install numpy before starting with the building, compilation, and installation.

pip3 install numpy

Step 5: CMake and compile OpenCV 4

Increase the SWAP on the Raspberry

I will make the build and compilation of the OpenCV on the Pi itself, So I need as much “power“ as I can get. One way to do it, is by increasing the swap size.

The increase of the swap size will help with the compilation and avoid issues with memory. I start by opening/etc/dphys-swapfile file:

sudo nano /etc/dphys-swapfile

and add:

# set the size to absolute value, leaving empty (default) then uses computed value
# you most likely don’t want this, unless you have a special disk situation
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=2048

restart the swap service:

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Be aware. The increase in the SWAP’s sizes might affect the MicroSD card. It is worthy to remember SD cards have a limited number of write and read.

It is a good idea to make a copy of the image including OpenCV and python in case the SD card fails.

Run CMake for OpenCV

I will use the CMake command make to compile OpenCV.

cd ~/opencvmkdir build
cd build

The next step is to tell Cmake what, where, and how to make the OpenCV.

This command will contain several flags. These flags will customize the OpenCV build.

One example of customization is the absence of the python examples-D INSTALL_PYTHON_EXAMPLES=OFF.

NOTE: It should be all in one line, the space between statements and -D is a single space, however, for readability, I use “\” to separate each flag so it is easier to read.

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 WITH_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_ZLIB=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..

the `..` at the end is not a typo, It is a way to tell CMake where is the CmakeList.txt file

💢what happens if the `..` are missing at the end
if you miss the last two dots at the end an error message will appear.

Once the process is finished, a log report will be displayed.

Bellow, I add some images of part of the mentioned report. This report will give us information about the components in the build.

Compile OpenCV4

Finally, the time-consuming task.

On the terminal, type:

make -j4

This command will take several hours, on raspberry pi 3, around five hours. In Raspberry pi 4 might take two hours.

The -j4 the argument tells the raspberry to use 4 cores for compilation.

if there are problems with the compilation try make without -j4.

Install OpenCV 4

Once the Raspberry finishes with the build and compile process, I proceed to install the OpenCV:

sudo make install
sudo ldconfig

⚠️DON’T FORGET to reset the SWAP size ⚠️
open the file with /etc/dphys-swapfile and reset CONF_SWAPSIZE to 100MB, and reset the swap service.

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Cleaning

I suggest you clean the make and update the system as well as the .zip, it will release some valuable space.

make clean
sudo apt-get update
cd~
rm opencv.zip
rm opencv_contrib.zip
sudo reboot

Step 6: Link OpenCV to python virtual environment

Photo by JJ Ying on Unsplash

I’m using a virtual environment so this is an important step

I need to link the OpenCV with the virtual environment.

cd ~/.virtualenvs/cv453/lib/python3.7/site-packages
ln -s /usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-arm-linux-gnueabihf.so
cd ~

Step 7: Test OpenCV

Photo by Ferenc Almasi on Unsplash

Finally, I finished the configuration, the last step is to test if everything is working:

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

OpenCV is up and running, I made a copy of the OS, in case the SDcard failed.

It might be better to use the Pi4 but the Pi3 is just fine for simple projects.

Victor Fernandez

--

--

Victor Fernandez
Victor Fernandez

Written by Victor Fernandez

I’m Victor, I’m a Field Application Engineer for a CCTV manufacturer. I love Raspberry Pi, Python, and Microcontrollers and I write about my personal projects.

No responses yet