Skip to content
Snippets Groups Projects
Commit c9a27b75 authored by Leendert Hendricus Pruissen's avatar Leendert Hendricus Pruissen
Browse files

Added center line

parent e6ca9fe4
No related branches found
No related tags found
No related merge requests found
#/!usr/bin/python
#!/usr/bin/python
import numpy as np
#import rospy
......@@ -35,7 +35,7 @@ def detect_edge(pic):
# pic: original image to apply the pre-set region of interest too
def ROI(pic):
height = pic.shape[0]
triangle = np.array([[(100, height), (600, height), (350, 100)]])
triangle = np.array([[(250, height), (1100, height), (550, 250)]])
mask = np.zeros_like(pic)
cv2.fillPoly(mask, triangle, 255)
roi = cv2.bitwise_and(pic, mask)
......@@ -46,7 +46,7 @@ def ROI(pic):
# pic: original image to apply the pre-set region of interest too
def ROI_real(pic):
height = pic.shape[0]
triangle = np.array([[(0, height), (620, height), (430, 250), (150, 250)]])
triangle = np.array([[(0, height), (145, 300), (475, 300), (600, height)]], dtype=np.int32)
mask = np.zeros_like(pic)
cv2.fillPoly(mask, triangle, 255)
roi = cv2.bitwise_and(pic, mask)
......@@ -56,7 +56,7 @@ def ROI_real(pic):
# params
# edge_pic: the image with the edge detection performed
def getLines(edge_pic):
return cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 100, maxLineGap=80, minLineLength=20)
return cv2.HoughLinesP(edge_pic, 1, np.pi / 180, 50, maxLineGap=80, minLineLength=10)
# Apply the passed in lines the the original picture
# params
......@@ -82,6 +82,35 @@ def applyLines(original_pic, lines):
return original_pic
def find_middle(leftPoints, rightPoints):
middle_lines = [[],[]]
if(leftPoints[1] is None or rightPoints[1] is None
):
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Caught the empty list~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
return 0
else:
print(leftPoints[1])
print(rightPoints[1])
for x in range(150):
#print("Right x point: " + str(rightPoints[0][x]))
#print("Left x point: " + str(leftPoints[0][x]))
#print("Right Y point: " + str(rightPoints[1][x]))
#print("Left Y point: " + str(leftPoints[1][x]))
midPoint = (rightPoints[0][149-x] + leftPoints[0][x]) / 2
#print("midPoint X: " + str(midPoint))
#print("midPoint Y: " + str(leftPoints[1][x]))
middle_lines[1].append(leftPoints[1][x])
middle_lines[0].append(midPoint)
return middle_lines
# Find the two average lines given the set of lines
# params
# pic: the original image
......@@ -168,13 +197,13 @@ def find_average_lane(pic, lines):
for line in lines:
x1, y1, x2, y2 = line[0]
parameters = np.polyfit((x1, x2), (y1, y2), 1)
print("Slope, intercept")
print(parameters)
#print("Slope, intercept")
#print(parameters)
slope = parameters[0]
intercept = parameters[1]
if(slope < 0):
print("Left insert")
#print("Left insert")
left_lines.append((slope, intercept))
left_lines_points[0].append(x1)
left_lines_points[0].append(x2)
......@@ -182,7 +211,7 @@ def find_average_lane(pic, lines):
left_lines_points[1].append(y2)
else:
print("Right insert")
#print("Right insert")
right_lines.append((slope, intercept))
right_lines_points[0].append(x1)
right_lines_points[0].append(x2)
......@@ -190,34 +219,34 @@ def find_average_lane(pic, lines):
right_lines_points[1].append(y2)
if not left_lines:
print("Left is empty")
#print("Left is empty")
left_line = [0, 0, 0, 0]
else:
left_average = np.average(left_lines, axis=0)
left_line = make_coordinates(pic, left_average)
print("Left Line: ")
print(left_line)
#print("Left Line: ")
#print(left_line)
if not right_lines:
print("Right is emtpy")
#print("Right is emtpy")
right_line = [0, 0, 0, 0]
else:
right_average = np.average(right_lines, axis=0)
right_line = make_coordinates(pic, right_average)
print("Right line : ")
print(right_line)
#print("Right line : ")
#print(right_line)
print("Left fit")
print(left_line)
#print("Left fit")
#print(left_line)
print("\nRight fit")
print(right_line)
#print("\nRight fit")
#print(right_line)
return np.array([left_line, right_line])
def make_coordinates(image, line_parameters):
print(line_parameters)
#print(line_parameters)
slope, intercept = line_parameters
y1 = image.shape[0]
y2 = int(y1*(1/2))
......@@ -257,27 +286,29 @@ def detectDeparture(left, car, right):
#video = cv2.VideoCapture("test2.mp4")
video = cv2.VideoCapture("test2.mp4")
#video = cv2.VideoCapture("highway.mp4")
video = cv2.VideoCapture(1)
#video = cv2.VideoCapture(1)
plt.ion()
while True:
ret, frame = video.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# frame = cv2.imread("../frame.jpeg")
frame_edge = detect_edge(frame)
if not ret:
video = cv2.VideoCapture(0)
video = cv2.VideoCapture(1)
continue
new_img = formatImg(frame)
wEdges = detect_edge(new_img)
cropped = ROI_real(wEdges)
#cropped = ROI_real(wEdges)
cropped = ROI(wEdges)
lines = getLines(cropped)
......@@ -287,14 +318,18 @@ while True:
else:
Rpoints, Lpoints = find_poly_lane(new_img, lines)
Mpoints = find_middle(Lpoints, Rpoints)
#(type(Rpoints[0][0]))
plt.cla()
plt.clf()
plt.scatter(Rpoints[0], Rpoints[1])
plt.scatter(Lpoints[0], Lpoints[1])
plt.scatter(Mpoints[0], Mpoints[1])
plt.scatter(310, 300)
#plt.scatter(310, 300)
plt.imshow(frame, zorder=0)
......@@ -305,9 +340,9 @@ while True:
lane = applyLines(frame, lines)
cv2.imshow("edges", wEdges)
cv2.imshow("cropped", cropped)
cv2.imshow("frame", lane)
#cv2.imshow("edges", wEdges)
#cv2.imshow("cropped", cropped)
#cv2.imshow("frame", lane)
key = cv2.waitKey(25)
if key == 27:
......@@ -315,27 +350,3 @@ while True:
video.release()
cv2.destroyAllWindows()
'''
# Load image
img = cv2.imread('lol_image.jpg')
new_img = formatImg(img)
wEdges = detect_edge(new_img)
cropped = ROI(wEdges)
lines = getLines(cropped)
#lanes = applyLines(img, lines)
average_lines = find_average_lane(img, lines)
average_lanes = applyLines(img, average_lines)
cv2.imshow("edges", wEdges)
#cv2.imshow("with lines", lanes)
cv2.imshow("Average", average_lanes)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment