python - 使用 numpy 对 2d 校准模式点进行排序

标签 python sorting numpy matplotlib delaunay

我有一个 n:2 矩阵,其中点 (x,y) 从矩形校准图案中的点中找到。 我喜欢将这些点逐行排序。 我已经用 lexsort 对这些点进行了排序,但是相机的失真太大,以至于 y 坐标会重叠。

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

有没有办法借助 delaunay 模式对行进行排序?

import matplotlib.tri as tri 
x=coordinates[:,0] y=coordinates[:,1]
triang = tri.Triangulation(x, y)

enter image description here

最佳答案

使用三角测量确实很有趣,可以用于您的应用:

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()

结果,你在 edges_horizo​​ntal 中得到水平边,它是一个二维数组 [[p_{0},p_{1}], ..., [p_{n}, p_{n+ 1}]],其中 p_i 是 coordinates 数组的索引。

关于python - 使用 numpy 对 2d 校准模式点进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13935913/

相关文章:

python - 在多指标横截面的一个级别指定多个可能的标准

python - 替换 Pandas 数据框列表中的空白元素

java - "same ordering"对象相等的重要性是什么?

python - 查找 Pandas 中列的标准差,其中每个元素都是 numpy 数组

python - 提高性能 - 符号函数应用于 numpy 数组的每一行

python - 使用 for 循环和 if 语句求根迭代

python - ATpy 导入错误 : No module named astropy. io

algorithm - 找到 m 个最大的数字

linux - 在 Linux 中使用过滤器删除电子邮件地址的一部分

python - 在 true_divide 中遇到被零除 + 在 true_divide 中遇到无效值 + 在 reduce 中遇到无效值