Open3D點(diǎn)云處理
一、Open3D
Open3D is an open-source library that supports rapid development of software that deals with 3D data. The Open3D frontend exposes a set of carefully selected data structures and algorithms in both C++ and Python. The backend is highly optimized and is set up for parallelization.
Open3D是一個(gè)支持3D數(shù)據(jù)處理軟件快速開發(fā)的開源庫,在前端提供了一組精挑細(xì)選的C++和Python數(shù)據(jù)結(jié)構(gòu)與算法。并且在后端高度優(yōu)化且支持并行化。
其核心要素包括:
- 3D數(shù)據(jù)結(jié)構(gòu)
- 3D數(shù)據(jù)處理算法
- 場景重建
- 3D可視化
- 3D機(jī)器學(xué)習(xí)等
Python版快速安裝
需要的環(huán)境為:
- OS:Ubuntu 18.04+、macOS 10.15+、Windows 10(64-bit)
- Python: 3.6-3.9
- Pre-packages:
pip
andconda
# Install
pip install open3d
# Verify installation
python -c "import open3d as o3d; print(o3d.__version__)"
# Python API
python -c "import open3d as o3d; \
mesh = o3d.geometry.TriangleMesh.create_sphere(); \
mesh.compute_vertex_normals(); \
o3d.visualization.draw(mesh, raw_mode=True)"
# Open3D CLI
open3d example visualization/draw
二、Open3D點(diǎn)云加載與顯示
2.1 點(diǎn)云讀取
Open3D提供了直接從文件中讀取點(diǎn)云數(shù)據(jù)的API:
open3d.io.read_point_cloud(filename, format='auto', remove_nan_points=False, \
remove_infinite_points=False, print_progress=False)
Parameters
- filename (str) – 文件路徑
-
format (str,optional,default=‘a(chǎn)uto’) – 文件的格式,默認(rèn)是
auto
,將影響如何讀取文件 -
remove_nan_points (bool*,* optional*,* default=False) – 是否移除值為
nan
的點(diǎn) -
remove_infinite_points (bool*,* optional*,* default=False) – 是否移除值為
inf
的點(diǎn) - print_progress (bool*,* optional*,* default=False) – 當(dāng)該值為True時(shí),將會(huì)在可視化時(shí)出現(xiàn)一個(gè)過程條
Return
- open3d.geometry.PointCloud對(duì)象
其中,format
參數(shù)的可選參數(shù)為:
格式 | 描述 |
---|---|
xyz | 每一行包含[x,y,z] |
xyzn | 每一行包含[x,y,z,nx,ny,nz] |
xyzrgb | 每一行包括[x,y,z,r,g,b] rgb為[0,1]之間的float類型 |
pts | 第一行表示點(diǎn)數(shù),之后每行包括[x,y,z,i,r,g,b] rgb為unit8類型 |
ply | ply文件 |
pcd | pcd文件 |
我們來嘗試讀取一下數(shù)據(jù)
import open3d as o3d
pcd=o3d.io.read_point_cloud(r"Cloud.pcd")
print(pcd)
'''
PointCloud with 2001009 points.
'''
# 此時(shí)點(diǎn)云數(shù)據(jù)已經(jīng)被讀入了
當(dāng)然,對(duì)于某些格式稀奇古怪的,我們也可以通過轉(zhuǎn)成ndarray
然后再進(jìn)行讀?。?/p>
import numpy as np
import open3d as o3d
# 讀取到ndarray
data=np.genfromtxt(r'modelnet40_normal_resampled\airplane\airplane_0001.txt',delimiter=",")
# 創(chuàng)建PointCloud類
pcd=o3d.geometry.PointCloud()
pcd.points=o3d.utility.Vector3dVector(data[:,:3])
print(pcd)
'''
PointCloud with 10000 points.
'''
關(guān)于PointCloud的屬性,主要有以下四類:
- colors: 顏色信息,在可視化時(shí)能為幾何體賦予視覺信息
- covariances: 協(xié)方差
- normal: 法向量
- points: 位置信息
2.2 點(diǎn)云可視化
在Open3D中,點(diǎn)云可視化其中之一的API為:
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,\
lookat, up, front, zoom)
Parameters
- geometry_list (List[open3d.geometry.Geometry]) – 需要可視化的幾何體列表.
- window_name (str, optional, default=‘Open3D’) – 窗口名稱
- width (int, optional, default=1920) – 窗口寬度
- height (int, optional, default=1080) – 窗口高度
- left (int, optional, default=50) – 窗口左邊界
- top (int, optional, default=50) – 窗口頂部邊界
- point_show_normal (bool, optional, default=False) – 是否展示法向量
- mesh_show_wireframe (bool, optional, default=False) – 是否可視化網(wǎng)格線框
- mesh_show_back_face (bool, optional, default=False) – 同時(shí)可視化格網(wǎng)三角形背部
- **lookat ** (numpy.ndarray[float64[3,1]]) – 相機(jī)注視向量
- up (numpy.ndarray[float64[3,1]]) – 相機(jī)的上方向向量
- front (numpy.ndarray[float64[3,1]]) – 相機(jī)的前矢量
- zoom (float) – 相機(jī)縮放倍數(shù)
Returns
- None
我們來嘗試一下:
o3d.visualization.draw_geometries([pcd])
顯示法向量:
pcd.normals=o3d.utility.Vector3dVector(data[:,3:])
o3d.visualization.draw_geometries([pcd],window_name="o3d",width=1920,height=1080,
left=50,top=50,point_show_normal=True)
看起來跟毛毛蟲一樣…
提供了一組用戶交互指令:
-- Mouse view control --
Left button + drag : Rotate.
Ctrl + left button + drag : Translate.
Wheel button + drag : Translate.
Shift + left button + drag : Roll.
Wheel : Zoom in/out.
-- Keyboard view control --
[/] : Increase/decrease field of view.
R : Reset view point.
Ctrl/Cmd + C : Copy current view status into the clipboard.
Ctrl/Cmd + V : Paste view status from clipboard.
-- General control --
Q, Esc : Exit window.
H : Print help message.
P, PrtScn : Take a screen capture.
D : Take a depth capture.
O : Take a capture of current rendering settings.
也可以指定點(diǎn)云的顏色:
pcd.colors=o3d.utility.Vector3dVector(data[:,3:])
參數(shù)geometry_list
支持多個(gè)空間集合對(duì)象:
def read_txt(path):
data=np.genfromtxt(path,delimiter=",")
pcd=o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(data[:, :3])
pcd.normals = o3d.utility.Vector3dVector(data[:, 3:])
pcd.colors = o3d.utility.Vector3dVector(data[:, 3:])
return pcd
path=r'\airplane'
pcd1=read_txt(path+r"\airplane_0001.txt")
pcd2=read_txt(path+r"\airplane_0012.txt")
o3d.visualization.draw_geometries([pcd1,pcd2],window_name="o3d",width=1920,height=1080,
left=50,top=50,mesh_show_back_face=True)
o3d提供了自動(dòng)計(jì)算法向量的API:
radius=0.01 # 搜索半徑
max_nn=30 # 鄰域內(nèi)用于估算法線的最大點(diǎn)數(shù)
# 執(zhí)行KD樹搜索
pcd1.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius,max_nn))
o3d.visualization.draw_geometries([pcd1],window_name="o3d",width=1920,height=1080,
left=50,top=50,point_show_normal=True)
# 同樣能用KD樹構(gòu)建協(xié)方差表
2.3 點(diǎn)云保存
API如下:
open3d.io.write_point_cloud(filename, pointcloud, write_ascii=False, compressed=False, print_progress=False)
Parameters
- filename (str) – 文件路徑
- pointcloud (open3d.geometry.PointCloud) – 點(diǎn)云對(duì)象
- write_ascii (bool,optional,default=False) – 該參數(shù)為True時(shí),將會(huì)寫入ASCII碼,否則一般寫入二進(jìn)制文件
- compressed (bool,optional,default=False) – 是否以壓縮格式進(jìn)行輸出
- print_progress (bool,optional,default=False) –是否在控制臺(tái)打印一個(gè)進(jìn)度條
Returns
- bool
o3d.io.write_point_cloud("02.pcd",pcd2,write_ascii=True)
此時(shí)可以看到已經(jīng)將讀取的點(diǎn)云寫入到文件中了。
三、Open3D點(diǎn)云常見操作
3.1 體素下采樣
體素下采樣(Voxel downsampling)采用規(guī)則體素格網(wǎng)從輸入點(diǎn)云中創(chuàng)建分布均勻的下采樣點(diǎn)云,是許多點(diǎn)云處理任務(wù)的預(yù)處理步驟。該算法主要分為兩步:
- 創(chuàng)建指定大小(分辨率)的體素網(wǎng)絡(luò)
- 當(dāng)點(diǎn)云中至少有一個(gè)點(diǎn)落在某個(gè)體素內(nèi),則認(rèn)為該體素被占用,體素的顏色(屬性)是該體素內(nèi)所有點(diǎn)的平均值
print("Downsample the point cloud with a voxel of 0.05")
downpcd = pcd1.voxel_down_sample(voxel_size=0.05)
o3d.visualization.draw_geometries([downpcd])
print("The number of PC is : ",pcd1)
print("The number of downPC is : ",downpcd)
'''
Downsample the point cloud with a voxel of 0.05
The number of PC is : PointCloud with 10000 points.
The number of downPC is : PointCloud with 1389 points.
Downsample the point cloud with a voxel of 0.005
The number of PC is : PointCloud with 10000 points.
The number of downPC is : PointCloud with 9825 points.
'''
3.2 點(diǎn)云正態(tài)估計(jì)
在交互頁面,可以通過N
查看點(diǎn)法線,+
,-
控制法線長度。
作為點(diǎn)云的基本操作之一,點(diǎn)云正態(tài)估計(jì)通過指定算法參數(shù)估測每個(gè)點(diǎn)可能的法向量,estimate_normals
查找指定搜索半徑內(nèi)的臨近點(diǎn),通過這些臨近點(diǎn)的協(xié)方差計(jì)算其主軸,從而估計(jì)法向量。正常情況下會(huì)產(chǎn)生兩個(gè)方向相反的法向量,在不知道幾何體的全局結(jié)構(gòu)下,兩者都可以是正確的。Open3D會(huì)嘗試調(diào)整法線的方向,使其與原始法線對(duì)齊。
print("Recompute the normal of the downsampled point cloud")
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)
如果想要訪問頂點(diǎn)法線的話,可以直接通過索引獲?。?/p>
print("Print a normal vector of the 0th point")
print(downpcd.normals[0])
'''
Print a normal vector of the 0th point
[ 0.99552379 -0.03798043 0.08654404]
'''
也可以將其轉(zhuǎn)為numpy
數(shù)組:
print("Print the normal vectors of the first 10 points")
print(np.asarray(downpcd.normals)[:10, :])
'''
Print the normal vectors of the first 10 points
[[ 0.99552379 -0.03798043 0.08654404]
[-0.00180642 -0.97317626 0.23005372]
[-0.03311035 0.95990356 -0.27836821]
[-0.18007638 -0.98233851 -0.05082867]
[ 0.03201738 -0.92865206 0.36956763]
[-0.09411325 0.9584897 -0.26914715]
[-0.00804695 0.97716482 -0.21233029]
[-0.95046739 -0.20590633 0.2328397 ]
[ 0.58566868 0.7923609 0.17075245]
[-0.19273423 -0.87191173 0.45013714]]
'''
3.3 點(diǎn)云裁剪
Open3D的點(diǎn)云裁剪需要通過read_selection_polygon_volume
讀取多邊形選擇區(qū)域的json文件,接著通過.crop_point_cloud()
方法過濾出點(diǎn)。
print("Load a polygon volume and use it to crop the original point cloud")
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])
3.4 繪制點(diǎn)云
paint_uniform_color
可以將點(diǎn)云顏色繪制成同一的色彩。注意顏色是在[0,1]之間的float
類型。
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])
3.5 選擇點(diǎn)云
在Open3D中,可以通過點(diǎn)云索引來進(jìn)行篩選。select_by_index
也可以通過修改invert
方法進(jìn)行反向選取。
inner=pcd1.select_by_index([i for i in range(len(pcd1.points)) if i%2==0])
outer=pcd1.select_by_index([i for i in range(10)],invert=True)
o3d.visualization.draw_geometries([pcd1])
o3d.visualization.draw_geometries([inner])
o3d.visualization.draw_geometries([outer])
四、點(diǎn)云數(shù)據(jù)計(jì)算
4.1 點(diǎn)云距離
Open3D提供了compute_point_cloud_distance
方法,能夠計(jì)算源點(diǎn)云到目標(biāo)點(diǎn)云的最近距離,該方法也能用于計(jì)算兩點(diǎn)云之間的切角距離。
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)
# 從原始圖像到裁剪圖像中最近點(diǎn)的距離
dists=pcd.compute_point_cloud_distance(chair)
dists=np.asarray(dists)
ind=np.where(dists>0.1)[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])
4.2 邊界體積
與其幾何類型相似,PointCloud
也具有邊界體積。
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])
4.3 凸包計(jì)算
點(diǎn)云凸包是包含所有點(diǎn)的最小凸集,在Open3D中,可采用compute_convex_hull
計(jì)算。
bunny = o3d.data.BunnyMesh()
mesh = o3d.io.read_triangle_mesh(bunny.path)
mesh.compute_vertex_normals()
pcl = 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])
4.4 DBSCAN聚類
DBSCAN是Ester在1996年提出的一種聚類算法,Open3D中也提供了該算法的APIpc.cluster_dbscan(eps,min_points,print_progress)
,eps
定義了簇的半徑距離,而min_points
定義形成簇的最小點(diǎn)數(shù)量。返回是一個(gè)標(biāo)簽對(duì)象,若值為-1
則表示噪聲。
import matplotlib.pyplot as plt
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.1
4.5 平面分割
Open3D支持使用RANSAC
方法從點(diǎn)云中分割幾何基元(geometric primitives)。通過segment_plane
方法,可以找到點(diǎn)云中的最大支持平面(the plane with the largest support)。該方法提供了三個(gè)參數(shù):
-
distance_threshold
:定義了一個(gè)點(diǎn)可被視為內(nèi)嵌點(diǎn)的估計(jì)平面的最大距離 -
ransac_n
:定義用來估計(jì)平面的隨機(jī)抽樣點(diǎn)數(shù)量 -
num_iterations
:定義了隨機(jī)平面抽樣和驗(yàn)證的頻率
4.6 消隱點(diǎn)
當(dāng)我們從給定視角渲染點(diǎn)云時(shí),由于前方?jīng)]有遮擋,可能會(huì)有背面的點(diǎn)滲入到前景中。Katz提出了一種消隱算法(Hidden point removal),可以從給定的視圖中近似地獲得點(diǎn)云的可見性,而無需表面重建或正常的估計(jì)。
print("Convert mesh to a point cloud and estimate dimensions")
armadillo = o3d.data.ArmadilloMesh()
mesh = o3d.io.read_triangle_mesh(armadillo.path)
mesh.compute_vertex_normals()
pcd = 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])
文章來源:http://www.zghlxwxcb.cn/news/detail-416027.html
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])
文章來源地址http://www.zghlxwxcb.cn/news/detail-416027.html
到了這里,關(guān)于Open3D點(diǎn)云處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!