从2D列表中删除重复项(浮点)

问题描述 投票:1回答:3

在我的荣誉项目上使用3D扫描仪,我知道会有重叠的数据可能会弄乱网格曲面细分。我已经获得了[[x1 y1 z1][x2 y2 z2]...[xn yn zn]]形式的二维数组中的所有点

如果给出像[[1.1 1.2 1.3][2.1 2.2 2.3][1.1 1.2 1.3]]这样的2D列表,我想删除重复项,例如第二次出现的[1.1 1.2 1.3]

如果可能的话,我希望能够在一定的容差范围内移除(这可能会使浮动更容易,因为我听说它们很难处理)。这是因为我知道来自不同扫描仪输入的数据不会给出完全相同的值。所以在伪代码中:

if original + tolerance >= duplicate >= original - tolerance:
    remove
#e.g.
original = [1.1 1.2 1.3] 
duplicate = [1.2 1.2 1.3]
tolerance = 0.1
[1.2 1.3 1.4] >= [1.2 1.2 1.3] >= [1.0 1.1 1.2]

有任何想法吗?感谢您的帮助,我的大脑已经被炒了,而且我仍然是编程的新手。

python list multidimensional-array floating-point duplicates
3个回答
1
投票

使用numpy删除重复项非常简单

np.unique(x, axis=0)

在哪里xis你的阵列


0
投票

我会说过滤条目,当从其他条目减少时,没有点通过某个阈值:

def subtract(pts1, pts2):
    # subtract two entries:
    # subtract((1.1, 1.2, 1.3), (1.2, 1.1, 1.3)) = (-0.1, 0.1, 0)
    return tuple(map(operator.sub, zip(pts1, pts2)))

def is_close_enough(match, thresh=0.15):
    return abs(x) <= thresh

def is_close_match(pts1, pts2):
    # check if the match is close enough, create your own THRESH
    distances = subtract(pts1, pts2)
    return all(map(is_close_enough, distances))

def get_duplicates(original, candidates):
    # given an entry and duplicate candidates, return all duplicates
    # candidates.remove(original) # don't match self, not that important
    _is_close_match = lambda candidate: is_close_match(original, candidate)
    return filter(_is_close_match, candidates)

要返回唯一条目,请使用filterfalse


0
投票

正如所指出的,这与问题here非常相似。出于好奇(或比我更多的知识),值得尝试这里发布的答案。

但是我发现修改链接问题的第一个答案非常有效。

© www.soinside.com 2019 - 2024. All rights reserved.