Python OpenCV获取拟合线的角度方向

Python OpenCV Get Angle Direction of fitLine

使用Python OpenCv,如何找到通过元素的拟合线的角度? 我正在尝试使用调试器并定位,因为我在文档中没有看到它,

rows,cols = img.shape[:2]
[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
img = cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)

拟合线参考:

Fitline Documentation

Open CV Tutorial

不确定在调试器中为斜率获取哪些项目

因为你已经有一个单位向量来定义你的线的方向 [vx, vy] 来自,

[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01)

然后,如果您正在寻找拟合线与 x 轴之间的角度,您可以使用点积来获取角度,

import numpy as np

x_axis      = np.array([1, 0])    # unit vector in the same direction as the x axis
your_line   = np.array([vx, vy])  # unit vector in the same direction as your line
dot_product = np.dot(x_axis, your_line)
angle_2_x   = np.arccos(dot_product)