-
Notifications
You must be signed in to change notification settings - Fork 207
/
Line.py
56 lines (45 loc) · 1.25 KB
/
Line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pickle
# Define a class to receive the characteristics of each line detection
class Line():
def __init__(self, n):
"""
n is the window size of the moving average
"""
self.n = n
self.detected = False
# Polynomial coefficients: x = A*y^2 + B*y + C
# Each of A, B, C is a "list-queue" with max length n
self.A = []
self.B = []
self.C = []
# Average of above
self.A_avg = 0.
self.B_avg = 0.
self.C_avg = 0.
def get_fit(self):
return (self.A_avg, self.B_avg, self.C_avg)
def add_fit(self, fit_coeffs):
"""
Gets most recent line fit coefficients and updates internal smoothed coefficients
fit_coeffs is a 3-element list of 2nd-order polynomial coefficients
"""
# Coefficient queue full?
q_full = len(self.A) >= self.n
# Append line fit coefficients
self.A.append(fit_coeffs[0])
self.B.append(fit_coeffs[1])
self.C.append(fit_coeffs[2])
# Pop from index 0 if full
if q_full:
_ = self.A.pop(0)
_ = self.B.pop(0)
_ = self.C.pop(0)
# Simple average of line coefficients
self.A_avg = np.mean(self.A)
self.B_avg = np.mean(self.B)
self.C_avg = np.mean(self.C)
return (self.A_avg, self.B_avg, self.C_avg)