Ubuntu下Colmap源码编译调试

Ubuntu下Colmap源码编译调试 1. 构建docker环境下载官方镜像拉镜像docker pull colmap/colmap生成容器docker run -d --name colmap_container --gpus all -v /home/admin/colmap/code:/root/code -p 9000:22 colmap/colmap:latest tail -f /dev/null直接从dockerhub下载镜像就可以使用colmap的内置命令对文件夹下的图像进行重建但是实际操作发现这个镜像要求cuda版本是12.9如果主机的cuda版本不匹配就会导致报错查看了一下历史的镜像版本都比较新也没有说明各自对cuda版本的要求干脆放弃官方镜像自己构建镜像。自己构建镜像build_container.sh脚本用docker-compose构建容器#/bin/bashcd image/colmap./build.sh cd../..docker compose up-ddocker-compose.ymlservices:colmap_custom:container_name:colmap_container ports:-8888:6666build:context:.image:colmap_container:v1.0.0deploy:resources:reservations:devices:-driver:nvidiacount:allcapabilities:[gpu]restart:always command:sh/root/code/run.sh volumes:-/etc/localtime:/etc/localtime:ro-/tmp/.X11-unix:/tmp/.X11-unix-/home/admin/colmap/code:/root/code-/etc/hosts:/etc/hosts environment:-DISPLAY-QT_X11_NO_MITSHM1-TZAsia/Shanghai-CONTAINER_TIMEZONEAsia/Shanghai tty:truebuild.sh#!/bin/bashIMAGE_NAMEcolmap_containerIMAGE_TAGv1.0.0DockerID$(docker images-q $IMAGE_NAME:$IMAGE_TAG)echoQuery Image ID:$DockerID IDcount${#DockerID}if[$IDcount-gt0];then echo镜像 $IMAGE_NAME:$IMAGE_TAG 存在。elseecho镜像 $IMAGE_NAME:$IMAGE_TAG 不存在,准备构建镜像。docker build--no-cache-t $IMAGE_NAME:$IMAGE_TAG.echo镜像 $IMAGE_NAME:$IMAGE_TAG 构建完成。fiDockerfile内容# 使用 Ubuntu 22.04 作为基础镜像FROM registry.cn-hangzhou.aliyuncs.com/acs/ubuntu:22.04# FROM ubuntu:22.04# 设置环境变量避免交互式配置ENV DEBIAN_FRONTENDnoninteractive ENV TZAsia/Shanghai# 更新软件源RUN sed-isarchive.ubuntu.commirrors.aliyun.comg/etc/apt/sources.listRUN sed-issecurity.ubuntu.commirrors.aliyun.comg/etc/apt/sources.list# 更新软件包列表并安装必要的依赖RUN apt-get update RUN apt-get install-y \ git \ vim \ wget \ tzdata\ ntp\ net-tools \ sudo \ ssh \ python3.10\ python3-pip \rm-rf/var/lib/apt/lists/*RUN mkdir-p/root/dev RUN pip configsetglobal.index-url https://pypi.tuna.tsinghua.edu.cn/simple/RUN pip install-i https://pypi.tuna.tsinghua.edu.cn/simple requests RUN pip install-i https://pypi.tuna.tsinghua.edu.cn/simple flask RUN pip install-i https://pypi.tuna.tsinghua.edu.cn/simple waitress RUN ln-sf/usr/bin/python3/usr/bin/python# 安装前更新并固定版本RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb RUN dpkg-i cuda-keyring_1.0-1_all.deb RUN apt-get update\ apt-get install-y--no-install-recommends \ cuda-toolkit-11-811.8.0-1\ libcudnn88.6.0.163-1cuda11.8\ libcudnn8-dev8.6.0.163-1cuda11.8ENV PATH/usr/local/cuda/bin:${PATH}ENV LD_LIBRARY_PATH/usr/local/cuda/lib64:${LD_LIBRARY_PATH}WORKDIR/root# ssh服务配置RUN mkdir/var/run/sshd ARG USERNAMEroot ARG PASSWORD123RUN echo $USERNAME:$PASSWORD|chpasswd \echo$USERNAME ALL(ALL) NOPASSWD:ALL/etc/sudoers RUN sed-is/#Port 22/Port 6666//etc/ssh/sshd_config RUN sed-is/#PermitRootLogin prohibit-password/PermitRootLogin yes//etc/ssh/sshd_config EXPOSE66662. 源码编译执行build_container.sh构建容器然后进入容器。下载colmap-3.13.0版本代码解压到code路径下。编译源代码的全部流程git clone https://github.com/colmap/colmap.git cd colmap mkdir build cd build cmake..-GNinja-DBLA_VENDORIntel10_64lp ninja sudo ninja install注意默认编译的是GPU版本因为colmap到稠密重建的时候需要gpu但是如果想只编译cpu版本只使用到稀疏重建可以使用下面命令cmake..-GNinja-DBLA_VENDORIntel10_64lp-DCUDA_ENABLEDOFF-DGUI_ENABLEDOFF-DOPENGL_ENABLEDOFF在执行cmake .. -GNinja -DBLA_VENDORIntel10_64lp和ninja时可能会报下面的错误如果遇到相同的错误按照提供的方法解决即可。问题1报freeimage错误CMake Errorinsrc/colmap/sensor/CMakeLists.txt:Imported targetfreeimage::FreeImageincludes non-existent path/root/code/colmap/FREEIMAGE_INCLUDE_DIRS-NOTFOUNDinits INTERFACE_INCLUDE_DIRECTORIES.Possible reasons include:*The path was deleted,renamed,ormoved to another location.*An installoruninstall procedure didnotcomplete successfully.*The installation package was faultyandreferences files it doesnotprovide.解决方法sudo apt-get install libfreeimage-dev# Debian/Ubuntu问题2报cuda错误解决方法# 在CMakeLists.txt中添加显卡架构配置set(CMAKE_CUDA_ARCHITECTURES86)# RTX 4090对应架构86set(CMAKE_CUDA_FLAGS${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr --default-stream per-thread)注意这里架构要根据实际显卡来配置问题3报openexr错误解决方法如果openexr存在需要先卸载再安装apt purge-y libopenexr-dev libilmbase-dev apt autoremove-y apt clean sudo apt-get install-y libopenexr-dev问题4报OpenImageIO错误解决方法sudo apt-get install libopenimageio-dev问题5报QT错误CMake Error at cmake/FindDependencies.cmake:201(find_package):Couldnotfind a package configurationfileprovided byQTwithanyof the following names:Qt5Config.cmake qt5-config.cmake Qt6Config.cmake qt6-config.cmake Add the installation prefix ofQTto CMAKE_PREFIX_PATHorsetQT_DIRto a directory containing one of the above files.IfQTprovides a separate development packageorSDK,be sure it has been installed.Call Stack(most recent call first):CMakeLists.txt:112(include)解决方法sudo apt-get install \ qt6-base-dev \ libqt6opengl6-dev \ libqt6openglwidgets6安装完发现生成了新文件编译的时候就可以找到相应的库了/usr/lib/x86_64-linux-gnu/cmake/Qt6/Qt6Config.cmake验证安装是否成功基础验证colmap-h进阶验证准备好images图像执行下面指令colmap feature_extractor \--database_path/root/code/colmap-test/database.db \--image_path/root/code/colmap-test/images colmap exhaustive_matcher \--database_path/root/code/colmap-test/database.db colmap mapper \--database_path/root/code/colmap-test/database.db \--image_path/root/code/colmap-test/images \--output_path/root/code/colmap-test/sparse colmap image_undistorter \--image_path/root/code/colmap-test/images \--input_path/root/code/colmap-test/sparse/0\--output_path/root/code/colmap-test/dense \--output_type COLMAP colmap patch_match_stereo \--workspace_path/root/code/colmap-test//dense \--workspace_format COLMAP \--PatchMatchStereo.geom_consistency true colmap stereo_fusion \--workspace_path/root/code/colmap-test/dense \--workspace_format COLMAP \--input_type geometric \--output_path/root/code/colmap-test/dense/fused.ply到这里实际上已经可以用colmap进行三维重建了只是无法单步调试源码。3. 源码编译调试VScode环境安装在插件扩展中安装cmake、C和gdb配置CMake按快捷键CtrlShiftP输入CMake: Configure回车选择你的编译器比如GCC 11.4.0 x86_64-linux-gnu回车打开CMakeLists.txt然后点击CtrlS检查.vscode文件夹下是否存在settings.json如果没有可以手动创建并按照下面内容将源码路径配置进去{cmake.sourceDirectory:/root/code/colmap-3.13.0}配置launch.json按快捷键CtrlShiftD打开运行和调试面板点击创建 launch.json 文件 → 选择 C (GDB/LLDB) → 选择 默认配置将自动生成的launch.json替换为以下内容适配你的路径{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息请访问: https://go.microsoft.com/fwlink/?linkid830387 version: 0.2.0, configurations: [ { name: (gdb) 调试示例, type: cppdbg, request: launch, program: ${workspaceFolder}/build/src/colmap/exe/colmap, // 你的可执行文件路径 args: [feature_extractor, --database_path, /root/code/colmap-test/sh24/database.db, --image_path, /root/code/colmap-test/sh24/images], // 调试时传入的参数对应你的--message stopAtEntry: false, cwd: ${workspaceFolder}, environment: [], externalConsole: false, MIMode: gdb, setupCommands: [ { description: 为 gdb 启用整齐打印, text: -enable-pretty-printing, ignoreFailures: true } ], preLaunchTask: build } ] }配置tasks.json在VSCode中打开项目根目录${workspaceFolder}。按下CtrlShiftP打开命令面板。输入 ‌Tasks: Configure Task‌并选择它然后选择 ‌Create tasks.json file from template‌。在弹出的模板列表中选择适合您项目的模板例如 ‌Others‌来创建一个自定义任务。在生成的tasks.json文件中修改或添加一个label为build的任务。以下是一个编译C项目的示例{ version: 2.0.0, tasks: [ { label: build, type: shell, command: cd build cmake --build . -j22, group: { kind: build, isDefault: true }, presentation: { echo: true, reveal: always, focus: false, panel: shared }, problemMatcher: [ $gcc ] } ] }然后按F5报错误:Unable to determine path to debugger. Please specify the MIDebuggerPath option表明VS Code无法自动定位gdb调试器的安装路径。虽然您在launch.json中已经指定了MIMode: gdb但缺少具体的调试器路径配置。在终端中运行以下命令查找gdb路径which gdb如果没有输出说明没有安装gdb需要安装sudo apt-get update sudo apt-get install gdb到这里按F5已经可以正常执行特征提取了注意这里编译的都在根目录而不是源码内部那个打上断点后发现已经可以正常调试了这样后面想要优化colmap就方便了