-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroad_segmentation.py
233 lines (200 loc) · 9.18 KB
/
road_segmentation.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#Python script to perform road segmentation
import cv2
import numpy as np
import glob
import imutils
from matplotlib import pyplot as plt
from statistics import mean
#Measure compute time
import timeit
#Utils
def display_image(winname,frame):
cv2.namedWindow(winname,cv2.WINDOW_NORMAL)
cv2.imshow(winname,frame)
key = cv2.waitKey(0)
if(key & 0xFF == ord('q')):
cv2.destroyAllWindows()
exit(0)
def breakpoint():
inp = input("Waiting for input...")
class road_seg_utils():
def __init__(self,img_path=None):
if(img_path!=None):
self.rgb_img = cv2.imread(img_path)
self.grayscale_img = cv2.imread(img_path,0)
else:
self.rgb_img = None
self.grayscale_img = None
def colour_segment(self,frame=None):
#TODO: Improve segmentation based on colour
#NOTE: Because of the tree canopy along the main road the colours are not consistent
# and exhibit a lot of variance
if(frame != None):
self.rgb_img = frame
self.grayscale_img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#Mask top half of the image
mask_img = np.zeros_like(self.rgb_img[:,:,0])
mask_img[int(self.rgb_img.shape[0]/2):int(self.rgb_img.shape[0]),:] = [255]
self.rgb_img = cv2.bitwise_and(self.rgb_img,self.rgb_img,mask=mask_img)
# display_image('mask_image',self.rgb_img)
rgb_color_seg = np.copy(self.rgb_img)
line_img = np.copy(self.rgb_img)
hsv_img = cv2.cvtColor(self.rgb_img,cv2.COLOR_BGR2HSV)
yuv_img = cv2.cvtColor(self.rgb_img,cv2.COLOR_BGR2YUV)
s_img = hsv_img[:,:,1]
# v_img = hsv_img[:,:,2]
# display_image("Hue Image",h_img)
# display_image("Sat Image",s_img)
# display_image("Value Image",v_img)
road_color_range_hsv = [(0,0,40),(180,50,255)]
# road_color_range_yuv = [(90,0,0),(140,255,255)]
road_mask_hsv = cv2.inRange(hsv_img,road_color_range_hsv[0],road_color_range_hsv[1])
# display_image("road_mask_hsv",road_mask_hsv)
# road_mask_yuv = cv2.inRange(yuv_img,road_color_range_yuv[0],road_color_range_yuv[1])
# display_image("road_mask_yuv",road_mask_yuv)
road_mask = road_mask_hsv
# display_image('road_mask:',road_mask)
# rgb_color_seg = cv2.bitwise_and(rgb_color_seg,rgb_color_seg,mask=road_mask)
# display_image("rgb_color_seg",rgb_color_seg)
edge_img = np.copy(road_mask)
edge_img = cv2.bilateralFilter(edge_img,15,75,75)
#Canny edge detection thresholds
sigma = 0.33
v = np.median(road_mask)
lower = int(max(0,(1.0-sigma)*v))
upper = int(min(255,(1.0+sigma)*v))
edge_img = cv2.Canny(edge_img,lower,upper)
display_image('Edge Image',edge_img)
#Hough Lines
votes = 0.25 * self.grayscale_img.shape[0]
lines = cv2.HoughLines(self.grayscale_img,1,1*(np.pi/180),int(votes/2))
left_lane_line = []
right_lane_line = []
#Draw lines on the image
if(lines.any() != None):
for line in lines:
rho,theta = line[0]
# print('rho,theta:',rho,theta)
if((abs(theta) >= 0.349) and (abs(theta) <= 1.22)):
#Left Lane: Find the average slope of all these lines
left_lane_line.append([rho,theta])
elif((abs(theta) >= 0.349 and abs(theta) <= 1.22) or (abs(theta) >= 1.92 and abs(theta) <= 2.79)):
right_lane_line.append([rho,theta])
else:
continue
# a = np.cos(theta)
# b = np.sin(theta)
# x0 = a*rho
# y0 = b*rho
# x1 = int(x0 + 1500*(-b))
# y1 = int(y0 + 1500*(a))
# x2 = int(x0 - 1500*(-b))
# y2 = int(y0 - 1500*(a))
# cv2.line(line_img,(x1,y1),(x2,y2),(0,0,255),2)
# display_image("line_img",line_img)
np_left_lane_line = np.asarray(left_lane_line)
np_right_lane_line = np.asarray(right_lane_line)
for i in range(0,2):
if(i==0 and np.size(np_left_lane_line) != 0):
rho,theta = np.mean(np_left_lane_line,axis=0)
elif(i==1 and np.size(np_right_lane_line) != 0):
rho,theta = np.mean(np_right_lane_line,axis=0)
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1500*(-b))
y1 = int(y0 + 1500*(a))
x2 = int(x0 - 1500*(-b))
y2 = int(y0 - 1500*(a))
cv2.line(line_img,(x1,y1),(x2,y2),(0,0,255),2)
display_image("Line Image",line_img)
#Gabor Filter
# ksize = 31
# for angle in np.arange(0,np.pi,np.pi/16):
# start = timeit.default_timer()
# kern = cv2.getGaborKernel((ksize,ksize),sigma = 4.0, theta = angle , lambd=10.0, gamma=0.5, psi=0, ktype=cv2.CV_32F)
# fimg = cv2.filter2D(self.rgb_img,cv2.CV_8UC3,kern)
# stop = timeit.default_timer()
# print('Time:',stop-start)
# breakpoint()
# display_image("fimg",fimg)
def test_1(self,frame=None):
if(np.size(frame) != 0):
self.rgb_img = frame
self.grayscale_img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# display_image('grayscale_img',self.grayscale_img)
mask_img = np.zeros_like(self.rgb_img[:,:,0])
mask_img[int(self.rgb_img.shape[0]/2):int(self.rgb_img.shape[0]),:] = [255]
edge_img = np.copy(self.grayscale_img)
self.rgb_img = cv2.bitwise_and(self.rgb_img,self.rgb_img,mask=mask_img)
line_img = np.copy(self.rgb_img)
self.grayscale_img = cv2.bitwise_and(self.grayscale_img,self.grayscale_img,mask=mask_img)
display_image("grayscale Image",self.grayscale_img)
#Edge detection
self.grayscale_img = cv2.bilateralFilter(self.grayscale_img,15,75,75)
display_image("Blurred Image",self.grayscale_img)
#Canny edge detection thresholds
sigma = 0.33
v = np.median(edge_img)
lower = int(max(0,(1.0-sigma)*v))
upper = int(min(255,(1+sigma)*v))
self.grayscale_img = cv2.Canny(self.grayscale_img,lower,upper)
display_image('Edge Image',self.grayscale_img)
#Hough Lines
#Min number of votes required
votes = 0.25 * self.grayscale_img.shape[0]
lines = cv2.HoughLines(self.grayscale_img,1,1*(np.pi/180),int(votes/2))
left_lane_line = []
right_lane_line = []
#Draw lines on the image
if(lines.any() != None):
for line in lines:
rho,theta = line[0]
print('rho,theta:',rho,theta)
if((abs(theta) >= 0.349) and (abs(theta) <= 1.22)):
#Left Lane: Find the average slope of all these lines
left_lane_line.append([rho,theta])
elif((abs(theta) >= 0.349 and abs(theta) <= 1.22) or (abs(theta) >= 1.92 and abs(theta) <= 2.79)):
right_lane_line.append([rho,theta])
# if((abs(theta) >= 0.349 and abs(theta) <= 1.22) or (abs(theta) >= 1.92 and abs(theta) <= 2.79)):
# a = np.cos(theta)
# b = np.sin(theta)
# x0 = a*rho
# y0 = b*rho
# x1 = int(x0 + 1500*(-b))
# y1 = int(y0 + 1500*(a))
# x2 = int(x0 - 1500*(-b))
# y2 = int(y0 - 1500*(a))
#Draw left and right lines on the image
np_left_lane_line = np.asarray(left_lane_line)
np_right_lane_line = np.asarray(right_lane_line)
for i in range(0,2):
if(i==0 and np.size(np_left_lane_line) != 0):
rho,theta = np.mean(np_left_lane_line,axis=0)
elif(i==1 and np.size(np_right_lane_line) != 0):
rho,theta = np.mean(np_right_lane_line,axis=0)
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1500*(-b))
y1 = int(y0 + 1500*(a))
x2 = int(x0 - 1500*(-b))
y2 = int(y0 - 1500*(a))
cv2.line(line_img,(x1,y1),(x2,y2),(0,0,255),2)
display_image("Line Image",line_img)
if __name__ == '__main__':
for img_path in glob.glob('/home/varghese/Transvahan/demo/autonomous_parking_vision/python_scripts/*.jpeg'):
# img_path = '/home/varghese/Transvahan/demo/autonomous_parking_vision/python_scripts/ZED_image825.jpeg'
print('img_path:',img_path)
road_seg_utils_obj = road_seg_utils(img_path)
road_seg_utils_obj.colour_segment()
#Testing
# road_seg_utils_obj = road_seg_utils()
# cap = cv2.VideoCapture('/home/varghese/Transvahan/demo/autonomous_parking_vision/data_ece_23rd_August/Varghese/second/out.avi')
# while(True):
# ret,frame = cap.read()
# road_seg_utils_obj.test_1(frame)
# cap.release()
# cv2.destroyAllWindows()