当前位置:网站首页>Open3d官网代码学习
Open3d官网代码学习
2022-07-21 05:04:00 【Amelie_11】
DBSCAN clustering报错
源代码:
Given a point cloud from e.g. a depth sensor we want to group local point cloud clusters together. For this purpose, we can use clustering algorithms. Open3D implements DBSCAN [Ester1996] that is a density based clustering algorithm. The algorithm is implemented in cluster_dbscan and requires two parameters: eps defines the distance to neighbors in a cluster and min_points defines the minimum number of points required to form a cluster. The function returns labels, where the label -1 indicates noise.
ply_point_cloud = o3d.data.PLYPointCloud()
pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
with o3d.utility.VerbosityContextManager(
o3d.utility.VerbosityLevel.Debug) as cm:
labels = np.array(
pcd.cluster_dbscan(eps=0.02, min_points=10, print_progress=True))
max_label = labels.max()
print(f"point cloud has {
max_label + 1} clusters")
colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))
colors[labels < 0] = 0
pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
o3d.visualization.draw_geometries([pcd],
zoom=0.455,
front=[-0.4999, -0.1659, -0.8499],
lookat=[2.1813, 2.0619, 2.0999],
up=[0.1204, -0.9852, 0.1215])
报错:
module ‘matplotlib’ has no attribute ‘get_cmap’
解决:colors = plt.cm.get_cmap(“tab20”)(labels / (max_label if max_label > 0 else 1))
官网链接:http://www.open3d.org/docs/release/python_api/open3d.visualization.html
学习代码如下:
import open3d as o3d
import numpy as np
import matplotlib as plt
# print("Load a ply point cloud, print it, and render it")
ply_point_cloud = o3d.data.PLYPointCloud()
pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
pcd = o3d.io.read_point_cloud("wall_001.pcd")
print(pcd)
print(np.asarray(pcd.points))
# o3d.visualization.draw_geometries([pcd],
# zoom=0.3412,
# front=[0.4257, -0.2125, -0.8795],
# lookat=[2.6172, 2.0475, 1.532],
# up=[-0.0694, -0.9768, 0.2024])
#
'''下采样'''
downpcd = pcd.voxel_down_sample(voxel_size=0.05)
o3d.visualization.draw_geometries([downpcd],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024])
'''法线估计'''
downpcd.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
o3d.visualization.draw_geometries([downpcd],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024],
point_show_normal=True)
'''访问估计的顶点法线'''
print(downpcd.normals[0]) # 打印第0个点
print(np.asarray(downpcd.normals)[:10, :]) # 将法线向量转换为 numpy 数组
'''裁剪点云'''
demo_crop_data = o3d.data.DemoCropPointCloud()
pcd = o3d.io.read_point_cloud(demo_crop_data.point_cloud_path)
vol = o3d.visualization.read_selection_polygon_volume(demo_crop_data.cropped_json_path)
chair = vol.crop_point_cloud(pcd)
o3d.visualization.draw_geometries([chair],
zoom=0.7,
front=[0.5439, -0.2333, -0.8060],
lookat=[2.4615, 2.1331, 1.338],
up=[-0.1781, -0.9708, 0.1608])
'''将所有点绘制成统一颜色'''
chair.paint_uniform_color([1, 0.706, 0])
o3d.visualization.draw_geometries([chair],
zoom=0.7,
front=[0.5439, -0.2333, -0.8060],
lookat=[2.4615, 2.1331, 1.338],
up=[-0.1781, -0.9708, 0.1608])
'''计算从源点云到目标点云最近点的距离'''
# 加载点云
demo_crop_data = o3d.data.DemoCropPointCloud()
pcd = o3d.io.read_point_cloud(demo_crop_data.point_cloud_path)
vol = o3d.visualization.read_selection_polygon_volume(demo_crop_data.cropped_json_path)
chair = vol.crop_point_cloud(pcd)
# 计算距离
dists = pcd.compute_point_cloud_distance(chair)
dists = np.asarray(dists)
ind = np.where(dists > 0.01)[0]
pcd_without_chair = pcd.select_by_index(ind)
o3d.visualization.draw_geometries([pcd_without_chair],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024])
'''计算边界体积'''
aabb = chair.get_axis_aligned_bounding_box()
aabb.color = (1, 0, 0)
obb = chair.get_oriented_bounding_box()
obb.color = (0, 1, 0)
o3d.visualization.draw_geometries([chair, aabb, obb],
zoom=0.7,
front=[0.5439, -0.2333, -0.8060],
lookat=[2.4615, 2.1331, 1.338],
up=[-0.1781, -0.9708, 0.1608])
'''DBSCAN聚类'''
pcd = o3d.io.read_point_cloud("wall_001.pcd")
with o3d.utility.VerbosityContextManager(
o3d.utility.VerbosityLevel.Debug) as cm:
labels = np.array(
pcd.cluster_dbscan(eps=0.02, min_points=10, print_progress=True))
# eps定义到集群中邻居的距离和min_points定义形成集群所需的最小点数
max_label = labels.max()
print(f"point cloud has {
max_label + 1} clusters")
colors = plt.cm.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))
colors[labels < 0] = 0
pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
o3d.visualization.draw_geometries([pcd],
zoom=0.455,
front=[-0.4999, -0.1659, -0.8499],
lookat=[2.1813, 2.0619, 2.0999],
up=[0.1204, -0.9852, 0.1215])
'''平面分割'''
pcd = o3d.io.read_point_cloud("wall_001.pcd")
plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
ransac_n=3,
num_iterations=1000)
[a, b, c, d] = plane_model
print(f"Plane equation: {
a:.2f}x + {
b:.2f}y + {
c:.2f}z + {
d:.2f} = 0")
# distance_threshold定义一个点到估计平面的最大距离,以被认为是一个ransac_n内点,定义随机采样以估计一个平面的点的数量,以及num_iterations定义一个随机平面被采样和验证的频率。
inlier_cloud = pcd.select_by_index(inliers)
inlier_cloud.paint_uniform_color([1.0, 0, 0])
outlier_cloud = pcd.select_by_index(inliers, invert=True)
o3d.visualization.draw_geometries([inlier_cloud, outlier_cloud],
zoom=0.8,
front=[-0.4999, -0.1659, -0.8499],
lookat=[2.1813, 2.0619, 2.0999],
up=[0.1204, -0.9852, 0.1215])
边栏推荐
- 初学谷歌bert模型的预训练和fine-tuning微调
- 1024 科学计数法
- 跑TDD-net遇到的一些坑
- Mmdetection environment matching (cuda10.1+mmdet2.24)
- 【PCB】电路板绘制笔记之生产文件整理及输出--画板笔记
- [record] the operation of optisystem is stuck, and it is unable to click to close, input variable values and other solutions
- bert-serving
- pycharm专业版创建flask项目|下载flask包|以及一些例子
- learning opencv3_2-11_写入AVI文件
- Semantic segmentation, instance segmentation
猜你喜欢
tensorflow-GPU环境配置
语义分割、实例分割
(environment configuration) TDD net
【PCB】Altium Designer 中(Via过孔)(文本string)(走线Linear Dimension)等项目修改默认设置
深度剖析 —— 结构体
Acwing 175电路维修
Hetai 32 onenet WiFi module - Hetai MCU data cloud through mqtt protocol (I)
Hetai ht32 & taojingchi tjc--t0 serial port screen learning notes
小游戏类项目 —— 扫雷
4. 10 lines of code MNIST handwritten numeral recognition of paddlepaddle
随机推荐
深度剖析 string —— strcmp & strncmp
APISIX微服务网关
Amy-Tabb机器人世界手眼标定(1、环境搭配)
Cat and dog picture resources
深度剖析 string —— strcpy & strncpy
语义分割、实例分割
printf 详解 - 你所不知道的printf用法
Codeforces 1642B Power Walking
Stm32f407-ov7670 (no FIFO) -onenet- upload camera pictures to onenet (EDP protocol)
深度剖析 string —— memset & memcmp
ESP8266固件下载及烧录(收录AT固件下载地址+固件烧录注意事项)
[C language] some commonly used string functions -- learning notes
(好数对)一个数组的值赋给另一个数组
learning opencv3_2-11_写入AVI文件
Amy-Tabb机器人世界手眼标定(2、实验结果)
MMdetection环境搭配(cuda10.1+mmdet2.24)
深度剖析 —— 递归
1. Learn the environment installation and basic calculation of paddlepaddle from scratch
1020月饼
Initializing libiomp5. dylib, but found libomp. dylib already initialized