用numpy排序二维校准图案点

2024-06-25 23:53:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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模式对行进行排序?在

^{pr2}$

enter image description here


Tags: 排序np分类矩阵arrayblob校准图案
1条回答
网友
1楼 · 发布于 2024-06-25 23:53:52

使用三角剖分确实很有趣,可用于以下应用:

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_horizontal中的水平边,这是一个2d数组[[p_{0},p_{1}], ..., [p_{n}, p_{n+1}]],其中p_i是coordinates数组的索引。在

相关问题 更多 >