[open3d]点云投影2D图像

输入:三维点云

输出:2D DSM

import open3d as o3d
import numpy as np
import cv2

#读取点云,转换为栅格图像
#将栅格图像分块
resolution = 0.2
save_path = r"dsm.png"
print("->正在加载点云... ")
m_pc = o3d.io.read_point_cloud("WHU.ply")
print(m_pc)
print('体素化')
points = np.asarray(m_pc.points)  # 将点云数据转化为 numpy 数组
print("数组形状:", points.shape)  # 输出数组的形状(行列数)
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]

# 获取栅格范围
xmin, xmax = np.nanmin(x), np.nanmax(x)
ymin, ymax = np.nanmin(y), np.nanmax(y)

# 创建栅格, dtype=np.uint8
arr_width = int((xmax - xmin) / resolution)
arr_height = int((ymax - ymin) / resolution)
img = np.zeros((arr_height, arr_width))
# xyz转行列号
for i in range(0, x.shape[0]):
    col = int((x[i] - xmin) / resolution)
    row = int((y[i] - ymin) / resolution)
    if (col < 0 or col >= arr_width):
        continue
    if (row < 0 or row >= arr_height - 1):
        continue
    img[row][col] = 255#注意此处转换为二值图
cv2.imwrite(save_path, img)
cv2.namedWindow("img", 0)
cv2.resizeWindow("img", 400, 300)  # 设置窗口大小
cv2.imshow("img", img)
cv2.waitKey(0)

 

posted @ 2024-09-05 10:10  太一吾鱼水  阅读(142)  评论(0编辑  收藏  举报