-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main
168 lines (119 loc) · 5.74 KB
/
Main
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
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from skimage import exposure
input_type = 'video' #'video' # 'image'
input_name = 'harder_challenge_video.mp4' #'test_images/straight_lines1.jpg' # 'challenge_video.mp4'
final=[]
left_line = Line()
right_line = Line()
th_sobelx, th_sobely, th_mag, th_dir = (35, 100), (30, 255), (30, 255), (0.7, 1.3)
th_h, th_l, th_s = (10, 100), (0, 60), (85, 255)
# camera matrix & distortion coefficient
mtx, dist = calib()
if __name__ == '__main__':
input_type == 'video'
cap = cv2.VideoCapture(input_name)
while (True):
success, frame = cap.read()
if(success):
# Correcting for Distortion
undist_img = undistort(frame, mtx, dist)
# resize video
undist_img = cv2.resize(undist_img, None, fx=1 / 2, fy=1 / 2, interpolation=cv2.INTER_AREA)
rows, cols = undist_img.shape[:2]
# gradient x + gradient y with sobel
combined_gradient = gradient_combine(undist_img, th_sobelx, th_sobely, th_mag, th_dir)
#cv2.imshow('gradient combined image', combined_gradient)
# combined of h, l , s
combined_hls = hls_combine(undist_img, th_h, th_l, th_s)
#cv2.imshow('HLS combined image', combined_hls)
# the combination of the two approaches
combined_result = comb_result(combined_gradient, combined_hls)
#cv2.imshow('combined image', combined_result)
# get the wrap image
c_rows, c_cols = combined_result.shape[:2]
print(c_rows)
print(c_cols)
s_LTop2, s_RTop2 = [c_cols / 2 - 24, 5], [c_cols / 2 + 24, 5]
s_LBot2, s_RBot2 = [110, c_rows], [c_cols - 110, c_rows]
src = np.float32([s_LBot2, s_LTop2, s_RTop2, s_RBot2])
dst = np.float32([(170, 720), (170, 0), (550, 0), (550, 720)])
cv2.imshow('nana', combined_result)
warp_img, M, Minv = warp_image(combined_result, src, dst, (720, 720))
#cv2.imshow('warp', warp_img)
# decide if line is detected or not
searching_img = find_LR_lines(warp_img, left_line, right_line)
#cv2.imshow('LR searching', searching_img)
# draw lane on prespective view
w_comb_result, w_color_result = draw_lane(searching_img, left_line, right_line)
#cv2.imshow('w_comb_result', w_comb_result)
# Drawing the lines back down onto the road after croping the above
color_result = cv2.warpPerspective(w_color_result, Minv, (c_cols, c_rows))
lane_color = np.zeros_like(undist_img)
lane_color[220:rows - 12, 0:cols] = color_result
# Combine the result with the original image
result = cv2.addWeighted(undist_img, 1, lane_color, 0.3, 0)
#cv2.imshow('result', result.astype(np.uint8))
info = np.zeros_like(result)
info[5:110, 5:190] = (255, 255, 255)
info = cv2.addWeighted(result, 1, info, 0.2, 0)
info = print_road_status(info, left_line, right_line)
#cv2.imshow('road info', info)
#out.write(frame)
combined_gradient = cv2.resize(combined_gradient,(0,0),None,0.7,4)
combined_hls = cv2.resize(combined_hls,(0,0),None,0.7,4)
combined_result = cv2.resize(combined_result,(0,0),None,0.7,4)
# warp_img = cv2.resize(warp_img,(0,0),None,0.5,0.5)
searching_img = cv2.resize(searching_img,(0,0),None,0.8,0.8)
w_comb_result = cv2.resize(w_comb_result,(0,0),None,0.8,0.8)
hoz1 = np.hstack((combined_gradient,combined_hls,combined_result))
hoz2 = np.hstack((searching_img,w_comb_result))
#,searching_img,w_comb_result
#vec = np.vstack(hoz,info)
cv2.imshow('gradient_combined_________combined_hls_________combined_result',hoz1)
cv2.imshow('lane_with_prespective______without lane______with lane',hoz2)
cv2.imshow('wrap',warp_img)
cv2.imshow('final_image',info)
#print(combined_gradient.shape,combined_hls.shape,combined_result.shape,warp_img.shape,searching_img.shape,w_comb_result.shape,info.shape)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.waitKey(0)
final.append(info)
else:
break
cap.release()
cv2.destroyAllWindows()
def write_image(path, img):
# img = img*(2**16-1)
# img = img.astype(np.uint16)
# img = img.astype(np.uint8)
img = cv2.convertScaleAbs(img, alpha=(255.0))
cv2.imwrite(path, img)
import os
if not os.path.exists("data5"):
os.makedirs('data5')
for i in range(len(final)):
name = './data5/frame'+ ""+str(i)+ '.jpg'
cv2.imwrite(name,final[i])
import cv2
import numpy as np
import glob
img_array = []
i = 0
#for filename in glob.glob("data5/frame"+ ""+str(i)+".jpg"):
for i in range(len(final)):
img = cv2.imread("data5/frame"+ ""+str(i)+".jpg")
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('project2.avi',cv2.VideoWriter_fourcc(*'DIVX'), 25 , size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
def img_show(img , title, cmap_type="gray" ):
plt.figure(figsize=(13,13))
plt.title(title)
plt.imshow(img,cmap_type)