import cv2 as cv
import numpy as np
width =640
height = 480
cap = cv.VideoCapture('lane_example.mp4')
last_data = [0,0]
def roi_(image,color3=(255,255,255), color1=255):
mask = np.zeros_like(image)
if len(image.shape)>2:
color = color3
else:
color = color1
vertices = np.array([[(0, height), (0, height*2//3), (width//3, height//2), (width*2//3, height//2),
(width, height*2//3+30), (width, height)]], dtype=np.int32)
#vertices = np.array([[(0, height), (0, height*0.5), (width, height*0.5), (width, height)]], dtype=np.int32)
cv.fillPoly(mask, vertices, color)
ROI_image = cv.bitwise_and(image, mask)
return ROI_image
def gray_(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
return gray
def blur_(image):
kernel_size = 5
blur = cv.GaussianBlur(image, (kernel_size, kernel_size), 0)
return blur
def canny_(image):
low_threshold = 50
high_threshold = 150
canny = cv.Canny(np.uint8(image), low_threshold, high_threshold)
return canny
#def draw_lines(image, lines, color = [0,0,255], thickness =5):
#line_image = np.zeros_like()
def display_lines(image, lines, color = [0, 0, 255], thickness = 5):
line_image = np.zeros_like(image)
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
cv.line(line_image, (x1, y1), (x2, y2), color, thickness)
return line_image
def houghlineP(image):
minLineLength = 50
maxLineGap = 10
hough = cv.HoughLinesP(image, 1 ,np.pi/180, 50, None, minLineLength, maxLineGap)
#line_img = np.zeros((image.shape[0], image.shape[1], 3), dtype = np.uint8)
#draw_lines(line_img, hough)
#return line_img
return hough
def make_coordinates(image, line_parameters):
slope, intercept = line_parameters
y1 = int(image.shape[0])
y2 = int(y1*3/5)
x1= int((y1-intercept)/(slope))
x2 = int((y2 - intercept)/(slope)) #?
return np.array([x1, y1, x2, y2])
def average_slope_intercept(image, lines):
left_fit = []
right_fit = []
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
if ((x1 or x2) < width // 4 and (y1 or y2) > height*3//4) or ((x1 or x2) > width // 4 *3 and (y1 or y2) > height*3//4) or (
(x1+y1) > width +height * 2 // 3 + 1) or ((x2 + y2) > width + height * 2 // 3 + 1):
parameters = np.polyfit((x1, x2), (y1, y2), 1)
slope = parameters[0] #gi-ul-gi
intercept = parameters[1] #y-julpyeon
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
else:
parameters = np.polyfit((x1, x2), (y1, y2), 1)
slope = parameters[0] #gi-ul-gi
intercept = parameters[1] #y-julpyeon
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
left_fit_average = np.average(left_fit, axis = 0)
right_fit_average = np.average(right_fit, axis = 0)
left_line = make_coordinates(image, left_fit_average)
right_line = make_coordinates(image, right_fit_average)
return np.array([left_line, right_line])
#main
while (cap.isOpened()):
ret, image = cap.read()
#roi = roi_(image)
# cv.imshow('ROI image', roi)
gray = gray_(image)
cv.imshow('GRAY image', gray)
blur = blur_(gray)
cv.imshow('Blured image', blur)
canny = canny_(blur)
cv.imshow('Canny edge image', canny)
roi = roi_(canny)
cv.imshow('roi image', roi)
hough = houghlineP(canny)
line_image = display_lines(image, hough)
cv.imshow('HoughLineP image', line_image)
averaged_lines = average_slope_intercept(image, hough)
line_image = display_lines(image, averaged_lines)
combo_image = cv.addWeighted(image, 0.8, line_image, 1, 1)
cv.imshow('RESULT', combo_image)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()