How to Install OpenCV 4.2.0 on ubuntu 18.04 system
Install OpenCV 4.2.0 on ubuntu 18.04 system
Update system
1 2 |
$ sudo apt update $ sudo apt upgrade |
Install pre-dependent packages
- General tools
1 2 |
$ sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall |
- Image I/O library
1 |
$ sudo apt install libjpeg-dev libpng-dev libtiff-dev |
- Video/audio library-FFMPEG, GSTREAMER, x264, etc.
1 2 3 4 |
$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev $ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev $ sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev $ sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev |
- OpenCore-
1 2 |
$ sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev |
- Camera programming interface library
1 2 3 4 |
$ sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils $ cd /usr/include/linux $ sudo ln -s -f ../libv4l1-videodev.h videodev.h $ cd ~ |
- GTK library-graphical user interface
1 |
$ sudo apt-get install libgtk-3-dev |
- Libraries used by python3
1 2 3 |
$ sudo apt-get install python3-dev python3-pip $ sudo -H pip3 install -U pip numpy $ sudo apt install python3-testresources |
- C++ parallel library for CPU
1 |
$ sudo apt-get install libtbb-dev |
- Optimized library for OpenCV
1 |
sudo apt-get install libatlas-base-dev gfortran |
- Optional installation
1 2 3 |
$ sudo apt-get install libprotobuf-dev protobuf-compiler $ sudo apt-get install libgoogle-glog-dev libgflags-dev $ sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen |
Steps to officially install Opencv 4.2.0
If you just want to compile a static library, you only need to attach it in Cmake-D BUILD_SHARED_LIBS=OFF
1 2 |
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_CUDA=ON -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_GSTREAMER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.6/site-packages -D OPENCV_EXTRA_MODULES_PATH=~/installcv/opencv_contrib-4.2.0/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON -D BUILD_SHARED_LIBS=OFF .. |
If you don’t want to include CUDA, just set -D WITH_CUDA=OFF
1 2 |
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-6 -D CMAKE_INSTALL_PREFIX=/home/{youraccount}/opencv -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_CUDA=OFF -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_GSTREAMER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.6/site-packages -D OPENCV_EXTRA_MODULES_PATH=~/installcv1/opencv_contrib-4.2.0/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON .. |
If you want to use CUDNN, you must include these tags in Cmake (set the CUDA_ARCH_BIN value correctly)
1 2 3 |
-D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=6.1 \ |
On https://developer.nvidia.com/cuda-gpus , you can see the compatible CUDA version of your network card.
Before the official compilation, you must check whether CUDA is enabled in Cmake output
1 2 3 |
-- NVIDIA CUDA: YES (ver 10.0, CUFFT CUBLAS NVCUVID FAST_MATH) -- NVIDIA GPU arch: 30 35 37 50 52 60 61 70 75 -- NVIDIA PTX archs: |
Official compilation and installation
1 2 3 |
$ nproc $ make -j8 #!ubuntu 18.04sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen $ sudo make install |
Configure environment variables
1 2 3 |
$ sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf' $ sudo ldconfig $ |
If you want the python cv package to be used in the system environment, you must copy the work below
Test installation
Case procedure
Compile and execute
1 2 |
$ g++ -o test test.cpp `pkg-config opencv --cflags --libs` $ ./test |
FAQ
If the following error occurs during compilation, it means that the graphics card does not support the minimum requirements of CUDA
1 |
CUDA backend for DNN module requires CC 5.3 or higher. Please remove unsupported architectures from CUDA_ARCH_BIN option. |
Installation or compilation error
opencv_contrib-4.1.1/modules/cudaimgproc/src/cuda/bilateral_filter.cu:140: error: (-217:Gpu API call) invalid configuration argument in function'bilateral_caller'
The cmake project compilation appears undefined reference to `cv::freetype::createFreeType2()