【问题标题】:Filtering coordinate points on the basis of nearest distance根据最近距离过滤坐标点
【发布时间】:2017-06-21 00:03:59
【问题描述】:

我有一个 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] & [ 582 939],因为这些是最接近[581 925]的直接坐标。

在这种情况下很明显[582 926] 是最接近[581 925] 的坐标,我只希望该坐标存在并且删除其他2 个候选坐标。所以结果数组应该是

[[ 581  925]
 [ 582  926]
      .
      .
      .
 [ 589  944]]

现在应该从[582 926]开始执行相同的操作,以此类推直到结束。

未过滤坐标的轮廓:

带有过滤坐标的轮廓:

什么是最pythonic(最好是numpy)的方法,时间复杂度最低,因为它是最受关注的?

注意:线细化无关紧要,只需要删除不必要的点/坐标。

【问题讨论】:

  • 能否请您指定要使用哪个距离度量来确定最近的坐标?
  • 所以,基本上你想计算最近坐标之间的距离,最近的意思是那些距离较近的坐标?不先计算距离?
  • @Tristan Euclidean,我将使用numpy.linalg.norm(a-b) 来计算距离
  • @NAmorim 不,我想使用这些点绘制轮廓,并希望删除那些会阻止我绘制最佳轮廓的点。我会在问题中添加图片。
  • @NAmorim 由于拥有多个具有相同 x 轴值的点会创建一个粗而锯齿状的轮廓,我只想保留最接近前一个的点。

标签: python arrays numpy


【解决方案1】:

我设法做到了:

要使该方法起作用,首先必须根据相等的 x 轴值将数组拆分为 sup 组。请参阅this post 了解详细信息。不过,我将在下面添加代码。 数组按照 x 轴的升序排序很重要。 如果不是,您可以通过在数组上应用 np.lextsort 来实现。请参阅this post 以了解如何正确应用 lexsort。非常感谢 @Divakar 为这些帖子提供了很棒的答案。

代码:

# 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]]

【讨论】:

  • 很高兴看到你终于熬过来了!
  • @Divakar 谢谢!你也帮了大忙!
猜你喜欢
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多