-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinefilter.py
More file actions
61 lines (53 loc) · 2.11 KB
/
linefilter.py
File metadata and controls
61 lines (53 loc) · 2.11 KB
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
55
56
57
58
59
60
61
from utils import roi, scale_abs
import numpy as np
import cv2
class LineFilter:
def __init__(self, p):
self.sat_thresh = p['sat_thresh']
self.light_thresh = p['light_thresh']
self.light_thresh_agr = p['light_thresh_agr']
self.grad_min, self.grad_max = p['grad_thresh']
self.mag_thresh, self.x_thresh = p['mag_thresh'], p['x_thresh']
self.hls, self.l, self.s, self.z = None, None, None, None
self.color_cond1, self.color_cond2 = None, None
self.sobel_cond1, self.sobel_cond2, self.sobel_cond3 = None, None, None
def sobel_breakdown(self, img):
self.apply(img)
b1, b2, b3 = self.z.copy(), self.z.copy(), self.z.copy()
b1[(self.sobel_cond1)] = 255
b2[(self.sobel_cond2)] = 255
b3[(self.sobel_cond3)] = 255
return np.dstack((b1, b2,b3))
def color_breakdown(self, img):
self.apply(img)
b1, b2 = self.z.copy(), self.z.copy()
b1[(self.color_cond1)] = 255
b2[(self.color_cond2)] = 255
return np.dstack((b1, b2, self.z))
def apply(self, rgb_image):
self.hls = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2HLS)
self.l = self.hls[:, :, 1]
self.s = self.hls[:, :, 2]
self.z = np.zeros_like(self.s)
color_img = self.apply_color_mask()
sobel_img = self.apply_sobel_mask()
filtered_img = cv2.bitwise_or(sobel_img, color_img)
return filtered_img
def apply_color_mask(self):
self.color_cond1 = (self.s > self.sat_thresh) & (self.l > self.light_thresh)
self.color_cond2 = self.l > self.light_thresh_agr
b = self.z.copy()
b[(self.color_cond1 | self.color_cond2)] = 1
return b
def apply_sobel_mask(self):
lx = cv2.Sobel(self.l, cv2.CV_64F, 1, 0, ksize = 5)
ly = cv2.Sobel(self.l, cv2.CV_64F, 0, 1, ksize = 5)
gradl = np.arctan2(np.absolute(ly), np.absolute(lx))
l_mag = np.sqrt(lx**2 + ly**2)
slm, slx, sly = scale_abs(l_mag), scale_abs(lx), scale_abs(ly)
b = self.z.copy()
self.sobel_cond1 = slm > self.mag_thresh
self.sobel_cond2 = slx > self.x_thresh
self.sobel_cond3 = (gradl > self.grad_min) & (gradl < self.grad_max)
b[(self.sobel_cond1 & self.sobel_cond2 & self.sobel_cond3)] = 1
return b