如何解决从二维二进制矩阵中提取直线
我想从二维二进制矩阵中提取直线。
我会找到按邻居坐标分组的 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。