OpenCV (Open Source Computer Vision Library) is an open source computer vision library that supports C++, Python, and Java on all major operating systems. It can play multi-core processes and GPU acceleration for real-time operation.
OpenCV has a wide range of applications, including medical image analysis, street view image processing, surveillance video, detection and recognition of faces, tracking moving objects, extracting 3D models, and so on.
This article describes how to install OpenCV on Ubuntu 20.04. To install the latest version of OpenCV from the source code, slide down to the section Installing OpenCV from the Source. Please choose the installation method that suits you best.
OpenCV is available in Ubuntu 20.04 software sources. To install it, run:
sudo apt update
sudo apt install libopencv-dev python3-opencv
The above command will install all the necessary packages to run OpenCV:
Verify the installation result by importing the cv2
module and printing the OpenCV version:
python3 -c "import cv2; print(cv2.__version__)"
At the time of writing, the version of the software source is 4.2:
Output:
4.2.0
Installing OpenCV from source code allows you to install the latest available version. It will also be optimized for your specific system, and you have complete control over all build options. This is the most recommended way to install OpenCV.
Perform the following steps to install the latest OpenCV version from source code:
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
At the time of writing, the default version in the github software source is 4.3.0. If you want to install an older version of OpenCV, cd to the opencv
and opencv_contrib
directories, and run git checkout<opencv-version>
.
cd ~/opencv_build/opencv
mkdir -p build && cd build
Use CMake command to configure OpenCV 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 ..
The output will be as follows:
- - Configuring done
- - Generating done
- - Build files have been written to:/home/vagrant/opencv_build/opencv/build
make -j8
Modify the value of -f
according to your processor. If you don't know the number of your processor cores, you can find it by typing nproc
.
The compilation will take a few minutes or more, depending on your system configuration.
sudo make install
C++ bindings:
pkg-config --modversion opencv4
Output:
4.3.0
Python bindings:
python3 -c "import cv2; print(cv2.__version__)"
Output:
4.3.0- dev
We have shown two different ways to install OpenCV on your Ubuntu 20.04 server. Your first choice depends on your requirements and preferences.
Even though it is easy to install packages directly from Ubuntu source, building and installing OpenCV from source will give you more flexibility, and it should be your first choice for installing OpenCV.
Recommended Posts