forked from ndrplz/self-driving-car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line.py
37 lines (28 loc) · 1 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
import numpy as np
import cv2
class Line:
"""
A Line is defined from two points (x1, y1) and (x2, y2) as follows:
y - y1 = (y2 - y1) / (x2 - x1) * (x - x1)
Each line has its own slope and intercept (bias).
"""
def __init__(self, x1, y1, x2, y2):
self.x1 = np.float32(x1)
self.y1 = np.float32(y1)
self.x2 = np.float32(x2)
self.y2 = np.float32(y2)
self.slope = self.compute_slope()
self.bias = self.compute_bias()
def compute_slope(self):
return (self.y2 - self.y1) / (self.x2 - self.x1 + np.finfo(float).eps)
def compute_bias(self):
return self.y1 - self.slope * self.x1
def get_coords(self):
return np.array([self.x1, self.y1, self.x2, self.y2])
def set_coords(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def draw(self, img, color=[255, 0, 0], thickness=10):
cv2.line(img, (self.x1, self.y1), (self.x2, self.y2), color, thickness)