-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrad_color_combo.py
98 lines (68 loc) · 2.25 KB
/
grad_color_combo.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import numpy as np
import cv2
#Compute Gradient in x or y direction depending on orient variable
def grad_xy (gray, orient = 1, thresh = ( 0 , 255) , ksize = 3 ):
if orient ==1 :
dx = 1
dy = 0
else:
dx = 0
dy = 1
#sobel
sobel = np.absolute(cv2.Sobel(gray,cv2.CV_64F,dx,dy,ksize= ksize))
#Rescale
sobel_scale = np.uint8((255*sobel)/np.max(sobel))
#Thresholding
binary_output = np.zeros_like(gray)
binary_output [(sobel_scale >= thresh[0]) & (sobel_scale <= thresh[1])] = 1
return binary_output
#Compute gradient magnitude
def grad_magn (gray, thresh = ( 0 , 255) , ksize = 3):
#Sobelx
sobelx = cv2.Sobel(gray,cv2.CV_64F,1,0)
#Sobely
sobely = cv2.Sobel(gray,cv2.CV_64F,0,1)
#Magintue
sobelxy = np.sqrt(sobelx**2+sobely**2)
#Rescale
sobelmagn = np.uint8((sobelxy*255)/np.max(sobelxy))
#Binary
binary_output = np.zeros_like(gray)
binary_output [ (sobelmagn >= thresh[0]) & (sobelmagn <= thresh[1])] =1
return binary_output
#Compute gradient direction
def grad_dir (gray, thresh = (0 , np.pi/2) , ksize = 11):
#sobel x
sobelx = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize)
#sobel y
sobely = cv2.Sobel(gray,cv2.CV_64F,0,1,ksize)
#Gradient Direction
gradDir = np.arctan2(np.absolute(sobely),np.absolute(sobelx))
#Binary output
binary_output = np.zeros_like(gray)
binary_output [ (gradDir >= thresh[0]) & (gradDir <= thresh[1])] =1
return binary_output
#Construct the combined graidents thresholding
def grad_combined (img):
#grayscale
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#Sobelx
sobelx = grad_xy (gray,1,(30,255),9)
#Sobely
sobely = grad_xy (gray,0,(30,255),9)
#Combined mask
combined = np.zeros_like(gray)
combined [ ((sobelx == 1) & (sobely == 1)) ] = 1
return combined
#HLS color space thresholding
def hlsThresh (img , thresh = (0,255)):
#Convert to hls color space
hls = cv2.cvtColor(img,cv2.COLOR_RGB2HLS)
#Select v channel
l = hls[:,:,1]
#Select S channel
s = hls[:,:,2]
#Create binary output image
hls_binary = np.zeros_like (s)
hls_binary [ ((s >= thresh[0]) & (s <= thresh[1])) & ((l>= 100) & (l<=255))] =1
return hls_binary