How to visualize colmap export that contains Intrinsic and Extrinsic Camera Parameters using python

491 views Asked by At

I have a transforms.json file which contains Intrinsic and Extrinsic for 23 cameras. I would like to visualize those using Python, colmap, pycolmap and jupyternotebook. Below is a part of the .json file

{
  "w": 1920,
  "h": 1080,
  "fl_x": 1098.8550003271516,
  "fl_y": 1110.2997543513977,
  "cx": 970.1319034923014,
  "cy": 542.0541746563172,
  "k1": -0.28118870977442023,
  "k2": 0.06674186867742171,
  "p1": 0.0026768267765996103,
  "p2": -0.00237229158478273,
  "camera_model": "OPENCV",
  "frames": [
    {
      "file_path": "images/frame_00023.jpg",
      "transform_matrix": [
        [
          -0.07042611592680023,
          -0.9713950978549236,
          0.22678563900496068,
          2.1881674886247935
        ],
        [
          0.9325864609677816,
          0.016566699247072256,
          0.3605662730978109,
          1.7471888187630829
        ],
        [
          -0.3540093996139834,
          0.2368904986264339,
          0.9047431882282766,
          -0.21938707719027645
        ],
        [
          0.0,
          0.0,
          0.0,
          1.0
        ]
      ],
1

There are 1 answers

0
Code On

assuming you have pycolmap installed.

In your Jupyter Notebook, you can use the following code to load the transforms from the JSON file and visualize the camera poses

import json
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Line3DCollection

# Load transforms from JSON file
with open('transforms.json', 'r') as f:
    data = json.load(f)

# Extract camera poses
camera_poses = [frame['transform_matrix'] for frame in data['frames']]

# Visualize camera poses
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Extract camera positions
camera_positions = [pose[:3, 3] for pose in camera_poses]

# Plot camera positions
ax.scatter(*zip(*camera_positions), c='r', marker='o', label='Camera Poses')

# Connect camera positions with lines
lines = []
for i in range(len(camera_positions) - 1):
    lines.append([camera_positions[i], camera_positions[i + 1]])

# Create a line collection
lc = Line3DCollection(lines, colors='b', linewidths=1, label='Camera Trajectory')
ax.add_collection3d(lc)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Camera Poses')
ax.legend()

plt.show()

Make sure to replace 'transforms.json' with the actual path to your JSON file.

This code uses Matplotlib to create a 3D plot showing the camera positions and their trajectory. The camera positions are represented by red spheres, and the trajectory is represented by blue lines connecting consecutive camera positions.

Note: Make sure that your Jupyter Notebook environment has access to the required packages, and you may need to adjust the code based on your specific JSON file structure.

here some resources: PyColmap Documentation:

PyColmap GitHub Repository: https://github.com/mihaidusmanu/pycolmap

PyColmap Documentation: https://mihaidusmanu.github.io/pycolmap/

Matplotlib Documentation:

Matplotlib Official Documentation:

https://matplotlib.org/stable/contents.html

Computer Vision and Structure from Motion:

OpenCV Tutorials: https://docs.opencv.org/4.x/contents.html

Multiple View Geometry in Computer Vision (Book by Richard Hartley and Andrew Zisserman): http://www.robots.ox.ac.uk/~vgg/hzbook/

3D Visualization with Matplotlib:

Matplotlib 3D Tutorial: https://matplotlib.org/stable/gallery/mplot3d/index.html