Are you trying to install OpenCV on Ubuntu?
This guide is for you.
OpenCV (Open Source Computer Vision Library) is an open source computer vision library and has bindings for C++, Python, and Java.
OpenCV can take advantage of multi-core processing and features GPU acceleration for real-time operation.
Some of our Customers use OpenCV for a wide range of applications. Some of which are medical image analysis, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and so on.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related tasks.
In this context, we shall look into how to install OpenCV on Ubuntu 20.04.
Basically, there are two different ways to install OpenCV:
i. Using Ubuntu repository.
ii. Through the source.
OpenCV is available for installation from the default Ubuntu 20.04 repositories:
$ sudo apt update
$ sudo apt install libopencv-dev python3-opencv
Now we have all packages necessary to run OpenCV.
We verify the installation by importing the cv2 module and printing the OpenCV version:
$ python3 -c “import cv2; print(cv2.__version__)”
For example, the output will like this:
Output
4.2.0
This implies, we have the 4.2.0 OpenCV version.
If we build the OpenCV library from the source we can optimize it for our particular system.
In addition, we will have complete control over the build options.
Our Support Experts recommend this method of installing OpenCV.
In order to install the latest OpenCV version from the source:
$ 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
$ 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
$ 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 ..
Output:
— Configuring done
— Generating done
— Build files have been written to: /home/vagrant/opencv_build/opencv/build
$ 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
Output:
4.3.0
Python bindings:
python3 -c “import cv2; print(cv2.__version__)”
Output
4.3.0-dev
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
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
While installing OpenCV one of our customers came across the following error:
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
This article will guide you on different methods to install OpenCV on #Ubuntu 20.04. Even though installing the packaged version from the Ubuntu repository is easier, building OpenCV from source gives you more flexibility, and it should be your first option when installing OpenCV.
#OpenCV is the huge open-source library for the computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today's systems.
By using it, one can process images and videos to identify objects, faces, or even handwriting of a human.
To install the latest OpenCV version from the source:
1. Install the required 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
2. 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
3. Once the download is complete, create a temporary build directory, and switch to it:
cd ~/opencv_build/opencv
mkdir build && cd build
4. 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 ..
5. Start the compilation process:
make -j8
6. Install OpenCV with:
sudo make install
To uninstall OpenCV Linux:
i. If you installed OpenCV from package manager, it's best to remove those packages. Check: apt list --installed | grep opencv .
ii. If you built it yourself, and you still got the build folder, run sudo make uninstall from the OpenCV build directory.