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