1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| def detect_lane(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) edges = cv2.Canny(blur, 50, 150) height, width = edges.shape mask = np.zeros_like(edges) polygon = np.array([[ (0, height), (width, height), (width // 2, height // 2) ]], np.int32) cv2.fillPoly(mask, polygon, 255) masked_edges = cv2.bitwise_and(edges, mask) lines = cv2.HoughLinesP( masked_edges, 1, np.pi/180, 50, minLineLength=50, maxLineGap=150 ) line_image = np.zeros_like(image) if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 5) return cv2.addWeighted(image, 0.8, line_image, 1, 0)
|