|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +# 1. convert the image to gray scale |
| 6 | +# 2. blur the image |
| 7 | +# 3. detect the edges |
| 8 | +# 4. create a mask |
| 9 | +# 5. apply the mask to the image |
| 10 | +# 6. detect the lines |
| 11 | +# 7. average the lines |
| 12 | +# 8. display the lines |
| 13 | + |
| 14 | + |
| 15 | +def make_coordinate(image, line_parameters): |
| 16 | + slope, intercept = line_parameters |
| 17 | + y1 = image.shape[0] |
| 18 | + y2 = int(y1*(3/5)) |
| 19 | + x1 = int((y1-intercept)/slope) |
| 20 | + x2 = int((y2-intercept)/slope) |
| 21 | + return np.array([x1, y1, x2, y2]) |
| 22 | + |
| 23 | + |
| 24 | +def average_lines_intercept(image, lines): |
| 25 | + left_fit = [] |
| 26 | + right_fit = [] |
| 27 | + for line in lines: |
| 28 | + x1, y1, x2, y2 = line.reshape(4) |
| 29 | + parameters = np.polyfit((x1, x2), (y1, y2), 1) |
| 30 | + slope = parameters[0] |
| 31 | + intercept = parameters[1] |
| 32 | + if slope < 0: |
| 33 | + left_fit.append((slope, intercept)) |
| 34 | + else: |
| 35 | + right_fit.append((slope, intercept)) |
| 36 | + left_fit_average = np.average(left_fit, axis=0) |
| 37 | + right_fit_average = np.average(right_fit, axis=0) |
| 38 | + left_line = make_coordinate(image, left_fit_average) |
| 39 | + right_line = make_coordinate(image, right_fit_average) |
| 40 | + return np.array([left_line, right_line]) |
| 41 | + |
| 42 | + |
| 43 | +def canny(image): |
| 44 | + gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) |
| 45 | + blur = cv2.GaussianBlur(gray, (5, 5), 0) |
| 46 | + canny = cv2.Canny(blur, 50, 150) |
| 47 | + return canny |
| 48 | + |
| 49 | + |
| 50 | +def display_lines(image, lines): |
| 51 | + line_image = np.zeros_like(image) |
| 52 | + if lines is not None: |
| 53 | + for x1, y1, x2, y2 in lines: |
| 54 | + cv2.line(line_image, (x1, y1), (x2, y2), (0, 0, 255), 10) |
| 55 | + return line_image |
| 56 | + |
| 57 | + |
| 58 | +def roi(image): |
| 59 | + height = image.shape[0] |
| 60 | + polygons = np.array([ |
| 61 | + [(200, height), (1100, height), (550, 250)] |
| 62 | + ]) |
| 63 | + mask = np.zeros_like(image) |
| 64 | + cv2.fillPoly(mask, polygons, 255) |
| 65 | + masked_image = cv2.bitwise_and(image, mask) |
| 66 | + return masked_image |
| 67 | + |
| 68 | + |
| 69 | +cap = cv2.VideoCapture("Finding_Lanes/video.mp4") |
| 70 | + |
| 71 | +while(cap.isOpened()): |
| 72 | + _, frame = cap.read() |
| 73 | + canny_image = canny(frame) |
| 74 | + cropped_image = roi(canny_image) |
| 75 | + lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, |
| 76 | + 100, np.array([]), minLineLength=40, maxLineGap=5) |
| 77 | + averaged_lines = average_lines_intercept(frame, lines) |
| 78 | + line_image = display_lines(frame, averaged_lines) |
| 79 | + combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1) |
| 80 | + cv2.imshow("result", combo_image) |
| 81 | + if cv2.waitKey(10) == ord('q'): |
| 82 | + break |
| 83 | + |
| 84 | +cap.release() |
| 85 | +cv2.destroyAllWindows() |
0 commit comments