Ubuntu20.04安装三维重建的openMVGopenMVS和colmap环境

Posted chenhaoooooooo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu20.04安装三维重建的openMVGopenMVS和colmap环境相关的知识,希望对你有一定的参考价值。

前言:

安装openMVS和colmap顺序不一样,会导致出现各种问题。这里建议先安装openMVS,再安装colmap,最后安装openMVG。

安装步骤参考了官网的dockerfile文件

一、openMVS安装过程

sudo apt-get -y install git mercurial cmake libpng-dev libjpeg-dev libtiff-dev libglu1-mesa-dev
git clone https://gitlab.com/libeigen/eigen --branch 3.4

安装eigen(一定要手动安装,自动安装的版本会低于3.4)

mkdir eigen_build
cd eigen_build &&\\
	cmake . ../eigen -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ &&\\
	make && sudo make install &&\\
	cd ..

安装Boost

sudo apt-get -y install libboost-iostreams-dev libboost-program-options-dev libboost-system-dev libboost-serialization-dev

安装OpenCV

sudo DEBIAN_FRONTEND=noninteractive apt-get install -yq libopencv-dev

安装CGAL

sudo apt-get -y install libcgal-dev libcgal-qt5-dev

安装VCGLib

git clone -b v1.0.1 https://github.com/cdcseacave/VCG.git vcglib

下载openMVS

git clone -b v1.1.1  https://github.com/cdcseacave/openMVS.git

编译

mkdir openMVS_build

注意了这里的cuda版本,需要改成自己的

cd openMVS_build &&\\
	cmake . ../openMVS -DCMAKE_BUILD_TYPE=Release -DVCG_ROOT=../vcglib -DOpenMVS_USE_CUDA=ON -DCMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs/ -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.6.0/ -DCUDA_INCLUDE_DIRS=/usr/local/cuda-11.6.0/include/ -DCUDA_CUDART_LIBRARY=/usr/local/cuda-11.6.0lib64 -DCUDA_NVCC_EXECUTABLE=/usr/local/cuda-11.6.0/bin/

安装

	make -j4 &&\\
	sudo make install

二、colmap安装过程

sudo apt-get install -y \\
    git \\
    cmake \\
    vim \\
    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 \\
    libcgal-qt5-dev

Build and install ceres solver

sudo apt-get -y install \\
    libatlas-base-dev \\
    libsuitesparse-dev
git clone https://ghproxy.com/https://github.com/ceres-solver/ceres-solver.git --branch 1.14.0
cd ceres-solver && \\
	mkdir build && \\
	cd build && \\
	cmake .. -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF && \\
	make -j4 && \\
	sudo make install

下载colmap

git clone -b 3.6 https://ghproxy.com/https://github.com/colmap/colmap.git #--branch 3.6-dev.3

 如果编译出现代码报错,可以更换其colmap的其它版本

cd colmap && \\
	#git checkout dev && \\
	mkdir build && \\
	cd build && \\
	cmake .. && \\
	make -j4 && \\
	sudo make install

 三、安装openMVG

安装依赖

由于ubuntu20的资源库缺少python-minimal和liblemon-dev,我将python-minimal换成python2-minimal,而liblemon-dev采用手动安装。

apt-get install -y \\
  cmake \\
  build-essential \\
  graphviz \\
  git \\
  coinor-libclp-dev \\
  libceres-dev \\
  libflann-dev \\
  #liblemon-dev \\
  libjpeg-dev \\
  libpng-dev \\
  libtiff-dev \\
  python2-minimal; \\
  apt-get autoclean && apt-get clean

安装liblemon-dev

打开lemon库网站,将lemon下载下来,我这里直接给出下载地址:http://security.ubuntu.com/ubuntu/pool/universe/s/sqlite3/lemon_3.31.1-4ubuntu0.5_amd64.deb

然后使用下面命令进行安装:

sudo dpkg -i lemon_3.31.1-4ubuntu0.5_amd64.deb

下载openMVG

git clone --recursive https://github.com/openMVG/openMVG.git

编译

mkdir openMVG_Build; \\
  cd openMVG_Build;
cmake -DCMAKE_BUILD_TYPE=RELEASE \\
    -DOpenMVG_BUILD_TESTS=ON \\
    -DOpenMVG_BUILD_EXAMPLES=OFF \\
    ../openMVG/src; \\
    make -j 4;

安装

make test && sudo make install;

ps:如果安装过程中出现各种乱七八糟的问题,可以使用docker进行安装,确认好安装步骤后再迁移本地。

结构光三维重建Projector-Camera Calibration投影仪相机标定

结构光三维重建-Projector-Camera Calibration投影仪相机标定

目录

结构光三维重建-Projector-Camera Calibration投影仪相机标定

1.结构光相机标定软件

2. 依赖环境

2.1 安装TIFF

2.2 安装OpenCV

(1) 源码编译

(2) 编译OpenCV常见错误

2.3 安装QT5

3. 编译标定源码:3D Scanning Software

(1)源码编译

(2)常见的错误和解决方法

4.懒人必备

5. 结构光三维重建-3D Scanning Software实现三维重建


1.结构光相机标定软件

结构光相机标定是一件十分复杂的过程,所幸布朗大学开源了一套投影仪摄像机标定系统(Projector-Camera Calibration/3D Scanning Software) 

官方地址: Projector-Camera Calibration / 3D Scanning Software

官网提供了Window和Apple OSX编译好的标定软件,很不幸的是,版本比较老旧,仅支持Windows 7 x64,Opencv也是比较老旧的OpenCV 2.4.11,没有提供Linux版本的,你想在Ubuntu成功运行还是要采坑的。

下面手把手教你,如何在Ubuntu上,编译源码

 当然,若你不想经历填坑的痛苦,可以直接下载本人已经修改好的源码,已经包括Opencv,TIFF,scan3d-capture-src-2015等源码,直接使用即可

下载地址: https://download.csdn.net/download/guyuealian/36484460

 关于如果实现三维重建,请查看下一篇博客《结构光三维重建-3D Scanning Software实现三维重建》:https://panjinquan.blog.csdn.net/article/details/121113787


2. 依赖环境

本地使用版本说明
UbuntuUbuntu18.04本机系统
tifftiff-4.3.0 (tiff-4.0.x以上亦可)opencv编译需要TIFF

OpenCV

opencv-4.3.0 (opencv-3.2.0.x以上亦可)3D Scanning Software需要用的OpenCV
 QtQt 5.5.1(Qt 5.0.x以上亦可)3D Scanning Software的UI是使用Qt布局的

2.1 安装TIFF

下载地址:Index of /libtiff/ ,tiff-4.0.4.tar.gz (tiff-4.0.x以上亦可)

   # 解压tar -zxvf tiff-4.0.4.tar.gz
   # 进入tiff目录
   ./configure
   make
   sudo make install

2.2 安装OpenCV

(1) 源码编译

安装 OpenCV 所需要的库,在终端依次执行以下命令:

sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

官网下载 OpenCV 4.3.0 的源代码(下载地址:Releases - OpenCV)并解压到 opencv4.3.0 目录,然后进入到 opencv4.3.0 的目录,执行

mkdir build
cd build

配置 OpenCV 工程文件

sudo cmake -DBUILD_TIFF=ON -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

PS: 编译选项中,一定要加上-DBUILD_TIFF=ON, 因为标定系统源码需要用到TIFF,否则会出现TIFFReadDirectory@LIBTIFF_4.0’未定义的引用的错误

如果执行命令时出现下载 ippicv 失败,可以参考文章解决:ubuntu编译安装opencv3.3+python3

编译安装

sudo make install

编译OpenCV,加上-DBUILD_TIFF=ON选项后,可能会出现以下各种错误

(2) 编译OpenCV常见错误

1.error: ‘O_RDONLY’ undeclared

/home/dm/app/opencv-4.3.0/3rdparty/libtiff/tif_close.c: In function ‘TIFFCleanup’:
/home/dm/app/opencv-4.3.0/3rdparty/libtiff/tif_close.c:50:23: error: ‘O_RDONLY’ undeclared (first use in this function)
  if (tif->tif_mode != O_RDONLY)

 解决方法:在opencv-4.3.0/3rdparty/libtiff/tiff.h头文件添加头文件#include <fcntl.h>

....
#ifndef _TIFF_
#define	_TIFF_

#include "tiffconf.h"
#include <fcntl.h>
....

PS:如果还有类似的错误,请在发生错误的该源码文件中加入头文件#include <fcntl.h>

2.error: conflicting declaration of ‘int _TIFF_snprintf_f

In file included from /home/dm/app/opencv-4.3.0/3rdparty/libtiff/tif_stream.cxx:28:0:
/home/dm/app/opencv-4.3.0/3rdparty/libtiff/tiffiop.h:60:18: error: conflicting declaration of ‘int _TIFF_snprintf_f(char*, size_t, const char*, ...)’ with ‘C’ linkage
 #define snprintf _TIFF_snprintf_f

 解决方法:声明存在冲突,应该Linux已经存在了,所以可以注释opencv-4.3.0/3rdparty/libtiff/tiffiop.h源码的下面两行:

//#define snprintf _TIFF_snprintf_f
//extern int snprintf(char* str, size_t size, const char* format, ...);

3.error: ‘strchr’ was not declared in this scope

/home/dm/app/opencv-4.3.0/3rdparty/libtiff/tif_stream.cxx:361:6: error: ‘strchr’ was not declared in this scope
  if( strchr(mode, 'w') )
      ^~~~~~
/home/dm/app/opencv-4.3.0/3rdparty/libtiff/tif_stream.cxx:361:6: note: suggested alternative: ‘strtoq’
  if( strchr(mode, 'w') )

 解决方法:在发生错误的该源码文件中opencv-4.3.0/3rdparty/libtiff/tif_stream.cxx加入头文件 #include <string.h>

2.3 安装QT5

官网下载:https://download.qt.io/archive/qt/
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/qt/
下载qt-opensource-linux-x64-5.12.4.run (Qt 5.0.x以上亦可)

对于Linux系统,Qt官方的安装包总是假定C++编译器compiler、调试器debugger、生成器make以及其他开发工具 由该操作系统自己提供,另外构建Qt图形界面程序还需要OpenGL库文件(扩展名为 .a 和 .so)和头文件(扩展名为 .h), OpenGL的开发库也是需要操作系统自己提供。所以在安装Qt官方run文件之前,必须先安装好Linux系统自己软件源里面 的GNU开发工具集和OpenGL开发库。下面针对三类 Linux 操作系统安装相应的GNU开发工具集和OpenGL开发库, 请注意安装软件都需要root权限,并且要联网安装。

sudo apt-get install build-essential libgl1-mesa-dev

给QT安装文件可执行权限:

chmod +x qt-opensource-linux-x64-5.12.4.run

安装

./qt-opensource-linux-x64-5.12.4.run

QT安装教程,可参考:https://blog.csdn.net/a435262767/article/details/99317188 

安装后,终端输入qmake,会出现下截图,表示QT已经安装成功:


3. 编译标定源码:3D Scanning Software

(1)源码编译

 官方地址: Projector-Camera Calibration / 3D Scanning Software  ,把下面的内容都下载下来

解压scan3d-capture-src-2015.zip

在project/scan3d-capture.pri,配置qmake的opencv路径:

INCLUDEPATH += /usr/local/include/opencv4 \\
                /usr/local/include/opencv4/opencv2


LIBS += /usr/local/lib/libopencv_* \\

 配置好opencv,就可以开始编译标定源码了:

cd scan3d-capture-src
mkdir build
cd build
qmake ../project # 使用QT的qmake进行build
make # (or nmake to build with Microsoft Visual Studio)

每什么意外的话,直接make会出现各种opencv错误的,究其原因是opencv版本对应不上,源码使用的OpenCV 2.4.11,如果需要更高版本的OpenCV,如OpenCV4.3.0,则需要改动地方挺多

(2)常见的错误和解决方法

(1) error: ‘CV_BGR2GRAY’ was not declared in this scope

 将所有CV_BGR2GRAY改为cv::COLOR_BGR2GRAY

//cvtColor(rgb_image, gray_image, CV_BGR2GRAY); // 旧版本
cvtColor(rgb_image, gray_image, cv::COLOR_BGR2GRAY);// 新版本

(2)error: ‘CV_RANSAC’ was not declared in this scope

 同上,将CV_RANSAC改为cv::RANSAC

//cv::Mat H = cv::findHomography(img_points, proj_points, CV_RANSAC);
cv::Mat H = cv::findHomography(img_points, proj_points, cv::RANSAC);

(3)error: ‘CV_TERMCRIT_EPS’ was not declared in this scope

 将参数CV_TERMCRIT_EPS + CV_TERMCRIT_ITER改为cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER

// cv::cornerSubPix(gray_image, cam_corners, cv::Size(11, 11), cv::Size(-1, -1),
// cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
cv::cornerSubPix(gray_image, cam_corners, cv::Size(11, 11), cv::Size(-1, -1),
                                 cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.1));

(4)error: no matching function for call to ‘stereoCalibrate

修改参数:cv::stereoCalibrate如下

// calib.stereo_error = cv::stereoCalibrate(world_corners_active, camera_corners_active, projector_corners_active, calib.cam_K, calib.cam_kc, calib.proj_K, calib.proj_kc, 
//                                                imageSize /*ignored*/, calib.R, calib.T, E, F, 
//                                                cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 150, DBL_EPSILON), 
//                                                cv::CALIB_FIX_INTRINSIC /*cv::CALIB_USE_INTRINSIC_GUESS*/ + cal_flags);
calib.stereo_error = cv::stereoCalibrate(world_corners_active, camera_corners_active, projector_corners_active, calib.cam_K, calib.cam_kc, calib.proj_K, calib.proj_kc,
                                             imageSize /*ignored*/, calib.R, calib.T, E, F,
                                             cv::CALIB_FIX_INTRINSIC /*cv::CALIB_USE_INTRINSIC_GUESS*/ + cal_flags,
                                             cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 150, DBL_EPSILON));

(5)error: ‘CV_CAP_PROP_FRAME_WIDTH’ was not declared in this scope

(6)error: ‘CV_CAP_PROP_FRAME_HEIGHT’ was not declared in this scope

 这些错误都是因为版本差异问题导致的,需要改用的地方很多

修改头文件VideoInput.hpp:

//CvCapture * _video_capture;
cv::VideoCapture * _video_capture;

修改源文件VideoInput.cpp,其他地方也参考修改

//cvSetCaptureProperty(_video_capture, CV_CAP_PROP_FRAME_WIDTH, width);
//cvSetCaptureProperty(_video_capture, CV_CAP_PROP_FRAME_HEIGHT, height);
_video_capture->set(cv::CAP_PROP_FRAME_WIDTH, width);
_video_capture->set(cv::CAP_PROP_FRAME_HEIGHT, height);

(7)error: ‘IplImage’ was not declared in this scope 

 源码修改如下:

    while(_video_capture && !_stop && error<max_error)
    
        IplImage * frame = cvQueryFrame(_video_capture);
        if (frame)
           //ok
            error = 0;
            emit new_image(cv::Mat(frame));
        
        else
           //error
            if (timer.elapsed()>warmup) error++;
        
    
/*************************改为****************************/
    while(_video_capture && !_stop && error<max_error)
    
        //IplImage * frame = cvQueryFrame(_video_capture);
        cv::Mat frame;
        //*_video_capture >> frame;
        _video_capture->read(frame);
        if (not frame.empty())
           //ok
            error = 0;
            emit new_image(cv::Mat(frame));
        
        else
           //error
            if (timer.elapsed()>warmup) error++;
        
    

(7) error: ‘cvReleaseCapture’ was not declared in this scope

如下修改: 

        //cvReleaseCapture(&_video_capture);
        _video_capture->release();

 (8)error: ‘CV_CAP_V4L2’ was not declared in this scope

将CV_CAP_V4L2修改为cv::CAP_V4L2

 (9)error: ‘cvCreateCameraCapture’ was not declared in this scope

修改如下 

//_video_capture = cvCreateCameraCapture(CLASS + index);
 _video_capture->open(CLASS + index);

最终,最后。。。。。,NND,还有错

 (10)VideoInput.cpp:(.text+0xb73):对‘VideoInput::list_device_resolutions_v4l2(int, bool)’未定义的引用

    //list = list_device_resolutions_v4l2(index, silent);
    list = list_device_resolutions_dshow(index, silent);

OK,终于成功:

然后,你在 bin目录就可以找到一个可执行文件:scan3d-capture,

运行他即可:

./scan3d-capture

千呼万唤始出来,终于见到这个界面了:


4.懒人必备

兄弟,如果,你不想经历上面填坑的痛苦,可以直接下载本人已经修改好的源码,已经包括Opencv,TIFF,scan3d-capture-src-2015等源码,直接使用即可

下载地址: https://download.csdn.net/download/guyuealian/36484460

 使用方法,如下:

## 安装tiff-4.3.0

```bash
cd tiff-4.3.0
./configure
make
sudo make install

```


## 安装OpenCV

已经把常见的错误,都修改了,可以直接build

```bash
cd opencv-4.3.0
bash build.sh

```


## 安装QT5


```bash
sudo apt-get install build-essential libgl1-mesa-dev
chmod +x qt-opensource-linux-x64-5.12.4.run
./qt-opensource-linux-x64-5.12.4.run

```


## 安装3D Scanning Software

已经把常见的错误,都修改了,可以直接build

```bash
cd scan3d-capture-src-2015/scan3d-capture-src
mkdir build
cd build
qmake ../project # 使用QT的qmake进行build
make # (or nmake to build with Microsoft Visual Studio)
```

## 运行3D Scanning Software
```bash
./bin/scan3d-capture
```


5. 结构光三维重建-3D Scanning Software实现三维重建

 关于如果实现三维重建,请查看下一篇博客《结构光三维重建-3D Scanning Software实现三维重建》:https://panjinquan.blog.csdn.net/article/details/121113787

以上是关于Ubuntu20.04安装三维重建的openMVGopenMVS和colmap环境的主要内容,如果未能解决你的问题,请参考以下文章

重建我的3D世界代码开源连载-3Colmap和OpenMVG对比

重建我的3D世界代码开源连载-3Colmap和OpenMVG对比

重建我的3D世界代码开源连载-3Colmap和OpenMVG对比

Ubuntu18.04下VIsualSFM的安装

重建我的3D世界代码开源连载-2

重建我的3D世界代码开源连载-2