OpenCV is an open source, highly optimized computer vision library used in wide array of applications for image processing. The project is backed by a large community of Developers and users. OpenCV has lots of inbuilt image processing functions that helps you get started easily with documentation that has improved overtime. In this guide we will walk you through the steps used to install OpenCV on Ubuntu 20.04 Linux system.
OpenCV is developed with a modular structure to allow for feature extensions. The following are the main modules in the project but there are a number of extra modules available as well.
- core. Core functionality
- imgproc. Image Processing
- imgcodecs. Image file reading and writing
- videoio. Video I/O
- highgui. High-level GUI
- video. Video Analysis
- calib3d. Camera Calibration and 3D Reconstruction
- features2d. 2D Features Framework
- objdetect. Object Detection
- dnn. Deep Neural Network module
- ml. Machine Learning
- flann. Clustering and Search in Multi-Dimensional Spaces
- photo. Computational Photography
- stitching. Images stitching
- gapi. Graph API
Install OpenCV on Ubuntu 20.04
You can install OpenCV on Ubuntu 20.04 from Pre-built Binaries or build the packages required from source code if you need bleeding edge release of the software. We will show you the two methods so you can choose one that works best for you.
Method 1: Install OpenCV on Ubuntu 20.04 from Pre-built Binaries
Update your Ubuntu system to get latest packages.
sudo apt update
sudo apt upgrade
If you have any Kernel updates, consider performing a system reboot.
sudo systemctl reboot
Once the system is back up, install OpenCV Python package using the commands below.
sudo apt update
sudo apt install python3-opencv
You will see a long list of packages to be installed, just hit the y key to proceed with the installation.
.....
The following NEW packages will be installed:
adwaita-icon-theme fontconfig fontconfig-config fonts-dejavu-core gdal-data gtk-update-icon-cache hicolor-icon-theme humanity-icon-theme libaec0
libaom0 libarchive13 libarmadillo9 libarpack2 libatk-bridge2.0-0 libatk1.0-0 libatk1.0-data libatspi2.0-0 libavahi-client3 libavahi-common-data
libavahi-common3 libavcodec58 libavformat58 libavutil56 libblas3 libbluray2 libcairo-gobject2 libcairo2 libcfitsio8 libcharls2 libchromaprint1
libcodec2-0.9 libcolord2 libcups2 libcurl3-gnutls libdap25 libdapclient6v5 libdatrie1 libdc1394-22 libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2
libdrm-radeon1 libepoxy0 libepsilon1 libevent-2.1-7 libevent-core-2.1-7 libevent-pthreads-2.1-7 libfabric1 libfontconfig1 libfreexl1 libfyba0
libgdal26 libgdcm3.0 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-common libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran5 libgif7 libgl1 libgl1-mesa-dri
libgl2ps1.4 libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgme0 libgomp1 libgraphite2-3 libgsm1 libgstreamer-plugins-base1.0-0 libgtk-3-0
libgtk-3-common libharfbuzz0b libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103 libhwloc-plugins libhwloc15 libibverbs1 libice6 libilmbase24 libjbig0
libjpeg-turbo8 libjpeg8 libjson-glib-1.0-0 libjson-glib-1.0-common libjsoncpp1 libkmlbase1 libkmldom1 libkmlengine1 liblapack3 liblcms2-2 liblept5
libllvm9 libminizip1 libmp3lame0 libmpg123-0 libmysqlclient21 libnetcdf-c++4 libnetcdf15 libnl-route-3-200 libnspr4 libnss3 libodbc1 libogdi4.1
libopencv-calib3d4.2 libopencv-contrib4.2 libopencv-core4.2 libopencv-dnn4.2 libopencv-features2d4.2 libopencv-flann4.2 libopencv-highgui4.2
libopencv-imgcodecs4.2 libopencv-imgproc4.2 libopencv-ml4.2 libopencv-objdetect4.2 libopencv-photo4.2 libopencv-shape4.2 libopencv-stitching4.2
libopencv-superres4.2 libopencv-video4.2 libopencv-videoio4.2 libopencv-videostab4.2 libopencv-viz4.2 libopenexr24 libopenjp2-7 libopenmpi3
libopenmpt0 libopus0 liborc-0.4-0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpciaccess0 libpixman-1-0 libpmix2 libpoppler97 libpq5
libproj15 libprotobuf17 libpsm-infinipath1 libpsm2-2 libqhull7 libquadmath0 libraw1394-11 librdmacm1 librest-0.7-0 librsvg2-2 librsvg2-common
libsensors-config libsensors5 libshine3 libsm6 libsnappy1v5 libsocket++1 libsoup-gnome2.4-1 libsoxr0 libspatialite7 libspeex1 libssh-gcrypt-4
libsuperlu5 libswresample3 libswscale5 libsz2 libtbb2 libtesseract4 libthai-data libthai0 libtheora0 libtiff5 libtwolame0 liburiparser1 libva-drm2
libva-x11-2 libva2 libvdpau1 libvorbisenc2 libvpx6 libvtk6.3 libvulkan1 libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwebp6
libwebpmux3 libx11-6 libx11-data libx11-xcb1 libx264-155 libx265-179 libxau6 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-render0
libxcb-shm0 libxcb-sync1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxdmcp6 libxerces-c3.2 libxext6 libxfixes3 libxi6 libxinerama1
libxkbcommon0 libxnvctrl0 libxrandr2 libxrender1 libxshmfence1 libxt6 libxvidcore4 libxxf86vm1 libzvbi-common libzvbi0 mysql-common
ocl-icd-libopencl1 odbcinst odbcinst1debian2 proj-data python3-numpy python3-opencv ubuntu-mono x11-common
0 upgraded, 230 newly installed, 0 to remove and 0 not upgraded.
Need to get 131 MB of archives.
After this operation, 766 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Confirm installation of OpenCV on Ubuntu 20.04.
$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 as cv >>> print(cv.__version__) 4.2.0
If you get results printed out without any errors, it means you have successfully installed OpenCV on Ubuntu 20.04.
Method 2: Install OpenCV on Ubuntu 20.04 by building from source
With the first method, it is not guaranteed you’ll get the latest version of OpenCV installed on Ubuntu 20.04 machine. The alternative method is building it yourself from source code. There are a number of dependencies required for this method to work.
Install required dependencies.
sudo apt update
sudo apt install git gcc g++ ffmpeg cmake make python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev
Download the latest source from OpenCV’s GitHub Repository.
cd ~/
git clone https://github.com/opencv/opencv.git
To include extra modules, also download OpenCV’s extra modules source.
cd ~/
git clone https://github.com/opencv/opencv_contrib.git
Create build directory:
cd ~/
cd opencv
mkdir build
cd build
As we build OpenCV, we will include the modules from ~/opencv_contrib/modules.
cmake -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..
OpenCV configuration output sample.
.....
--
-- GUI:
-- GTK+: YES (ver 3.24.18)
-- GThread : YES (ver 2.64.2)
-- GtkGlExt: 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: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
-- 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 Jasper (ver 1.900.1)
-- OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2_3)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: YES
-- avcodec: YES (58.54.100)
-- avformat: YES (58.29.100)
-- avutil: YES (56.31.100)
-- swscale: YES (5.5.100)
-- avresample: NO
-- GStreamer: YES (1.16.2)
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2020.0.0 Gold [2020.0.0]
-- at: /root/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2020.0.0)
-- at: /root/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: /root/opencv/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.8.2)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.2)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
-- install path: lib/python3.8/dist-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: /root/opencv/build
Now build and install OpenCV on Ubuntu 20.04.
make -j $(nproc)
sudo make install
Be patient as the installation will take some minutes.
......
Scanning dependencies of target example_tutorial_pointPolygonTest_demo
[100%] Built target example_tutorial_generalContours_demo2
Scanning dependencies of target example_tutorial_cornerDetector_Demo
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_pointPolygonTest_demo.dir/tutorial_code/ShapeDescriptors/pointPolygonTest_demo.cpp.o
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_cornerDetector_Demo.dir/tutorial_code/TrackingMotion/cornerDetector_Demo.cpp.o
[100%] Linking CXX executable ../../bin/example_tutorial_porting_anisotropic_image_segmentation_gapi
[100%] Linking CXX executable ../../bin/example_cpp_dft
[100%] Built target example_tutorial_porting_anisotropic_image_segmentation_gapi
Scanning dependencies of target example_tutorial_periodic_noise_removing_filter
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_periodic_noise_removing_filter.dir/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp.o
[100%] Linking CXX executable ../../bin/example_tutorial_cornerDetector_Demo
[100%] Built target example_cpp_dft
[100%] Linking CXX executable ../../bin/example_tutorial_pointPolygonTest_demo
Scanning dependencies of target example_tutorial_cornerSubPix_Demo
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_cornerSubPix_Demo.dir/tutorial_code/TrackingMotion/cornerSubPix_Demo.cpp.o
[100%] Built target example_tutorial_cornerDetector_Demo
[100%] Built target example_tutorial_pointPolygonTest_demo
[100%] Linking CXX executable ../../bin/example_tutorial_cornerSubPix_Demo
[100%] Linking CXX executable ../../bin/example_tutorial_periodic_noise_removing_filter
[100%] Built target example_tutorial_cornerSubPix_Demo
[100%] Built target example_tutorial_periodic_noise_removing_filter
.........
-- Installing: /usr/local/share/opencv4/samples/python/lk_homography.py
-- Installing: /usr/local/share/opencv4/samples/python/lk_track.py
-- Installing: /usr/local/share/opencv4/samples/python/logpolar.py
-- Installing: /usr/local/share/opencv4/samples/python/morphology.py
-- Installing: /usr/local/share/opencv4/samples/python/mosse.py
-- Installing: /usr/local/share/opencv4/samples/python/mouse_and_match.py
-- Installing: /usr/local/share/opencv4/samples/python/mser.py
-- Installing: /usr/local/share/opencv4/samples/python/opencv_version.py
-- Installing: /usr/local/share/opencv4/samples/python/opt_flow.py
-- Installing: /usr/local/share/opencv4/samples/python/peopledetect.py
-- Installing: /usr/local/share/opencv4/samples/python/plane_ar.py
-- Installing: /usr/local/share/opencv4/samples/python/plane_tracker.py
-- Installing: /usr/local/share/opencv4/samples/python/qrcode.py
-- Installing: /usr/local/share/opencv4/samples/python/squares.py
-- Installing: /usr/local/share/opencv4/samples/python/stereo_match.py
-- Installing: /usr/local/share/opencv4/samples/python/stitching.py
-- Installing: /usr/local/share/opencv4/samples/python/stitching_detailed.py
-- Installing: /usr/local/share/opencv4/samples/python/text_skewness_correction.py
-- Installing: /usr/local/share/opencv4/samples/python/texture_flow.py
-- Installing: /usr/local/share/opencv4/samples/python/tst_scene_render.py
-- Installing: /usr/local/share/opencv4/samples/python/turing.py
-- Installing: /usr/local/share/opencv4/samples/python/video.py
-- Installing: /usr/local/share/opencv4/samples/python/video_threaded.py
-- Installing: /usr/local/share/opencv4/samples/python/video_v4l2.py
-- Installing: /usr/local/share/opencv4/samples/python/watershed.py
All the files are installed in the “/usr/local/” directory.
$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 as cv >>> print(cv.__version__) 4.3.0-dev
You now have OpenCV installed on Ubuntu 20.04 Linux using either of the methods shown in this guide. Refer to OpenCV documentation for more details.