1
+ def find_linear_regression_line (points ):
2
+ # Separate points into X and y to fit LinearRegression model
3
+ points_x = [[point [0 ]] for point in points ]
4
+ points_y = [point [1 ] for point in points ]
5
+ print ("X points: " , points_x , "Length: " , len (points_x ))
6
+ print ("Y points: " , points_y , "Length: " , len (points_y ))
7
+
8
+ # Fit points to LinearRegression line
9
+ clf = LinearRegression ().fit (points_x , points_y )
10
+
11
+ # Get parameters from line
12
+ coef = clf .coef_ [0 ]
13
+ intercept = clf .intercept_
14
+ print ("Coefficients: " , coef , "Intercept: " , intercept )
15
+ return coef , intercept
16
+
17
+ def intersection_x (coef1 , intercept1 , coef2 , intercept2 ):
18
+ """Returns x-coordinate of intersection of two lines."""
19
+ x = (intercept2 - intercept1 )/ (coef1 - coef2 )
20
+ return x
21
+
22
+ def draw_linear_regression_line (coef1 , intercept1 , intersection_x , imshape = [540 ,960 ]):
23
+
24
+ # Get starting and ending points of regression line, ints.
25
+ point_one = (int (intersection_x ), int (intersection_x * coef1 + intercept1 ))
26
+ print ("Point one: " , point_one )
27
+ point_two = (imshape [1 ], int (imshape [1 ] * coef1 + intercept1 ))
28
+ print ("Point one: " , point_one , "Point two: " , point_two )
29
+ # Draw line using cv2.line
30
+ cv2 .line (img , point_one , point_two , color , thickness )
0 commit comments