gpt4 book ai didi

python - 如何在Python中连接段?

转载 作者:行者123 更新时间:2023-12-01 06:42:43 25 4
gpt4 key购买 nike

这个问题涉及段的并集。

我有一个由 10* 4 个元素组成的数组 (a)(它们是线的末端:(x1,y1,x2,y2))。如果我绘制这 10 个线段(见图),实际上只有 2 条线(因为有些线相互重叠或长度较小)。

a = np.array([[40,33,54,4],[40,34,54,5], [41,34,55,5], [43,34,57,7], [18,85,34,52], [15,83,30,51], [16,84,33,52], [42,34,56,5], [16,83,32,51]])

为了处理这个问题,我正在考虑获取每条线的 x1 和 x2 位置,然后定义线段。与所提议的类似,可以按如下方式进行( finding unions of line segments on a number line )。

对于第一行 (x1=40, x2=54),我可以创建一个仅包含 0 和 1 的向量,其内容如下:00000....0000001111....111100。即 40 个零,然后是 15 个零,然后是 2 个零。长度为 57,因为这是最大的 x2 值。

我可以对所有行都这样进行。

假设三行:

00011100000000

00000000001111

00111111000000

结果是

00111111001111

然后我就会知道有两条长度分别为 6(从 2 开始)和 4(从 10 开始)的线。

Python 中有高效的方法吗?

为了创建 0 和 1 的数组,我正在做:

import numpy as np

def array_zero_ones(x1, x2, xmax):
# convert a segment into 0 and Ones
arr = np.zeros(x1,dtype=np.bool)
arr1 = np.ones((x2-x1+1),dtype=np.bool)
arr2= np.zeros((xmax-x2),dtype=np.bool)
arrall= np.concatenate([arr,arr1,arr2])
return arrall


a = np.array([[3,33,6,4],[4,40,7,70], [9,98,11,111]])
max=np.max(a[:,2])

sum_array_all = np.zeros(max+1,dtype=np.bool)

for i in range(len(a)):
sum_array_all = sum_array_all + array_zero_ones(a[i,0],a[i,2],max)

print(sum_array_all)

#still need to find all non 1 values in this array

不确定它是否有效......最好的,

W

enter image description here

最佳答案

您可以使用RANSAC来估计直线并从那里得到它。 RANSAC 是非常简单且强大的方法,尽管具有不确定性。除非您的图像容易出现噪声,否则它应该适合您的情况。有例子herewiki 上还有很棒的视觉和直观解释。 .

关于python - 如何在Python中连接段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59375628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com