|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +def canny(img): |
| 5 | + if img is None: |
| 6 | + cap.release() |
| 7 | + cv2.destroyAllWindows() |
| 8 | + exit() |
| 9 | + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 10 | + kernel = 5 |
| 11 | + blur = cv2.GaussianBlur(gray,(kernel, kernel),0) |
| 12 | + canny = cv2.Canny(gray, 50, 150) |
| 13 | + return canny |
| 14 | + |
| 15 | +def region_of_interest(canny): |
| 16 | + height = canny.shape[0] |
| 17 | + width = canny.shape[1] |
| 18 | + mask = np.zeros_like(canny) |
| 19 | + triangle = np.array([[ |
| 20 | + (200, height), |
| 21 | + (800, 350), |
| 22 | + (1200, height),]], np.int32) |
| 23 | + cv2.fillPoly(mask, triangle, 255) |
| 24 | + masked_image = cv2.bitwise_and(canny, mask) |
| 25 | + return masked_image |
| 26 | + |
| 27 | +def houghLines(cropped_canny): |
| 28 | + return cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100, |
| 29 | + np.array([]), minLineLength=40, maxLineGap=5) |
| 30 | + |
| 31 | +def addWeighted(frame, line_image): |
| 32 | + return cv2.addWeighted(frame, 0.8, line_image, 1, 1) |
| 33 | + |
| 34 | +def display_lines(img,lines): |
| 35 | + line_image = np.zeros_like(img) |
| 36 | + if lines is not None: |
| 37 | + for line in lines: |
| 38 | + for x1, y1, x2, y2 in line: |
| 39 | + cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),10) |
| 40 | + return line_image |
| 41 | + |
| 42 | +def make_points(image, line): |
| 43 | + slope, intercept = line |
| 44 | + y1 = int(image.shape[0]) |
| 45 | + y2 = int(y1*3.0/5) |
| 46 | + x1 = int((y1 - intercept)/slope) |
| 47 | + x2 = int((y2 - intercept)/slope) |
| 48 | + return [[x1, y1, x2, y2]] |
| 49 | + |
| 50 | +def average_slope_intercept(image, lines): |
| 51 | + left_fit = [] |
| 52 | + right_fit = [] |
| 53 | + if lines is None: |
| 54 | + return None |
| 55 | + for line in lines: |
| 56 | + for x1, y1, x2, y2 in line: |
| 57 | + fit = np.polyfit((x1,x2), (y1,y2), 1) |
| 58 | + slope = fit[0] |
| 59 | + intercept = fit[1] |
| 60 | + if slope < 0: |
| 61 | + left_fit.append((slope, intercept)) |
| 62 | + else: |
| 63 | + right_fit.append((slope, intercept)) |
| 64 | + left_fit_average = np.average(left_fit, axis=0) |
| 65 | + right_fit_average = np.average(right_fit, axis=0) |
| 66 | + left_line = make_points(image, left_fit_average) |
| 67 | + right_line = make_points(image, right_fit_average) |
| 68 | + averaged_lines = [left_line, right_line] |
| 69 | + return averaged_lines |
| 70 | + |
| 71 | +cap = cv2.VideoCapture("test1.mp4") |
| 72 | +while(cap.isOpened()): |
| 73 | + _, frame = cap.read() |
| 74 | + canny_image = canny(frame) |
| 75 | + cropped_canny = region_of_interest(canny_image) |
| 76 | + # cv2.imshow("cropped_canny",cropped_canny) |
| 77 | + |
| 78 | + lines = houghLines(cropped_canny) |
| 79 | + averaged_lines = average_slope_intercept(frame, lines) |
| 80 | + line_image = display_lines(frame, averaged_lines) |
| 81 | + combo_image = addWeighted(frame, line_image) |
| 82 | + cv2.imshow("result", combo_image) |
| 83 | + |
| 84 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 85 | + break |
| 86 | + |
| 87 | +cap.release() |
| 88 | +cv2.destroyAllWindows() |
| 89 | + |
0 commit comments