How to Install OpenCV on Ubuntu Linux

|

|

The tutorial provides instructions to install OpenCV (Open Source Computer Vision Library) on Ubuntu. OpenCV, used in software development for real-time computer vision, can be installed in two ways. The simpler method installs it from the Ubuntu repository using simple command prompts. A more customizable installation approach involves downloading and installing directly from the Source.…

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

OpenCV (Open Source Computer Vision Library) is a powerful tool allowing programmers to develop real-time computer vision software. It analyzes medical images, detects and recognizes faces, stitches street view images, surveillance video, etc.

Installing OpenCV on Ubuntu, an open-source Linux operating system, allows developers to use its powerful features and tools to create applications that can be used on desktops, laptops, servers, and other devices.

Additionally, Ubuntu provides a stable and secure platform for developers to work on, making it an ideal choice for installing and working with OpenCV.

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

Install from the 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 a similar output as below: 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 the ~/opencv_base/OpenCV folder and run the commands below:

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

After running the commands above, set 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 lines similar to those 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:           
--     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 two processors, so I used the number 2. If yours has more 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 a 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:


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



2 responses to “How to Install OpenCV on Ubuntu Linux”

  1. Kristian Avatar
    Kristian

    Installed perfectly.
    I done several other online guides that have errors but this was working great and clear instructions.

  2. kc Avatar
    kc

    Hi
    It did install, now how to configure paths for library?

Leave a Reply to KristianCancel reply

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

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading