| by Arround The Web | No comments

How To Install OpenCV in Ubuntu

The open-source Computer Vision Library is a popular computer vision library with bindings for different languages, including Java, Python, and C++. Furthermore, it supports other Operating Systems and has multiple applications, such as tracking moving objects, 3D-model extractions, facial recognition, and image analysis.

For developers, OpenCV offers an extensive collection of algorithms, especially for 3D modelling, and things can’t get any better than when using the tool. If you are looking for how to install OpenCV on Ubuntu and spice your support vector projects or enhance algorithms, you are in the right place. This guide covers two easy ways of installing OpenCV.

Installing OpenCV in Ubuntu

The OpenCV platform has two installation methods. Either using the repository or directly from the source. We will cover the steps for each method.

1. Install OpenCV via Repository

It is the easiest of the two methods as only two commands are needed.

First, update your repository.

$ sudo apt update

Next, install OpenCV using the following command:

$ sudo apt install libopencv-dev python3-opencv

That’s it! OpenCV is successfully installed on your system. You can confirm by checking its version using the C++ binding. Run the following command:

$  pkg-config –modversion opencv4

The output should return the current version, as shown in the image below:

2. Install OpenCV via Source Code

One shortcoming of installing OpenCV using the repository is that you may not get the latest version. The solution is to install it directly using its source code. Besides, this method optimizes OpenCV per your system, giving you absolute control over its build options. That said, the following steps will guide you.

Step 1. Install OpenCV Dependencies and Build Tools
The OpenCV is dependent on some packages for it to run. Therefore, copy the following command to 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. Clone OpenCV and Its Repositories
OpenCV can be cloned from its GitHub repository.

Start by creating a directory on your system.

$  mkdir ~/opencv_build && cd ~/opencv_build

Next, clone OpenCV using the following command:

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

Finally, clone its contrib repositories.

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

The previous steps installed the current version of OpenCV, but you can also choose to go with an older version. All you need is to navigate into the opencv and opencv_contrib directories, then run the following command:

$  git checkout <prefreed-opencv-old-version>

Step 3: Create a Build Directory
A temporary build directory is required where the OpenCV will get set up using CMake.

To create and navigate into the directory, use the commands shown below.

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

Next, use CMake to set up OpenCV build using the following command:

$  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 ..

You should note an output showing the configuration and generating is done and the path to the build files.

Step 4: Start Compilation
Once the build is done, it’s time to initiate the compilation. However, first, check the number of processors you have using the nproc Linux command.

$  nproc

Once checked, run the following command and replace the number after j with your processors:

$  make -j2

The compilation is time-consuming, so be patient.

Step 5: Install OpenCV
The final step is to run the following command, which installs OpenCV:

$ sudo make install

Step 6: Confirm Installation
Check the OpenCV version using bindings for different programming languages to verify the installation. We already checked the C++ bindings in method 1. Let’s use Python in this method. The command is:

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

Depending on when you are reading this article, your output may be the same or a newer version than the following image provided:

Conclusion

The OpenCV is a tool you can’t ignore if you are a computer vision developer. It has different applications and algorithms at your disposal. If you are stuck installing it on Ubuntu, this guide is your best friend as it offers two ways to go about it. One method should work for you.

Share Button

Source: linuxhint.com

Leave a Reply