Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort 2d calibration pattern points with numpy

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]

enter image description here

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)

enter image description here

like image 386
Malefs Matthias Malefski Avatar asked Dec 18 '12 15:12

Malefs Matthias Malefski


People also ask

How do you sort a 2D array in Python using Numpy?

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.

What is Numpy Lexsort?

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.

How do you sort an array row in Python?

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.


1 Answers

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.

like image 108
Hansemann Avatar answered Sep 21 '22 13:09

Hansemann