OpenCV is an open-source machine learning software library and computer vision. It is used for image processing and performing computer vision tasks. With OpenCV, you can build real-time computer vision applications, including tracking moving objects, surveillance video, medical image analysis, and many more. It is cross-platform and supports many operating systems, including Linux, Windows, macOS, Android, OpenBSD, FreeBSD, and many more.

This tutorial will teach us how to install OpenCV on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on your server.

Getting Started

First, update and upgrade all system packages to the latest version using the following command:

apt update -y

apt upgrade -y

Once all the packages are updated, install the Python package using the following command:

apt install python3

Once Python is installed, verify the Python version using the following command:

python3 --version

You should see the following output:

Python 3.10.6

Install OpenCV from Debian Repository

By default, the OpenCV module is available in the Ubuntu 22.04 default repository. You can install it with the following command:

apt-get install python3-opencv -y

After the successful installation, you can verify the installed version of a module with the following command:

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

You should get the following output:

4.5.4

Once you are finished, you can proceed to the next step.

Install OpenCV from the Source

It is recommended way to install OpenCV from the source. So you can optimize if for a specific system.

First, install all the dependencies required to build OpenCV with the following command:

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

After installing all the dependencies, download the latest version of OpenCV and OpenCV contrib with the following command:

git clone https://github.com/opencv/opencv.git

git clone https://github.com/opencv/opencv_contrib.git

Next, change the directory to the opencv with the following command:

cd opencv

Next, create a temporary build directory and change the directory to it:

mkdir build

cd build

Next, set up the OpenCV build with CMake:

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_contrib/modules -D BUILD_EXAMPLES=ON ..

After successful completion, you should see the following output:

--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /root/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /root/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          NO
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /root/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.10.6)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.10.so (ver 3.10.6)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.21.5)
--     install path:                lib/python3.10/dist-packages/cv2/python-3.10
-- 
--   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
-- Build files have been written to: /root/opencv/build

Next, compile the OpenCV with the following command:

make -j2

The build process may take some time to finish. After the successful process, you can install the OpenCV with the following command:

make install

Next, verify the OpenCV installation with the following command:

pkg-config --modversion opencv4

You should get the following output:

4.6.0

You can also check the version of the OpenCV module with the following command:

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

You should see the following output:

4.2.0-dev

Conclusion

Congratulations! you have successfully installed the OpenCV library on Ubuntu 22.04 repository. You can now use OpenCV and start developing computer vision, and machine learning related applications. Feel free to ask me if you have any questions.

<img alt="Hitesh Jethva" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/hitesh-80.jpg65170ea4a4a55.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="76" src="data:image/svg xml,” width=”80″>

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My skills include a depth knowledge of Redhat/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.