我正在使用Python + Numpy(也可以使用Scipy)并且有三个2D点
(P1, P2, P3);
Run Code Online (Sandbox Code Playgroud)
我试图让P3的距离垂直于P1和P2之间绘制的直线.让P1=(x1,y1),P2=(x2,y2)和P3=(x3,y3)
在矢量符号中,这将非常简单,但我对python/numpy相当新,并且无法获得任何有效(甚至关闭)的东西.
任何提示赞赏,谢谢!
我的任务是找到线(startX,startY,endX,endY)和矩形(4条线)的坐标。这是输入文件:
我使用下一个代码:
img = cv2.imread(image_src)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
edges = cv2.Canny(thresh1,50,150,apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,10,minLineLength,maxLineGap)
print(len(lines))
for line in lines:
cv2.line(img,(line[0][0],line[0][1]),(line[0][2],line[0][3]),(0,0,255),6)
Run Code Online (Sandbox Code Playgroud)
从上一张图像中,您可以看到大量的小红线。
问题:
我在两个 XY 点(p1 和 p2)和该线外的第三个 XY 点(p3)之间有一条线/向量。根据这篇文章,我知道如何获得该点到线的距离。但我实际上要寻找的是该线上的一个点 (p4),它与第三个点 (p3) 的距离 (d) 最小。我找到了这篇文章,但我觉得这不是正确的解决方案。也许 Numpy 或 Python 中包含了一些东西?
根据@allo,我尝试了以下操作。您可以将我的代码下载为Python 文件或Jupyter Notebook(都是 Python3)。
points = [[1, 1], [3, 1], [2.5, 2], [2.5, 1]]
import matplotlib.pyplot as plt
%matplotlib inline
fig, ax = plt.subplots()
fig.set_size_inches(6,6)
x, y = zip(*points[:2])
l1, = ax.plot(x,y, color='blue')
scatter1 = ax.scatter(x=x,y=y, color='blue', marker='x', s=80, alpha=1.0)
x, y = zip(*points[2:])
l2, = ax.plot(x,y, color='red')
scatter2 = ax.scatter(x=x,y=y, color='red', marker='x', s=80, alpha=1.0)
p1 …Run Code Online (Sandbox Code Playgroud)