Ubuntu18.04系统安装和使用colmap

本文记录在docker容器中安装colmap时踩的坑和使用过程
系统ubuntu:18.04
参考资料:三维重建_COLMAP安装、使用和参数说明(翻译自官方文档)
官方文档:COLMAP — COLMAP 3.6 documentation

apt换源过程

ubuntu的apt源不在国内,更新和下载速度非常慢,而且容易出错,所以推荐换成国内镜像源。这里推荐清华镜像站阿里镜像站

具体更换过程如下:

cd /etc/apt
sudo cp sources.list sources.list.bak # 备份sources.list
sudo vim sources.list # 进入sources.list文件

按i进入输入模式,将所有内容删除,并使用如下阿里源覆盖掉:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

按Esc退出输入模式,输入:wq保存并退出。在命令行中输入sudo apt-get update,即更新完成。

在本地安装colmap运行

colmap官网里有针对不同系统的安装方法,详见Installation,其中linux可以直接通过apt进行安装,具体过程如下:

warning:不要在装colmap之前安装anaconda,环境变量的路径可能会有冲突!
没有nvidia显卡驱动也可以在cpu运行,但是会很慢,如果安装了显卡驱动可以去下载对应的cuda和cudnn用显卡计算

首先从ubuntu仓库中安装依赖项:

sudo apt-get install \
    git \
    cmake \
    build-essential \
    libboost-program-options-dev \
    libboost-filesystem-dev \
    libboost-graph-dev \
    libboost-regex-dev \
    libboost-system-dev \
    libboost-test-dev \
    libeigen3-dev \
    libsuitesparse-dev \
    libfreeimage-dev \
    libgoogle-glog-dev \
    libgflags-dev \
    libglew-dev \
    qtbase5-dev \
    libqt5opengl5-dev \
    libcgal-dev

针对ubuntu18.04版本,需要安装CGAL Qt5:sudo apt-get install libcgal-qt5-dev

之后安装ceres-solver(由于googlesource在国内没办法用,这里从github克隆到本地):

sudo apt-get install libatlas-base-dev libsuitesparse-dev
git clone https://github.com/ceres-solver/ceres-solver # 克隆源码到本地当前目录下,注意自己切换
cd ceres-solver # 进入文件夹
git checkout $(git describe --tags) # Checkout the latest release
mkdir build # 创建build文件夹
cd build # 进入build文件夹
cmake .. -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF # 编译
make
sudo make install # 安装

最后,编译并安装colmap,git之前注意切换目录:

git clone https://github.com/colmap/colmap.git
cd colmap
git checkout dev
mkdir build
cd build
cmake ..
make
sudo make install

最后检查一下,命令行中键入colmap,显示如下则代表安装成功:

COLMAP 3.6 -- Structure-from-Motion and Multi-View Stereo
              (Commit 1432f00 on 2020-05-09 without CUDA)

Usage:
  colmap [command] [options]

Documentation:
  https://colmap.github.io/

Example usage:
  colmap help [ -h, --help ]
  colmap gui
  colmap gui -h [ --help ]
  colmap automatic_reconstructor -h [ --help ]
  colmap automatic_reconstructor --image_path IMAGES --workspace_path WORKSPACE
  colmap feature_extractor --image_path IMAGES --database_path DATABASE
  colmap exhaustive_matcher --database_path DATABASE
  colmap mapper --image_path IMAGES --database_path DATABASE --output_path MODEL
  ...

Available commands:
  help
  gui
  automatic_reconstructor
  bundle_adjuster
  color_extractor
  database_creator
  database_merger
  delaunay_mesher
  exhaustive_matcher
  feature_extractor
  feature_importer
  hierarchical_mapper
  image_deleter
  image_filterer
  image_rectifier
  image_registrator
  image_undistorter
  image_undistorter_standalone
  mapper
  matches_importer
  model_aligner
  model_analyzer
  model_converter
  model_merger
  model_orientation_aligner
  patch_match_stereo
  point_filtering
  point_triangulator
  poisson_mesher
  project_generator
  rig_bundle_adjuster
  sequential_matcher
  spatial_matcher
  stereo_fusion
  transitive_matcher
  vocab_tree_builder
  vocab_tree_matcher
  vocab_tree_retriever

在本地键入colmap gui,即可进入图形界面。

使用colmap进行三维重建

官方文档在此:Command-line Interface由于要在服务器上跑,用命令行更方便一些。图形界面的操作官方也有介绍:Graphical User Interface

在使用colmap进行重建之前,首先需要了解colmap的工程结构。

开始之前,工作目录下的结构为:

/path/to/project/...
+── images
│   +── image1.jpg
│   +── image2.jpg
│   +── ...
│   +── imageN.jpg
# The project folder must contain a folder "images" with all the images.

经过自动重建,你的project文件夹下的目录会是如下形式:

+── images
│   +── image1.jpg
│   +── image2.jpg
│   +── ...
+── sparse
│   +── 0
│   │   +── cameras.bin
│   │   +── images.bin
│   │   +── points3D.bin
│   +── ...
+── dense
│   +── 0
│   │   +── images
│   │   +── sparse
│   │   +── stereo
│   │   +── fused.ply
│   │   +── meshed-poisson.ply
│   │   +── meshed-delaunay.ply
│   +── ...
+── database.db

当然如果选择分步操作,可以自己指定文件输出位置(当然推荐按照官方结构操作)。


  1. Command-line Interface
  • 使用自动重建是比较简便的方法,输入colmap automatic_reconstructor -h查看帮助,其它命令类似。
COLMAP 3.6 (Commit 1432f00 on 2020-05-09 without CUDA)

Options can either be specified via command-line or by defining
them in a .ini project file passed to `--project_path`.

  -h [ --help ] 
  --random_seed arg (=0)
  --project_path arg
  --workspace_path arg
  --image_path arg
  --mask_path arg
  --vocab_tree_path arg
  --data_type arg (=individual)       {individual, video, internet}
  --quality arg (=high)               {low, medium, high, extreme}
  --camera_model arg (=SIMPLE_RADIAL)
  --single_camera arg (=0)
  --sparse arg (=1)
  --dense arg (=0)
  --mesher arg (=poisson)             {poisson, delaunay}
  --num_threads arg (=-1)
  --use_gpu arg (=1)
  --gpu_index arg (=-1)

相关参数的意义可以在官方文档中查询,括号中为默认参数值。在命令行中输入以下命令进行自动重建:

首先设置工作目录:DATASET_PATH=/data/path/to/project #这里根据自己的工作文件夹路径设置

再将图片解压到project文件夹下,unzip images.zip -d $DATASET_PATH

然后进行重建:

$ colmap automatic_reconstructor \
   --workspace_path $DATASET_PATH \      # 工作文件夹的路径
   --image_path $DATASET_PATH/images \      # images文件夹的路径
   --gpu_index=0      # 显卡的编号

如果有多块显卡,输入nvidia-smi来查看当前有哪些显卡处于空闲状态,并选择其中一块。

  • 如果想要观察每一步重建过程的输出,可以使用以下分步重建命令代替:
# The project folder must contain a folder "images" with all the images.
$ DATASET_PATH=/path/to/dataset

$ colmap feature_extractor \      # 特征提取
   --database_path $DATASET_PATH/database.db \
   --image_path $DATASET_PATH/images \
   --SiftExtraction.gpu_index=0      #GPU加速,可选,下同

$ colmap exhaustive_matcher \      # 特征点匹配
   --database_path $DATASET_PATH/database.db \
   --SiftMatching.gpu_index=0

$ mkdir $DATASET_PATH/sparse      # 创建稀疏图文件夹

$ colmap mapper \      # 建稀疏图,不需要GPU加速
    --database_path $DATASET_PATH/database.db \
    --image_path $DATASET_PATH/images \
    --output_path $DATASET_PATH/sparse

$ mkdir $DATASET_PATH/dense      # 创建稠密图文件夹

$ colmap image_undistorter \      # 图形去畸变
    --image_path $DATASET_PATH/images \
    --input_path $DATASET_PATH/sparse/0 \
    --output_path $DATASET_PATH/dense \
    --output_type COLMAP \
    --max_image_size 2000

$ colmap patch_match_stereo \      # 为稠密图再匹配(必需GPU)
    --workspace_path $DATASET_PATH/dense \
    --workspace_format COLMAP \
    --PatchMatchStereo.geom_consistency true \
    --PatchMatchStereo.gpu_index=0

$ colmap stereo_fusion \      # 开始建图
    --workspace_path $DATASET_PATH/dense \
    --workspace_format COLMAP \
    --input_type geometric \
    --output_path $DATASET_PATH/dense/fused.ply

$ colmap poisson_mesher \      # 使用poisson方法建网格添加纹理
    --input_path $DATASET_PATH/dense/fused.ply \
    --output_path $DATASET_PATH/dense/meshed-poisson.ply

$ colmap delaunay_mesher \      # 使用delaunay方法建网格添加纹理
    --input_path $DATASET_PATH/dense \
    --output_path $DATASET_PATH/dense/meshed-delaunay.ply
  1. Graphical User Interface

gui界面将命令行中的功能可视化,如果操作感觉顺手可以选择gui,优点在于可以将最后生成的模型可视化,缺点是不能远程操作。对于稀疏图点云和稠密图的可视化,官方文档里有详细介绍,详见VisualizationOutput Format。对于depth_maps的可视化,需要借助python或者matlab进行,相关的源码在Visualization有指出。

连接远程服务器并在docker容器中安装colmap

  1. 连接服务器

在服务器上运算可以节省很多时间:ssh -p 端口号 用户名@IP地址,之后输入密码便登录了远程服务器。

  1. 建立docker容器

为了能够使用nvidia显卡并拥有cuda支持,建立docker容器时应选择合适的镜像。

docker images      # 查看可用镜像
nvidia-docker run --name mydocker -v /home/username/data0:/data -it nvidia/cuda /bin/bash       # 创建一个nvidia/cuda容器,并映射文件目录(主机目录:容器目录)
docker ps -a      # 查看所有已存在的容器
docker start mydocker      # 启动容器
docker exec -it mydocker /bin/bash      # 进入容器
nvidia-smi      # 检查cuda是否可用
......      # 一系列安装操作
  1. 在本地与服务器之间传输文件
scp -P 端口号 [-r] 本地文件目录 用户名@IP地址:服务器文件目录      # 本地向服务器上传,文件夹加-r参数
scp -P 端口号 [-r] 用户名@IP地址:服务器文件目录 本地文件目录      # 服务器向本地下载
posted @ 2020-06-30 16:04  littlebirdicy  阅读(3799)  评论(0)    收藏  举报