Skip to content

Camera position is not updated after model transform #123

@zwl995

Description

@zwl995

My goal was to move the coordinate system to the center of the scene after aligning the arkit, but I found that my camera's center didn't change after aligning, and then I exported the colmap format and found that the model did have conversions. Here is my processing code:

import time
import torch
import pycolmap
import numpy as np
import Metashape
from pathlib import Path

def create_project(output_folder):
    """创建或加载项目(修改为支持加载已有项目)"""
    project_path = Path(output_folder) / "project.psx"
    doc = Metashape.Document()
    if project_path.exists():
        # 加载已有项目(关键:支持增量执行步骤)
        doc.open(str(project_path))
        # 使用第一个chunk(若不存在则新建)
        chunk = doc.chunks[0] if doc.chunks else doc.addChunk()
    else:
        # 创建新项目
        doc.save(str(project_path))
        chunk = doc.addChunk()
    return doc, chunk

def align_scene_center(chunk, doc):
    chunk.crs = Metashape.CoordinateSystem("LOCAL")
    p3ds = []
    for cam in chunk.cameras:
        if cam.center:
            p3ds.append(list(cam.center))
    p3ds = torch.from_numpy(np.array(p3ds).astype(np.float32))
    mean_p3d = torch.mean(p3ds, dim=0)
    print(f"mean_p3d: {mean_p3d}")
    transform2center = Metashape.Matrix.Translation(-mean_p3d.numpy())
    print(f"mean_p3d: {mean_p3d}")
    print(f"transform2center: {transform2center}")
    chunk.transform.matrix = transform2center * chunk.transform.matrix
    # chunk.transform.matrix =  chunk.transform.matrix * transform2center
    chunk.updateTransform()
    return chunk, doc
    
def align_arkit(chunk, doc):
    """对齐ARKIT数据"""
    start = time.time()
    chunk.crs = Metashape.CoordinateSystem("LOCAL")
    for cam in chunk.cameras:
        cam.reference.location = None
        cam.reference.rotation = None
    csv_file = f"/ws/zwl/Data/MetaMaker1160/test_output1/test0904_zhanting/arkit_keyfrm/keyframes_data.csv"
    chunk.importReference(csv_file, columns="nxyz", delimiter=",")
    chunk.updateTransform()
    end = time.time()
    print(f"对齐ARKIT数据耗时: {end - start:.2f} 秒")
    return chunk, doc

if __name__ == "__main__":
    # 创建或加载项目
    output_path = f"/ws/zwl/Data/MetaMaker1160/test_output1/test0904_zhanting/metashape"
    result_folder = f"/ws/zwl/Code/Metashape/Metashape/data"
    doc, chunk = create_project(output_folder=output_path)
    before_align = f"/ws/zwl/Code/Metashape/Metashape/data/before_align/sparse.txt"
    chunk.exportCameras(path=before_align,
                            format=Metashape.CamerasFormatColmap,
                            convert_to_pinhole=True,
                            binary=True,
                            save_images=False,
                            )
    i = 0
    for cam in chunk.cameras:
        if cam.center and i<10:
            print(cam.center)
            i += 1
    chunk, doc = align_arkit(chunk, doc)
    # chunk, doc = align_scene_center(chunk, doc)
    print(f"=============================================================================")
    j = 0
    for cam in chunk.cameras:
        if cam.center and j<10:
            print(cam.center)
            j += 1

    after_align = f"/ws/zwl/Code/Metashape/Metashape/data/after_align_move/align_move.txt"
    chunk.exportCameras(path=after_align,
                        format=Metashape.CamerasFormatColmap,
                        convert_to_pinhole=True,
                        binary=True,
                        save_images=False,
                        )

result is:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions