Python OpenCV Get Angle Direction of fitLine

Viewed 1982

Using Python OpenCv, How does one find the Angle of fitLine through an element? I am trying to use debugger and locate, since I do not see it here in documentation ,

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 References:

Fitline Documentation

Open CV Tutorial

Not sure which items to acquire for slope in debugger

enter image description here

1 Answers

As you already have a unit vector that defines the direction of your line [vx, vy] from,

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

Then if you are looking for the angle between your fitted line & the x axis you can use the dot product to get the angle,

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)
Related