根据最近距离过滤坐标点 [英] Filtering coordinate points on the basis of nearest distance
问题描述
我有一个numpy数组,其中包含类似这样的坐标点
I have a numpy array which contains coordinates points like this
[[ 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]
的立即坐标.
As you can see there are points which have same x coordinates but different y coordinates. Starting from the first coordinate, distance to the next closest immediate coordinate is calculated.
Like for example, distance from [581 925]
to next nearest coordinate is found out. The candidates are [ 582 926], [ 582 931] & [ 582 939]
since these are the immediate coordinates which are the closest to [581 925]
.
很明显,在这种情况下,[582 926]
是最接近[581 925]
的坐标,我只希望该坐标存在并且要删除其他2个候选坐标.所以结果数组应该是
As it's obvious in this case that [582 926]
is the nearest coordinate to [581 925]
, I only want that coordinate to exist and the other 2 candidate coordinates to be deleted. So the resultant array should be
[[ 581 925]
[ 582 926]
.
.
.
[ 589 944]]
现在应该从[582 926]
开始执行相同的操作,依此类推,直到结束.
Now same operation should be performed starting from [582 926]
and so on till the end.
具有未过滤的坐标的轮廓:
Contour with unfiltered coordinates:
具有已过滤坐标的轮廓:
Contour with filtered coordinates:
由于这是最令人担忧的事情,因此最省时的方法是最方便的pythonic(最好是numpy)方法?
注意:细化线不是问题,只是要消除不必要的点/坐标.
NOTE: Line Thinning is not of concern, only concern is of removing the unnecessary points/coordinates.
推荐答案
我设法做到了:
要使用该方法,首先必须在相等的x轴值的基础上将数组分为多个sup组.请参考这篇文章以了解如何正确应用lexsort.非常感谢 @Divakar 为这些帖子提供了出色的答案.
For the method to work, first the array has to be split into sup groups on the basis of equal x-axis values. Please refer this post for detailed information. I'll be adding the code below though. It's important that the array is sorted in ascending order with respect to x-axis. If it's not, you can do so by applying np.lextsort
on the array. Refer this post to understand how to apply lexsort correctly. A huge thanks to @Divakar for providing awesome answers to these posts.
代码:
# 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]]
这篇关于根据最近距离过滤坐标点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!