从二维二进制矩阵中提取直线



我想从二维二进制矩阵中提取直线。

256 x128矩阵

我会找到按邻居坐标分组的1。
在附图中,有一个256x128二进制矩阵。
从给出的示例中可以提取出4条直线。
在找到所有分组的1之后,我想要一个包含2个边坐标的向量[x1,y1,x2,y2]。在给定的例子中,当然应该有4个向量。

他们不可能画十字。
矩阵总是包含类似直线的形状。

语言无关紧要,最好是Python代码。
我尝试使用numpy和scipy. space .distance库来创建解决方案,但失败了。

我应该使用哪些技术,或者我应该研究哪些库/函数?
谢谢。

我找到了一个解决方案。
scipy.ndimage.measurements.label就是把它们按标签分组。
scipy.spatial对于凸包和最远端对。

labeled_array, num_features = label(data)  # group and label
vectors = []
for i in range(num_features):
index = i + 1
indices = np.argwhere(labeled_array == index)  # find the group
if len(indices) < THRESHOLD:  # not an important line if under threshold
continue
candidates = indices[spatial.ConvexHull(indices).vertices]  # convex hull
dist_mat = spatial.distance_matrix(candidates, candidates)  # distance btw
points = np.unravel_index(dist_mat.argmax(), dist_mat.shape)  # furthest cands
vectors.append([candidates[points[0]], candidates[points[1]]])

您可以在我的存储库中找到完整的python文件。https://github.com/lotdcotw/ets2ap/blob/main/matrix2vector.py

最新更新