# multi_lane_detection **Repository Path**: WUYIP/multi_lane_detection ## Basic Information - **Project Name**: multi_lane_detection - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-01-22 - **Last Updated**: 2020-12-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Multi Lane Line Detection using Python and OpenCV The project was designed based on the site's single lane detection. If you want to know detail single lane detection, click [Single_Lane_detection](https://github.com/tatsuyah/Lane-Lines-Detection-Python-OpenCV) ## How to use command : python3 lane_v3.py ## Color filter and edge detector ```python import cv2 import numpy as np def Color_filtering(image): img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) lower_yellow = np.array([20, 100, 100], dtype= "uint8") upper_yellow = np.array([30, 255, 255], dtype="uint8") mask_yellow = cv2.inRange(img_hsv, lower_yellow, upper_yellow) mask_white = cv2.inRange(gray_image, 200, 255) mask_yw = cv2.bitwise_or(mask_white, mask_yellow) mask_yw_image = cv2.bitwise_and(gray_image, mask_yw) return mask_yw_image def detect_edge(masked): frame = cv2.cvtColor(masked, cv2.COLOR_BGR2GRAY) thresh = 200 frame = cv2.threshold(frame, thresh, 255, cv2.THRESH_BINARY)[1] blurred =cv2.GaussianBlur(frame, (11, 11), 0) edged = cv2.Canny(blurred, 20, 150) return edged ``` ## histogram ```python import numpy as np import cv2 def get_histogram(binary_warped): histogram = np.sum(binary_warped[binary_warped.shape[0] // 2:, :], axis=0) return histogram ``` ## Multi lane classifier ```python import cv2 import numpy as np def multi_lane_classifier(histogram, threshold): hist_index_info = np.where(histogram>threshold) hist_index = hist_index_info[0] new_arr = hist_index.copy() new_arr = np.zeros_like(new_arr) for i in range(len(hist_index)): if i > 0 and i < new_arr.shape[0]: new_arr[i] = hist_index[i] - hist_index[i - 1] pixel_index = np.where(new_arr > 100) lane_num = len(pixel_index[0] ) + 1 print(pixel_index[0]) print(lane_num) return lane_num, pixel_index, hist_index ``` ## where am I ```python import cv2 import numpy as np # midpoint = 10 # base_line1 = 4 # base_line2 = 8 # base_line3 = 12 def where_am_I_4(midpoint, base_line1, base_line2, base_line3, base_line4): distance_1 = abs(midpoint - base_line1) distance_2 = abs(midpoint - base_line2) distance_3 = abs(midpoint - base_line3) distance_4 = abs(midpoint - base_line4) where_list = np.array([distance_1, distance_2, distance_3, distance_4]) # step_1 = np.where(where_list == where_list.min(), 250, where_list) step_1 = np.argmin(where_list) where_list[step_1] = 1200 step_2 = np.argmin(where_list) where_list[step_2] = 1200 min_index = np.where(where_list ==1200) if (step_1 == 0 and step_2 == 1) or (step_1 == 1 and step_2 == 0): text0 = "Where am I : Lane Num 1" return text0 if (min_index[0][0] == 1 and min_index[0][1] == 2) or (min_index[0][0] == 2 and min_index[0][1] == 1): text1 = "Where am I : Lane Num 2" return text1 if (step_1 == 2 and step_2 == 3) or (step_1 == 3 and step_1 == 2): text2 = "Where am I : Lane Num 3" return text2 def where_am_I_3(midpoint, base_line1, base_line2, base_line3): distance_1 = abs(midpoint - base_line1) distance_2 = abs(midpoint - base_line2) distance_3 = abs(midpoint - base_line3) where_list = np.array([distance_1, distance_2, distance_3]) # step_1 = np.where(where_list == where_list.min(), 250, where_list) step_1 = np.argmin(where_list) where_list[step_1] = 1200 step_2 = np.argmin(where_list) where_list[step_2] = 1200 min_index = np.where(where_list ==1200) if (step_1 == 0 and step_2 == 1) or (step_1 == 1 and step_2 == 0): text0 = "Where am I : Lane Num 1" return text0 if (min_index[0][0] == 1 and min_index[0][1] == 2) or (min_index[0][0] == 2 and min_index[0][1] == 1): text1 = "Where am I : Lane Num 2" return text1 # text = where_am_I_3(midpoint, base_line1, base_line2, base_line3) # print(text) ```