我们知道,照相机的原理是将一个三维场景投影到二维平面。所谓视觉三维重建,顾名思义就是从已有的二维图像中复原原始三维场景。
三维重建的原理大致如下:
基于图像的三维重建基本流程
多张图像的特征点匹配
多视图稠密重建(MVS)
目前,有不少开源的三维重建系统,本文简单介绍使用OpenMVG(有CUDA的可以用colmap)+PMVS(OpenMVS安装的坑比较多),实现三维场景的三维重建。
openMVG (Open Multiple View Geometry):开源多视角立体几何库,这是一个 cv 界处理多视角立体几何的著名开源库,信奉“简单,可维护”,提供了一套强大的接口,每个模块都被测试过,尽力提供一致可靠的体验。
openMVG 实现以下典型应用:
CMVS-PMVS(a modified version):将运动结构(SfM)软件的输出作为输入,然后将输入图像分解成一组可管理大小的图像簇。MVS 软件可以用来独立和并行地处理每个簇,其中来自所有簇的重建不错过任何细节。
常见的多视图三维重建管线:重建稀疏点云-Structure from Motion(Sfm)→重建稠密点云-Multi-View Stereo(MSV)→重建表面-Surface Generation(SG)→纹理映射-Texture Mapping(TM)
在本文中,OpenMVG负责从原始图像到稀疏点云,PMVS负责重建稠密点云、重建表面和纹理映射。我这里还使用了Meshlab查看模型(点云)生成效果。
OpenMVG安装过程:(参考:openMVG官方BUILD.md)
# 安装依赖
sudo apt-get install libpng-dev libjpeg-dev libtiff-dev libxxf86vm1 libxxf86vm-dev libxi-dev libxrandr-dev graphviz
# 克隆代码
git clone --recursive https://github.com/openMVG/openMVG.git
# configure && build
mkdir openMVG_Build && cd openMVG_Build
cmake -DCMAKE_BUILD_TYPE=RELEASE ../openMVG/src/ -DOpenMVG_BUILD_TESTS=ON
sudo cmake --build . --target install
# test
make test
ctest --output-on-failure -j
# .bashrc
export PATH=$PATH:/home/work/tools/openMVG_Build/Linux-x86_64-RELEASE/
CMVS-PMVS安装过程:
git clone https://github.com/pmoulon/CMVS-PMVS.git
cd CMVS-PMVS
mkdir build && cd build
cmake ../program/
make
sudo cp main/pmvs2 main/genOption main/cmvs /usr/local/bin/
1. OpenMVG提取稀疏点云(参考:openMVG使用示例)
原始数据:11张从不同角度拍摄的城堡照片
openMVG提取稀疏点云过程:(参考openMVG_Build/software/SfM/tutorial_demo.py,测试图片和脚本:openmvg_test.tar)
cd openMVG
vim 3dr_test.py
#!/usr/bin/python
#! -*- encoding: utf-8 -*-
# openmvg使用示例
# usage : python tutorial_demo.py
import os
import subprocess
import sys
# openmvg编译bin目录(可cp -p到/usr/local/bin/)
OPENMVG_SFM_BIN = "/home/work/tools/openMVG_Build/Linux-x86_64-RELEASE"
# pmvs编译bin目录(可cp -p到/usr/local/bin/)
PMVS_BIN = "/home/work/tools/CMVS-PMVS/build/main"
# openmvg相机参数目录
CAMERA_SENSOR_WIDTH_DIRECTORY = "/home/work/tools/openMVG/src/openMVG/exif/sensor_width_database"
# 0. 下载测试照片
os.chdir(os.path.dirname(os.path.abspath(__file__)))
data_dir = os.path.abspath("./book")
#data_dir = os.path.abspath("./ImageDataset_SceauxCastle")
'''if not os.path.exists(data_dir):
pImageDataCheckout = subprocess.Popen([ "git", "clone", "https://github.com/openMVG/ImageDataset_SceauxCastle.git" ])
pImageDataCheckout.wait()'''
input_dir = os.path.join(data_dir, "images")
output_dir = data_dir
print ("Using input dir : ", input_dir)
print (" output_dir : ", output_dir)
matches_dir = os.path.join(output_dir, "matches")
camera_file_params = os.path.join(CAMERA_SENSOR_WIDTH_DIRECTORY, "sensor_width_camera_database.txt") #相机参数
if not os.path.exists(matches_dir):
os.mkdir(matches_dir)
# 1. 从图片数据集中生成场景描述文件sfm_data.json
print ("----------1. Intrinsics analysis----------")
pIntrisics = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_SfMInit_ImageListing"), "-i", input_dir, "-o", matches_dir, "-d", camera_file_params, "-c", "3"] )
#*注:如果产出的sfm_data.json里intrinsics内容为空,通常是在图片没有exif信息导致获取不到相机焦距、ccd尺寸等参数,用带exif的原图即可。
pIntrisics.wait()
# 2. 计算图像特征
print ("----------2. Compute features----------")
pFeatures = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeFeatures"), "-i", matches_dir+"/sfm_data.json", "-o", matches_dir, "-m", "SIFT", "-f" , "1"] )
pFeatures.wait()
# 3. 计算几何匹配
print ("----------3. Compute matches----------")
pMatches = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeMatches"), "-i", matches_dir+"/sfm_data.json", "-o", matches_dir, "-f", "1", "-n", "ANNL2"] )
pMatches.wait()
# 4. 执行增量三维重建
reconstruction_dir = os.path.join(output_dir,"reconstruction_sequential")
print ("----------4. Do Incremental/Sequential reconstruction----------") #set manually the initial pair to avoid the prompt question
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_IncrementalSfM"), "-i", matches_dir+"/sfm_data.json", "-m", matches_dir, "-o", reconstruction_dir] )
pRecons.wait()
# 5. 计算场景结构颜色
print ("----------5. Colorize Structure----------")
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeSfM_DataColor"), "-i", reconstruction_dir+"/sfm_data.bin", "-o", os.path.join(reconstruction_dir,"colorized.ply")] )
pRecons.wait()
# 6. 测量稳健三角
print ("----------6. Structure from Known Poses (robust triangulation)----------")
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeStructureFromKnownPoses"), "-i", reconstruction_dir+"/sfm_data.bin", "-m", matches_dir, "-o", os.path.join(reconstruction_dir,"robust.ply")] )
pRecons.wait()
'''
# 使用全局SfM管道重建Reconstruction for the global SfM pipeline
# 3.1 全局sfm管道几何匹配
print ("----------3.1. Compute matches (for the global SfM Pipeline)----------")
pMatches = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeMatches"), "-i", matches_dir+"/sfm_data.json", "-o", matches_dir, "-r", "0.8", "-g", "e"] )
pMatches.wait()
# 4.1 执行全局三维重建
reconstruction_dir = os.path.join(output_dir,"reconstruction_global")
print ("----------4.1. Do Global reconstruction----------")
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_GlobalSfM"), "-i", matches_dir+"/sfm_data.json", "-m", matches_dir, "-o", reconstruction_dir] )
pRecons.wait()
# 5.1 计算场景结构颜色
print ("----------5.1. Colorize Structure----------")
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeSfM_DataColor"), "-i", reconstruction_dir+"/sfm_data.bin", "-o", os.path.join(reconstruction_dir,"colorized.ply")] )
pRecons.wait()
# 6.1 测量稳健三角
print ("----------6.1. Structure from Known Poses (robust triangulation)----------")
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeStructureFromKnownPoses"), "-i", reconstruction_dir+"/sfm_data.bin", "-m", matches_dir, "-o", os.path.join(reconstruction_dir,"robust.ply")] )
pRecons.wait()
'''
# 7. 把openMVG生成的SfM_Data转为适用于PMVS输入格式的文件
print ("----------7. Export to PMVS/CMVS----------")
pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_openMVG2PMVS"), "-i", reconstruction_dir+"/sfm_data.bin", "-o", reconstruction_dir] )
pRecons.wait()
#*注:执行后会在-o路径下生成一个PMVS目录,包含 models, txt, visualize 三个子目录:models为空;txt包含对应图像的txt文档,每个里面都是一个3x4的矩阵,大概是相机位姿;visualize包含11张图像,不确定是原图像还是校正过的图像
# 8. 使用PMVS重建稠密点云、表面、纹理
print ("----------8. pmvs2----------")
pRecons = subprocess.Popen( [os.path.join(PMVS_BIN, "pmvs2"), reconstruction_dir+"/PMVS/", "pmvs_options.txt"] ) # 注:不要修改pmvs_options.txt文件名
pRecons.wait()
#*注:执行后会在./PMVS/models文件夹中生成一个pmvs_options.txt.ply点云文件,用meshlab打开即可看到重建出来的彩色稠密点云。
#执行三维重建测试
python 3dr_test.py
2.安装MeshLab,查看生成的稀疏点云文件:
下载安装:http://www.meshlab.net/#download
右上方俯视城堡稀疏点云 :(打开reconstruction_xxx下的colorized.ply或robust.ply)
3.PMVS重建稠密点云、重建表面和纹理映射过程:(测试生成的PMVS目录:pmvs_test.tar)
# 1. 把openMVG生成的SfM_Data转为适用于PMVS输入格式的文件
cd openMVG/ImageDataset_SceauxCastle/reconstruction_global/
openMVG_main_openMVG2PMVS -i sfm_data.bin -o ./
*注:执行后会在-o路径下生成一各PMVS目录,包含 models, txt, visualize 三个子目录:models为空;txt包含对应图像的txt文档,每个里面都是一个3x4的矩阵,大概是相机位姿;visualize包含11张图像,不确定是原图像还是校正过的图像
# 2. 使用PMVS重建稠密点云、表面、纹理
pmvs2 ./PMVS/ pmvs_options.txt # 注:不要修改pmvs_options.txt文件名
*注:执行后会在./PMVS/models文件夹中生成一个pmvs_options.txt.ply点云文件,用meshlab打开即可看到重建出来的彩色稠密点云。
生成的三维稠密点云俯视角:
1.准备照片
注意事项:
拿手边的书随便拍了几张:book.zip
2.三维重建
python 3dr_test.py
1.openMVG_main_IncrementalSfM: error while loading shared libraries: liblapack.so: cannot open shared object file: No such file or directory
解决方法:
$ which openMVG_main_IncrementalSfM
/usr/local/bin/openMVG_main_IncrementalSfM
$ ll openMVG_Build/Linux-x86_64-RELEASE/openMVG_main_IncrementalSfM
openMVG_Build/Linux-x86_64-RELEASE/openMVG_main_IncrementalSfM
#经过对比测试发现/usr里确实是最新build的文件,但链接关系貌似有问题;直接使用openMVG_Build/Linux-x86_64-RELEASE/openMVG_main_IncrementalSfM就能正常
$ sudo cp ../openMVG_Build/Linux-x86_64-RELEASE/* /usr/local/bin/ #整体cp覆盖后恢复正常
2.type INITIAL pair ids: X enter Y enter
----------------------------------------------------
SequentialSfMReconstructionEngine::ChooseInitialPair
----------------------------------------------------
Pairs that have valid intrinsic and high support of points are displayed:
Choose one pair manually by typing the two integer indexes
----------------------------------------------------
(1,5) 77 matches
(2,4) 39 matches
(0,2) 39 matches
(3,7) 31 matches
(2,5) 29 matches
(2,3) 19 matches
(0,1) 18 matches
type INITIAL pair ids: X enter Y enter
2
4
Putative starting pair is: (2,4)
A-Contrario initial pair residual: 7.61613
SfM过程中遇到这个提示,可以按以上方式输入跳过,不过通常这时照片的质量堪忧,不要指望建出来预期的效果了,不如老老实实多重拍些,用视频切桢也行。
生成的三维点云数据如果要用于自动驾驶避障,最好增加超声波、毫米波雷达,并把各传感器生成的点云与视觉重建点云进行融合。对融合后的点云数据进行噪点清理、三维目标检测,生成三维障碍物的类型/大小/位置信息,再根据三维空间的行进方向进行避障路径规划。
本文仅做学术分享,如有侵权,请联系删文。