体元法--体积计算

文章目录

    • 环境:
    • 1.1 体元法介绍:
    • 2.1 python代码
    • 3.1 可视化

环境:

Open3D

1.1 体元法介绍:

用一个个体素去占据点云,然后对所有体素求和

2.1 python代码

conda activete deeplabv3plus(环境名称–安装好open3D的)
python demo.py

import open3d as o3d
import numpy as np

def draw_point_cloud(result, batch=False):
    if not batch:
        for i in range(len(result)):
            o3d.visualization.draw_geometries([result[i]], "result", 800, 600,
                                               50, 50, False, False, True)
    else:
        o3d.visualization.draw_geometries(result, "result", 800, 600,
                                          50, 50, False, True, True)

def main():
    # 读取点云数据
    pc = o3d.io.read_point_cloud("../../TestData/Pr1.pcd")

    # 体元累加法
    d = 0.1  # 体元的大小
    min_bound = pc.get_min_bound()
    max_bound = pc.get_max_bound()
    x_min, x_max = min_bound[0], max_bound[0]
    y_min, y_max = min_bound[1], max_bound[1]
    z_min, z_max = min_bound[2], max_bound[2]

    # 创建体元
    print("开始计算...")
    row = int(np.ceil((x_max - x_min) / d) + 1)
    col = int(np.ceil((y_max - y_min) / d) + 1)
    height = int(np.ceil((z_max - z_min) / d) + 1)
    flags = np.zeros((row, col, height), dtype=bool)

    for i in range(len(pc.points)):
        x, y, z = pc.points[i]
        m = int(np.floor((x - x_min) / d))
        n = int(np.floor((y - y_min) / d))
        k = int(np.floor((z - z_min) / d))
        flags[m, n, k] = True

    count = np.sum(flags)
    print("计算完成!")
    v = d * d * d * count
    print("当前有效体元为:", count)
    print("树冠体积为:", v)

    result = [pc]
    draw_point_cloud(result, False)
    input("按任意键继续...")

if __name__ == "__main__":
    main()

3.1 可视化

体元法--体积计算_第1张图片


一箭三连
⭐⭐ 祝你成功 ⭐⭐

你可能感兴趣的:(python,开发语言)