22.7 C
Texas

How to Install OpenCV on Ubuntu

An open-source computer vision library containing bindings for C++, Python, and Java is also referred to as the Open Source Computer Vision(OpenCV) Library and is compatible with all commonly used operating systems. It can leverage multi-core processing and offers GPU acceleration for real-time operation. OpenCV is used for many different purposes, including 3D model extraction, face identification and recognition, object tracking, street view image stitching, medical image analysis, and many more things.

The Debian GNU/Linux distribution is the foundation of the open-source operating system (OS) known as Ubuntu. Ubuntu is well-liked in universities and research companies because it integrates all the functionality of a Unix OS with an additional customizable GUI. Although Ubuntu has a server edition, it is more frequently used on personal computers. Today, Ubuntu is available in a variety of flavors and specialized variants. Additionally, there are specialized editions for linked devices, OpenStack clouds, and servers.

This article outlines the OpenCV installation process for Ubuntu. Scroll down to the section of this article titled “Utilizing the Source to install OpenCV” to learn how to install the most recent stable version of OpenCV from source code. You can select the installation technique that best satisfies your needs.

Utilizing the Ubuntu Repository to install OpenCV

- Advertisement -

Installing OpenCV is possible using the standard Ubuntu repository. In order to install OpenCV, issue the command listed below.

$ sudo apt install libopencv-dev python3-opencv

The aforementioned script will install all the necessary packages for OpenCV.

In order to verify the installation, import the cv2 module and output the OpenCV version:

$ python3 -c “import cv2; print(cv2.__version__)”  

The output above shows that the version in the repositories at the time of writing is “4.2.0.”

Utilizing the Source to install OpenCV

By compiling the OpenCV library yourself from the source code, you can get the most recent version. The build will be customized for your particular machine under your complete control. It is suggested to use this technique to install OpenCV.

To install the most recent OpenCV version directly from the source, follow these instructions:

Step 1: Install the necessary dependencies and build tools:

$  sudo apt 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 libdc1394-22-dev libopenexr-dev \     libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev  

Step 2: Copies of the OpenCV and its contrib repositories should be made:

$ mkdir ~/opencv_build && cd ~/opencv_build $ git clone https://github.com/opencv/opencv.git $ git clone https://github.com/opencv/opencv_contrib.git  

The most recent default version in the github source is version 4.3.0. By entering the command git checkout opencv-version in the opencv and opencv contrib directories, you can install an older version of OpenCV.

Step 3:  Create a temporary build directory, and then go to it after the download is finished:

$ cd ~/opencv_build/opencv $ mkdir -p build && cd build  

You can easily configure CMake to create OpenCV:

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

Step 4: Begin the compilation process using:

$ make -j8  

Adapt the -j flag to your processor’s needs. You can find out your processor’s core count by typing nproc if you’re unsure. The length of time it takes to compile varies depending on your system setup.

Step 5: Set up OpenCV:

$ sudo make install

Step 6: In order to check the installation, type the following instructions; the OpenCV version should appear.

Bindings in C++:

$ pkg-config –modversion opencv4  

Bindings in Python:

$ python3 -c “import cv2; print(cv2.__version__)”  

Conclusion

You’ve now seen two different methods for installing OpenCV on Ubuntu. The approach you select is determined by your needs and interests. Installing OpenCV from the Ubuntu repository’s packaged version is simpler, but creating OpenCV from source allows you more freedom and ought to be your first choice.

For more how-tos and tutorials, please visit linuxgenie.net

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article