簡體   English   中英

open3d計算網格和點雲之間的距離

[英]open3d compute distance between mesh and point cloud

對於一個研究項目,我嘗試進行點雲比較。 為了簡短起見,我有一個 CAD 文件 (.stl) 和幾個由激光掃描儀創建的點雲。 現在我想計算 CAD 文件和每個點雲之間的差異。

首先,我從 Cloud Compare 開始,這有助於獲得基本的理解。 (減少點、刪除重復項、創建網格並比較距離)

在 python 中,我能夠導入文件並進行一些基本計算。 但是,我無法計算距離。

這是我的代碼:

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():不兼容的function arguments。支持以下參數類型:1.(self:open3d.cpu.pybind.geometry.PointCloud,目標:open3d.cpu幾何.PointCloud) -> open3d.cpu.pybind.utility.DoubleVector"

問題:需要對網格和點雲進行哪些預轉換來計算它們的距離? 有沒有推薦的方法來顯示差異?

到目前為止,我只使用了下面的可視化線

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

function“計算點雲距離()”需要 2 個點雲,但其中一個幾何圖形是由多邊形和頂點組成的網格。 只需將其轉換為點雲:

pcd = o3d.geometry.PointCloud() # create a empty geometry
pcd.points = mesh.vertices      # take the vertices of your mesh

我將說明如何可視化兩朵雲之間的距離,這兩朵雲都是在平均間隔 1 米的移動機器人(Velodyne LIDAR)上捕獲的。 考慮注冊前后的2個雲,它們之間的距離應該減小,對吧? 這是一些代碼:

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

報名前2朵雲

# 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()

之前的圖

# 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

在此處輸入圖像描述

# 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()

注冊后的圖表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM