NumPy unique()返回越界的索引

2024-04-26 06:18:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从点云中删除彼此太近的点。我的输入是一个mx3矩阵,其中的列表示xyz坐标。代码如下:

def remove_duplicates(points, threshold):
    # Convert to numpy
    points = np.array(points)

    # Round to within the threshold
    rounded_points = points
    if threshold > 0.0:
        rounded_points = np.round(points/threshold)*threshold

    # Remove duplicate points
    point_tuples = [tuple(point) for point in rounded_points]
    unique_rounded_points, unique_indices = np.unique(point_tuples, return_index = True)

    points = points[unique_indices]

    return points

我遇到的问题是,唯一的索引包含的值大于点的长度(我的测试数据是2265和1000)。是我做错什么了,还是这是纽比的小虫子?在

编辑:我应该注意到,对于非常小的输入(尝试27个点),unique()似乎可以正常工作。在


Tags: to代码thresholdreturndefnp矩阵points
2条回答

那么points是一个二维数组,(m,3)的形状,对吗?在

point_tuples是元组的列表,即rounded_points的行现在是一个由3个浮点组成的元组。在

np.unique将把它变成一个数组来完成它的任务

np.array(point_tuples)是一个(m,3)数组(也是2d类似的points)。元组什么也没做。在

unique将作用于该数组的散列形式,因此unique_indices的值可能介于0和3*m之间。因此,您的错误。在

我看到了两个问题-如果你想让unique找到唯一的“行”,你需要创建一个结构化数组

np.array(point_tuples, 'f,f,f')

同样地,将unique应用于浮动也是很棘手的。几乎不可能找到两个相等的浮动。舍入可以减少这个问题,但不能消除它。在

所以使用round的方式最好是rounded_points是一个整数数组。这些值不需要缩小以匹配points。在

如果需要的话,我可以加一个例子,但是首先试试这些建议。我对你的数据做了很多猜测,我想在进一步研究之前得到一些反馈。在

因为前面的程序可以工作,所以我建议您有一个重叠问题:NumPy是在右侧访问,而在左侧更改它。使用其他变量名

orig_points = points[unique_indices]
return orig_points

或者直接归还

^{pr2}$

相关问题 更多 >