有序坐标

时间:2016-03-17 15:29:28

标签: python opencv numpy coordinates

我有一个2D无序坐标列表:

[[ 95 146]
 [118 146]
 [ 95 169]
 [ 95 123]
 [ 72 146]
 [118 169]
 [118 123]
 [141 146]
 [ 95 100]
 [ 72 123]
 [ 95 192]
 [ 72 169]
 [141 169]
 [118 100]
 [141 123]
 [ 72 100]
 [ 95  77]
 [118 192]
 [ 49 146]
 [ 48 169]]

Diplay coordinates

我如何找到每个点的相应行和列?我的观点并不完美,可以存在小的旋转。我正在查看Opencv findCirclesGrid代码,但我没有找到排序算法。

编辑:@armatita解决方案使用数据集,但当坐标旋转7°

data = array([[ 95, 146],[72,143],[92,169],[98,123],[75,120],[69,166],[49,140],[89,192],[115,172],[46,163],[52,117],[66,189],[112,194],[121,126],[123,103],[101,100],[78,97],[141,152],[86,215],[138,175]])

def find(arr,threshold):
    rmin = sys.maxint
    for i in range(len(arr)):
        for j in range(i+1,len(arr)):
            diff = abs( arr[i] - arr[j] )
            if diff > threshold and diff < rmin:
                rmin = diff
    return rmin

threshold = 10
space = np.array([ find(data[:,0],threshold), find(data[:,1],threshold) ], dtype=np.float32)
print "space=",space
first = np.min(data,axis=0)

order = np.around( ( data - first ) / space )

plt.scatter(data[:,1], data[:,0],c=range(len(data)),cmap="ocean")
for pt in zip(order,data):
    c, rc = ( pt[1], pt[0] )
    plt.text( c[1], c[0]+5, "[%d,%d]" % (rc[1],rc[0]),color='black')
plt.show()    

enter image description here

问题来自空间计算

2 个答案:

答案 0 :(得分:2)

将它们放在伪规则网格中:

    c = [[ 95, 146],[118, 146],[ 95, 169],[ 95, 123],[ 72, 146],[118, 169],
         [118, 123],[141, 146],[ 95, 100],[ 72 ,123] ,[ 95 ,192],[ 72 ,169]
         ,[141 ,169],[118 ,100],[141 ,123],[ 72 ,100],[ 95 , 77],[118 ,192]
         ,[ 49 ,146],[ 48 ,169]]

    nodesx = 100
    sizex = 20
    sizey = 20
    firstx = 70
    firsty = 40

    new,xt,yt = [],[],[]
    for i in c:
        xo = int((i[0]-firstx)/sizex)
        yo = int((i[1]-firsty)/sizey)
        new.append(nodesx*yo+xo)
        xt.append(i[0])
        yt.append(i[1])

    sortedc = [x for (y,x) in sorted(zip(new,c))]

    import matplotlib.pyplot as plt
    plt.scatter(xt,yt)
    for i in range(len(sortedc)):
        plt.text(sortedc[i][0],sortedc[i][1],str(i))
    plt.show()

,这将导致这一点(告诉你是否理解逻辑):

sorted 2D coordinates

答案 1 :(得分:0)

如果要按x轴排序,请选择坐标对列表并使用排序

>>> a = [(5, 3), (3, 6), (1, 10), (1, 5)]
>>> a.sort()
>>> print a
[(1, 5), (1, 10), (3, 6), (5, 3)]

如果你想根据y坐标进行排序,你可以看看Sort tuples based on second parameter

获得排序列表

如果要检查最小增量并合并它们

计算每对的距离,如果在delta内合并它们。 (例如,将新点计算为两点的平均值)

如果发生合并,则从合并的坐标对计算到下一个坐标的距离。

重复直至列表中没有变化。

按y坐标排序并重复合并。

一旦两组合并完成,你应该留下一组有效的点。

彼此三角洲内的所有点都已合并。