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

问题描述

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

256x128 matrix

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

他们没有机会交叉自己。
矩阵总是包含类似于直线的形状。

语言无关紧要,最好是 Python 代码
我尝试使用 numpy 和 scipy.spatial.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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...