8 C
Texas

How To Install OpenCV on Ubuntu 20.04

Today we are going to learn that how to install OpenCV on Ubuntu 20.04. Before proceeding towards the installation process, let’s have a short introduction about it.

OpenCV is an open source, highly optimized computer library mainly used in applications for image processing. It has variety of inbuilt images processing functions helping you to get started easily. You can read the documentation to handle OpenCV in more easy way. Here I’ll list the main & important modules in the project.

  • core = Core functionality
  • imgproc = Image Processing
  • imgcodecs = Image file reading and writing
  • videoio = Video I/O
  • highgu = 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

We’ll see two methods to install OpenCV on Ubutnu 20.04.

  • From Ubutnu Pre-built Binaries
  • By Building From Source (Recommmended)

Just follow the below steps for an easy installation.

Step 1: Update Your System

- Advertisement -

As usual we do, before installing any new package, fetch the latest updates for all packages if available. Hit the below command to update & upgrade your system.

sudo apt update && sudo apt upgrade

After the update finished, restart your system.

sudo reboot

Step 2: Install OpenCV on Ubuntu 20.04 from Binaries

When the system is backup, hit the following command to install the OpenCV on Ubuntu 20.04.

sudo apt update
sudo apt install python3-opencv

Here you’ll see a large list of packages to be installed on Ubuntu 20.04. Press “Y” to continue to the installation process.

sabir@Ubuntu20:~$ sudo apt install python3-opencv
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
autoconf automake autotools-dev cpp-8 gcc-8 gcc-8-base
gdal-data gfortran gfortran-8 gfortran-9 i965-va-driver
ibverbs-providers intel-media-va-driver libaacs0 libaec0
libaom0 libarmadillo9 libarpack2 libavcodec58
libavformat58 libavutil56 libbdplus0 libblas3 libbluray2
libcaf-openmpi-3 libcfitsio8 libcharls2 libchromaprint1
libcoarrays-dev libcoarrays-openmpi-dev libcodec2-0.9
libdap25 libdapclient6v5 libdc1394-22 libepsilon1
libevent-core-2.1-7 libevent-dev libevent-extra-2.1-7
libevent-openssl-2.1-7 libevent-pthreads-2.1-7 libfabric1
libfreexl1 libfyba0 libgcc-8-dev libgdal26 libgdcm3.0
libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran-8-dev
libgfortran-9-dev libgfortran5 libgl2ps1.4 libgme0
libgsm1 libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103
libhwloc-dev libhwloc-plugins libhwloc15 libibverbs-dev
libibverbs1 libigdgmm11 libilmbase24 libjsoncpp1
libkmlbase1 libkmldom1 libkmlengine1 liblapack3 liblept5
libltdl-dev libminizip1 libmpx2 libnetcdf-c++4
libnetcdf15 libnl-3-dev libnl-route-3-dev libnuma-dev
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 libopenmpi-dev libopenmpi3
libopenmpt0 libpmix2 libpq5 libproj15 libpsm-infinipath1
libpsm2-2 libqhull7 librdmacm1 libshine3 libsocket++1
libspatialite7 libssh-gcrypt-4 libsuperlu5 libswresample3
libswscale5 libsz2 libtbb2 libtesseract4 libtool
liburiparser1 libva-drm2 libva-x11-2 libva2 libvdpau1
libvtk6.3 libx264-155 libx265-179 libxerces-c3.2
libxnvctrl0 libxvidcore4 libzvbi-common libzvbi0 m4
mesa-va-drivers mesa-vdpau-drivers ocl-icd-libopencl1
odbcinst odbcinst1debian2 openmpi-bin openmpi-common
proj-bin proj-data python3-numpy va-driver-all
vdpau-driver-all
Suggested packages:
autoconf-archive gnu-standards autoconf-doc gettext
gcc-8-locales gcc-8-multilib gcc-8-doc gfortran-multilib
gfortran-doc gfortran-8-multilib gfortran-8-doc
gfortran-9-multilib gfortran-9-doc i965-va-driver-shaders
libbluray-bdj geotiff-bin gdal-bin libgeotiff-epsg
libhdf4-doc libhdf4-alt-dev hdf4-tools
libhwloc-contrib-plugins libtool-doc libmyodbc
odbc-postgresql tdsodbc unixodbc-bin ogdi-bin openmpi-doc
gcj-jdk mpi-default-bin m4-doc opencl-icd
python-numpy-doc python3-dev python3-pytest
python3-numpy-dbg libvdpau-va-gl1 nvidia-vdpau-driver
nvidia-legacy-340xx-vdpau-driver
nvidia-legacy-304xx-vdpau-driver
The following NEW packages will be installed:
autoconf automake autotools-dev cpp-8 gcc-8 gcc-8-base
gdal-data gfortran gfortran-8 gfortran-9 i965-va-driver
ibverbs-providers intel-media-va-driver libaacs0 libaec0
libaom0 libarmadillo9 libarpack2 libavcodec58
libavformat58 libavutil56 libbdplus0 libblas3 libbluray2
libcaf-openmpi-3 libcfitsio8 libcharls2 libchromaprint1
libcoarrays-dev libcoarrays-openmpi-dev libcodec2-0.9
libdap25 libdapclient6v5 libdc1394-22 libepsilon1
libevent-core-2.1-7 libevent-dev libevent-extra-2.1-7
libevent-openssl-2.1-7 libevent-pthreads-2.1-7 libfabric1
libfreexl1 libfyba0 libgcc-8-dev libgdal26 libgdcm3.0
libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran-8-dev
libgfortran-9-dev libgfortran5 libgl2ps1.4 libgme0
libgsm1 libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103
libhwloc-dev libhwloc-plugins libhwloc15 libibverbs-dev
libibverbs1 libigdgmm11 libilmbase24 libjsoncpp1
libkmlbase1 libkmldom1 libkmlengine1 liblapack3 liblept5
libltdl-dev libminizip1 libmpx2 libnetcdf-c++4
libnetcdf15 libnl-3-dev libnl-route-3-dev libnuma-dev
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 libopenmpi-dev libopenmpi3
libopenmpt0 libpmix2 libpq5 libproj15 libpsm-infinipath1
libpsm2-2 libqhull7 librdmacm1 libshine3 libsocket++1
libspatialite7 libssh-gcrypt-4 libsuperlu5 libswresample3
libswscale5 libsz2 libtbb2 libtesseract4 libtool
liburiparser1 libva-drm2 libva-x11-2 libva2 libvdpau1
libvtk6.3 libx264-155 libx265-179 libxerces-c3.2
libxnvctrl0 libxvidcore4 libzvbi-common libzvbi0 m4
mesa-va-drivers mesa-vdpau-drivers ocl-icd-libopencl1
odbcinst odbcinst1debian2 openmpi-bin openmpi-common
proj-bin proj-data python3-numpy python3-opencv
va-driver-all vdpau-driver-all
0 upgraded, 149 newly installed, 0 to remove and 0 not upgraded.
Need to get 135 MB/135 MB of archives.
After this operation, 557 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Once the installation finished, verify the OpenCV installed by

sabir@Ubuntu20:~$ 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

After running these if you see the printed result than you’ve installed OpenCV otherwise check the above steps again.

Step 3: Install OpenCV on Ubutnu 20.04 by building from Source

In first method we’installed OpenCV using the Ubuntu binaries but it is not guaranteed that you’ll install the latest version of OpenCV installed on Ubuntu 20.04 system. So, the recommended method is building it from the source code. Follow the below steps to learn how you can build from source.

First of all install the 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

Then download the latest version from Github.

git clone https://github.com/opencv/opencv.git

You can also download the extra modules if you want by hitting below command.

git clone https://github.com/opencv/opencv_contrib.git

Then make the build directory.

cd opencv
sudo mkdir build
cd build

Now, include the downloaded modules.

cmake -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

Configuration Output:

You’ll see the similar configuration output of OpenCV.

--
-- 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: /home/sabir/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2020.0.0)
-- at: /home/sabir/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: /home/sabir/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/python2.7
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
-- Install to: /usr/local
-- -----------------------------------------------------------------

Then build & install the OpenCV by hitting

make -j $(nproc)
sudo make install

Wait for the installation to be finished.

You can find all the files under “/usr/local/” directory.

Print the version installed by

python 3
import cv2 as cv
print(cv.version)

So, this is how you can install OpenCV on Ubuntu 20.04. For more information and detailed documentation visit OpenCV official documentation guide.

So, share this post and join our Telegram Channel. Also, buy us a coffe.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article