The OpenCV is used for a wide range of applications. Some of them are medical image analysis, surveillance video, detecting, extracting 3D models, and many more. Let’s install OpenCV on ubuntu 20.04 in two different ways.
From this blog, you will get an apparent view regarding the installation and configuration of openCV on ubuntu 20.04 as a part of our server management plan. You can also take assistance from our technical team support 24/7 for further Migration and configuration.
Installing OpenCV on Ubuntu 20.04
You can install OpenCV by using two different methods.
- Using Ubuntu repository
- Through the source
Install OpenCV from the Ubuntu Repository
From the Default Ubuntu20.04 repositories you can install the OpenCV.Run the command.
$ sudo apt update $ sudo apt install libopencv-dev python3-opencv
All packages are necessary to run OpenCV.
Verify the installation by importing the cv2 module and printing the OpenCV version:
$ python3 -c “import cv2; print(cv2.__version__)"
This displaces the 42.0 OpenCV version.
Install OpenCV from the Source
Building the OpenCV library from the source where you can optimize the latest version for your particular system. you have complete control over the build options.
Follow the steps to install the latest OpenCV version from the source.
Install the build tools and dependencies:
$ 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
Clone the OpenCV’s and OpenCV contrib repositories:
$ mkdir ~/opencv_build && cd ~/opencv_build $ git clone https://github.com/opencv/opencv.git $ git clone https://github.com/opencv/opencv_contrib.git
For an older version of OpenCV, cd to both OpenCV and opencv_contrib directories and run git checkout <opencv-version>
Once done, create a temporary build directory and navigate to it:
$ cd ~/opencv_build/opencv $ mkdir -p build && cd build
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_build/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
— Configuring done — Generating done — Build files have been written to: /home/vagrant/opencv_build/opencv/build
Start the compilation process:
$ make -j8
Initially, we modify the -j flag according to the processor. To find the number of cores in the processor, type nproc.
Wait for the compilation to finish.
$ sudo make install
In order to verify, type the following commands.
C++ bindings: $ pkg-config –modversion opencv4
4.3.0 Python bindings: python3 -c “import cv2; print(cv2.__version__)” Output 4.3.0-dev
- Pip Install OpenCV For Desktop Environments
To install OpenCV, run:
$ pip3 install opencv-python $ sudo pip3 install scikit-build $ sudo pip3 install cmake $ sudo pip3 install opencv-python
open-cv python will install only main modules. However, to install both main and contrib modules, run:
$ sudo pip3 install opencv-contrib-python
Pip install OpenCV For Server (headless) Environments
To install for headless environments where there is no GUI available, run:
For main modules only:
$ sudo pip3 install opencv-python-headless
For both main and contrib modules:
$ sudo pip3 install opencv-contrib-python-headless
To verify installation, invoke Python3 console and run:
$ import csv2
Error while installing OpenCV on Ubuntu 20.04
Error: ModuleNotFoundError: No module named ‘skbuild’
In order to resolve this, we make sure that Pip is the latest. If not, update Pip.
Eventually, run the following commands:
$ sudo pip3 install scikit-build $ sudo pip3 install cmake $ sudo pip3 install opencv-python
In short, you can install the OpenCV on ubuntu 20.04 in two different ways. you can also take our technical team support to install OpenCV from the source on your behalf.