首页 > 解决方案 > 在OpenCV中获得两点之间的直线方程

问题描述

我在图像中有两个点,比如说 (30, 220) 和 (1385, 1050)。我想找到通过这两点的方程。我可以在 python 中与 OpenCV 的库划清界限。

cv.line(frame, (30, 220),  (1385, 1050), (0, 255, 0), thickness=3, lineType=8)

PS。 我的最终目标是计算一个点和那条线之间的最短距离。

标签: pythonimageopencv3.0

解决方案


在这里我如何找到它:

from numpy import ones,vstack
from numpy.linalg import lstsq
import math

points = [(30, 220),(1385, 1050)]
x_coords, y_coords = zip(*points)
A = vstack([x_coords,ones(len(x_coords))]).T
m, c = lstsq(A, y_coords)[0]
print("Line Solution is y = {m}x + {c}".format(m=m,c=c))


推荐阅读