metashape-pro python scripts render specified viewpoint

官方python脚本使用文档

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

在这里插入图片描述

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

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

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

# 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矩阵之后取逆

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值