Feb 22nd, 2022
- Operating system: Ubuntu 20.04 x84_64 (64-bit)
- Architecture: amd64
- GPU: NVIDIA GeForce RTX 3090
- Python 3.8
-
Uninstall built OpenCV (if there is a built installation of opencv already)
-
Update and upgrade your system
$ sudo apt update $ sudo apt upgrade
-
Required packages
$ sudo apt-get install build-essential cmake pkg-config unzip yasm git checkinstall # generic tools $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev # required
-
Python dependencies
$ sudo apt-get install python3-dev python3-numpy python3-pip $ sudo apt install python3-testresources
-
Image I/O
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
-
Video and audio
$ 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 for speed codec (adaptive multi-rate narrow band (AMRNB) and wide band (AMRWB))
$ sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev
-
Camera programming interface
$ 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 $ sudo ldconfig $ cd ~
-
GTK for OpenCV highghui
$ sudo apt-get install libgtk-3-dev
-
Parallelism C++ for CPU
$ sudo apt-get install libtbb2 libtbb-dev
-
OpenCV optimization
$ sudo apt-get install libatlas-base-dev gfortran
-
Optional
$ 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
-
Remove
opencv-python-headless
$ python3 -m pip uninstall opencv-python-headless
-
Remove
python3-opencv
or add-D HAVE_opencv_python3=ON
to CMake Cofiguration$ sudo apt-get remove python3-opencv
$ cd ~
download opencv with git repository
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd ~/opencv
$ git checkout <opencv-version>
$ cd ~/opencv_contrib
$ git checkout <opencv-version>
or with archive packages
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/<version>.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/<version>.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ mv opencv-<version> opencv # rename
$ mv opencv_contrib-<version> opencv_contrib # rename
$ mkdir opencv_build
$ cd opencv_build
- Important: Check GPU Compute Capability to set
CUDA_ARCH_BIN
flag - NVIDIA GeForce RTX 3090 is 8.6
- Note: OpenGL Dependencies
- Following setup does not provide a proper way to enable OpenGL Support in OpenCV. However, OpenGL could still be used normally outside OpenCV. (Setup OpenGL for Ubuntu 20.04)
- Avoid using
libgtkglext1
andlibgtkglext1-dev
packages to enable OpenGL Support. It helps CMake recognize OpenGL but causes compile error (OpenCV v4.x)!
- Sample Configuration:
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules/ \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.8/ \
-D PYTHON3_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.8/ \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.8.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=8.6 \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUFFT=ON \
-D WITH_CUBLAS=ON \
-D WITH_V4L=ON \
-D WITH_OPENCL=ON \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON ../opencv
- Check build-summary to have
NVIDIA CUDA: YES (ver <CUDA version>, CUFFT CUBLAS FAST_MATH)
NVIDIA GPU arch: <GPU Compute Capability>
NVIDIA PTX archs:
cuDNN: YES (ver <cuDNN version>)
- Sample build-summary
-- General configuration for OpenCV 4.5.5 =====================================
-- Version control: 4.5.5
--
-- Extra modules:
-- Location (extra): /home/hieu/opencv_contrib/modules
-- Version control (extra): 4.5.5
--
-- Platform:
-- Timestamp: 2022-02-22T04:49:13Z
-- Host: Linux 5.13.0-30-generic x86_64
-- CMake: 3.16.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (18 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (33 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /usr/bin/c++ (ver 9.3.0)
-- C++ flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
-- Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cvv java julia matlab ovis python2 viz
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI: GTK3
-- GTK+: YES (ver 3.24.20)
-- GThread : YES (ver 2.64.6)
-- GtkGlExt: NO
-- OpenGL support: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
-- TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
-- JPEG 2000: build (ver 2.4.0)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: YES (2.2.5)
-- FFMPEG: YES
-- avcodec: YES (58.54.100)
-- avformat: YES (58.29.100)
-- avutil: YES (56.31.100)
-- swscale: YES (5.5.100)
-- avresample: YES (4.0.0)
-- GStreamer: YES (1.16.2)
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: TBB (ver 2020.1 interface 11101)
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2020.0.0 Gold [2020.0.0]
-- at: /home/hieu/opencv_build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2020.0.0)
-- at: /home/hieu/opencv_build/3rdparty/ippicv/ippicv_lnx/iw
-- VA: NO
-- Lapack: NO
-- Eigen: YES (ver 3.3.7)
-- Custom HAL: NO
-- Protobuf: build (3.19.1)
--
-- NVIDIA CUDA: YES (ver 11.6, CUFFT CUBLAS FAST_MATH)
-- NVIDIA GPU arch: 86
-- NVIDIA PTX archs:
--
-- cuDNN: YES (ver 8.3.2)
--
-- OpenCL: YES (no extra features)
-- Include path: /home/hieu/opencv/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.8.10)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.22.2)
-- install path: lib/python3.8/site-packages/cv2/python-3.8
--
-- Python (for build): /usr/bin/python3
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hieu/opencv_build
- Check number of processors
$ nproc
- Compile
$ make -j<number of processors>
- Verify build
$ ls bin $ ls lib $ ls OpenCVConfig*.cmake $ ls OpenCVModules.cmake
$ sudo make install
$ sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
$ sudo ldconfig
- Check
cv2
package
$ ls /usr/local/lib/python3.8/site-packages/cv2
- Check
cv2
module in Python:
import cv2
print(cv2.__version__)
print(cv2.getBuildInformation())
- If
ModuleNotFoundError: No module named 'cv2'
, check for existence of/usr/local/lib/python3.8/site-packages
in pythonsite
.
$ python3 -m site
- If
/usr/local/lib/python3.8/site-packages
was not included in the python path yet, add it permanently to the python path usingPYTHONPATH
variable
PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.8/site-packages
export PYTHONPATH
- Add the script to either:
~/.bashrc
for user session usage/etc/profile
for system wide usage
$ cd ~/opencv_build
$ sudo make uninstall
$ cd ..
$ sudo rm -r opencv_build
- Check for the removal that whether the
import cv2
successful or not (using Python)
import cv2
print(cv2.__version__) #if successfully imported
- Careful: Make sure to remove all relevant files in
/usr/local
/usr/local/bin
/usr/local/lib
/usr/local/cmake/opencv4
/usr/local/include/opencv4
/usr/local/share/opencv4
/usr/local/lib/python3.8/dist-packages/cv2
/usr/local/lib/python3.8/site-packages/cv2
$ sudo find /usr/local -name "*opencv*" -exec rm -i {} \;
$ sudo find /usr/local -name "*cv2*" -exec rm -i {} \;
- Operating System: Ubuntu 20.04 x84_64 (64-bit)
- Architecture: amd64
- GPU: Nvidia GeForce RTX 3090
- OpenCV version: 4.5.5
- Python version: 3.8.10
- Installation with success on: Feb 22nd, 2022