-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_image.py
More file actions
107 lines (89 loc) · 3.69 KB
/
all_image.py
File metadata and controls
107 lines (89 loc) · 3.69 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
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
import numpy as np
import matplotlib.pyplot as plt
import cv2
#vid =cv2.VideoCapture('test_videos/challenge.mp4')
#vid =cv2.VideoCapture('test_videos/solidWhiteRight.mp4')
cv2.namedWindow('Road View')
#RGB Threshold Parameters
red_val=0
green_val=200
blue_val=200
#HSV Threshold Parameters
Hue=41
Sat=96
Val=41
#Gaussian Blur Parameters
kernel_size=5
low_threshold=50
high_threshold=150
# Hough transform parameters
rho_ = 8
theta_ = (np.pi/180)
threshold_ = 90
min_line_length =1
max_line_gap =250
pic_set=['test_images/solidWhiteCurve.jpg','test_images/solidWhiteRight.jpg','test_images/solidYellowCurve.jpg',
'test_images/solidYellowCurve2.jpg','test_images/solidYellowLeft.jpg','test_images/whiteCarLaneSwitch.jpg']
for i in range(0,6):
pic = cv2.imread(pic_set[i])#load image
line_image= np.copy(pic)*0 #creating a blank to draw lines on
#image assets
vid_img=np.copy(pic)
y_size=vid_img.shape[0]
x_size=vid_img.shape[1]
apex=[490,315]
image_bottomleft=[0,y_size]
image_bottomright=[x_size,y_size]
rm=np.array([apex,image_bottomleft,image_bottomright],np.int32)
rm=rm.reshape((-1,1,2))
##################################################################
############ RGB Color Based######################################
##################################################################
rgb_threshold=[red_val,green_val,blue_val]
pic_m = cv2.fillPoly(pic, [rm], (0, 0, 0))
pic_m = cv2.bitwise_and(~pic, vid_img)
hsv_img=np.copy(pic_m)#hasv image copy from masked image
threshold=(pic_m[:,:,0]<rgb_threshold[0])|(pic_m[:,:,1]<rgb_threshold[1])|(pic_m[:,:,2]<rgb_threshold[2])
pic_m[threshold]=[0,0,0]
pic_m_gray=cv2.cvtColor(pic_m,cv2.COLOR_RGB2GRAY)
pic_blur=cv2.GaussianBlur(pic_m_gray,(kernel_size,kernel_size),0)
pic_edge=cv2.Canny(pic_blur,low_threshold,high_threshold)
# Run 1Hough on edge detected image
lines = cv2.HoughLinesP(pic_edge, rho_, theta_, threshold_,np.array([]),min_line_length,max_line_gap)
try:
# Iterate over the output "lines" and draw lines on the blank
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 3)
except:
pass
##################################################################
############ HSV Color Based######################################
##################################################################
hsv_threshold=[Hue,Sat,Val]
hsv_img=cv2.cvtColor(hsv_img,cv2.COLOR_RGB2HSV)
threshold_1=(hsv_img[:,:,0]<hsv_threshold[0])|(hsv_img[:,:,1]<hsv_threshold[1])|(hsv_img[:,:,2]<hsv_threshold[2])
hsv_img[threshold_1]=[0,0,0]
hsv_img=cv2.cvtColor(hsv_img,cv2.COLOR_HSV2RGB)
hsv_gray=cv2.cvtColor(hsv_img,cv2.COLOR_RGB2GRAY)
hsv_blur=cv2.GaussianBlur(hsv_gray,(kernel_size,kernel_size),0)
hsv_edge=cv2.Canny(hsv_blur,low_threshold,high_threshold)
# Run 1Hough on edge detected image
hsv_lines = cv2.HoughLinesP(hsv_edge, rho_, theta_, threshold_,np.array([]),min_line_length,max_line_gap)
try:
# Iterate over the output "lines" and draw lines on the blank
#p1=hsv_lines[0, 0, 0:2]
#p2=hsv_lines[0, 0, 2:4]
#print "(X1,Y1):",p1
#print "(X2,Y2):",p2
#print "slop:", (p2[1]-p1[1])/(p2[0]-p1[0])
for line in hsv_lines:
for x1, y1, x2, y2 in line:
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 3)
except:
pass
# Draw the lines on the edge image
combo=cv2.addWeighted(vid_img,0.8,line_image,1,0)#draw detection lines on orignal image
while cv2.waitKey(1)==-1:
cv2.imshow('Road View',combo)
cv2.destroyWindow('Road View')