How to Install and Set up OpenCV on Ubuntu

What Do I Need?

  • Any Dedicated or Virtual Server
  • Ubuntu
  • OpenCV
  • Terminal

What is OpenCV?

OpenCV is one of the most exciting releases of 2020. Originally launched in 1999, it’s a library of programming functions mainly aimed at real-time computer vision. The library is cross-platform and free for any use, academic, research, and commercial.

    1. Install OpenCV 4 Dependencies
      1. Make sure that your Ubuntu install is up to date:
        sudo apt-get upgrade
        sudo apt-get update

      1. Install developer tools:
        sudo apt-get install build-essential cmake unzip pkg-config

        How to Install and Set up OpenCV on Ubuntu

      1. Next, install the required image and video input, output, I/O, libraries. These libraries enable the loading of images from disk as well as reading video files:
        sudo apt-get install libjpeg-dev libpng-dev libtiff-dev

        How to Install and Set up OpenCV on Ubuntu

        sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

        How to Install and Set up OpenCV on Ubuntu

        sudo apt-get install libxvidcore-dev libx264-dev

        How to Install and Set up OpenCV on Ubuntu

      1. We have to install GTK for our graphical user interface backend:
        sudo apt-get install libgtk-3-dev

        How to Install and Set up OpenCV on Ubuntu

      1. This is followed by installing two packages that contain the mathematical optimizations for OpenCV:
        sudo apt-get install libatlas-base-dev gfortran

        How to Install and Set up OpenCV on Ubuntu

      1. And finally, let’s install the necessary Python 3 development headers:
        sudo apt-get install python3-dev

        How to Install and Set up OpenCV on Ubuntu

    1. Download OpenCV 4
      1. With the prerequisites now installed we can move on to downloading and installing OpenCV 4:
        wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip

        How to Install and Set up OpenCV on Ubuntu

        wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip

        How to Install and Set up OpenCV on Ubuntu

      1. Now, let’s unzip the compressed folders that we’ve just downloaded:
        unzip opencv.zip
        unzip opencv_contrib.zip

      1. It’s also recommended to rename the uncompressed directories:
        mv opencv-4.0.0 opencv
        mv opencv_contrib-4.0.0 opencv_contrib

  1. Install Pip

Now we’re going to install an awesome package management system, called Pip. It’s used to install and manage software packages in Python.

wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

How to Install and Set up OpenCV on Ubuntu

  1. Install OpenCV using Pip

Normally it’d be recommended to install OpenCV on Ubuntu into a virtual environment with Pip; however, for this demonstration, we’re keeping it ‘simple stupid’:

sudo pip install opencv-contrib-python

How to Install and Set up OpenCV on Ubuntu

Next Steps

If you’re working in a research or production environment I recommend installing OpenCV into a virtual environment. Doing this offers huge benefits; for example, the ability to develop and run multiple projects on your system with isolated packages, many with version dependencies, without having to compromise your system integrity. It’s also super flexible, allowing you to add and remove virtual environments freely, at your own discretion. Bottom line, virtual environments are the best practice for Python development.

In order to create and manage Python virtual environments you’ll need to install virtualenv and virtualenvwrapper:

        sudo pip install virtualenv virtualenvwrapper
        sudo rm -rf ~/get-pip.py ~/.cache/pip

Conclusion

Computer vision is potentially one of the most important research and development areas of the twenty-first century. The applications are untold and practically cover all facets of society, from document scanning and object tracking to facial recognition and surveillance. OpenCV includes deep learning algorithms that make recognizing the world around you quick and efficient.

If you’re interested in learning to apply facial or vehicle number plate recognition to video streams, building a complete deep learning pipeline for image classification, or simply want to experiment with your Raspberry Pi to create an autonomous camera monitoring system, you’ll need to learn OpenCV.

Learning this bleed-edge technology used to be very hard to get into. The documentation was tough to navigate and largely arcane. Plus, most of the books required a near doctorate level understanding of math. Now, that’s no longer necessary. It’s become infinitely easier over the years; more recently, with incredible resources like PyImageSearch and OpenCV readily available online, including large cooperative and helpful communities.

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Install the Django Web Framework on a Ubuntu 22.04 VPS

Brief description When building a website, similar components are required, and
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How To Change the Hostname on an Ubuntu 18.04 VPS or Dedicated Server

Ubuntu 18.04 has been described as awesome by Mark Shuttleworth
3 min read
Vladimir Rakov
Vladimir Rakov
Hosting Expert

How to Add Swap Space on Your Ubuntu 16.04 Server

Step 1: First of all you need to see if there is some swap space availab
5 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top