COLMAP Windows Build from Source踩坑合集

如果不想看废话请直接跳转至正戏

前戏

一般使用COLMAP进行三维重建时,如果没有特别的使用需求,可以直接下载pre-built binaries,双击图标即可使用GUI。但是,某些特殊需求单单在GUI中是无法完成的。

一种情况是,当对物体做三维重建时,我们的相机参数是已知的,比如DTU和BlendedMVS中既提供了图片,也提供了图片所对应的相机参数。那么这个时候,对数据集重新进行COLMAP重建就会置这些已知的参数不顾,重新生成它自己所理解的参数,这样就会对测试baseline的performance的工作造成很大影响。

好消息是,COLMAP知道我们的这个难处,并提供了一个解决方案(详见https://colmap.github.io/faq.html中的Reconstruct sparse/dense model from known camera poses部分)。该方案要求我们按照给定的structure和格式提供已知的参数。那么,就需要对数据集进行预处理,以符合其期待。此处给出转换完以后的structure和格式的截图。
COLMAP Windows Build from Source踩坑合集_第1张图片
COLMAP Windows Build from Source踩坑合集_第2张图片
COLMAP Windows Build from Source踩坑合集_第3张图片

cameras.txt

摘抄自https://colmap.github.io/format.html#output-format。

# Camera list with one line of data per camera:
#   CAMERA_ID, MODEL, WIDTH, HEIGHT, PARAMS[]
# Number of cameras: 3
1 SIMPLE_PINHOLE 3072 2304 2559.81 1536 1152
2 PINHOLE 3072 2304 2560.56 2560.56 1536 1152
3 SIMPLE_RADIAL 3072 2304 2559.69 1536 1152 -0.0218531

目前不太清楚SIMPLE_RADIAL中最后一个参数的含义。但是在SIMPLE_PINHOLE中,根据文档描述,3个参数的意义为:焦距为2559.81像素,主点(principal point,我理解为相机中心)位于像素位置(1536,1152)。这些参数都可以由数据集图像和相机参数文件中获取。

images.txt

# Image list with two lines of data per image:
#   IMAGE_ID, QW, QX, QY, QZ, TX, TY, TZ, CAMERA_ID, NAME
#   POINTS2D[] as (X, Y, POINT3D_ID)  # 表示特征点的对应关系,此处不需太在意,因为我们的输入没有它
# Number of images: 2, mean observations per image: 2
1 0.851773 0.0165051 0.503764 -0.142941 -0.737434 1.02973 3.74354 1 P1180141.JPG
2362.39 248.498 58396 1784.7 268.254 59027 1784.7 268.254 -1
2 0.851773 0.0165051 0.503764 -0.142941 -0.737434 1.02973 3.74354 1 P1180142.JPG
1190.83 663.957 23056 1258.77 640.354 59070

一般将(QW, QX, QY, QZ)称作qvec,表示四元数旋转;(TX, TY, TZ)称作tvec,表示位移。NAME需与images文件夹中的图片对应。有将四元数转换为旋转矩阵的算法(不小心把上一篇文章的坑填了):

def qvec2rotmat(qvec):
    return np.array([
        [1 - 2 * qvec[2]**2 - 2 * qvec[3]**2,
         2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
         2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2]],
        [2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
         1 - 2 * qvec[1]**2 - 2 * qvec[3]**2,
         2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1]],
        [2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
         2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
         1 - 2 * qvec[1]**2 - 2 * qvec[2]**2]])


def rotmat2qvec(R):
    Rxx, Ryx, Rzx, Rxy, Ryy, Rzy, Rxz, Ryz, Rzz = R.flat
    K = np.array([
        [Rxx - Ryy - Rzz, 0, 0, 0],
        [Ryx + Rxy, Ryy - Rxx - Rzz, 0, 0],
        [Rzx + Rxz, Rzy + Ryz, Rzz - Rxx - Ryy, 0],
        [Ryz - Rzy, Rzx - Rxz, Rxy - Ryx, Rxx + Ryy + Rzz]]) / 3.0
    eigvals, eigvecs = np.linalg.eigh(K)
    qvec = eigvecs[[3, 0, 1, 2], np.argmax(eigvals)]
    if qvec[0] < 0:
        qvec *= -1
    return qvec
# Author: Johannes L. Schoenberger (jsch at inf.ethz.ch)

那么从数据集的相机参数就可以转为COLMAP认可的形式。到此整个输入的structure和form就符合规范了。
(其实在DTU和BlendedMVS的转换中还有坑,比如DTU是w2c表示,而BlendedMVS是c2w表示。比如,以下代码适用于转换DTU数据集)

with open(os.path.join(new_dir, 'images.txt'), 'w') as f:
    for i in range(n_images):
        w2c_mat = camera_extrinsics[i]
        c2w_mat = np.linalg.inv(w2c_mat)
        qvec = rotmat2qvec(c2w_mat[:3, :3]).flat
        tvec = c2w_mat[:3, 3].flat
        f.write(f'{i + 1} {qvec[0]} {qvec[1]} {qvec[2]} {qvec[3]} {tvec[0]} {tvec[1]} {tvec[2]} {i + 1} {img_dir[0]}\n\n')

正戏

https://colmap.github.io/install.html

一般来讲,COLMAP都在Linux系统上Build from Source,而在windows上装真的是要了人命了。虽然有文档,但是架不住vcpkg安装时遇到各种各样的情况。最主要还是它的文档写的实在不清楚,一些编译器的要求从来没有提过。另外还有两个额外要求:

  • 路径不能是中文
  • 路径不能有空格

第一关

Error: in triplet x64-windows: Unable to find a valid Visual Studio instance

即使已经安装了VS2019,依然报了这个错误。
光是有裸的VS2019还不够,要给他加对应的编译用的组件,才能跑(而文档里讲都没讲)。

需要的组件:Visual Studio Installer > Visual Studio Community 2019 > 修改(M) > 单个组件 > 用于Windows的C++ CMake工具
(好像会自动附带另一个组件,一块装上)
(有人说是因为没有英语语言包,打个眼)

第二关

Installing 1/43 gmp:x64-windows...
Building gmp[core]:x64-windows...
-- Using cached gmp-6.2.1.tar.xz.
-- Using cached gmp-arm64-asm-fix-5f32dbc41afc.patch.
-- Cleaning sources at D:/Projects/20230317-COLMAP-3.8-windows-cuda/vcpkg/buildtrees/gmp/src/v6.2.1-0c723d4b6f.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source D:/Projects/20230317-COLMAP-3.8-windows-cuda/vcpkg/downloads/gmp-6.2.1.tar.xz
-- Applying patch asmflags.patch
-- Applying patch cross-tools.patch
-- Applying patch subdirs.patch
-- Applying patch msvc_symbol.patch
-- Applying patch arm64-coff.patch
-- Applying patch D:/Projects/20230317-COLMAP-3.8-windows-cuda/vcpkg/downloads/gmp-arm64-asm-fix-5f32dbc41afc.patch
-- Using source at D:/Projects/20230317-COLMAP-3.8-windows-cuda/vcpkg/buildtrees/gmp/src/v6.2.1-0c723d4b6f.clean
-- Getting CMake variables for x64-windows
-- Using cached LLVM-15.0.6-win64.7z.exe.
-- Using cached 7z1900.msi.

在安装gmp时,download完这个7z1900.msi之后就卡住不动了。通过求助github大佬,发现是因为LLVM导致的,因为解压LLVM需要花很多时间。

https://github.com/microsoft/vcpkg/discussions/28296
@dg0yt:
For gmp in particular:

– Using cached LLVM-15.0.6-win64.7z.exe.
– Using cached 7z1900.msi.

I wouldn’t be surprised if extracting LLVM-15.0.6-win64.7z.exe takes very long. But this is not really what is meant: It should use clang-cl which comes with modern versions of visual studio.

于是,安装clang-cl。
需要的组件:Visual Studio Installer > Visual Studio Community 2019 > 修改(M) > 单个组件 > 适用于 Windows 的 C++ Clang 编译器(12.0.0)

附:
LLVM:https://learn.microsoft.com/zh-cn/cpp/build/clang-support-msbuild?view=msvc-170
LLVM 与 Clang 介绍:https://blog.csdn.net/weixin_33726943/article/details/94475625

第三关

Installing 1/29 qt5-base:x64-windows...
Building qt5-base[core]:x64-windows...
CMake Warning at scripts/cmake/vcpkg_buildpath_length_warning.cmake:4 (message):
  qt5-base's buildsystem uses very long paths and may fail on your system.

  We recommend moving vcpkg to a short path such as 'C:\src\vcpkg' or using
  the subst command.
Call Stack (most recent call first):
  ports/qt5-base/portfile.cmake:1 (vcpkg_buildpath_length_warning)
  scripts/ports.cmake:147 (include)


-- Figuring out qt target mkspec. Target arch x64
-- Target mkspec set to: win32-msvc
-- Host mkspec not set. Qt's own buildsystem will try to figure out the host system
CMake Warning at scripts/cmake/vcpkg_buildpath_length_warning.cmake:4 (message):
  qt5-base's buildsystem uses very long paths and may fail on your system.

  We recommend moving vcpkg to a short path such as 'C:\src\vcpkg' or using
  the subst command.
Call Stack (most recent call first):
  ports/qt5-base/cmake/qt_download_submodule.cmake:13 (vcpkg_buildpath_length_warning)
  ports/qt5-base/portfile.cmake:49 (qt_download_submodule)
  scripts/ports.cmake:147 (include)

...

error: building qt5-base:x64-windows failed with: BUILD_FAILED
error: Please ensure you're using the latest port files with `git pull` and `vcpkg update`.
Then check for known issues at:
    https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+qt5-base
You can submit a new issue at:
    https://github.com/microsoft/vcpkg/issues/new?template=report-package-build-failure.md&title=[qt5-base]+Build+error
Include '[qt5-base] Build error' in your bug report title, the following version information in your bug description, and attach any relevant failure logs from above.
    vcpkg-tool version: 2023-03-14-105f7f7e8a5ea10dfac9874ff9d1a6bacba5f454
    vcpkg-scripts version: b51f391cd 2023-03-16 (10 hours ago)

没想到路径太长都能有问题…

第四关

Installing 18/29 cuda:x64-windows...
Building cuda[core]:x64-windows...
-- Executing NVCC-NOTFOUND --version resulted in error: 1
CMake Error at ports/cuda/vcpkg_find_cuda.cmake:69 (message):
  Could not find CUDA.  Before continuing, please download and install CUDA
  (v10.1.0 or higher) from:

      https://developer.nvidia.com/cuda-downloads

那确实是没装,新电脑。
怕有人不会,CUDA安装参考https://blog.csdn.net/m0_37605642/article/details/98854753。
另外CUDA安装速度奇慢,挂个阿美莉卡的VPN上去就会快很多(我用的proton,免费的,之前用来试ChatGPT,刚好现在也能用)。

第五关

Computing installation plan...
The following packages will be built and installed:
    colmap[core,cuda,tests]:x64-windows -> 2023-03-12
Detecting compiler hash for triplet x64-windows...
Restored 0 package(s) from C:\Users\xrr\AppData\Local\vcpkg\archives in 154.9 us. Use --debug to see more details.
Installing 1/1 colmap:x64-windows...
Building colmap[core,cuda,tests]:x64-windows...
-- Using cached colmap-colmap-30da037ce19bdceb6d239c45342fadb221bdabb2.tar.gz.
-- Cleaning sources at C:/Users/xrr/vcpkg/buildtrees/colmap/src/b221bdabb2-6df8038bd6.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source C:/Users/xrr/vcpkg/downloads/colmap-colmap-30da037ce19bdceb6d239c45342fadb221bdabb2.tar.gz
-- Applying patch fix-dependencies.patch
-- Using source at C:/Users/xrr/vcpkg/buildtrees/colmap/src/b221bdabb2-6df8038bd6.clean
-- Configuring x64-windows-dbg
-- Configuring x64-windows-rel
-- Building x64-windows-dbg
CMake Error at scripts/cmake/vcpkg_execute_build_process.cmake:134 (message):
    Command failed: C:/Users/xrr/vcpkg/downloads/tools/cmake-3.25.1-windows/cmake-3.25.1-windows-i386/bin/cmake.exe --build . --config Debug --target install -- -v -j21
    Working Directory: C:/Users/xrr/vcpkg/buildtrees/colmap/x64-windows-dbg
    See logs for more information:
      C:\Users\xrr\vcpkg\buildtrees\colmap\install-x64-windows-dbg-out.log

Call Stack (most recent call first):
  installed/x64-windows/share/vcpkg-cmake/vcpkg_cmake_build.cmake:74 (vcpkg_execute_build_process)
  installed/x64-windows/share/vcpkg-cmake/vcpkg_cmake_install.cmake:16 (vcpkg_cmake_build)
  ports/colmap/portfile.cmake:60 (vcpkg_cmake_install)
  scripts/ports.cmake:147 (include)


error: building colmap:x64-windows failed with: BUILD_FAILED
error: Please ensure you're using the latest port files with `git pull` and `vcpkg update`.
Then check for known issues at:
    https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+colmap
You can submit a new issue at:
    https://github.com/microsoft/vcpkg/issues/new?template=report-package-build-failure.md&title=[colmap]+Build+error
Include '[colmap] Build error' in your bug report title, the following version information in your bug description, and attach any relevant failure logs from above.
    vcpkg-tool version: 2023-03-14-105f7f7e8a5ea10dfac9874ff9d1a6bacba5f454
    vcpkg-scripts version: b51f391cd 2023-03-16 (14 hours ago)

好吧…这关目前我还无法突破。因为项目时间紧,先转战Ubuntu了,有时间再把坑填了吧(不知道CSDN支不支持文章编辑)。

装这一个东西耽误了我两天,真的蚌埠住了希望后来人少走点弯路…

你可能感兴趣的:(CG,windows,visualstudio)