└── lanedetectionproject.py /lanedetectionproject.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def region_of_interest(img, vertices): 5 | """ 6 | Applies an image mask. 7 | 8 | Only keeps the region of the image defined by the polygon 9 | formed from `vertices`. The rest of the image is set to black. 10 | """ 11 | mask = np.zeros_like(img) 12 | cv2.fillPoly(mask, vertices, 255) 13 | masked_image = cv2.bitwise_and(img, mask) 14 | return masked_image 15 | 16 | def draw_lines(img, lines, color=[255, 0, 0], thickness=5): 17 | """ 18 | Draws `lines` with `color` and `thickness`. 19 | Lines are drawn on the image inplace (mutates the image). 20 | """ 21 | for line in lines: 22 | for x1, y1, x2, y2 in line: 23 | cv2.line(img, (x1, y1), (x2, y2), color, thickness) 24 | 25 | def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap): 26 | """ 27 | `img` should be the output of a Canny transform. 28 | 29 | Returns an image with hough lines drawn. 30 | """ 31 | lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap) 32 | return lines 33 | 34 | def fit_lines(lines, y_min, y_max): 35 | """ 36 | Fit lines to the detected lane markings. 37 | 38 | Returns two lines representing left and right lanes. 39 | """ 40 | left_lines = [] 41 | right_lines = [] 42 | for line in lines: 43 | for x1, y1, x2, y2 in line: 44 | slope = (y2 - y1) / (x2 - x1) 45 | if slope < 0: 46 | left_lines.append((x1, y1)) 47 | left_lines.append((x2, y2)) 48 | else: 49 | right_lines.append((x1, y1)) 50 | right_lines.append((x2, y2)) 51 | 52 | if left_lines and right_lines: 53 | left_points = np.array(left_lines) 54 | right_points = np.array(right_lines) 55 | 56 | left_coeffs = np.polyfit(left_points[:, 1], left_points[:, 0], 1) 57 | right_coeffs = np.polyfit(right_points[:, 1], right_points[:, 0], 1) 58 | 59 | left_lane = np.poly1d(left_coeffs) 60 | right_lane = np.poly1d(right_coeffs) 61 | 62 | left_line = [(int(left_lane(y_min)), int(y_min)), (int(left_lane(y_max)), int(y_max))] 63 | right_line = [(int(right_lane(y_min)), int(y_min)), (int(right_lane(y_max)), int(y_max))] 64 | 65 | return left_line, right_line 66 | else: 67 | return None, None 68 | 69 | def process_frame(frame): 70 | """ 71 | Apply lane detection on a single frame. 72 | """ 73 | # Convert image to HLS color space 74 | hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS) 75 | # Apply color thresholding to detect white and yellow lanes 76 | lower_white = np.array([0, 200, 0], dtype=np.uint8) 77 | upper_white = np.array([255, 255, 255], dtype=np.uint8) 78 | mask_white = cv2.inRange(hls, lower_white, upper_white) 79 | 80 | lower_yellow = np.array([10, 0, 100], dtype=np.uint8) 81 | upper_yellow = np.array([40, 255, 255], dtype=np.uint8) 82 | mask_yellow = cv2.inRange(hls, lower_yellow, upper_yellow) 83 | 84 | # Combine the masks 85 | mask = cv2.bitwise_or(mask_white, mask_yellow) 86 | 87 | # Apply Gaussian blur 88 | blur = cv2.GaussianBlur(mask, (5, 5), 0) 89 | 90 | # Define region of interest 91 | height, width = frame.shape[:2] 92 | vertices = np.array([[(0, height), (width / 2, height / 2), (width, height)]], dtype=np.int32) 93 | roi = region_of_interest(blur, vertices) 94 | 95 | # Apply Canny edge detection 96 | edges = cv2.Canny(roi, 50, 150) 97 | 98 | # Apply Hough transform to detect lines 99 | lines = hough_lines(edges, rho=1, theta=np.pi/180, threshold=15, min_line_len=40, max_line_gap=20) 100 | 101 | # Fit lines to the detected lane markings 102 | left_line, right_line = fit_lines(lines, height * 0.6, height) 103 | 104 | # Create a blank image to draw lines on 105 | line_img = np.zeros((frame.shape[0], frame.shape[1], 3), dtype=np.uint8) 106 | 107 | # Draw the lane lines 108 | if left_line is not None and right_line is not None: 109 | cv2.line(line_img, left_line[0], left_line[1], [0, 0, 255], 10) 110 | cv2.line(line_img, right_line[0], right_line[1], [0, 0, 255], 10) 111 | 112 | # Overlay lines on original frame 113 | result = cv2.addWeighted(frame, 0.8, line_img, 1, 0) 114 | 115 | return result 116 | 117 | # Open video file 118 | cap = cv2.VideoCapture('input_video.mp4') 119 | 120 | # Process each frame 121 | while cap.isOpened(): 122 | ret, frame = cap.read() 123 | if not ret: 124 | break 125 | 126 | # Process frame 127 | processed_frame = process_frame(frame) 128 | 129 | # Display processed frame 130 | cv2.imshow('Lane Detection', processed_frame) 131 | 132 | # Press 'q' to quit 133 | if cv2.waitKey(1) & 0xFF == ord('q'): 134 | break 135 | 136 | # Release resources 137 | cap.release() 138 | cv2.destroyAllWindows() 139 | --------------------------------------------------------------------------------