我正在尝试从点云中删除彼此太近的点。我的输入是一个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()似乎可以正常工作。在
那么
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
找到唯一的“行”,你需要创建一个结构化数组同样地,将
unique
应用于浮动也是很棘手的。几乎不可能找到两个相等的浮动。舍入可以减少这个问题,但不能消除它。在所以使用round的方式最好是
rounded_points
是一个整数数组。这些值不需要缩小以匹配points
。在如果需要的话,我可以加一个例子,但是首先试试这些建议。我对你的数据做了很多猜测,我想在进一步研究之前得到一些反馈。在
因为前面的程序可以工作,所以我建议您有一个重叠问题:NumPy是在右侧访问点,而在左侧更改它。使用其他变量名
或者直接归还
^{pr2}$相关问题 更多 >
编程相关推荐