Install OpenCV from Source
Ubuntu Desktop 22.04 (RPi 4)
Installing OpenCV from source can prove to be quite a difficult process, especially for a first timer. After surfing through countless documentations available on the internet, I still failed to compile OpenCV many times! At the end, the process I followed was a combination of steps and solutions I picked up from different articles altogether. My struggle to build OpenCV on my Raspberry Pi Model B is what encouraged me to write about it.
Why build from Source?
Unlike installing OpenCV from Pre-Built Binaries (apt-get or pip install), building from source gives you more flexibility. With the CMake command, you can enable and disable certain functionalities like including the OpenCV Contrib module, CUDA, GStreamer, SIFT and SURF Features. You also get to include additional dependencies that are required for your project.
So lets begin.
Required Build Dependencies
Cmake is required to configure the installation and GCC is needed for the compilation.
sudo apt-get install cmake
sudo apt-get install gcc g++
To support Python2:
sudo apt-get install python-dev python-numpy
To support Python3:
sudo apt-get install python3-dev python3-numpy
Camera Support (Libav to capture V4L2 device) and Media Support (Libswscale-dev for FFMpeg, GStreamer)
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
GTK Support for GUI features
To support gtk2:
sudo apt-get install libgtk2.0-dev
To support gtk3:
sudo apt-get install libgtk-3-dev
Optional Dependencies
There are supporting files in OpenCV for image formats like PNG, JPEG, JPEG2000, TIFF, WebP. Depending on your requirements you may need additional dependencies. Look for dependence files for those formats.
sudo apt-get install libpng-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libopenexr-dev
sudo apt-get install libtiff-dev
sudo apt-get install libwebp-dev
Downloading Opencv
To install the latest version of OpenCV, we will download the source from OpenCV’s Github repository. Before that we need to install git.
sudo apt-get install git
git clone https://github.com/opencv/opencv.git
This will give us the latest version of OpenCV, i.e. 4.7.0-dev at the time I am writing this. However if you want to download an older version checkout the tags in the repository.
Since I wanted to work with OpenCV version 4.6.0, I entered the OpenCV directory and wrote the following command:
cd ~/opencv
git checkout 4.6.0
OpenCV Contrib
Opencv Contrib is a specialized module that contains extra modules apart from the main modules that are present in OpenCV, and several non-free algorithms such as SURF. Since I was working on an Object Tracking project, it was important for me to also download the OpenCV Contrib repository, which contained the Tracking module I wanted.
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_contrib
git checkout 4.6.0
Navigate back to the “Opencv” folder path and create a build folder. We will be configuring and installing OpenCV inside this folder.
cd ~/opencv
mkdir build
cd build
Configuring and Compiling
The installation is configured with the CMAKE command. Please give this command carefully as an incorrect configuration can lead to unstable and faulty installation of OpenCV.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=ON ..
Check your OPEN_EXTRA_MODULES_PATH before running the command. You should include the path of your open_contribs folder/modules.
Make sure to set OPENCV_ENABLE_NONFREE=ON, so that you will have access to patented algorithms. Without this particular command, my Tracking APIs were not being recognised by Python3. So make sure you check for any non-free algorithm requirements.
We should see these lines in the CMake output (they mean that Python is properly found):
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.6)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
-- packages path: lib/python2.7/dist-packages
--
-- Python 3:
-- Interpreter: /usr/bin/python3.4 (ver 3.4.3)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
-- packages path: lib/python3.4/dist-packages
The make command helps system administrators compile and install open-source utilities. For a Pi which has a quad-core processor, this command will take 3–5 hours to execute. So be ready to wait it out for a while :,)
make -j4
The -j command defines the number of jobs/processes. You can find the number of processes for your system using the nproc command. This will utilize your cores to the maximum for a faster output.
sudo make install
This command will copy the compiled files into the appropriate folders.
We have successfully installed OpenCV! You can confirm the installation in the python shell.
python3
>>> import cv2
>>> cv2.__version__