首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[python][pycolmap]基于PyCOLMAP从图片中的稀疏点云重建与可视化

[python][pycolmap]基于PyCOLMAP从图片中的稀疏点云重建与可视化

作者头像
云未归来
发布2025-07-22 15:13:15
发布2025-07-22 15:13:15
18700
代码可运行
举报
运行总次数:0
代码可运行

测试环境:

pycolmap==3.11.1

测试代码:

代码语言:javascript
代码运行次数:0
运行
复制
import os
import numpy as np
import pycolmap
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


def reconstruct_and_visualize(image_dir, output_dir):
    # 确保输出目录存在
    os.makedirs(output_dir, exist_ok=True)

    # 1. 设置数据库路径
    database_path = os.path.join(output_dir, "database.db")
    if os.path.exists(database_path):
        os.remove(database_path)

    # 2. 运行特征提取
    pycolmap.extract_features(database_path, image_dir)

    # 3. 运行特征匹配
    pycolmap.match_exhaustive(database_path)

    # 4. 运行增量式重建
    maps = pycolmap.incremental_mapping(
        database_path=database_path,
        image_path=image_dir,
        output_path=output_dir,
    )

    # 5. 检查重建结果
    if not maps:
        print("重建失败!")
        return None

    # 获取第一个重建结果
    reconstruction = maps[0]

    # 6. 检查是否有3D点
    if not reconstruction.points3D:
        print("重建成功,但没有生成3D点云。")
        return reconstruction

    # 7. 提取点云数据
    points3D = reconstruction.points3D
    xyz = np.array([p.xyz for p in points3D.values()])
    rgb = np.array([p.color for p in points3D.values()]) / 255.0

    # 8. 可视化点云
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection="3d")

    ax.scatter(
        xyz[:, 0], xyz[:, 1], xyz[:, 2],
        c=rgb,
        s=1,
        alpha=0.8,
        marker="."
    )

    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.set_zlabel("Z")
    ax.set_title("3D Reconstruction Point Cloud")

    plt.tight_layout()
    plt.show()

    return reconstruction


# 使用示例
image_dir = r"D:\images"
output_dir = r"D:\output"

reconstruction = reconstruct_and_visualize(image_dir, output_dir)

结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档