Coexistence and switching of multiple versions of opencv under ubuntu20.04

I have installed opencv4.5.5 and the extension library opencv_contrib4.5.5 on the ubuntu20.04 system before, but recently I need to run the source code of PL-SLAM, and the PL-SLAM system is developed based on opencv3.xx, there will be many problems when running directly Opencv version incompatibility problem. Therefore, I want to install opencv3.4.5 and the extension library opencv_contrib3.4.5 again. There are many problems in the process. Write this article to record it.

1. Download and install dependencies

1. First update apt-get. It is best to update the system before installation, otherwise the installation may fail. Type in 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

2. Download opencv3.4.5 and opencv_contrib3.4.5 source compression packages

1. Download address of opencv3.4.5:
https://github.com/opencv/opencv/releases

2. Download address of opencv_contrib3.4.5:
https://github.com/opencv/opencv_contrib/tree/3.4.5

3. Configure opencv

1. Decompress (extract) OpenCV3.4.5 and OpenCV_contrib3.4.5 and put them in a folder, as shown in the figure below:
insert image description here2. Double-click to enter the decompressed opencv-3.4.5 folder, right-click to open the terminal (or open it elsewhere Terminal, enter the current directory by entering cd opencv-3.4.5), and then enter in sequence (don’t forget the last space and two dots in the third line):

mkdir build
cd build 
cmake-gui .. 

3. Then the graphical interface of CMake will pop up. In the two paths above, select the path of the folder where the code is located and the path to be installed: 4. Click the Configure
insert image description here
button at the bottom left, select Unix Makefiles, and select Use default native compilers (default), and then click Finish:
insert image description here
5. Some files need to be downloaded, and it will take a while. The download may fail due to network reasons, resulting in missing files such as boostdesc_bgm.i, so manually download the missing files, and then copy the following files:

boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i

Copy it to the opencv_contrib/modules/xfeatures2d/src/ directory, as shown in the figure:
insert image description hereBaidu cloud link: https://pan.baidu.com/s/1BeYF8kqEZLAJYQj-MvxpmA
Extraction code: e1wc
6. Click the Configure button on the lower left again, and then CMake loads the default configuration, and here it is configured through a graphical interface, which is more intuitive and convenient.
There are three places that need to be modified:
(1) Enter RELEASE at the CMAKE_BUILD_TYPE value, and keep the others unchanged (if it already exists, you don't need to modify it).
(2) The CMAKE_INSTALL_PREFIX below shows the default installation directory, which is installed in the usr/local directory by default, and will be installed in this directory when the makefile is generated and finally executed when make install is executed. However, since opencv4.5.5 has been installed before, if the default path is not modified, the previous opencv4.5.5 will be overwritten, so create a new installed folder under the ~/opencv3.4.5/opencv-3.4.5/build folder, and Install opencv3.4.5 to this path.
insert image description hereModify the default installation directory of CMAKE_INSTALL_PREFIX to ~/opencv3.4.5/opencv-3.4.5/build/installed
insert image description here
(3) At OPENCV_EXTRA_MODULES_PATH, select the input directory of the opencv_contrib3.4.5 extension library (click the blank space behind this line to select it) Then select the modules folder in the opencv_contrib-3.2.0 folder. Note that it is not enough to just select the opencv_contrib-3.2.0 folder, you need to select the modules folder inside! As shown in the figure below, then click Choose.
Note: OPENCV_EXTRA_MODULES_PATH is used to specify the extension modules to be compiled, including the OpenCV_contrib module.
insert image description hereinsert image description here
7. Click Generate to generate the configuration file, this step should be completed relatively quickly.
8. Next, open the terminal in the build directory and enter:

make -j4

At this time, an error is reported:

The reason is that the ceres version is too high and incompatible with opencv3.4.5. The original installed version is ceres2.2.0, so reinstall ceres1.14.0, and use the cmake-gui command to modify the installation path of ceres1.14.0 as in the previous steps so that it is not Overwrite the original ceres. If you don't want to be so troublesome, you can uninstall the original ceres and reinstall it.

After reinstalling ceres, follow the above steps to configure opencv again, then open the terminal in the build directory, and enter:

make -j4

After the compilation is successful, enter

sudo make install

After that, opencv3.4.5 can be successfully installed.

Reference blog: https://blog.csdn.net/jindunwan7388/article/details/80397700

3. Configure environment variables

1. After the installation is successful, you need to set the environment variables of opencv. open a file:

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

2. Add the path of the opencv3.4.5 library file to the environment variable, and add the following to the end::

/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed/lib

3. Next, configure the library:

sudo ldconfig

4. Change environment variables:

sudo gedit /etc/bash.bashrc

5. Add after the file:

export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed/lib/pkgconfig
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed/lib

6. Update ~/.bashrc

source ~/.bashrc 

7. Query the version of OpenCV

pkg-config --modversion opencv

If output 3.4.5, the configuration is successful.
If you want to use the previous version, comment out the added content in ~/.bashrc, and then source ~/.bashrcyou can.

4. Switching between multiple versions of opencv

If there is only one version of OpenCV, use the following statement in CMakeList.txt. After OpenCV is compiled, the OpenCVConfig.cmake file will be generated in the directory where it is located. This file specifies where CMake is going to find OpenCV, where is its .h file, etc. This situation applies to only one version of opencv, which is installed in the default path, usually under /usr/local/lib, such as my opencv4.4.5, because if you do not specify the prefix path during installation, it will be installed during make install Install all .so files under /usr/local/lib and all header files under /usr/include.

FIND_PACKAGE(OpenCV REQUIRED)

But here we specify the path for the installation of opencv3.4.5, so if you want to use opencv3.4.5, you need to set the cmake search path before find_package, that is, let cmake "/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed"find opencv;

set(CMAKE_PREFIX_PATH "/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed")

What needs to be mentioned here is that find_package is actually looking for the OpenCVConfig.cmake file. Open this file and you can see the version corresponding to the current opencv. At the same time, some paths are defined, similar to the feeling of macros in c++, especially : OpenCV_LIBS OpenCV_INCLUDE_DIRS two paths, which ensures that cmake can normally find the header files and library files required by opencv;

Therefore, there is a third method that includes opencv, which is to directly set these two paths. The path of the OpenCVConfig.cmake file is/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed/share/OpenC

set(OpenCV_DIR "/home/xqs/3rdparty/opencv3.4.5/opencv-3.4.5/build/installed/share/OpenCV")
find_package(OpenCV 3.4.5 REQUIRED COMPONENTS core highgui imgproc flann calib3d)  
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

Reference blog: https://blog.csdn.net/qq_25458977/article/details/103931360

Five, uninstall ceres

Ceres has only one library file in "/usr/local/lib", and all header files are in "/usr/local/include/ceres"

sudo rm -r /usr/local/lib/cmake/Ceres
sudo rm -r /usr/local/include/ceres /usr/local/lib/libceres.a

6. Install Ceres

1. Install dependent libraries:

sudo apt-get install liblapack-dev libsuitesparse-dev libgflags-dev 
sudo apt-get install libgoogle-glog-dev libgtest-dev
sudo apt-get install libcxsparse3

2. Download ceres-solver-1.14.0

wget ceres-solver.org/ceres-solver-1.14.0.tar.gz

3. Unzip

tar -zxvf ceres-solver-1.14.0.tar.gz

4. Compile and install ceres

sudo mkdir build
cd build
sudo cmake ..
sudo make -j4
sudo make install

Guess you like

Origin blog.csdn.net/xiao_qs/article/details/126650329