Compile OpenCV 4 On Systems Using Debian/Ubuntu

1. Uninstall/remove old version if existing

a. In the case you still keep compilation result of old version

Go to build directory of old version and run command below:

sudo make uninstall

b. Find and remove in system

sudo find /usr -name "opencv" -exec rm -rf {} \;
sudo find /var -name "opencv" -exec rm -rf {} \;

2. Update system

sudo apt-get update

3. Install tools

sudo apt-get install -y build-essential cmake cmake-curses-gui pkg-config yasm tmux

4. Install libraries

a. Required libraries

sudo apt-get install -y libjpeg-dev libtiff5-dev libpng-dev zlib1g-dev libleptonica-dev libtesseract-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev v4l-utils libmpg123-dev ffmpeg
sudo apt-get install -y libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev libavresample-dev libdc1394-22-dev libxine2-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y libtbb-dev libgflags-dev libeigen3-dev libprotobuf-dev libhdf5-dev libatlas-base-dev libva-dev

b. Compile libpng12 if needed

cd
mkdir src
wget https://ppa.launchpadcontent.net/linuxuprising/libpng12/ubuntu/pool/main/libp/libpng/libpng_1.2.54.orig.tar.xz
tar Jxfv libpng_1.2.54.orig.tar.xz
cd libpng-1.2.54
./configure
make
sudo make install
sudo ln -s /usr/local/lib/libpng12.so.0.54.0 /usr/lib/libpng12.so
sudo ln -s /usr/local/lib/libpng12.so.0.54.0 /usr/lib/libpng12.so.0

5. Install Qt5

sudo apt-get install -y qt5-default qtbase5-dev qtcreator
sudo apt-get install -y qtdeclarative5-dev qttools5-dev

6. Install python libraries if using python

sudo apt-get install -y python3-dev python3-numpy
sudo update-alternatives --install /usr/bin/python python $(which python3) 10

7. Download OpenCV and additional library

This tutorial uses version 4.5.2

wget https://github.com/opencv/opencv/archive/4.5.5.zip -O opencv_source.zip
wget https://github.com/opencv/opencv_contrib/archive/4.5.5.zip -O opencv_contrib.zip

8. Extract downloaded files

unzip opencv_source.zip
unzip opencv_contrib.zip

9. Creat build directory

cd opencv-4.5.5
mkdir build
cd build

10. Generate Makefile

a. For systems using ARM based on ARMv7 instruction set or newer

Example: Raspberry Pi from version 2 or newer such as RPi 2, RPi 3/3+, RPi 4 or DE10-nano, Jetson Nano

Note:

  • Do not turn on ENABLE_VFPV3 if using 64-bit operating system.
  • Do not turn on ENABLE_VFPV3 if hardware is Jetson Nano.
  • Except Jetson Nano, do not turn on WITH_CUDA for hardwares without NVIDIA GPU.

Command below works well for 32-bit operating system without NVIDIA GPU:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
     -D ENABLE_PRECOMPILED_HEADERS=OFF \
     -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 OPENCV_GENERATE_PKGCONFIG=ON \
     -D WITH_LIBV4L=ON \
     -D BUILD_PERF_TESTS=OFF \
     -D BUILD_opencv_python2=OFF \
     -D BUILD_opencv_python3=ON \
     -D INSTALL_C_EXAMPLES=ON \
     -D INSTALL_PYTHON_EXAMPLES=OFF \
     -D WITH_QT=ON \
     -D WITH_OPENGL=ON \
     -D WITH_FFMPEG=ON \
     -D WITH_JPEG=ON \
     -D BUILD_JPEG=ON \
     -D BUILD_JASPER=ON \
     -D BUILD_ZLIB=ON \
     -D BUILD_TIFF=ON \
     -D BUILD_PNG=ON \
     -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.5/modules \
     -D OPENCV_ENABLE_NONFREE=ON \
     -D ENABLE_CXX11=ON \
     -D BUILD_TBB=ON \
     -D WITH_TBB=ON \
     -D ENABLE_VFPV3=ON \
     -D ENABLE_NEON=ON \
     -D WITH_CUDA=OFF \
     -D WITH_V4L=ON \
     -D WITH_ADE=OFF \
     -D BUILD_opencv_ts=OFF \
     -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
     -D PYTHON_INCLUDE_DIR=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_inc())') \
     -D PYTHON_LIBRARY=$(dirname $(python3-config --configdir)) \
     -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_lib())')/numpy/core/include \
     ../

b. For systems using Intel CPU (PC/laptop) or chip ARM with ARMv6 instruction set or older

Example: Raspberry Pi version 1 or older such as Pi Zero, Pi 1

Note:

  • Do not turn on ENABLE_VFPV3 and ENABLE_NEON.
  • Only turn on WITH_CUDA if having NVIDIA cards.

Without CUDA

cmake -D CMAKE_BUILD_TYPE=RELEASE \
     -D ENABLE_PRECOMPILED_HEADERS=OFF \
     -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 OPENCV_GENERATE_PKGCONFIG=ON \
     -D WITH_LIBV4L=ON \
     -D BUILD_PERF_TESTS=OFF \
     -D BUILD_opencv_python2=OFF \
     -D BUILD_opencv_python3=ON \
     -D INSTALL_C_EXAMPLES=ON \
     -D INSTALL_PYTHON_EXAMPLES=OFF \
     -D WITH_QT=ON \
     -D WITH_OPENGL=ON \
     -D WITH_FFMPEG=ON \
     -D WITH_JPEG=ON \
     -D BUILD_JPEG=ON \
     -D BUILD_JASPER=ON \
     -D BUILD_ZLIB=ON \
     -D BUILD_TIFF=ON \
     -D BUILD_PNG=ON \
     -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.5/modules \
     -D OPENCV_ENABLE_NONFREE=ON \
     -D ENABLE_CXX11=ON \
     -D BUILD_TBB=ON \
     -D WITH_TBB=ON \
     -D WITH_CUDA=OFF \
     -D WITH_V4L=ON \
     -D WITH_ADE=OFF \
     -D BUILD_opencv_ts=OFF \
     -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
     -D PYTHON_INCLUDE_DIR=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_inc())') \
     -D PYTHON_LIBRARY=$(dirname $(python3-config --configdir)) \
     -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_lib())')/numpy/core/include \
     ../

With CUDA

Check the https://developer.nvidia.com/cuda-gpus to get CUDA_ARCH_BIN value:

sm_10 is a compute capability 1.0 device
sm_11 is a compute capability 1.1 device
sm_12 is a compute capability 1.2 device
sm_13 is a compute capability 1.3 device
sm_20 is a compute capability 2.0 device
sm_21 is a compute capability 2.1 device
sm_30 is a compute capability 3.0 device
sm_32 is a compute capability 3.2 device
sm_35 is a compute capability 3.5 device
sm_37 is a compute capability 3.7 device
sm_50 is a compute capability 5.0 device
sm_52 is a compute capability 5.2 device
sm_53 is a compute capability 5.3 device
sm_60 is a compute capability 6.0 device
sm_61 is a compute capability 6.1 device
sm_62 is a compute capability 6.2 device

cmake -D CMAKE_BUILD_TYPE=RELEASE \
     -D ENABLE_PRECOMPILED_HEADERS=OFF \
     -D ENABLE_FAST_MATH=1 \
     -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 OPENCV_GENERATE_PKGCONFIG=ON \
     -D WITH_LIBV4L=ON \
     -D BUILD_PERF_TESTS=OFF \
     -D BUILD_opencv_python2=OFF \
     -D BUILD_opencv_python3=ON \
     -D INSTALL_C_EXAMPLES=ON \
     -D INSTALL_PYTHON_EXAMPLES=OFF \
     -D WITH_QT=ON \
     -D WITH_OPENGL=ON \
     -D WITH_FFMPEG=ON \
     -D WITH_JPEG=ON \
     -D BUILD_JPEG=ON \
     -D BUILD_JASPER=ON \
     -D BUILD_ZLIB=ON \
     -D BUILD_TIFF=ON \
     -D BUILD_PNG=ON \
     -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.5/modules \
     -D OPENCV_ENABLE_NONFREE=ON \
     -D ENABLE_CXX11=ON \
     -D BUILD_TBB=ON \
     -D WITH_TBB=ON \
     -D WITH_CUDA=ON \
     -D BUILD_CUDA_STUBS=ON \
     -D CUDA_NVCC_FLAGS='--expt-relaxed-constexpr --Wno-deprecated-gpu-targets' \
     -D CUDA_FAST_MATH=1 \
     -D WITH_CUBLAS=1 \
     -D WITH_CUDNN=ON \
     -D HAVE_CUDNN=ON \
     -D OPENCV_DNN_CUDA=ON \
     -D CUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/libcudnn.so \
     -D CUDNN_INCLUDE_DIR=/usr/include \
     -D CUDA_ARCH_BIN=32,30,35,37 \
     -D WITH_V4L=ON \
     -D WITH_ADE=OFF \
     -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
     -D PYTHON_INCLUDE_DIR=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_inc())') \
     -D PYTHON_LIBRARY=$(dirname $(python3-config --configdir)) \
     -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_lib())')/numpy/core/include \
     ../

For x86 CPU, run command if missing libEGL and libGL:

sudo ln /usr/lib/x86_64-linux-gnu/libEGL.so.1 /usr/lib/x86_64-linux-gnu/libEGL.so
sudo ln /usr/lib/x86_64-linux-gnu/libGL.so.1 /usr/lib/x86_64-linux-gnu/libGL.so

c. Cross platform

  • Install suitable toolchain.
  • Base on type of CPU and number of bit of operating system to select correctly cmake options as mentioned in step 10a and 10b above.
cmake -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake \
     -D CMAKE_C_COMPILER=/opt/toolchain/gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \
     -D CMAKE_CXX_COMPILER=/opt/toolchain/gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ \
     -D CMAKE_LINKER=/opt/toolchain/gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-ld \
     -D CMAKE_AR=/opt/toolchain/gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-ar \
     -D CMAKE_BUILD_TYPE=RELEASE \
     -D ENABLE_PRECOMPILED_HEADERS=OFF \
     -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 OPENCV_GENERATE_PKGCONFIG=ON \
     -D WITH_LIBV4L=ON \
     -D BUILD_PERF_TESTS=OFF \
     -D BUILD_opencv_python2=OFF \
     -D BUILD_opencv_python3=ON \
     -D INSTALL_C_EXAMPLES=ON \
     -D INSTALL_PYTHON_EXAMPLES=OFF \
     -D WITH_QT=ON \
     -D WITH_OPENGL=ON \
     -D WITH_FFMPEG=ON \
     -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.5/modules \
     -D OPENCV_ENABLE_NONFREE=ON \
     -D ENABLE_CXX11=ON \
     -D BUILD_TBB=ON \
     -D WITH_TBB=ON \
     -D ENABLE_VFPV3=ON \
     -D ENABLE_NEON=ON \
     -D WITH_CUDA=OFF \
     -D WITH_V4L=ON \
     -D WITH_ADE=OFF \
     -D BUILD_opencv_ts=OFF \
     -D BUILD_opencv_stereo=OFF \
     -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
     -D PYTHON_INCLUDE_DIR=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_inc())') \
     -D PYTHON_LIBRARY=$(dirname $(python3-config --configdir)) \
     -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c 'import distutils.sysconfig as s; print(s.get_python_lib())')/numpy/core/include \
     ../

11. Compile

Go to this step if result of step 10 is Configuring done” as below

--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- Configuring done
-- Generating done
-- Build files have been written to: /media/root/DATA/app_src/opencv4/opencv-4.5.5/build

If any error in step 10, go back step 10 and check cmake options.

If no error, compile opencv with command below

make -j$(nproc)

12. Install

sudo make install
sudo cp lib/python3/cv2.* $(python3 -c 'import distutils.sysconfig as s; print(s.get_python_lib())')
sudo ldconfig

13. Test

Check installed version

# opencv_version
4.5.5

Test with python

# python3
>>import cv2
>>print (cv2.version)
4.5.5
>>exit()    

14. Reboot system if needed

sudo reboot

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *