基于openMVG和openMVS的实用编程Python+Pyqt5

基于openMVG和openMVS的使用编程Python+Pyqt5

前言:对于许多要建立三维模型的小伙伴而言,openMVG+openMVS是目前不多的开源,可用的资源,时隔半年,我又来搞了。

本次使用教程使用Python3.8+Pyqt5
(ps:python真的太好用了,QT真的太好用啦,完全停不下来,C++/MFC:所以爱会消失对吗? 对的)


1.由配置好的openMVG+openMVS编写程序

(1)我就是用配置好的exe写的,因为简单方便。
/1/ .如果没有配出来的小伙伴也可以不用配置,直接下载已经配置好的文件,如果有需要,邮箱联系[email protected]或私信
/2/ .目前GitHub上也有大佬用源代码写的,参考网址链接: link
注:大部分地区进入GitHub需要VPN哦
本次程序所有所需头文件如下,为了增强阅读体验,已删除QT界面以及线程部分代码

import sys
import win32gui
import win32con
import subprocess
import time
import os
import shutil

# from Show3D import Ui_MainWindow
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox, QLabel, QProgressBar
from PyQt5.QtGui import QWindow
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QThread, QMutex, pyqtSignal

(2)Python+openMVG
其实这部分在配置openMVG时,实例文件可以查到,在windows下使用方法如下:
**exe -i inputfile -o outputfile -m method **
解释:运行exe, 传入参数,-i表示输入,-o表示输出,等等,openMVG完整代码如下

		# 路径设定
        openMVG_dir = 'D:/show3D/openMVG/'
        openMVS_dir = 'D:/show3D/openMVS/bin/'
        images_dir = 'D:/show3D/images/'
        # 相机参数文件,在openMVG源文件下
        CAMERA_SENSOR_WIDTH_DIRECTORY = 'D:/show3D/openMVG/sensor_width_camera_database.txt'
        openMVG_output_dir = 'D:/show3D/openMVG_output/'
        openMVS_output_dir = 'D:/show3D/openMVS_output/'
        # openMVG步骤
        # 清空上次运行结果
        shutil.rmtree(openMVG_output_dir)
        os.mkdir(openMVG_output_dir)
        psubprocess = subprocess.Popen(
            [os.path.join(openMVG_dir, "openMVG_main_SfMInit_ImageListing"), "-i", images_dir,
             "-d", CAMERA_SENSOR_WIDTH_DIRECTORY, "-o", openMVG_output_dir])
        psubprocess.wait()
        psubprocess = subprocess.Popen(
            [os.path.join(openMVG_dir, "openMVG_main_ComputeFeatures"), "-i", openMVG_output_dir + "sfm_data.json",
             "-o", openMVG_output_dir, "-m", "SIFT", "-p", "HIGH"])

        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVG_dir, "openMVG_main_ComputeMatches"),
                                        "-i", openMVG_output_dir + "sfm_data.json", "-o", openMVG_output_dir])
        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVG_dir, "openMVG_main_IncrementalSfM"),
                                        "-i", openMVG_output_dir + "sfm_data.json", "-m", openMVG_output_dir,
                                        "-o", openMVG_output_dir])
        psubprocess.wait()
        psubprocess = subprocess.Popen([os.path.join(openMVG_dir, "openMVG_main_ComputeSfM_DataColor"),
                                        "-i", openMVG_output_dir + "sfm_data.bin",
                                        "-o", openMVG_output_dir + "colored.ply"])
        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVG_dir, "openMVG_main_ComputeStructureFromKnownPoses"),
                                        "-i", openMVG_output_dir + "sfm_data.bin", "-m", openMVG_output_dir,
                                        "-o", openMVG_output_dir + "robust.bin",
                                        "-f", openMVG_output_dir + "matches.f.bin"])
        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVG_dir, "openMVG_main_openMVG2openMVS"),
                                        "-i", openMVG_output_dir + "sfm_data.bin",
                                        "-o", openMVG_output_dir + "scene.mvs"])
        psubprocess.wait()

注:建议每次调用时,清空上次运行的结果,亲测,上次文件仍然在,会发生各种意外的错误
(2)openMVS步骤
其实openMVS也可以用openMVG的方法调用,只不过在python中不显示运行结果,具体代码如下:

        # openMVS步骤
        shutil.rmtree(openMVS_output_dir)
        os.mkdir(openMVS_output_dir)
        shutil.copy2(openMVG_output_dir + "scene.mvs", openMVS_output_dir + "scene.mvs")
        psubprocess = subprocess.Popen([os.path.join(openMVS_dir, "DensifyPointCloud.exe"),
                                        "-i", os.path.join(openMVS_output_dir, 'scene.mvs')])
        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVS_dir, "ReconstructMesh.exe"),
                                        "-i", os.path.join(openMVS_output_dir, "scene_dense.mvs")])
        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVS_dir, "RefineMesh.exe"),
                                        "-i", os.path.join(openMVS_output_dir, "scene_dense_mesh.mvs")])
        psubprocess.wait()

        psubprocess = subprocess.Popen([os.path.join(openMVS_dir, "TextureMesh.exe"),
                                        "-i", os.path.join(openMVS_output_dir, "scene_dense_mesh_refine.mvs")])
        psubprocess.wait()

        self.qmut.unlock()
        self.DeleteFile()
   # 删除根目录下生成的文件,主要都是.log和.dmap
   def DeleteFile(self):
        file_path = 'D:/pythonProject/sfm/'
        for roots, dirs, files in os.walk(file_path):
            for file in files:
                if file.endswith('.log') or file.endswith('.dmap'):
                    os.remove(os.path.join(roots, file))

注:Windows下编译的openMVS不带Viewer,有需要的小伙伴私信,邮箱发配置好的整套openMVS

2.总结

以上就是所有运行openMVG和openMVS的代码。查看最后生成的mvs文件使用编译的Viewer.exe。cmd命令为:Viewer.exe scene_dense_mesh_refine_texture.mvs。python命令中间加 -i

下一章,将Viewer显示的图像内嵌到QT窗口中,并保留旋转等功能。祝大家心想事成。

你可能感兴趣的:(openMVG+openMVS,python,pyqt5)