-
Notifications
You must be signed in to change notification settings - Fork 17
/
Detect_Lines_day.py
71 lines (59 loc) · 3.08 KB
/
Detect_Lines_day.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
#================================================================================
# PROJECT : ASSISTED DRIVING BY ARTIFICIAL VISION *
# : (DETECTING LINES ON THE DAY) *
# VERSION : 1.0 *
# AUTHOR : Jonas Carrillo Sisalima [email protected] *
# PROFESSOR : Rodrigo Barba [email protected] *
# COMPANY : Sic ElectriTelecom Loja-Ecuador *
# DATE : 13/07/2015 *
#================================================================================
#Import packages needed
import numpy as np
import math
import cv2
#call the video source
cap = cv2.VideoCapture('videos/video2.mp4')
w1 = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) #Obtain video dimension x
h1 = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) #Obtain video dimension y
w=int(w1)
h=int(h1)
#Data for the region of interest
roiWid = w1
roiHig = h1
roiX = 0
roiY = int(h1/2)-220
#Process for lines on the road
def slope(vx1, vx2, vy1, vy2): #Parameters to calculate slope
m=float(vy2-vy1)/float(vx2-vx1) #Slope equation
theta1 = math.atan(m) #calculate the slope angle
return theta1*(180/np.pi) #Calculated angle in radians
while (cap.isOpened()):
ret,frame1 = cap.read()
frame = cv2.resize(frame1,(600,600)) #resize video source
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #converted to gray
equ = cv2.equalizeHist(gray) #Using histogram equalization function
video = cv2.cvtColor(equ,cv2.COLOR_GRAY2BGR)
cv2.rectangle(video, (roiX, roiY), (w,h ), (255, 0, 0),1)
hsv = cv2.cvtColor(video, cv2.COLOR_BGR2HSV) #separate the processed image intensity
grayROI = hsv[roiY:roiY+roiHig, roiX:roiX+roiWid] #ROI
low_white = np.array([0,0,170],dtype=np.uint8) #lower parameter for filtering the image hsv
high_white = np.array([255,255,255],dtype=np.uint8) #Upper parameter for filtering the image hsv
mask = cv2.inRange(grayROI,low_white,high_white) #mask image with declared parameters
#Length for lines to find
minLineLength = 150
maxLineGap = 40
lines = cv2.HoughLinesP(mask,1,np.pi/180,10,minLineLength,maxLineGap)
frameClone = frame.copy()
for x1,y1,x2,y2 in lines[0]:
if (round(x2-x1)!=0):
arctan = slope(x1,x2,y1,y2)
if (round(arctan>=round(-80)) and round(arctan<=round(-30))):
cv2.line(frameClone,(x1 + roiX,y1 + roiY),(x2 + roiX,y2 + roiY),(0,255,0),2)
if ( round(arctan>=round(30)) and round(arctan<=round(80))):
cv2.line(frameClone,(x1 + roiX,y1 + roiY),(x2 + roiX,y2 + roiY),(0,255,0),2)
#cv2.imshow('frame',mask)
cv2.imshow('DETECTING LINES',frameClone)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()