【问题标题】:NumPy unique() returns indices that are out-of-boundsNumPy unique() 返回超出范围的索引
【发布时间】:2016-04-24 06:39:02
【问题描述】:

我正在尝试从点云中删除彼此太近的点。我的输入是一个 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

我遇到的问题是 unique_indices 包含的值大于点的长度(我的测试数据为 2265 和 1000)。是我做错了什么,还是这是 NumPy 中的错误?

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

【问题讨论】:

  • 您可能遇到了重叠问题。简单地抓取从 unique 返回的点列表怎么样:points = unique_rounded_points
  • 我以前是这样做的,但我更愿意返回原始的未取整点。
  • np.array(point_tuples)[ind] 返回什么?同样的错误? pointsunique_rounded_points的形状是什么?
  • tuple(point) 的目的是什么?您是否尝试创建一个结构化数组,以便 unique 返回唯一的元组,而不是唯一的单个值?

标签: python numpy matrix indexing point-clouds


【解决方案1】:

所以points 是一个二维数组,(m,3) 在形状上,对吧?

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

np.unique 将把它变成一个数组来做它的事情

np.array(point_tuples) 是一个 (m,3) 数组(同样是 2d,如 points)。元组什么也没做。

unique 将作用于这个数组的散列形式,所以unique_indices 的值可以在 0 到 3*m 之间。因此你的错误。

我看到 2 个问题 - 如果您希望 unique 找到唯一的“行”,您需要创建一个结构化数组

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

unique 应用于浮点数也很棘手。几乎不可能找到 2 个相等的浮点数。舍入减少了这个问题,但并没有消除它。

因此,最好以 rounded_points 是整数数组的方式使用 round。这些值不需要缩小以匹配points

如果需要,我可以添加一个示例,但请先尝试这些建议。我对你的数据做了很多猜测,在进一步讨论之前我想得到一些反馈。

【讨论】:

  • 将点打包在结构化数组中完全解决了这个问题。谢谢!
【解决方案2】:

既然早期的程序可以工作,那么我建议你有一个重叠问题:NumPy 是在右侧访问 points 而在左侧更改它。使用不同的变量名

orig_points = points[unique_indices]
return orig_points

或者干脆直接退货

return points[unique_indices]

【讨论】:

  • 试过了,但没有解决问题:/ 好主意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多