技術者の休日

技術者として働いています。休日の息抜きについて書いて行きます♪

【備忘録】openMVGとopenMVSを用いた3次元復元

1. 3次元復元結果

f:id:nocchi2017:20180429221421p:plain

2. 事前準備(インストール、必要ファイル等)

1) openMVGのインストール

GitHub - openMVG/openMVG: open Multiple View Geometry library. Basis for 3D computer vision and Structure from Motion.

2) openMVSのインストール

Building · cdcseacave/openMVS Wiki · GitHub
Usage · cdcseacave/openMVS Wiki · GitHub


3) MeshLabのインストール

 $ sudo apt-get update

 $ sudo apt-get upgrade

 $ sudo apt-get install meshlab

4) 撮影カメラのセンササイズを準備

 ・データベースに含まれている場合:追加作業不要

 ・データベースに含まれていない場合:センササイズを調査しデータベースへの追加が必要

 [データベース場所]

 ・・・/openMVG/src/openMVG/exif/sensor_width_database/sensor_width_camera_database.txt

openMVG/sensor_width_camera_database.txt at master · openMVG/openMVG · GitHub


    ※iPhone SEはデータベースに含まれていなかったため、下記サイトを参考に4.8を採用

Apple iPhone SE - Specifications


 

3. 3次元復元の手順

1) 3次元復元用の画像データを用意

    ・/MyImg・・・画像ファイル保管先

2) 下記Phythonコードを実行

 ・$ python 180429_3D_Reconstruction_by_openMVG_and_openMVS.py ・・・/MyImg・・・/MyImg/Output3D

 [Pythonコード要修正箇所]

 a) openMVGをインストールした場所の絶対パス

 b) openMVGをインストールした場所の絶対パス

180429_3D_Reconstruction_by_openMVG_and_openMVS.py(下記コードを参考に追記)
openMVG/SfM_SequentialPipeline.py.in at master · openMVG/openMVG · GitHub

OPENMVG_SFM_BIN = "/home/(a:要変更)/openMVG/openMVG_Build/Linux-x86_64-RELEASE"

# Indicate the openMVG camera sensor width directory
CAMERA_SENSOR_WIDTH_DIRECTORY = "/home/(b:要変更)/openMVG/src/software/SfM" + "/../../openMVG/exif/sensor_width_database"

import commands
import os
import subprocess
import sys

if len(sys.argv) < 3:
    print ("Usage %s image_dir output_dir" % sys.argv[0])
    sys.exit(1)

input_dir = sys.argv[1]
output_dir = sys.argv[2]
matches_dir = os.path.join(output_dir, "matches")
reconstruction_dir = os.path.join(output_dir, "reconstruction_sequential")
camera_file_params = os.path.join(CAMERA_SENSOR_WIDTH_DIRECTORY, "sensor_width_camera_database.txt")

print ("Using input dir  : ", input_dir)
print ("      output_dir : ", output_dir)

# Create the ouput/matches folder if not present
if not os.path.exists(output_dir):
  os.mkdir(output_dir)
if not os.path.exists(matches_dir):
  os.mkdir(matches_dir)

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] )
pIntrisics.wait()

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"] )
pFeatures.wait()

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] )
pMatches.wait()

# Create the reconstruction if not present
if not os.path.exists(reconstruction_dir):
    os.mkdir(reconstruction_dir)

print ("4. Do Sequential/Incremental reconstruction")
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()

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()

# optional, compute final valid structure from the known camera poses
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, "-f", os.path.join(matches_dir, "matches.f.bin"), "-o", os.path.join(reconstruction_dir,"robust.bin")] )
pRecons.wait()

pRecons = subprocess.Popen( [os.path.join(OPENMVG_SFM_BIN, "openMVG_main_ComputeSfM_DataColor"),  "-i", reconstruction_dir+"/robust.bin", "-o", os.path.join(reconstruction_dir,"robust_colorized.ply")] )
pRecons.wait()

# Use function of OpenMVS
pConvert = subprocess.Popen( ["openMVG_main_openMVG2openMVS",  "-i", reconstruction_dir+"/sfm_data.bin", "-o", os.path.join(reconstruction_dir,"scene.mvs")] )
pConvert.wait()

pDensify = subprocess.Popen( ["DensifyPointCloud", os.path.join(reconstruction_dir,"scene.mvs")] )
pDensify.wait()

pMesh = subprocess.Popen( ["ReconstructMesh", os.path.join(reconstruction_dir,"scene_dense.mvs")] )
pMesh.wait()

pRefine = subprocess.Popen( ["RefineMesh", os.path.join(reconstruction_dir,"scene_mesh.mvs")] )
pRefine.wait()

pTexture = subprocess.Popen( ["TextureMesh", os.path.join(reconstruction_dir,"scene_dense_mesh.mvs")] )
pTexture.wait()

最後まで読んで頂き、ありがとうございました。