Source: http://mspoweruser.com/wp-content/uploads/2014/07/CMake-logo.png

CMake is an open-source, cross-platform family of tools designed to build, test, and package software, mainly used to control the software compilation process using simple platform and compiler independent configuration files and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

Image Source: https://opencv.org/wp-content/uploads/2021/02/1_HfZmZayUqnYioPC9qTfd4A.png

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library for image processing and machine learning tasks that support Tensorflow, Torch/Pytorch, and Caffe. It has C++, Python, Java, and MATLAB interfaces and supports Windows, Linux, Android, and Mac OS. The library is cross-platform, and you can pip install it (where you are using it with Python) with CPU support. Alternatively, where you want to use it with GPU support, you can build it from the source, which is more involved. This article is a walk-through on how to build OpenCV from a source (by cloning OpenCV repo) on a Linux Ubuntu machine. There are a lot of articles and Q&A, but all they have is scattered information that may not work in general, for which I thought to document this so that we can handle each edge case and try to install it perfectly on your Linux Ubuntu.

In this article, we’ll discuss on following points:

Since my machine consists of miniconda3, I prefer to build and install my OpenCV in a separate environment with Python3.8 (other than base). Again, it’s not only using conda; one can also use the virtualenv package to create virtual env. Also, one can install it without requiring any virtual environment (i.e., in the Home directory). Don’t worry; we’ll cover all the cases one by one in each step.

Step 0: Warm-Up

$ conda activate <your_env_name>
$ source <your_env_name>/bin/activate

Step 1: Install required libraries and packages

$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install -y build-essential cmake git unzip pkg-config make
$ sudo apt-get install -y python3.8-dev python3-numpy libtbb2 libtbb-dev
$ sudo apt-get install -y  libjpeg-dev libpng-dev libtiff-dev libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

Step 2: Clone official OpenCV and OpenCV_contrib repo

$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv
$ git clone https://github.com/opencv/opencv_contrib
$ cd ~/opencv_build/opencv
$ mkdir -p build && cd build

Step 3: Compile and Install OpenCV with CMake

$ cmake -D WITH_CUDA=OFF -D BUILD_TIFF=ON -D BUILD_opencv_java=OFF -D WITH_OPENGL=ON -D WITH_OPENCL=ON -D WITH_IPP=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_V4L=ON -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D PYTHON3_EXECUTABLE=$(which python3) -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..
$ make -j8
$ sudo make install
$ sudo ldconfig

Step 4: Verify your installation

To verify the installation for both C++ and Python3, type the following commands:

$ pkg-config --modversion opencv4
Output:
4.5.5
$ python3 -c "import cv2; print(cv2.__version__)"
Output:
'4.5.5-dev'
$ cd ~/miniconda3/envs/rodo_env/lib/python3.8/site-packages/
$ ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so cv2.so
$ python3 -c "import cv2; print(cv2.__version__)"
Output:
'4.5.5-dev'

Conclusion

We have shown a more involved way to install OpenCV both for C++ and Python3 on your Ubuntu 20.04 machine, which guarantees to run given all the requirements and preferences are satisfied. Even though installing the packaged version from the Ubuntu repository is easier, building OpenCV from the source gives you more flexibility. It should be your first option when installing OpenCV.

Here is the bashfile I used to install it one go:

References:

If you have any questions or feedback, feel free to comment below. And if you liked it or it does come out helpful, do give a clap. Till then, Happy Coding.