metashape-pro python scripts render specified viewpoint

时间:2021-09-03 01:16:30

官方python脚本使用文档

主函数: render=chunk.model.renderImage()

metashape-pro python scripts render specified viewpoint

1. 实现特定视角的渲染需要通过脚本方式进行,原本的metashape pro中是没有这个功能的

首先在metashape pro所在的文件夹中新建 test.py ,将脚本内容写在该文件中,然后回到metashape Tools -> Run Script 运行 test.py 这样metashape界面就会出现一个新的 Scripts 选项,这个选项下,就是你所需要的通过脚本添加的功能。

注意:每一次 Run Scripts 集成的功能是固定死的… 如果代码发生修改,需要重新跑脚本添加相应功能,并且每个按钮是不覆盖的。

metashape-pro python scripts render specified viewpoint

# Script save model renders for selected cameras (or all aligned cameras if no aligned cameras selected)
# to the same folder where the source photos are present with the "_render" suffix.
#
# This is python script for Metashape Pro. Scripts repository: https://github.com/agisoft-llc/metashape-scripts
import json
import Metashape
import os

# Checking compatibility
compatible_major_version = "1.6"
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 get_cameras(chunk):
    selected_cameras = [camera for camera in chunk.cameras if camera.transform and camera.selected and camera.type == Metashape.Camera.Type.Regular]

    if len(selected_cameras) > 0:
        return selected_cameras
    else:
        return [camera for camera in chunk.cameras if camera.transform and camera.type == Metashape.Camera.Type.Regular]


def render_cameras():
    print("Script started...")

    chunk = Metashape.app.document.chunk
    # if not chunk.model:
    #     raise Exception("No model!")

    # for camera in get_cameras(chunk):
    #     if not camera.type == Metashape.Camera.Type.Regular: #skip camera track, if any
    #         continue

    #     render = chunk.model.renderImage(camera.transform, camera.sensor.calibration)

    #     photo_dir = os.path.dirname(camera.photo.path)
    #     print("###photo dir:",photo_dir)
    #     photo_filename = os.path.basename(camera.photo.path)
    #     render_filename = os.path.splitext(photo_filename)[0] + "_render.jpg"
    #     print("render filename:",os.path.join(photo_dir, render_filename))
    #     render.save(os.path.join(photo_dir, render_filename))

    scene_json="/home/paper/Desktop/meta_100/querry_transforms.json"
    with open(scene_json) as f:
        test_transforms = json.load(f)
    
    camera=get_cameras(chunk)
    cm1=camera[0]
    testnum = len(test_transforms["frames"])
    print("test num:",testnum)
    for  idx in range(testnum):

        frame = test_transforms["frames"][idx]
        pose=frame["transform_matrix"]
        picname = frame["file_path"][9:]
        render = chunk.model.renderImage(pose, cm1.sensor.calibration)
        photo_dir="/home/paper/Desktop/meta_100/meta_render/"
        render.save(os.path.join(photo_dir, picname))
    print("Script finished!")


label = "Scripts/Render free poses"
Metashape.app.addMenuItem(label, render_cameras)
print("To execute this script press {}".format(label))

2. 相机位姿的输入

可参照该问题:render an image of a point cloud seen from a pose, with certain intrinsics

3. Agisoft Photoscan/Mateshape 相机参数XML解读

实际的光心位置为图像 0.5×width / 0.5×height + cx / cy
实际相机旋转平移矩阵为将 transform 中的十六个元素组成4×4矩阵之后取逆