dfki-ric / pytransform3d

3D transformations for Python.
https://dfki-ric.github.io/pytransform3d/
Other
619 stars 65 forks source link

Visualizing camera trajectory in Open3D #147

Closed maheshkkumar closed 3 years ago

maheshkkumar commented 3 years ago

Hi @AlexanderFabisch,

I am trying to use pytransform3d for some visualizations. So, I was wondering if there is a particular example/tutorial in the documentation related to this image in README for visualizing the camera trajectory in Open3D.

Thank you!

AlexanderFabisch commented 3 years ago

Hi @maheshkkumar,

there is no magic. Figure.plot_camera is all you need to read. If the explanation is not enough, let me know how to improve it.

The specific example that you refer to has been generated from the output of the software meshroom with this code:


import sys
import json
import numpy as np
import pytransform3d.transformations as pt
import pytransform3d.camera as pc
import pytransform3d.visualizer as pv

mesh_filename = sys.argv[-2]
camera_filename = sys.argv[-1]

with open(camera_filename, "r") as f:
    cameras = json.load(f)

camera_poses = cameras["poses"]
camera_intrinsics = cameras["intrinsics"][0]

px_focal_length = float(camera_intrinsics["pxFocalLength"])
px_principal_point_x = float(camera_intrinsics["principalPoint"][0])
px_principal_point_y = float(camera_intrinsics["principalPoint"][1])
M = np.array([
    [px_focal_length, 0, px_principal_point_x],
    [0, px_focal_length, px_principal_point_y],
    [0, 0, 1]
])
sensor_size = (float(camera_intrinsics["width"]), float(camera_intrinsics["height"]))

transformation_matrices = np.empty((len(camera_poses), 4, 4))
for i, camera_pose in enumerate(camera_poses):
    R = np.array(list(map(float, camera_pose["pose"]["transform"]["rotation"]))).reshape(3, 3)
    p = np.array(list(map(float, camera_pose["pose"]["transform"]["center"])))
    transformation_matrices[i] = pt.transform_from(R=R, p=p)

fig = pv.figure()
fig.plot_mesh(mesh_filename)
for pose in transformation_matrices:
    fig.plot_transform(A2B=pose, s=0.1)
    fig.plot_camera(M=M, cam2world=pose, virtual_image_distance=0.1, sensor_size=sensor_size)
fig.show()