I have a n:2 Matrix with points(x,y) found from dots in a rectangular calibration pattern. I like to sort these points row by row. I have sorted these points with lexsort but the distortion from the camera is too big so that y-coordinates will be overlap.
imageloading...
blobs=imageprocessing....
coordinates=np.array([blob.centroid() for blob in blobs])
nd=np.lexsort((coordinates[:,0],coordinates[:,1]))
coordinates=coordinates[ind]
Is there a way to sort this with help of a delaunay pattern going a long the rows?
import matplotlib.tri as tri
x=coordinates[:,0] y=coordinates[:,1]
triang = tri.Triangulation(x, y)
Sorting 2D Numpy Array by column at index 1 Select the column at index 1 from 2D numpy array i.e. It returns the values at 2nd column i.e. column at index position 1 i.e. Now get the array of indices that sort this column i.e. It returns the index positions that can sort the above column i.e.
numpy. lexsort. This function is used for sorting using multiple sort keys involving more than one array. For example, we first sort data in Column A and then sort the values in column B. In the below example we take two arrays representing column A and column B.
NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value.
Using triangulation is indeed interesting, and can be used for you application:
import numpy as np
import matplotlib.tri as tri
import matplotlib.pyplot as plt
import random
# create fake data
x,y = np.meshgrid(np.arange(10), np.arange(10))
x = x.flatten()
y = y.flatten()
coordinates = np.column_stack([x,y])+0.04 * np.random.rand(len(x), 2)
np.random.shuffle(coordinates)
x=coordinates[:,0]
y=coordinates[:,1]
# perform triangulation
triang=tri.Triangulation(x,y)
f = plt.figure(0)
ax = plt.axes()
tri.triplot(ax,triang)
# find horizontal edges
f = plt.figure(1)
e_start = coordinates[triang.edges[:,0]]
e_end = coordinates[triang.edges[:,1]]
e_diff = e_end - e_start
e_x = e_diff[:,0]
e_y = e_diff[:,1]
e_len = np.sqrt(e_x**2+e_y**2)
alpha = 180*np.arcsin(e_y/e_len)/np.pi
hist, bins, patches = plt.hist(alpha, bins=20)
# in the histogram, we find that the 'horizontal' lines
# have an alpha < 10.
ind_horizontal = (-10<alpha) & (alpha < 10)
edges_horizontal = triang.edges[ind_horizontal]
plt.show()
As a result, you get the horizontal edges in edges_horizontal, which is an 2d array [[p_{0},p_{1}], ..., [p_{n}, p_{n+1}]]
, in which p_i are indices into the coordinates
array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With