首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >open3d计算网格和点云之间的距离

open3d计算网格和点云之间的距离
EN

Stack Overflow用户
提问于 2021-03-30 16:02:38
回答 1查看 1.3K关注 0票数 2

对于一个研究项目,我尝试进行点云比较。为了保持简短,我有一个CAD文件(.stl)和几个由激光扫描仪创建的点云。现在我要计算CAD文件和每个点云之间的差异。

首先,我从Cloud Compare开始,它对获得基本的理解有很大帮助。(减少点,删除重复项,创建网格和比较距离)

在python中,我可以导入文件并进行一些基本的计算。但是,我无法计算距离。

下面是我的代码:

代码语言:javascript
复制
import numpy as np 
import open3d as o3d

#read point cloud
dataname_pcd= "pcd.xyz"
point_cloud = np.loadtxt(input_path+dataname_pcd,skiprows=1)
#read mesh 
dataname_mesh = "cad.stl"
mesh = o3d.io.read_triangle_mesh(input_path+dataname_mesh)
print (mesh)

#calulate the distance
mD = o3d.geometry.PointCloud.compute_point_cloud_distance([point_cloud],[mesh])

#calculate the distance返回此错误:"TypeError: compute_point_cloud_distance():函数参数不兼容。支持以下参数类型: 1. (self: open3d.cpu.pybind.geometry.PointCloud,target: open3d.cpu.pybind.geometry.PointCloud) -> open3d.cpu.pybind.utility.DoubleVector“

问题:需要对网格和点云进行哪些预变换来计算它们的距离?有没有推荐的方式来显示差异?

到目前为止,我只使用了下面的可视化行

代码语言:javascript
复制
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])
EN

回答 1

Stack Overflow用户

发布于 2021-04-10 11:46:31

函数“计算点云距离()”需要2个点云,但其中一个几何图形是由多边形和顶点组成的网格。只需将其转换为点云即可:

代码语言:javascript
复制
pcd = o3d.geometry.PointCloud() # create a empty geometry
pcd.points = mesh.vertices      # take the vertices of your mesh

我将演示如何可视化两个云之间的距离,这两个云都是在移动的机器人( Velodyne LIDAR)上捕获的,平均间隔为1米。考虑注册前后的2个云,它们之间的距离应该会减小,对吗?下面是一些代码:

代码语言:javascript
复制
import copy
import pandas as pd
import numpy as np
import open3d as o3d
from matplotlib import pyplot as plt

# Import 2 clouds, paint and show both
pc_1 = o3d.io.read_point_cloud("scan_0.pcd") # 18,421 points
pc_2 = o3d.io.read_point_cloud("scan_1.pcd") # 19,051 points
pc_1.paint_uniform_color([0,0,1])
pc_2.paint_uniform_color([0.5,0.5,0])
o3d.visualization.draw_geometries([pc_1,pc_2])

代码语言:javascript
复制
# Calculate distances of pc_1 to pc_2. 
dist_pc1_pc2 = pc_1.compute_point_cloud_distance(pc_2)

# dist_pc1_pc2 is an Open3d object, we need to convert it to a numpy array to 
# acess the data
dist_pc1_pc2 = np.asarray(dist_pc1_pc2)

# We have 18,421 distances in dist_pc1_pc2, because cloud pc_1 has 18,421 pts.
# Let's make a boxplot, histogram and serie to visualize it.
# We'll use matplotlib + pandas. 
 
df = pd.DataFrame({"distances": dist_pc1_pc2}) # transform to a dataframe
# Some graphs
ax1 = df.boxplot(return_type="axes") # BOXPLOT
ax2 = df.plot(kind="hist", alpha=0.5, bins = 1000) # HISTOGRAM
ax3 = df.plot(kind="line") # SERIE
plt.show()

代码语言:javascript
复制
# Load a previos transformation to register pc_2 on pc_1 
# I finded it with the Fast Global Registration algorithm, in Open3D 
T = np.array([[ 0.997, -0.062 ,  0.038,  1.161],
              [ 0.062,  0.9980,  0.002,  0.031],
              [-0.038,  0.001,  0.999,  0.077],
              [ 0.0,    0.0  ,  0.0   , 1.0  ]])
# Make a copy of pc_2 to preserv the original cloud
pc_2_copy = copy.deepcopy(pc_2)
# Aply the transformation T on pc_2_copy
pc_2_copy.transform(T)
o3d.visualization.draw_geometries([pc_1,pc_2_copy]) # show again

代码语言:javascript
复制
# Calculate distances
dist_pc1_pc2_transformed = pc_1.compute_point_cloud_distance(pc_2_copy)
dist_pc1_pc2_transformed = np.asarray(dist_pc1_pc2_transformed)
# Do as before to show diferences
df_2 = pd.DataFrame({"distances": dist_pc1_pc2_transformed})
# Some graphs (after registration)
ax1 = df_2.boxplot(return_type="axes") # BOXPLOT
ax2 = df_2.plot(kind="hist", alpha=0.5, bins = 1000) # HISTOGRAM
ax3 = df_2.plot(kind="line") # SERIE
plt.show()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66866952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档