python - 如果段长度高于特定值,如何递归离散化段

标签 python arrays numpy euclidean-distance

为了提高 python 脚本的效率,我尝试使用 Numpy 来转换基于点云上大量“for 循环”操作的脚本,以加快该过程。

Points Cloud overview and some euclidean distance representation

简而言之,我有一个 3d 模型,表示为包含在 np.array 中的一组 3d 点(x,y,z 坐标)(维度:(188706, 3))

[[168.998 167.681   0.]    <-- point 1
 [169.72  167.695   0.]    <-- point 2
 [170.44  167.629   0.]    <-- point 3
 ...
 [148.986 163.271  25.8]    <-- point 188704
 [148.594 163.634  25.8]    <-- point 188705
 [148.547 163.667  25.8]]   <-- point 188706

每对 [[row x-1][row x]] 代表 3D 中的一个段

[[168.998 167.681   0.]    [169.72  167.695   0.]    <-- SEGMENT 1 (points 1 & 2)
 [169.72  167.695   0.]    [170.44  167.629   0.]    <-- SEGMENT 2 (points 2 & 3)
 [170.44  167.629   0.]    [171.149 167.483   0.]    <-- SEGMENT 3 (points 3 & 4)
 ...
 [149.328 162.853  25.8]   [148.986 163.271  25.8]    <-- SEGMENT 94351 (points 188703 & 188704)
 [148.986 163.271  25.8]   [148.594 163.634  25.8]    <-- SEGMENT 94352 (points 18874 & 188705)
 [148.594 163.634  25.8]   [148.547 163.667  25.8]]   <-- SEGMENT 94353 (points 188705 & 188706)

我的目标是测量每个有序点//行对之间的欧几里德距离(=每个线段的长度),以便我可以检测到需要在哪里添加更多点来表示更多的 3d 表面模型。换句话说,如果段长度高于阈值 (=0.5mm),我将必须用更多点离散化该特定段,并将这些附加点添加到我的点云中。

借助以下代码,我找到了一种递归测量每行之间的欧几里得距离的方法:

EuclidianDistance = np.linalg.norm(PtCloud[:-1] - PtCloud[1:],axis=1) 

这给出了这个结果:

[0.72213572 0.72301867 0.72387637 ... 0.54008148 0.5342593  0.05742822]

我还发现了如何根据线段的顶点(末端)对线段进行离散化:

def AddEquidistantPoints(p1, p2, parts):
    return np.stack((np.linspace(p1[0], p2[0], parts + 1), np.linspace(p1[1], p2[1], parts + 1)), axis=-1)

if EuclidianDistance > 0.5mm:
    dist = AddEquidistantPoints(currentRow, previousRow, 10) #10 --> nb subdivisions

但我的第一个问题是那些欧几里德距离只能在 z 坐标相等的点上计算。当 z 坐标不等于时我是否需要分割数组? 与:

PtCloud = np.split(PtCloud, np.where(np.diff(PtCloud[:,2])!=0)[0]+1)

这给了我一个数组列表,所以我想我将不幸地使用 for 循环......

以下是用 Excel 表示的正确行为:image

我的第二个问题与递归检查和离散化步骤有关,因为我不知道如何在这种特殊情况下实现它。我想知道是否有一种方法可以在没有任何 for 循环的情况下做到这一点。

因此,如果有人能帮助我解决这个挑战,我将非常高兴,因为我目前“陷入困境”。 这对我来说开始变得非常具有挑战性。

提前致谢, 埃尔维

最佳答案

只是与您分享,我刚刚找到了解决我的问题的方法。这可能不是最有效的方法,但它确实有效。

import numpy as np

print("====================")
print("Initial Points Cloud")
print("====================")

POINTCLOUD = np.array([[168.998, 167.681, 0.],
                       [169.72, 167.695,  0.],
                       [170.44, 167.629,  0.],
                       [171.149, 167.483,  0.],
                       [150.149, 167.483,  4.2],
                       [160.149, 167.483,  4.2],
                       [159.149, 166.483,  4.2],
                       [152.149, 157.483,  7.],
                       [149.328, 162.853, 25.8],
                       [148.986, 163.271, 25.8],
                       [148.594, 163.634, 25.8],
                       [180.547, 170.667, 25.8],
                       [200.547, 190.667, 25.8]])
print(POINTCLOUD)


print("============================================")
print("Reshaped Point Cloud in the form of segments")
print("============================================")
a = np.column_stack((POINTCLOUD[:-1],POINTCLOUD[1:]))
print(a)
b = a.reshape((a.shape[0],2, 3))
#print(b)

print("")
print("*******************************")
print("Non filtered euclidean distance")
print("*******************************")
EuclidianDistance = np.transpose(np.linalg.norm(b[:,0] - b[:,1],axis=1))
print(EuclidianDistance)

print("")
print("****************")
print("Mask computation")
print("****************")
mask = np.transpose([a[:,2] == a[:,5]])
mask2 = [a[:,:] == a[:,:]]*np.transpose([a[:,2] == a[:,5]])



print("")
print(mask2)

print("")
print("***********************************")
print("1rst Filter applyed on points cloud")
print("***********************************")

# b = np.where(mask2,a,0)
# b = np.squeeze(b, axis=0)
b = np.squeeze(np.where(mask2,a,0), axis=0)
print(b)
print("")

b2 = b[np.squeeze(mask2,axis=0),...].reshape((np.sum(mask),b.shape[1]))
print(b2)
print("")
#print(b2.reshape(b2.shape[0],2, 3))

b = b2.reshape(b2.shape[0],2, 3)


print("")
print("***************************************")
print("FIRST EUCLIDEAN DISTANCE FILTERING STEP")
print("***************************************")
EuclidianDistance = np.linalg.norm(b[:,0] - b[:,1],axis=1)
print(EuclidianDistance)

print("")
print("***************************")
print("# THRESHOLD MASK GENERATION")
print("***************************")
threshold = 7
mask_threshold = np.transpose(EuclidianDistance>threshold)
print(mask_threshold)

print("")
print("**********************************")
print("# FINAL FILTERED ECLIDEAN DISTANCE")
print("**********************************")
EuclidianDistance = EuclidianDistance[np.squeeze(mask_threshold),...]
print(EuclidianDistance)

print("")
print("**********************")
print("SEGMENTS TO DISCRETIZE")
print("**********************")
SegmentToDiscretize = b[np.squeeze(mask_threshold),...]
print(SegmentToDiscretize)



print("")
print("******************************")
print("EQUIDISTANT POINTS COMPUTATION")
print("******************************")

nbsubdiv2 = np.transpose(np.ceil(np.array(np.divide(EuclidianDistance,0.7))).astype(int)).reshape((SegmentToDiscretize.shape[0],1))
print(nbsubdiv2)
print(nbsubdiv2.shape)
print(nbsubdiv2[1,0])

print(SegmentToDiscretize.shape)
print(SegmentToDiscretize[:,0])


nbsubdiv = [10,30,10]

addedpoint = np.linspace(SegmentToDiscretize[:,0],SegmentToDiscretize[:,1],nbsubdiv[0], dtype = np.float)
addedpoint = addedpoint.reshape((addedpoint.shape[0]*addedpoint.shape[1],3))
print(np.sort(addedpoint,axis=0))


print("")
print("***********************************")
print("UPDATED POINT CLOUD WITH NEW POINTS")
print("***********************************")
# duplicates are removed with the command np.unique
POINTCLOUD = np.unique(np.append(POINTCLOUD,addedpoint, axis=0),axis=0)
print(POINTCLOUD)

print("")
print("************************")
print("FINAL SORTED POINT CLOUD")
print("************************")
sortedPOINTCLOUD = POINTCLOUD[np.argsort(POINTCLOUD[:, 2])]
print(sortedPOINTCLOUD)
print("***************************")

如果您愿意,请随时添加您自己的建议来改进它。我们将非常欢迎!

关于python - 如果段长度高于特定值,如何递归离散化段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60288272/

相关文章:

python - pandas str.contains 匹配多个字符串并获取匹配的值

python - struct.pack() Python 未知字段长度通过套接字发送

python - Github 桌面和 git hooks

Java 如何对二维字符串数组进行完全排序

php - 如何跳过数组的日期并将日期增加+1?

python - 根据最大值过滤一个numpy数组

Python - 链表 - 追加

python - 在 python 中更改 neo4j 数据库位置

java - 将数据存储到 Cassandra 时是大端还是小端?

python - 如何用按顺序开始的自然数填充 nan 列?