def draw_lane_polygon(img): offset_from_lane_edge = 20 color = Lane.COLORS['region_stable'] if not Lane.lines_exist(): return img # Polygon points p1 = [Lane.left_line.x1, Lane.left_line.y1] p2 = [ Lane.left_line.get_x_coord(Lane.left_line.y2 + offset_from_lane_edge), Lane.left_line.y2 + offset_from_lane_edge ] p3 = [ Lane.right_line.get_x_coord(Lane.left_line.y2 + offset_from_lane_edge), Lane.right_line.y2 + offset_from_lane_edge ] p4 = [Lane.right_line.x1, Lane.right_line.y1] polygon_points = np.array([p1, p2, p3, p4], np.int32).reshape((-1, 1, 2)) if not Lane.left_line.stable or not Lane.right_line.stable: color = Lane.COLORS['region_unstable'] poly_img = np.zeros_like(img) cv2.fillPoly(poly_img, [polygon_points], color) return weighted_img(img, poly_img)
def draw_dashboard(img, snapshot1, snapshot2): # TODO: refactor this if not Lane.lines_exist(): return img cv2.CV_FILLED = -1 image_copy = deepcopy(img) cv2.rectangle(image_copy, (0, 0), (540, 175), (0, 0, 0), cv2.CV_FILLED) img = weighted_img(image_copy, img, α=0.3, β=0.7) img[20:155, 20:260, :] = snapshot1 img[20:155, 280:520, :] = snapshot2 return img
def update_lane(segments, image): if segments is not None: left = [ segment for segment in segments if segment.lane_line == 'left_line' ] right = [ segment for segment in segments if segment.lane_line == 'right_line' ] if not Lane.lines_exist(): Lane.left_line = Lane(left) Lane.right_line = Lane(right) Lane.update_vanishing_point(Lane.left_line, Lane.right_line) Lane.left_line.update_lane_line([l for l in left if l.candidate], image) Lane.right_line.update_lane_line([r for r in right if r.candidate], image)
def image_pipeline(image): """ Main image pipeline with 3 phases: * Raw image preprocessing and noise filtering; * Lane lines state update with the information gathered in preprocessing phase; * Drawing updated lane lines and other objects on image. """ ### Phase 1: Image Preprocessing hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) binary_mask = get_lane_lines_mask(hsv_image, [WHITE_LINES, YELLOW_LINES]) masked_image = draw_binary_mask(binary_mask, hsv_image) masked_image = gaussian_blur(masked_image, 7) blank_image = np.zeros_like(image) edges_mask = canny(masked_image, 280, 360) if not Lane.lines_exist(): edges_mask = region_of_interest(edges_mask, ROI_VERTICES) edges_image = draw_canny_edges(edges_mask, blank_image) segments = hough_line_transform(edges_mask, 1, math.pi / 180, 5, 5, 8) ### Stage 2: Lane lines state update # was (segments, image) update_lane(segments, edges_image) ### Stage 3: Drawing # Snapshot 1 out_snap1 = np.zeros_like(image) out_snap1 = draw_binary_mask(binary_mask, out_snap1) # plt.imshow(out_snap1) # plt.show() out_snap1 = draw_filtered_lines(segments, out_snap1) snapshot1 = cv2.resize(deepcopy(out_snap1), (240, 135)) # plt.imshow(out_snap1) # plt.imshow(snapshot1) # plt.show() # Snapshot 2 out_snap2 = np.zeros_like(image) out_snap2 = draw_canny_edges(edges_mask, out_snap2) # plt.imshow(out_snap2) # plt.show() out_snap2 = draw_points(Lane.left_line.points, out_snap2, Lane.COLORS['left_line']) out_snap2 = draw_points(Lane.right_line.points, out_snap2, Lane.COLORS['right_line']) out_snap2 = draw_lane_polygon(out_snap2) snapshot2 = cv2.resize(deepcopy(out_snap2), (240, 135)) # plt.imshow(snapshot2) # plt.show() # Augmented image output = deepcopy(image) output = draw_lane_lines([Lane.left_line, Lane.right_line], output, shade_background=True) output = draw_dashboard(output, snapshot1, snapshot2) return output