Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Install OpenCV on Ubuntu 20.04 - DesignLinux

designlinux 0 Comments

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library with bindings for C++, Python, and Java and supports all major operating systems. It can take advantage of multi-core processing and features GPU acceleration for real-time operation.

OpenCV is used for a wide range of applications, including medical image analysis, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and much more.

This article describes how to install OpenCV on Ubuntu 20.04. To install the latest stable version of OpenCV from source, scroll down to the Installing OpenCV from the Source section of this tutorial. Choose the installation method that works best for you.

Installing OpenCV from the Ubuntu Repository #

OpenCV is available for installation from the default Ubuntu 20.04 repositories. To install it run:

sudo apt updatesudo apt install libopencv-dev python3-opencv

The command above will install all packages necessary to run OpenCV.

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

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

At the time of writing, the version in the repositories is 4.2:

4.2.0

Installing OpenCV from the Source #

Building the OpenCV library from the source allows you to have the latest available version. It will be optimized for your particular system, and you will have complete control over the build options. This is the recommended way of installing OpenCV.

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

  1. Install the build tools and dependencies:

    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
  2. Clone the OpenCV’s and OpenCV contrib repositories:

    mkdir ~/opencv_build && cd ~/opencv_buildgit clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git

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

  3. Once the download is complete, create a temporary build directory, and navigate to it:

    cd ~/opencv_build/opencvmkdir -p build && cd build

    Set up the OpenCV build with CMake:

    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 look something like below:

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

    make -j8

    Modify the -j flag according to your processor. If you do not know the number of cores your processor, you can find it by typing nproc.

    The compilation may take several minutes or more, depending on your system configuration.

  5. Install OpenCV with:

    sudo make install
  6. To verify the installation, type the following commands and you should see the OpenCV version.

    C++ bindings:

    pkg-config --modversion opencv4
    4.3.0

    Python bindings:

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

Conclusion #

We have shown you two different ways to install OpenCV on your Ubuntu 20.04 machine. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu repository is easier, building OpenCV from source gives you more flexibility, and it should be your first option when installing OpenCV.

If you have any questions or feedback, feel free to comment below.

opencv python ubuntu

Related

Tags: opencv, python, ubuntu

How to Install Elasticsearch on Ubuntu 20.04

Prev Post

Sysctl Command in Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 608
  • 1,055,380

DesignLinux.com © All rights reserved

Go to mobile version