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 library of open source programming functions that helps programmers develop software for real-time computer vision to analyse 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 Ubuntu repository or install from 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 systems that runs on desktops, laptops, server and other devices.
When using Ubuntu, you will find that Linux isn’t so different than Windows and other operating systems in so many ways, especially when it comes to using the system to get work done.
Both Ubuntu and Windows systems allow you to be productive, easy to use, reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses.
To get started with installing OpenCV, using the method below:
Step 1: Install from Ubuntu Repository
The easiest way to install OpenCV on Ubuntu is using Ubuntu repository to install it. All its packages will be downloaded and install via simple command.
To do install it from 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. 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.
Method 2: Installing from Source
For those who want to customize OpenCV installation can install from its source. Using this method is recommended which can be tailored to particular system configurations and can give you control on how its’ installed.
To install from source, run the commands below to install 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 OpenCV repository at Github to download the latest version.
cd ~/opencv_base git clone git clone
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 -- Custom HAL: NO -- Protobuf: build (3.5.1) -- -- OpenCL: YES (no extra features) -- Include path: /home/richard/opencv_base/opencv/3rdparty/include/opencl/1.2 -- Link libraries: Dynamic load -- -- Python 3: -- Interpreter: /usr/bin/python3 (ver 3.6.9) -- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.9) -- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3) -- install path: lib/python3.6/dist-packages/cv2/python-3.6 -- -- Python (for build): /usr/bin/python3 -- -- Java: -- ant: NO -- JNI: 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 the number of CPU cores your computer has.
My system has 2 processors, so I used the number 2. If yours has 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 from its source code. If you find any error above, please report it in the comment form.
You may also like the post below: