How to Install OpenCV on Ubuntu Linux

template 1599665 640
template 1599665 640

This brief tutorial shows students and new users how to install OpenCV on Ubuntu 20.04 | 18.04.

OpenCV (Open Source Computer Vision Library) is a library of open-source programming functions that helps programmers develop software for real-time computer vision to analyze medical images, detect and recognize faces, stitch street view images, surveillance video, and more.

This post shows you two ways to install OpenCV on Ubuntu. You can either install from the Ubuntu repository or install from the source. Either way should work for you.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.

Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.

To get started with installing OpenCV, use the method below:

Install from Ubuntu Repository

The easiest way to install OpenCV on Ubuntu is by installing the Ubuntu repository. All its packages will be downloaded and installed via simple commands.

To install it from the Ubuntu repository, run the commands below:

sudo apt update
sudo apt install python3-opencv

Running the commands above will download and compile all required packages for OpenCV and install them. After installing, you can begin using OpenCV functions in your applications.

To verify if OpenCV is installed, run the commands below:

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

You should see similar output as below which is the version number of OpenCV installed.

Output:
3.2.0

This is how to install OpenCV via Ubuntu default repositories.

Installing from Source

Those who want to customize OpenCV installation can install from its source. This recommended method can be tailored to particular system configurations and give you control over how it is installed.

To install from the source, run the commands below to install the required and optional packages to support OpenCV.

sudo apt update
sudo apt install build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev

After installing the packages above, run the commands below to create a folder for opencv_base in your home directory.

mkdir ~/opencv_base

Next, change into the directory and clone the OpenCV repository at Github to download the latest version.

cd ~/opencv_base
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Once the download is complete, change into ~/opencv_base/opencv folder and run the commands below:

cd ~/opencv_base/opencv
mkdir build && cd build

After running the commands above, setup OpenCV with Make by running the commands below:

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_base/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON .

After the make command above, you should see similar lines as below.

--     Intel IPP:                   2019.0.0 Gold [2019.0.0]
--            at:                   /home/richard/opencv_base/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2019.0.0)
--               at:                /home/richard/opencv_base/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done

Next, run the commands below to complete the compilation and install OpenCV. The make -j2 command option should represent your computer’s CPU cores.

My system has 2 processors, so I used the number 2. If yours has a higher number of processors, replace 2 with the number your system can handle.

make -j2
sudo make install

That should do it. To check whether OpenCV has been installed successfully type the following command and you should see the OpenCV version

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

You should see similar output as below:

Output:
4.2.0-dev

That’s it!

Conclusion:

This post shows you how to install OpenCV via Ubuntu default repositories and how to install it from its source code. If you find any error above, please report it in the comment form.

You may also like the post below:

Posted by
Richard W

I love computers; maybe way too much. What I learned I try to share at geekrewind.com.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: