How to install OpenCV on Ubuntu 20.04

OpenCV (Open Source Computer Vision Library) is an open source computer vision library that supports C++, Python, and Java on all major operating systems. It can play multi-core processes and GPU acceleration for real-time operation.

OpenCV has a wide range of applications, including medical image analysis, street view image processing, surveillance video, detection and recognition of faces, tracking moving objects, extracting 3D models, and so on.

This article describes how to install OpenCV on Ubuntu 20.04. To install the latest version of OpenCV from the source code, slide down to the section Installing OpenCV from the Source. Please choose the installation method that suits you best.

1. Install OpenCV from Ubuntu source repository

OpenCV is available in Ubuntu 20.04 software sources. To install it, run:

sudo apt update
sudo apt install libopencv-dev python3-opencv

The above command will install all the necessary packages to run OpenCV:

Verify the installation result by importing the cv2 module and printing the OpenCV version:

python3 -c "import cv2; print(cv2.__version__)"

At the time of writing, the version of the software source is 4.2:

Output:

4.2.0

2. Install OpenCV from source code

Installing OpenCV from source code allows you to install the latest available version. It will also be optimized for your specific system, and you have complete control over all build options. This is the most recommended way to install OpenCV.

Perform the following steps to install the latest OpenCV version from source code:

  1. Install the build tools and all dependent packages:
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
 libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
 libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
 gfortran openexr libatlas-base-dev python3-dev python3-numpy \
 libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
 libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
  1. Clone all OpenCV and OpenCV contrib sources:
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

At the time of writing, the default version in the github software source is 4.3.0. If you want to install an older version of OpenCV, cd to the opencv and opencv_contrib directories, and run git checkout<opencv-version> .

  1. Once the download is complete, create a temporary build directory and switch to this directory:
cd ~/opencv_build/opencv
mkdir -p build && cd build

Use CMake command to configure OpenCV build:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
 - D CMAKE_INSTALL_PREFIX=/usr/local \
 - D INSTALL_C_EXAMPLES=ON \
 - D INSTALL_PYTHON_EXAMPLES=ON \
 - D OPENCV_GENERATE_PKGCONFIG=ON \
 - D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
 - D BUILD_EXAMPLES=ON ..

The output will be as follows:

- - Configuring done
- - Generating done
- - Build files have been written to:/home/vagrant/opencv_build/opencv/build
  1. Start the compilation process:
make -j8

Modify the value of -f according to your processor. If you don't know the number of your processor cores, you can find it by typing nproc.

The compilation will take a few minutes or more, depending on your system configuration.

  1. Install OpenCV:
sudo make install
  1. To verify the installation result, enter the following command, then you will see the OpenCV version:

C++ bindings:

pkg-config --modversion opencv4

Output:

4.3.0

Python bindings:

python3 -c "import cv2; print(cv2.__version__)"

Output:

4.3.0- dev

Three, summary##

We have shown two different ways to install OpenCV on your Ubuntu 20.04 server. Your first choice depends on your requirements and preferences.

Even though it is easy to install packages directly from Ubuntu source, building and installing OpenCV from source will give you more flexibility, and it should be your first choice for installing OpenCV.

Recommended Posts

How to install opencv3.0.0 on ubuntu14.04
How to install OpenCV on Ubuntu 20.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install PrestaShop on Ubuntu 16.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install OpenCV on CentOS 8
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Bacula Server on Ubuntu 14.04
How to install Ubuntu 19.10 on Raspberry Pi 4
How to install Apache Kafka on Ubuntu 18.04
How to install Apache Maven on Ubuntu 20.04
How to install Apache Tomcat 8 on Ubuntu 16.04
How to install GCC compiler on Ubuntu 18.04
How to install Graylog 1.x on Ubuntu 14.04.
How to install Zabbix on Ubuntu 16.04 Server
How to install Opera web browser on Ubuntu 18.04
How to install Prometheus with Docker on Ubuntu 14.04
How to install Visual Studio Code on Ubuntu 20.04
How to install Squid proxy server on Ubuntu 18.04
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
How to install and configure NATS on Ubuntu 16.04
How to install and configure Gogs on Ubuntu 18.04