How to Install OpenCV on Ubuntu? [SOLVED]


Ubuntu

Author: Omer Cakmak
Reviewer: Deepak Prasad

OpenCV is a powerful open-source computer vision library that enables developers to perform complex image and video processing tasks in their applications. Installing OpenCV on Ubuntu is a straightforward process that requires a few simple steps.

In this article, we will provide a step-by-step guide on how to install OpenCV on Ubuntu. We will cover the prerequisites required for the installation, the installation process itself, and the necessary steps to verify that OpenCV is correctly installed and configured.

By following this guide, developers will be able to install OpenCV on their Ubuntu machine and start working with this powerful computer vision library in their applications. Whether you are a beginner or an experienced developer, this guide will provide you with the knowledge and resources you need to install OpenCV and start developing innovative computer vision applications.

 

Pre-requisites

Here are the necessary prerequisites required to install OpenCV on Ubuntu:

 

Install Development tools

To install OpenCV on Ubuntu, you will need development tools such as CMake, GCC, and Git. You can install these tools using apt-get command:

sudo apt-get update
sudo apt-get install build-essential cmake git

 

Install Required libraries

OpenCV has several dependencies that must be installed before building and installing the library. Some of the required libraries include:

  • Image I/O libraries: libjpeg-dev, libpng-dev, libtiff-dev
  • Video I/O libraries: libavcodec-dev, libavformat-dev, libswscale-dev, libv4l-dev
  • GUI libraries: libgtk2.0-dev, libgtk-3-dev
  • Math libraries: libatlas-base-dev, gfortran

You can install these libraries by running the following command in the terminal:

 

Different methods to install OpenCV on Ubuntu

There are many ways to install OpenCV on Ubuntu. After reviewing the methods, you can choose one for installation.

 

Method-1: Install  from Repository

First update the package list:

$ sudo apt update -y

Install the OpenCV package with the following command:

$ sudo apt install python3-opencv libopencv-dev

Both libopencv-dev and python3-opencv are packages related to the OpenCV library, but they serve different purposes.

libopencv-dev is a package that contains the development files and headers required to build applications that use the OpenCV library. It includes the C++ header files, libraries, and other files necessary for developing OpenCV applications. Developers can use this package to build and link their applications with the OpenCV library.

python3-opencv, on the other hand, is a package that provides the Python bindings for the OpenCV library. It allows developers to use OpenCV functions and modules in their Python programs. This package is required if you want to develop OpenCV applications using Python.

It may take some time to install the required dependencies. After installation, you can see the OpenCV version with the following steps:

$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.5.4
>>>

OpenCV version 4.5.4 has been successfully installed.

You can also verify if OpenCV has been installed using following commands:

// command 1
# pkg-config --list-all | grep opencv
opencv4                        OpenCV - Open Source Computer Vision Library

// command 2
# pkg-config --cflags --libs opencv4
-I/usr/include/opencv4 -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_ml -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

// command 3
# dpkg -l | grep libopencv
ii  libopencv-calib3d-dev:amd64                4.5.4+dfsg-9ubuntu4                     amd64        development files for libopencv-calib3d4.5d
ii  libopencv-calib3d4.5d:amd64                4.5.4+dfsg-9ubuntu4                     amd64        computer vision Camera Calibration library
ii  libopencv-contrib-dev:amd64                4.5.4+dfsg-9ubuntu4                     amd64        development files for libopencv-contrib4.5d
ii  libopencv-contrib4.5d:amd64                4.5.4+dfsg-9ubuntu4                     amd64        computer vision contrlib library

 

Method-2: Install from Source Code

For installation from source code, the following packages must first be installed on the system:

$ sudo apt install wget unzip build-essential cmake -y

The .zip file is downloaded from the versions published on the OpenCV official github page.

Install Opencv on Ubuntu

By copying the link of the zip file from the browser, it is written after the wget command and the download is started.

$ wget https://github.com/opencv/opencv/archive/refs/tags/4.7.0.zip
--2023-03-21 19:58:05-- https://github.com/opencv/opencv/archive/refs/tags/4.7.0.zip
Resolving github.com (github.com)... 140.82.121.3
Connecting to github.com (github.com)|140.82.121.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/opencv/opencv/zip/refs/tags/4.7.0 [following]
--2023-03-21 19:58:05-- https://codeload.github.com/opencv/opencv/zip/refs/tags/4.7.0
Resolving codeload.github.com (codeload.github.com)... 140.82.121.9
Connecting to codeload.github.com (codeload.github.com)|140.82.121.9|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘4.7.0.zip’

4.7.0.zip [<=> ] 91.17M 764KB/s in 40s

2023-03-21 19:58:46 (2.29 MB/s) - ‘4.7.0.zip’ saved [95594055]

Extract from the zip file:

$ unzip 4.7.0.zip

Go to the OpenCV directory:

$ cd opencv-4.7.0/

Create build directory and go to this directory:

$ mkdir build &&  cd build

Configure:

$ cmake ../../opencv-4.7.0

Then build:

$ cmake --build .

The build process may take some time.

$ sudo make install

OpenCV is installed in /usr/local. All files added to OpenCV were copied to the following locations:

  • /usr/local/bin - executables
  • /usr/local/lib - libraries (.so)
  • /usr/local/cmake/opencv4 - cmake package
  • /usr/local/include/opencv4 - headers
  • /usr/local/share/opencv4 - other files (eg XML-trained digits)

For example:

$ ls /usr/local/bin/opencv*
/usr/local/bin/opencv_annotation
/usr/local/bin/opencv_interactive-calibration
/usr/local/bin/opencv_model_diagnostics
/usr/local/bin/opencv_version
/usr/local/bin/opencv_visualisation

Installed version:

$ /usr/local/bin/opencv_version
4.7.0

 

Method-3: Install With PIP

Another method for OpenCV installation is to install with Pip. Pip is a python library, package manager that allows you to download and install packages from the (Python Package Index) PyPi repository. First, the pip package is installed on the system:

$ sudo apt install pip

Then the OpenCV installation is started with the help of pip:

$ pip3 install opencv-python
Defaulting to user installation because normal site-packages is not writeable
Collecting opencv-python
  Downloading opencv_python-4.7.0.72-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (61.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.8/61.8 MB 2.6 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.0 in /usr/local/lib/python3.10/dist-packages (from opencv-python) (1.24.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.7.0.72

Installation completed successfully.

After installation, you can see the OpenCV version with the following steps:

$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.7.0
>>> exit()

OpenCV version 4.7.0 has been successfully installed.

Note: If you get the error "ImportError: libGL.so.1: cannot open shared object file: No such file or directory" when you import OpenCV into the project, you should install the following package:

sudo apt install libgl1-mesa-glx

 

Summary

In summary, installing OpenCV on Ubuntu involves installing the necessary development tools and libraries, installing the OpenCV package, and verifying the installation. This can be done using the command line and does not require any special configuration or setup. Once installed, you can use OpenCV for a wide range of computer vision and image processing tasks, such as object detection, face recognition, and video analysis. Additionally, if you plan to use OpenCV with Python, you can install the Python bindings for OpenCV to use OpenCV functions and modules in your Python programs. Overall, OpenCV is a powerful and versatile library that can be used for a variety of applications, and installing it on Ubuntu is a simple and straightforward process.

Feel free to comment on any problems you encounter during installation.

 

References

docs.opencv.org -  Install OpenCV-Python in Ubuntu
docs.opencv.org - Installation in Linux

 

Views: 72
Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment