在Ubuntu24.04中配置開源直線特征提取軟件DeepLSD
本文提供在Ubuntu24.04中配置開源直線特征提取軟件DeepLSD的基礎環境配置、列出需要修改的文件內容,以及報錯解決方案集錦。
基礎的編譯安裝環境
- python3.8.12
- CUDA12
- gcc/g++ 9.5(系統自帶的g++-13版本太新,會產生額外編譯錯誤)
切換系統默認的gcc/g++版本
Ubuntu24中可以使用update-alternatives命令指定系統當前的gcc/g++版本,命令如下:
sudo apt-get install gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
上述命令首先安裝gcc-9 g+±9,然后利用--install
參數將系統中的兩個版本的gcc/g++各自標記為"9"/“13”,最后利用--config
選擇gcc/g++ 9為默認版本。
安裝glog、gflags
DeepLSD全功能中用到了glog和gflags兩個庫,使用如下命令安裝:
sudo apt-get install libgoogle-glog-dev libgflags-dev
編譯ceres
ceres solver除了依賴glog和gflags之外,還依賴ATLAS(BLAS和LAPACK)、Eigen和SuiteSparse
因此為編譯ceres,還需要運行以下命令安裝這些依賴:
sudo apt-get install libatlas-base-dev
sudo apt-get install libeigen3-dev
sudo apt-get install libsuitesparse-dev
后續安裝命令:
wget -O ceres-solver-2.2.0.tar.gz http://ceres-solver.org/ceres-solver-2.2.0.tar.gz
tar zxf ceres-solver-2.2.0.tar.gz
mkdir ceres-bin
cd ceres-bin
cmake ../ceres-solver-2.2.0
make -j3
make test
sudo make install
編譯OpenCV-Contrib
依賴OpenCV的線提取模塊,因此需要編譯OpenCV和Contrib。
否則將報錯:
DeepLSD/third_party/pytlbd/src/LineBandDescriptor.cpp:9:10: fatal error: opencv2/line_descriptor.hpp: No such file or directory
9 | #include <opencv2/line_descriptor.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/tlbd.dir/build.make:79: CMakeFiles/tlbd.dir/src/LineBandDescriptor.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:185: CMakeFiles/tlbd.dir/all] Error 2
gmake: *** [Makefile:146: all] Error 2
以opencv4.11.0版本為例,編譯命令為:
wget -O https://github.com/opencv/opencv/archive/refs/tags/4.11.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.11.0.zip
unzip opencv_contrib.zip
mkdir opencv_build
cd opencv_build/
conda activate DeepLSD
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.11.0/modules ../opencv-4.11.0
make
sudo make install
源碼修改
解決報錯:error: ‘unordered_map’ in namespace ‘std’ does not name a template type
修改以下兩個文件:
- flann_neighborhood_graph.h
- neighborhood_graph.h
在這兩個文件的開頭包含<unordered_map>
解決報錯:error: ‘clamp’ is not a member of ‘std’
在VSCode中搜索包含std::clamp
的文件。在所有調用std::clamp
的文件開頭加上包含語句#include <algorithm>
。需要修改的文件包括(可能也不限于):
DeepLSD/third_party/progressive-x/examples/cpp_example.cpp
DeepLSD/third_party/progressive-x/graph-cut-ransac/src/pygcransac/include/GCRANSAC.h
DeepLSD/third_party/progressive-x/graph-cut-ransac/src/pygcransac/include/estimators/essential_estimator.h
DeepLSD/third_party/progressive-x/graph-cut-ransac/src/pygcransac/include/estimators/estimator.h
DeepLSD/third_party/progressive-x/graph-cut-ransac/src/pygcransac/include/estimators/fundamental_estimator.h
DeepLSD/third_party/progressive-x/graph-cut-ransac/src/pygcransac/include/estimators/perspective_n_point_estimator.h
DeepLSD/third_party/progressive-x/graph-cut-ransac/src/pygcransac/include/estimators/solver_p3p.h
除此之外,還要修改.cmake文件的配置使用c++17標準編譯,詳見下節。
更改cmake配置
將以下兩個文件:
DeepLSD/third_party/progressive-x/lib/pybind11/tools/pybind11Tools.cmake
DeepLSD/third_party/progressive-x/graph-cut-ransac/lib/pybind11/tools/pybind11Tools.cmake
關于設置PYBIND11的c++語言標準部分改為:
function(select_cxx_standard)if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)check_cxx_compiler_flag("-std=c++17" HAS_CPP17_FLAG)check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)if (HAS_CPP17_FLAG)set(PYBIND11_CPP_STANDARD -std=c++17)elseif (HAS_CPP11_FLAG)set(PYBIND11_CPP_STANDARD -std=c++11)else()message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")endif()set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING"C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available." FORCE)endif()
刪除CMakeCache.txt,重新運行bash install.sh
,即可。
CMake最低版本更改
在VSCode中打開DeepLSD文件夾,搜索如下的表達式(正則):
將cmake_minimum_required(VERSION 2.*)或cmake_minimum_required(VERSION 2.8.12)
改為
cmake_minimum_required(VERSION 3.11)
將3.x版本都改為3.11:
搜索:cmake_minimum_required(VERSION 3.[0-5])
替換為:cmake_minimum_required(VERSION 3.11)
報錯解決集錦
忽略 /opt/anaconda3 目錄及其子目錄
Anaconda3中存在一些庫如glog/gflags甚至gdal等,可能在cmake配置過程中被當作最高優先級的鏈接目標,而這是不對的。因此,如果出現鏈接錯誤“GLIBCXX_3.4.30”類似的錯誤:
libopencv_core : undefined reference to std::condition_variable::wait(std::unique_lockstd::mutex&)@GLIBCXX_3.4.30’
std::condition_variable::wait(std::unique_lockstd::mutex&)@GLIBCXX_3.4.30’
則考慮在報錯的項目的CMakeLists.txt中添加下面的語句以忽略Anaconda的庫目錄:
list(APPEND CMAKE_IGNORE_PATH "/opt/anaconda3")
fatal error: arlsmat.h: 沒有那個文件或目錄 #include <arlsmat.h>
sudo apt install libarpack*
OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.
一般由于沒有安裝CUDA導致。運行以下命令安裝CUDA:
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda
python38/bin/…/lib/libstdc++.so.6: version `GLIBCXX_3.4.30’ not found
編譯完成后在運行時可能會產生此錯誤。DeepLSD編譯鏈接時所使用的是系統的libstdc++.so.6,而python運行時加載了Anaconda自帶的libstdc++.so.6,其版本比系統的libstdc++.so.6舊,所以找不到符號。
首先,運行下面命令確定系統的libstdc++.so.6具有GLIBCXX_3.4.30版本:
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX
如果命令行的輸出有此版本,則刪除python38/bin/…/lib/libstdc++.so.6,并創建符號鏈接:
rm ~/miniconda3/envs/python38/lib/libstdc++.so.6
ln -sf /usr/lib/x86_64-linux-gnu/libstdc++.so.6 ~/miniconda3/envs/python38/lib/libstdc++.so.6