I have a list of 2D unordered coordinates :
[[ 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]]
How could I find the corresponding row and column for each points? My points are not perfect and small rotation can exist. I'm looking at Opencv findCirclesGrid
code but I did not find ordering algorithm.
EDIT: @armatita solution work with set of data but when coordinate have rotation 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()
Problem come from space calculation