【实践经验】Metashape 使用 Python 脚本无GUI自动化重建流程

什么是Metashape

Metashape之间叫做 photoscan,是一款商业化的三维重建软件,可以利用拍摄的多视角图像重建出场景的三维模型。这款商业化软件在多个平台都适用,包括Windows,MacOS,Linux这些。同时这款软件也提供了变成语言接口比如Python API,Java API。

该软件的官网首页是 agisoft, 感兴趣可以了解一下。需要注意,该软件需要收取费用,但可以申请30天的免费试用。

Linux 平台 Python API 使用

实验室的服务器都是Linux版本,服务器也提供了优秀的GPU资源可以供 metashape调用,因此能在服务器上运行该软件比较方便。同时可以使用Python API运行在后台,避免操作GUI界面。

安装过程

首先需要到官网上下载Linux对应的软件版本,并且购买或者申请30天的免费试用。下载好安装包后,以1.6.5 Linux 版本为例子压缩包为 metashape-pro_1_6_5_amd64.tar.gz。直接解压,就可以在当前目录的metashape-pro文件夹下看到可执行文件 metashape。

Python 脚本和 Python API。

  • Python API
    如果你使用的是最新版本的软件,可以在官网找到其文档,如下所示:
    在这里插入图片描述
    直接下载就可以查找如何具体调用API,如果你使用老版本的软件,比如 1.6.5,你可以将pdf文档对应的链接 https://www.agisoft.com/pdf/metashape_python_api_2_0_0.pdf, 把 2_0_0 字段替换成你使用的对应的版本号就可以。

  • Python 例程

下面是重建流程的程序,命名为 launch.py

import Metashape
import os, sys, time

# Checking compatibility
compatible_major_version = "2.0"
found_major_version = ".".join(Metashape.app.version.split('.')[:2])
# if found_major_version != compatible_major_version:
#    raise Exception("Incompatible Metashape version: {} != {}".format(found_major_version, compatible_major_version))

def find_files(folder, types):
    return [entry.path for entry in os.scandir(folder) if (entry.is_file() and os.path.splitext(entry.name)[1].lower() in types)]

if len(sys.argv) < 3:
    print("Usage: general_workflow.py <image_folder> <output_folder>")
    sys.exit(1)

image_folder = sys.argv[1]
output_folder = sys.argv[2]

photos = find_files(image_folder, [".jpg", ".jpeg", ".tif", ".tiff"])

doc = Metashape.Document()
doc.save(output_folder + '/project.psx')

chunk = doc.addChunk()

chunk.addPhotos(photos)
doc.save()

print(str(len(chunk.cameras)) + " images loaded")

chunk.matchPhotos(keypoint_limit = 40000, tiepoint_limit = 10000, generic_preselection = True, reference_preselection = True)
doc.save()

chunk.alignCameras()
doc.save()

chunk.buildDepthMaps(downscale = 2, filter_mode = Metashape.MildFiltering)
doc.save()

chunk.buildModel(source_data = Metashape.DepthMapsData)
doc.save()

chunk.buildUV(page_count = 2, texture_size = 4096)
doc.save()

chunk.buildTexture(texture_size = 4096, ghosting_filter = True)
doc.save()

has_transform = chunk.transform.scale and chunk.transform.rotation and chunk.transform.translation

if has_transform:
    chunk.buildDenseCloud()
    doc.save()

    chunk.buildDem(source_data=Metashape.PointCloudData)
    doc.save()

    chunk.buildOrthomosaic(surface_data=Metashape.ElevationData)
    doc.save()

# export results
chunk.exportReport(output_folder + '/report.pdf')

if chunk.model:
    chunk.exportModel(output_folder + '/model.obj')

if chunk.point_cloud:
    chunk.exportPoints(output_folder + '/point_cloud.ply', source_data = Metashape.PointCloudData, binary=True, format=Metashape.PointsFormatPLY)

if chunk.elevation:
    chunk.exportRaster(output_folder + '/dem.tif', source_data = Metashape.ElevationData)

if chunk.orthomosaic:
    chunk.exportRaster(output_folder + '/orthomosaic.tif', source_data = Metashape.OrthomosaicData)

print('Processing finished, results saved to ' + output_folder + '.')

使用方法:

./metashape -r luanch.py <图像路径> <重建结果路径>

metashape是解压后得到的可执行文件,如果没有执行权限,需要加上可执行权限。在解压目录执行 chmod a+x metashape。

注意: 不同的版本可能API会有些不同,可以参考对应的文档,替换一下程序中的部分接口。比如 2.0.0 构建点云的接口是 buildPointCloud, 但是 1.6.5 的接口是 buildDenseCloud 。

参考资料

开源脚本
参考博客
下载链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值