Ubuntu20 installs opencv4 and opencv_contrib and multi-version coexistence

openCV uninstall

The source code of openCV after installation should be retained as much as possible, because the installed openCV can be uninstalled directly from the build folder.
Reference link: Visual Learning Notes 10 – Uninstallation, installation and multi-version management of opencv

If you have installed openCV and want to reinstall it later, you need to uninstall the installed openCV first.

Enter the following command on the ubuntu terminal command line to enter the build directory where opencv is installed and uninstall it.

cd build
sudo make uninstall
cd..
sudo rm -r build

The first line of code is to enter the build folder under opencv. For example, my folder path is ~/Downloads/opencv-4.5.4/build
You need to change the first line to your own build path under opencv.

Finally, enter the following command to clean up all opencv related items in /usr (this should be your own folder). (This step is not required)

sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/ OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*

Like my previous installation path was /usr/local/opencv4.5.4, this line of command can be directly changed to

sudo rm -r /usr/local/opencv4.5.4

Installation path
folder under opencv4 # openCV installation
Reference link: Coexistence and switching of multiple versions of opencv under ubuntu20.04
Since we intend to run PL-SLAM, openCV3.4.10 and openCV4.5.4 are installed.

Download and install dependency packages

1. Update apt-get first. It is best to update the system before installation, otherwise the installation may fail. Enter in the terminal:

sudo apt-get update
sudo apt-get upgrade

2. Then install the official opencv dependency package and enter in the terminal:

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
  1. Download the openCV3.4.10 and opencv_contrib3.4.10 source code compression packages. Note that the version numbers of the two need to be the same.
  2. After downloading, unzip it and move opencv_contrib3.4.10 directly to the opencv3.4.10 folder.

Enter the following command:

cd ~/Downloads/opencv-3.4.10
mkdir build
cd build
Cmake -d Cmake_build_type = Release -d Cmake_install_prefix =/USR/LOCAL/Opencv3.4.10 -D Opencv_extrary_path =/Home/Xiujie/DownNLOADS/O. PENCV -3.4.10/OpenCV_CONTRIB -3.4.10/Modules/-d OpenCV_GENATE_PKGCONFIG = Yes -d OPENCV_ENABLE_NONFREE=True -D BUILD_opencv_cudacodec=OFF ..


The file location following cd needs to be changed to the real location where you store the opencv installation source code.
The meaning of various parameters after cmake is very clear in this link. Personal test: Install OpenCV 5.x with CUDA on Ubuntu 20.04.
Special attention should be paid to CMAKE_INSTALL_PREFIX=/usr/local/opencv3.4.10
Indicates the subsequent installation location of openCV.

At this step, there may be problems with download failure. If possible, you need to use the external network or download manually. If you download manually, please refer to the link: [
Coexistence and switching of multiple versions of opencv under ubuntu20.04
(https://blog.csdn.net/xiao_qs/article/details/126650329)

  1. Compile and install.
    Check the maximum number of threads your computer supports:

nproc

The output of my computer is 8, so execute it line by line

make -j4
sudo make install

Environment configuration

Create file and open:

sudo vim /etc/ld.so.conf.d/opencv.conf

Add the following content to the opened file:

/usr/local/opencv3.4.10/lib

This line of code is to add the opencv dynamic library path, which needs to be set according to your own installation path. For example, my installation path is /usr/local/opencv3.4.10 , then it should be set to /usr/local /opencv3.4.10 /lib, as shown in the figure below, mine is because opencv4.5.4 is installed at the same time. If you want to use opencv3, just comment out the line of opencv4.
Write information
After saving, open a new terminal and enter the command:

sudo ldconfig

Make it save and take effect, then enter in the terminal:

sudo vim /etc/bash.bashrc

After opening the file, enter at the end:

export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/usr/local/opencv3.4.10/lib/pkgconfig
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/opencv3.4.10/lib

Pay attention to the folder location of these two lines of instructions:
Among them, /usr/local/opencv3.4.10 is the installation location.
/usr/local/opencv3.4.10/lib/pkgconfig , when compiling with cmake, you must set OPENCV_GENERATE_PKGCONFIG=YES, otherwise you need to create the pkgconfig file yourself, there are many tutorials Yes, I won’t go into details here. The picture below shows opencv4, and the file name below is opencv4.pc. The same is true for opencv3, the file name is opencv.pc.
pkgconfig folder
opencv4 file
Save and exit, enter in terminal:

source /etc/bash.bashrc

Test

Enter code

pkg-config --modversion opencv

Version number testopencv4 version number test
The steps to install opencv4 are the same as above. Pay attention to the installation path and attach part of the code:

cd ~/Downloads/opencv-3.4.10
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.5.4 -D OPENCV_EXTRA_MODULES_PATH=/home/xiujie/Downloads/opencv-4.5.4/opencv_contrib-4.5.4/modules/ -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_ENABLE_NONFREE=True -D BUILD_opencv_cudacodec=OFF ..
# It is best to increase the number of threads, speed up the process, and check the cpu threads:
grep 'processor' /proc/cpuinfo | sort -u | wc -l
# The thread is found to be 8, so it is j8
 make -j8
sudo make install

sudo vim /etc/ld.so.conf.d/opencv.conf
/usr/local/opencv4.5.4/lib
sudo ldconfig
sudo vim /etc/bash.bashrc
#e xport PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/usr/local/opencv4.5.4/lib/pkgconfig
# export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/opencv4.5.4/lib
source /etc/bash.bashrc

Check the version number pkg-config --modversion opencv4

For multi-version switching, see: Visual Learning Notes 10 – Uninstallation, installation and multi-version management of opencv