OPEN3D学习笔记(一)——File IO & Point Cloud_open3d c++ 绘制 lineset-程序员宅基地

技术标签: open3d  

安装

这个在linux下,按照官方教程给的提示,没出问题。其中换了清华镜像源可以快一点。

File IO

Pointcloud

print("Testing IO for point cloud ...")
pcd = o3d.io.read_point_cloud("../../TestData/fragment.pcd")
print(pcd)  # 可以打印这个点云的点数
o3d.io.write_point_cloud("copy_of_fragment.pcd", pcd)
# 输出如下
#Testing IO for point cloud ...
#geometry::PointCloud with 113662 points.

点云(PointCloud的一些格式):
xyz:每行包含xyz的3个坐标值
xyzn:每行都是x, y, z, xn, yn, zn.坐标值和法线值6个值
xyzrgb:每行也是6个值,其中rgb都是[0, 1]的范围内
pts:第一行是代表点数的整数。随后的每一行都包含[x,y,z,i,r,g,b],其中r,g,b是uint8
ply:
pcd:

# 这里明确了文件的类型,那么文件的后缀则被忽略
pcd = o3d.io.read_point_cloud("../../TestData/my_points.txt", format='xyz')

Mesh

print("Testing IO for meshes ...")
mesh = o3d.io.read_triangle_mesh("../../TestData/knot.ply")
print(mesh)  # 打印点数和三角面数
o3d.io.write_triangle_mesh("copy_of_knot.ply", mesh)
# 输出
#Testing IO for meshes ...
#geometry::TriangleMesh with 1440 points and 2880 triangles.

一些格式:
ply,stl,obj,off,gltf

Image

print("Testing IO for images ...")
img = o3d.io.read_image("../../TestData/lena_color.jpg")
print(img)  # 打印图片的大小和通道数
o3d.io.write_image("copy_of_lena_color.jpg", img)
# 输出
#Testing IO for images ...
#Image of size 512x512, with 3 channels.
#Use numpy.asarray to access buffer data.

PointCloud

可视化点云

print("Load a ply point cloud, print it, and render it")
pcd = o3d.io.read_point_cloud("../../TestData/fragment.ply")
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])
#我实现的时候报错,是版本不对吗?我的库函数是:def draw_geometries(geometry_list, window_name='Open3D', width=1920, height=1080, left=50, top=50, point_show_normal=False, mesh_show_wireframe=False, mesh_show_back_face=False)                                  
'''
Load a ply point cloud, print it, and render it
geometry::PointCloud with 196133 points.
[[0.65234375 0.84686458 2.37890625]
 [0.65234375 0.83984375 2.38430572]
 [0.66737998 0.83984375 2.37890625]
 ...
 [2.00839925 2.39453125 1.88671875]
 [2.00390625 2.39488506 1.88671875]
 [2.00390625 2.39453125 1.88793314]]
 '''

read_point_cloud从文件读取点云。它尝试根据扩展名对文件进行解码。支持的扩展名是:pcd,ply,xyz,xyzrgb,xyzn,pts。
draw_geometries可视化点云。使用鼠标/触控板从不同的角度查看几何。按h键以打印出该GUI的完整键盘说明列表。

体素降采样

该算法的两个步骤:
1.点被存储到体素中。
2.每个占用的体素通过平均内部的所有点来生成精确的一个点。

print("Downsample the point cloud with a voxel of 0.05")
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])  # 可视化

顶点法线估计

可视化的时候,按n查看法线。键-和键+可用于控制法线的长度。

print("Recompute the normal of the downsampled point cloud")
downpcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))  // 设定估计法线的参数,搜索方法是kdtree(半径0.1米,最大邻居数量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)

estimate_normals计算每个点的法线。该函数查找相邻点并使用协方差分析计算相邻点的主轴线(法线)。
法线有两个方向,open3d尝试与现有的方向对齐,也可以调用orient_normals_to_align_with_direction 和orient_normals_towards_camera_location。

访问估计的顶点法线

print("Print a normal vector of the 0th point")
print(downpcd.normals[0])  # 调用这个变量的属性
'''
输出第一个法向量的值
Print a normal vector of the 0th point
[-0.21838377 -0.94240442 -0.25334252]
'''


print("Print the normal vectors of the first 10 points")
print(np.asarray(downpcd.normals)[:10, :])  # 转换成numpy
'''
Print the normal vectors of the first 10 points
[[-0.21838377 -0.94240442 -0.25334252]
 [-0.39147152 -0.43746664 -0.8095511 ]
 [-0.00694405 -0.99478075 -0.10179902]
 [-0.00399871 -0.99965423 -0.02598917]
 [-0.93768261 -0.07378998  0.3395679 ]
 [-0.43476205 -0.62438493 -0.64894177]
 [-0.09739809 -0.9928602  -0.06886388]
 [-0.11728718 -0.95516445 -0.27185399]
 [-0.01038945 -0.99968858 -0.02268921]
 [-0.00816546 -0.99965616 -0.02491762]]
'''

修剪点云

print("Load a polygon volume and use it to crop the original point cloud")
vol = o3d.visualization.read_selection_polygon_volume("../../TestData/Crop/cropped.json")
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])

read_selection_polygon_volume读取一个指定多边形选择区域的json文件。根据这个指定区域,裁剪点云
vol.crop_point_cloud(pcd)过滤出点。只剩下椅子了。

涂颜色

print("Paint chair")
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])

paint_uniform_color将所有点绘制为统一的颜色。颜色在[0,1]范围的RGB空间中。

边界体积

Open3D实现了AxisAlignedBoundingBox和OrientedBoundingBox,它们也可以用于裁剪几何。

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])

边界体积

凸包

点云的凸包是包含所有点的最小凸集。Open3D包含方法compute_convex_hull,该方法计算例如点云的凸包。该实现基于Qhull
在下面的示例代码中,我们首先从网格中采样点云,然后计算作为三角形网格返回的凸包。然后,我们将凸包可视化为红色LineSet。

pcl = o3dtut.get_bunny_mesh().sample_points_poisson_disk(number_of_points=2000)
hull, _ = pcl.compute_convex_hull()
hull_ls = o3d.geometry.LineSet.create_from_triangle_mesh(hull)
hull_ls.paint_uniform_color((1, 0, 0))
o3d.visualization.draw_geometries([pcl, hull_ls])

基于密度的聚类算法DBSCAN

例如,给定一个来自深度传感器的点云,我们希望将局部点云分组在一起。
函数cluster_dbscan,eps定义到群集中邻居的距离,min_points定义形成群集所需的最小点数。该函数返回labels,其中labels=-1表示噪音。

pcd = o3d.io.read_point_cloud("../../TestData/fragment.ply")

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])
# 输出point cloud has 10 clusters
# label=0则为第0类。。。

平面分割

函数segement_plane,有3个参数:
distance_threshold定义了一个点到一个估计平面的最大距离,在最大距离内被视为一个内点,
ransac_n定义随机采样以估计平面的点数,
num_iterations定义对随机平面进行采样和验证的频率。
该函数返回(a,b,c,d)一个平面的参数(ax+by+cz+d=0)
该函数进一步返回一个内点索引列表


pcd = o3d.io.read_point_cloud("../../TestData/fragment.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")

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])
#输出 
#Plane equation: -0.06x + -0.10y + 0.99z + -1.06 = 0

隐藏点删除(不懂)

print("Convert mesh to a point cloud and estimate dimensions")
pcd = o3dtut.get_armadillo_mesh().sample_points_poisson_disk(5000)
diameter = np.linalg.norm(np.asarray(pcd.get_max_bound()) - np.asarray(pcd.get_min_bound()))
o3d.visualization.draw_geometries([pcd])

print("Define parameters used for hidden_point_removal")
camera = [0, 0, diameter]
radius = diameter * 100

print("Get all points that are visible from given view point")
_, pt_map = pcd.hidden_point_removal(camera, radius)

print("Visualize result")
pcd = pcd.select_by_index(pt_map)
o3d.visualization.draw_geometries([pcd])
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_36219957/article/details/106318406

智能推荐

c# 调用c++ lib静态库_c#调用lib-程序员宅基地

文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib

deepin/ubuntu安装苹方字体-程序员宅基地

文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang

html表单常见操作汇总_html表单的处理程序有那些-程序员宅基地

文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些

PHP设置谷歌验证器(Google Authenticator)实现操作二步验证_php otp 验证器-程序员宅基地

文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器

【Python】matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距-程序员宅基地

文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距

docker — 容器存储_docker 保存容器-程序员宅基地

文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器

随便推点

网络拓扑结构_网络拓扑csdn-程序员宅基地

文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn

JS重写Date函数,兼容IOS系统_date.prototype 将所有 ios-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios

如何将EXCEL表导入plsql数据库中-程序员宅基地

文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql

Git常用命令速查手册-程序员宅基地

文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...

分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120-程序员宅基地

文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120

【C++缺省函数】 空类默认产生的6个类成员函数_空类默认产生哪些类成员函数-程序员宅基地

文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数

推荐文章

热门文章

相关标签