我有一个transforms.json 文件,其中包含23 个相机的Intrinsic 和Extrinsic。我想使用 Python、colmap、pycolmap 和 jupyternotebook 来可视化那些。以下是 .json 文件的一部分
{
"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
]
],
假设您已安装
pycolmap
。
在 Jupyter Notebook 中,您可以使用以下代码从 JSON 文件加载转换并可视化相机姿势
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()
确保将“transforms.json”替换为 JSON 文件的实际路径。
此代码使用 Matplotlib 创建显示相机位置及其轨迹的 3D 绘图。相机位置由红色球体表示,轨迹由连接连续相机位置的蓝色线表示。
注意:请确保您的 Jupyter Notebook 环境能够访问所需的包,并且您可能需要根据您的具体 JSON 文件结构调整代码。
这里有一些资源: PyColmap 文档:
PyColmap GitHub 存储库:https://github.com/mihaidusmanu/pycolmap
PyColmap 文档:https://mihaidusmanu.github.io/pycolmap/
Matplotlib 文档:
Matplotlib 官方文档:
https://matplotlib.org/stable/contents.html
来自运动的计算机视觉和结构:
OpenCV 教程:https://docs.opencv.org/4.x/contents.html
计算机视觉中的多视图几何(Richard Hartley 和 Andrew Zisserman 所著书籍):http://www.robots.ox.ac.uk/~vgg/hzbook/
使用 Matplotlib 进行 3D 可视化:
Matplotlib 3D 教程:https://matplotlib.org/stable/gallery/mplot3d/index.html