0

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

256x128 矩阵

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

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

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

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

4

1 回答 1

0

我找到了解决方案。
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

于 2021-02-25T00:23:13.220 回答