Python 基于最近距离的坐标点过滤
我有一个numpy数组,它包含像这样的坐标点Python 基于最近距离的坐标点过滤,python,arrays,numpy,Python,Arrays,Numpy,我有一个numpy数组,它包含像这样的坐标点 [[ 581 925] [ 582 926] [ 582 931] [ 582 939] [ 584 933] [ 584 937] [ 585 943] [ 586 944] [ 589 944]] 正如您所看到的,有些点的x坐标相同,但y坐标不同从第一个坐标开始,计算到下一个最近的直接坐标的距离。 例如,可以找到从[581 925]到下一个最近坐标的距离。候选坐标为[582 926]、[582 931]、[58
[[ 581 925]
[ 582 926]
[ 582 931]
[ 582 939]
[ 584 933]
[ 584 937]
[ 585 943]
[ 586 944]
[ 589 944]]
正如您所看到的,有些点的x坐标相同,但y坐标不同从第一个坐标开始,计算到下一个最近的直接坐标的距离。
例如,可以找到从
[581 925]
到下一个最近坐标的距离。候选坐标为[582 926]、[582 931]、[582 939]
,因为它们是最接近[581 925]
的直接坐标
很明显,在这种情况下,[582 926]
是距离[581 925]
最近的坐标,我只希望该坐标存在,并删除其他两个候选坐标。所以结果数组应该是
[[ 581 925]
[ 582 926]
.
.
.
[ 589 944]]
现在,应该从[582 926]
开始执行相同的操作,以此类推,直到结束
具有未过滤坐标的轮廓:带过滤坐标的等高线:
什么是最具python风格(最好是numpy)的方法,时间复杂度最低,因为它是最受关注的问题?
注意:线条细化不重要,只需要删除不必要的点/坐标。我这样做: 要使该方法起作用,首先必须根据相等的x轴值将阵列分成sup组。请参阅详细信息。我将在下面添加代码。数组按相对于x轴的升序排序很重要。如果不是,您可以在数组上应用
np.lextsort
。请参阅了解如何正确应用lexsort。非常感谢为这些帖子提供了精彩的答案
代码:
# Initial array of coordinates
a = np.array([[ 581 925]
[ 582 926]
[ 582 931]
[ 582 939]
[ 584 933]
[ 584 937]
[ 585 943]
[ 586 944]
[ 589 944]])
# Following line splits the array into subgroups on the basis of equal x-axis elements
a = np.split(a, np.unique(a[:, 0], return_index=True)[1][1:], axis=0)
# Array after splitting
# [array([[581, 925]]),
# array([[582, 926], [582, 931], [582, 939]]),
# array([[584, 933], [584, 937]]),
# array([[585, 943]]),
# array([[586, 944]]),
# array([[589, 944]])]
i = 0
# filteredList will initially contain the first element of the array's first sub group
filteredList = np.reshape(np.asarray(a[0][0]), (-1, 2)) # filteredList = [[581 925]]
while not i == len(a) - 1:
if len(a[i + 1]) > 1:
# Following line calculates the euclidean distance between current point and the points in the next group
min_dist_point_addr = np.argmin(np.linalg.norm(filteredList[i] - a[i + 1], axis=1))
# Next group is reassigned with the element to whom the distance is the least
a[i + 1] = a[i + 1][min_dist_point_addr]
# The element is concatenated to filteredList
filteredList = np.concatenate((filteredList, np.reshape((a[i+1]), (1, 2))), axis=0)
i += 1
print filteredList
[[581 925]
[582 926]
[584 933]
[585 943]
[586 944]
[589 944]]
输出:
# Initial array of coordinates
a = np.array([[ 581 925]
[ 582 926]
[ 582 931]
[ 582 939]
[ 584 933]
[ 584 937]
[ 585 943]
[ 586 944]
[ 589 944]])
# Following line splits the array into subgroups on the basis of equal x-axis elements
a = np.split(a, np.unique(a[:, 0], return_index=True)[1][1:], axis=0)
# Array after splitting
# [array([[581, 925]]),
# array([[582, 926], [582, 931], [582, 939]]),
# array([[584, 933], [584, 937]]),
# array([[585, 943]]),
# array([[586, 944]]),
# array([[589, 944]])]
i = 0
# filteredList will initially contain the first element of the array's first sub group
filteredList = np.reshape(np.asarray(a[0][0]), (-1, 2)) # filteredList = [[581 925]]
while not i == len(a) - 1:
if len(a[i + 1]) > 1:
# Following line calculates the euclidean distance between current point and the points in the next group
min_dist_point_addr = np.argmin(np.linalg.norm(filteredList[i] - a[i + 1], axis=1))
# Next group is reassigned with the element to whom the distance is the least
a[i + 1] = a[i + 1][min_dist_point_addr]
# The element is concatenated to filteredList
filteredList = np.concatenate((filteredList, np.reshape((a[i+1]), (1, 2))), axis=0)
i += 1
print filteredList
[[581 925]
[582 926]
[584 933]
[585 943]
[586 944]
[589 944]]
你能指定你想用哪个距离度量来确定最近的坐标吗?所以,基本上你想计算最近的坐标之间的距离,最近的是指那些距离较小的坐标?没有先计算距离?@Tristan-Euclidean,我将使用
numpy.linalg.norm(a-b)
来计算距离distance@NAmorim不,我想使用这些点绘制轮廓,并想删除那些会阻止我绘制最佳轮廓的点。我将在问题中添加图像。@NAmorim因为有多个点具有相同的x轴值会创建一个厚而锯齿状的轮廓,我只想保留与上一个点最近的点。很高兴看到你终于完成了它@迪瓦卡谢谢!你也帮了很多忙!