在Python中使用最小二乘法找到多条线的中心 [英] Finding the centre of multiple lines using least squares approach in Python
问题描述
我有一系列的线在某个点上大致(但不完全)相交.
我需要找到使中心每条线之间的距离最小的点.我一直在尝试遵循这种方法:
当我在Python中创建脚本以执行此功能时,我得到了错误的答案:
这是我的代码,我想知道是否有人可以建议我做错了什么?或更简单的方法.每行由两个点x1和x2定义.
def directionalv(x1,x2):
point1=np.array(x1) #point1 and point2 define my line
point2=np.array(x2)
ortho= np.array([[0,-1],[1,0]]) #see wikipedia article
subtract=point2-point1
length=np.linalg.norm(subtract)
fraction = np.divide(subtract,length)
n1=ortho.dot(fraction)
num1=n1.dot(n1.transpose())
num = num1*(point1)
denom=n1.dot(n1.transpose())
return [num,denom]
n1l1=directionalv(x1,x2)
n1l2=directionalv(x3,x4)
n1l3=directionalv(x5,x6)
n1l4=directionalv(x7,x8)
n1l5=directionalv(x9,x10)
numerall=n1l1[0]+n1l2[0]+n1l3[0]+n1l4[0]+n1l5[0] #sum of (n.n^t)pi from wikipedia article
denomall=n1l1[1]+n1l2[1]+n1l3[1]+n1l4[1]+n1l5[1] #sum of n.n^t
point=(numerall/denomall)
我的观点如下 Line1由点x1 = [615,396]和x2 = [616,880]组成
第2行,x3 = [799,449] x4 = [449,799]
第3行,x5 = [396,637] x6 = [880,636]
第4行,x7 = [618,396] x8 = [618,880]
第5行,x9 = [483,456] x10 = [777,875]
任何帮助将不胜感激!
谢谢您的时间.
难道仅仅是您应该在Python中将矩阵定义为2个向量的事实吗?(理解是矩阵的一列,而不是行!
请参阅:如何在python中定义二维数组) ,则应定义如下的ortho
矩阵:
ortho= np.array([[0,1],[-1,0]])
否则,以下含义是什么?
numerall=n1l1[0]+n1l2[0]+n1l3[0]+n1l4[0]+n1l5[0] #sum of (n.n^t)pi from wikipedia article
denomall=n1l1[1]+n1l2[1]+n1l3[1]+n1l4[1]+n1l5[1] #sum of n.n^t
point=(numerall/denomall)
我不理解您对矩阵转置的解释;矩阵的逆不等于除法.
使用现有的Python库(例如Numpy)来进行计算,而不是自己实现.请参阅: https://docs.scipy. org/doc/numpy-1.10.4/reference/generation/numpy.matrix.html
I have a series of lines which roughly (but not exactly) intersect at some point.
I need to find the point which minimises the distance between each line in the centre. I have been trying to follow this methodology:
Nearest point to intersecting lines in 2D
When I create my script in Python to perform this function I get the incorrect answer:
Here is my code, I was wondering if anyone could suggest what I am doing wrong? Or an easier way of going about this. Each line is defined by two points x1 and x2.
def directionalv(x1,x2):
point1=np.array(x1) #point1 and point2 define my line
point2=np.array(x2)
ortho= np.array([[0,-1],[1,0]]) #see wikipedia article
subtract=point2-point1
length=np.linalg.norm(subtract)
fraction = np.divide(subtract,length)
n1=ortho.dot(fraction)
num1=n1.dot(n1.transpose())
num = num1*(point1)
denom=n1.dot(n1.transpose())
return [num,denom]
n1l1=directionalv(x1,x2)
n1l2=directionalv(x3,x4)
n1l3=directionalv(x5,x6)
n1l4=directionalv(x7,x8)
n1l5=directionalv(x9,x10)
numerall=n1l1[0]+n1l2[0]+n1l3[0]+n1l4[0]+n1l5[0] #sum of (n.n^t)pi from wikipedia article
denomall=n1l1[1]+n1l2[1]+n1l3[1]+n1l4[1]+n1l5[1] #sum of n.n^t
point=(numerall/denomall)
My points are as follows Line1 consists of points x1= [615, 396] and x2 = [616, 880]
Line 2, x3 = [799, 449] x4= [449, 799]
Line 3, x5 = [396, 637] x6 = [880, 636]
Line 4, x7 = [618, 396] x8 = [618, 880]
Line 5, x9 = [483, 456] x10 = [777, 875]
Any help would be really appreciated!
Thank you for your time.
Could it simply be the fact that you should define in Python the matrix as 2 vectors (understand is a column of the matrix, not row!
see: How to define two-dimensional array in python ), you'll then should define the ortho
matrix like this:
ortho= np.array([[0,1],[-1,0]])
Otherwise, what does the following means?
numerall=n1l1[0]+n1l2[0]+n1l3[0]+n1l4[0]+n1l5[0] #sum of (n.n^t)pi from wikipedia article
denomall=n1l1[1]+n1l2[1]+n1l3[1]+n1l4[1]+n1l5[1] #sum of n.n^t
point=(numerall/denomall)
I do not understand your interpretation of the transposition of a Matrix; and the inverse of a matrix does not equals to a division.
Use an existing Python library like Numpy to do the computing instead of implementing it yourself. See: https://docs.scipy.org/doc/numpy-1.10.4/reference/generated/numpy.matrix.html
这篇关于在Python中使用最小二乘法找到多条线的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!