Skip to content

Commit

Permalink
Remove Canny, Rewrote Lane Detection, Changed IPM
Browse files Browse the repository at this point in the history
  • Loading branch information
zappybiby committed Apr 22, 2018
1 parent 97d90d6 commit e1beb01
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
EuroTruckAutopilot
https://www.youtube.com/watch?v=_s00JKtwZUc
95 changes: 54 additions & 41 deletions hough_lines.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import cv2
import numpy as np

Expand All @@ -6,44 +8,55 @@


def hough_lines(warped, original):
linesP = cv2.HoughLinesP(warped, 1, np.pi / 180, 50, None, 25, 150)

if linesP is not None:
for i in range(0, len(linesP)):
#print("# of lines:", len(linesP))

l = linesP[i][0]
x1 = l[0]
y1 = l[1]
x2 = l[2]
y2 = l[3]

slope = (y2 - y1) / (x2 - x1)

if slope < 0:
for x1, y1, x2, y2 in linesP[i]:
if (np.isnan(x1) == False) and (np.isnan(y1) == False) and (np.isnan(x2) == False) and (
np.isnan(y2) == False):
if ((x2 - x1) != 0) and ((y2 - y1) != 0):
left_avg_slope = (y2 - y1) / (x2 - x1)
left_avg_intercept = y1 - left_avg_slope * x1
left_startx = int((starty - left_avg_intercept) / left_avg_slope)
left_endx = int((endy - left_avg_intercept) / left_avg_slope)
cv2.line(original, (left_startx, starty), (left_endx, endy), (0, 0, 255), 3)

if slope > 0:
for x1, y1, x2, y2 in linesP[i]:
if (np.isnan(x1) == False) and (np.isnan(y1) == False) and (np.isnan(x2) == False) and (
np.isnan(y2) == False):
if ((x2 - x1) != 0) and ((y2 - y1) != 0):
right_avg_slope = (y2 - y1) / (x2 - x1)
right_avg_intercept = y1 - right_avg_slope * x1
right_startx = int((starty - right_avg_intercept) / right_avg_slope)
right_endx = int((endy - right_avg_intercept) / right_avg_slope)
cv2.line(original, (right_startx, starty), (right_endx, endy), (255, 0, 0), 3)

cv2.namedWindow("Test2", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Test2", 500, 500)
cv2.moveWindow("Test2", -940, 0)
cv2.imshow("Test2", original)
cv2.waitKey(1)
lines_p = cv2.HoughLinesP(warped, 1, np.pi / 180, 50, None, 50, 300)
if lines_p is not None:
length = len(lines_p)
extrapolated_lines = []
start_time = datetime.datetime.now().time().microsecond
for i in range(0, length):
for j in range(0, length):
if i != j:
line_1 = lines_p[i][0]
line_2 = lines_p[j][0]

if False not in (line_1.all(), line_2.all()):
line_1_x1 = line_1[0]
line_1_y1 = line_1[1]
line_1_x2 = line_1[2]
line_1_y2 = line_1[3]

line_2_x1 = line_2[0]
line_2_y1 = line_2[1]
line_2_x2 = line_2[2]
line_2_y2 = line_2[3]

line_x_distance = np.sqrt(np.square(line_2_x1 - line_1_x1) + np.square(line_2_x2 - line_1_x2))
line_y_length = np.abs((line_2_y1 + line_1_y1) / 2 - (line_2_y2 + line_1_y2) / 2)
distance = (np.sqrt(np.square(line_2_x1 - line_1_x1) + np.square(line_2_y1 - line_1_y1)) +
np.sqrt(np.square(line_2_x2 - line_1_x2) + np.square(line_2_y2 - line_1_y2))) / 2

if line_x_distance < 20 and line_y_length > 40 and 10 < distance <= 100:
line_3_x1 = (line_2_x1 + line_1_x1) / 2
if line_3_x1 not in extrapolated_lines:
line_3_y1 = (line_2_y1 + line_1_y1) / 2
line_3_x2 = (line_2_x2 + line_1_x2) / 2
line_3_y2 = (line_2_y2 + line_1_y2) / 2

if 0 not in (
line_3_x1, line_3_x2, line_3_y1, line_3_y2,
np.abs(line_3_x1) - np.abs(line_3_x2),
np.abs(line_3_y1) - np.abs(line_3_y2)):
line_3_avg_slope = (line_3_y2 - line_3_y1) / (line_3_x2 - line_3_x1)
line_3_avg_intercept = line_3_y1 - line_3_avg_slope * line_3_x1
line_3_startx = int((starty - line_3_avg_intercept) / line_3_avg_slope)
right_endx = int((endy - line_3_avg_intercept) / line_3_avg_slope)
extrapolated_lines.append(line_3_x1)
cv2.line(original, (line_3_startx, starty), (right_endx, endy), (0, 0, 255), 3)

print("# of lines:", length, "found:", len(extrapolated_lines),
datetime.datetime.now().time().microsecond - start_time)
cv2.namedWindow("Test2", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Test2", 450, 500)
cv2.moveWindow("Test2", -500, 300)
cv2.imshow("Test2", original)
cv2.waitKey(1)
23 changes: 13 additions & 10 deletions image_process.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import cv2
import numpy as np

import hough_lines


def pre_process(warped, original):
blur = cv2.GaussianBlur(warped, (15, 15), 0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
adaptive_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, -3)
merge = cv2.morphologyEx(adaptive_thresh, cv2.MORPH_CLOSE, np.ones((9, 9), np.uint8))

median = np.median(merge)
lower = int(max(0, (1.0 - 0.1) * median))
upper = int(min(255, (1.0 + 0.1) * median))
canny = cv2.Canny(merge, lower, upper)
# median = np.median(merge)
# lower = int(max(0, (1.0 - 0.33) * median))
# upper = int(min(255, (1.0 + 0.33) * median))
# canny = cv2.Canny(merge, lower, upper)
# merge = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, np.ones((21, 21), np.uint8))

cv2.namedWindow("adaptiveThreshold", cv2.WINDOW_NORMAL)
cv2.resizeWindow("adaptiveThreshold", 450, 500)
cv2.moveWindow("adaptiveThreshold", -1380, 300)
cv2.imshow("adaptiveThreshold", adaptive_thresh)
cv2.waitKey(1)

cv2.namedWindow("canny", cv2.WINDOW_NORMAL)
cv2.resizeWindow("canny", 450, 500)
cv2.moveWindow("canny", -540, 300)
cv2.imshow("canny", canny)
cv2.waitKey(1)
# cv2.namedWindow("canny", cv2.WINDOW_NORMAL)
# cv2.resizeWindow("canny", 450, 500)
# cv2.moveWindow("canny", -540, 300)
# cv2.imshow("canny", canny)
# cv2.waitKey(1)

cv2.namedWindow("merge", cv2.WINDOW_NORMAL)
cv2.resizeWindow("merge", 450, 500)
cv2.moveWindow("merge", -940, 300)
cv2.imshow("merge", merge)
cv2.waitKey(1)

#hough_lines.hough_lines(canny, original)
hough_lines.hough_lines(merge, original)
10 changes: 5 additions & 5 deletions perspective_transform.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import cv2
import numpy as np
import keyboard
import numpy as np

import image_process

a = 920
b = -160
c = 70
a = 20
b = 40
c = 60
d = -70


Expand Down Expand Up @@ -53,7 +54,6 @@ def perspective_transform(window):
d -= 10
print("d:", d)


src = np.float32(
[[(-a), height - b],
[width + a, height - b],
Expand Down

0 comments on commit e1beb01

Please sign in to comment.