本文主要是介绍open3d墙面提取代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
门窗洞口提取需拍平墙,根据X、Y、Z轴的axsi()值设定,拍平后可以进行下一步处理,方便点云开发者下一步操作。
附上代码
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
from alpha_shapes import Alpha_Shaper, plot_alpha_shapedef extract_walls_pcd(pcd, axis='z', threshold=2e+1000):# 提取墙面的点云points = np.asarray(pcd.points)if axis == 'x':axis_index = 0elif axis == 'y':axis_index = 1elif axis == 'z':axis_index = 2else:raise ValueError("Invalid axis. Must be 'x', 'y', or 'z'.")wall_points = points[abs(points[:, axis_index]) < threshold]wall_pcd = o3d.geometry.PointCloud()wall_pcd.points = o3d.utility.Vector3dVector(wall_points)return wall_pcddef plane_fit(pcd):# 使用RANSAC平面拟合plane_model, inliers = pcd.segment_plane(distance_threshold=0.5, ransac_n=5, num_iterations=1000)return plane_model, inliersdef plane_projection(pcd, plane_model, inliers):# 将点云投影到拟合的平面上plane_pcd = pcd.select_by_index(inliers)projected_points = np.asarray(plane_pcd.points)plane_params = plane_model[:-1]d = plane_model[-1]array_1 = projected_points.dot(plane_params[:, np.newaxis]) + darray_1_reshaped = np.tile(array_1, (1, 3))# projected_points -= projected_points.dot(plane_params[:, np.newaxis]) + dprojected_points -= array_1_reshapedplane_pcd.points = o3d.utility.Vector3dVector(projected_points)return plane_pcdif __name__ == "__main__":# 读取点云文件pcd = o3d.io.read_point_cloud(r"C:\Users\zhaojunzhe\Desktop\pyRANSAC-3D-master\pyRANSAC-3D-master\tests\dataset\20230713.pcd")# 提取墙面点云并拍平wall_pcd_x = extract_walls_pcd(pcd, axis='x')flat_wall_pcd_x = plane_projection(wall_pcd_x, *plane_fit(wall_pcd_x))wall_pcd_y = extract_walls_pcd(pcd, axis='y')flat_wall_pcd_y = plane_projection(wall_pcd_y, *plane_fit(wall_pcd_y))wall_pcd_z = extract_walls_pcd(pcd, axis='z')flat_wall_pcd_z = plane_projection(wall_pcd_z, *plane_fit(wall_pcd_z))# 保存平面点云为独立的文件o3d.io.write_point_cloud("flat_wall_pcd_x.pcd", flat_wall_pcd_x)o3d.io.write_point_cloud("flat_wall_pcd_y.pcd", flat_wall_pcd_y)o3d.io.write_point_cloud("flat_wall_pcd_z.pcd", flat_wall_pcd_z)# 创建一个可视化窗口vis = o3d.visualization.Visualizer()vis.create_window()# 将处理后的点云添加到可视化窗口中# vis.add_geometry(flat_wall_pcd_x)# vis.add_geometry(flat_wall_pcd_y)vis.add_geometry(flat_wall_pcd_z)# 显示点云vis.run()vis.destroy_window()
这篇关于open3d墙面提取代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!