詳解車道線檢測算法之傳統(tǒng)圖像處理
透視變換
透視變換是將成像投影到一個新的視平面(鳥瞰圖)。車載相機拍攝到的圖像中,由于受相機角度和高度的影響往往會將平行的車道線拍成梯形,這樣的圖像會給后續(xù)檢測帶來較大難度,所以需要我們對原圖進行透視變換,從而得到一張鳥瞰圖。
具體思路是,先讀取兩組坐標(例如原圖像的四點坐標和目標圖像的四點坐標),計算變換矩陣;然后根據(jù)變換矩陣對圖像進行透視變換,并輸出到目標圖像。
相機標定
由于一般相機都多少存在畸變,而自動駕駛需要準確的車道曲率以執(zhí)行正確的方向控制,因此需要對相機進行校正。首先讀取兩個坐標數(shù)組,計算變換矩陣;然后根據(jù)變換矩陣對原圖進行透視變換,并輸出到目標畫布。
確定相機內參和畸變參數(shù)的過程就叫做相機標定。一般只需要一幅圖像(國際象棋棋盤圖像較為常用)就可以進行相機標定。標定時只需要從不同角度拍攝棋盤圖案,通過檢測棋盤圖案中的角點,得到角點像素,并且我們已知真實世界中的對象角點坐標 ,便可進行相機校正。若要進行精確的標定,通常從不同角度對同一棋盤拍攝多張照片(10-20張)。
直方圖濾波
簡單來說,就是沿著X軸統(tǒng)計每列像素的數(shù)值,并用直方圖表示。其中峰值位置的x坐標則對應左右兩側的車道線。通過這種方式,來獲取車道線位置信息。
圖片來自Udacity無人駕駛課程
初級車道線檢測算法--直線
顏色閾值處理
讀取圖片,對顏色RGB分別設置閾值,將高于所設閾值的像素(較亮的白色部分)分離出來。# 讀取圖片image = mpimg.imread('test.jpg')ysize = image.shape[0]xsize = image.shape[1]color_select = np.copy(image)
# 對RGB通道分別設置閾值red_threshold = 200 #Tuninggreen_threshold = 200 #Tuningblue_threshold = 200 #Tuning
# 將所設閾值以下的任何像素都設置為0rgb_threshold = [red_threshold, green_threshold, blue_threshold]thresholds = (image[:,:,0] < rgb_threshold[0]) | (image[:,:,1] < rgb_threshold[1]) | (image[:,:,2] < rgb_threshold[2])
color_select[thresholds] = [0,0,0]
提取ROIPython# 首先定義一個空圖像mask = np.zeros_like(image)
# 設置ROI區(qū)域坐標(任意多邊形)vertices = np.a(chǎn)rray([[image.shape[1]*.12, image.shape[0]], [image.shape[1]*.25,(image.shape[0]+image.shape[0]*.65)/2], [image.shape[1]*.42, image.shape[0]*.63], [image.shape[1]*.6,image.shape[0]*.63], [image.shape[1]*.8,(image.shape[0]+image.shape[0]*.65)/2], [image.shape[1]*.95,image.shape[0]]]) #Tuning
# 設置顏色通道,并填充ROIif len(image.shape) > 2: channel_count = image.shape[2] # i.e. 3 or 4 depending on your image ignore_mask_color = (255,) * channel_countelse: ignore_mask_color = 255
cv2.fillPoly(mask, vertices, ignore_mask_color)
# 合并得到ROI,其余部分為零masked_image = cv2.bitwise_and(image, mask)
Canny邊緣檢測
# 將圖像轉換為灰度圖gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# 進行高斯模糊,去除噪聲kernel_size = 3 #Tuningblur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size), 0) # 參數(shù)kernel_size可以是任何奇數(shù)
# 計算梯度(Canny邊緣檢測)low_threshold = 50 #Tuninghigh_threshold = 150 #Tuningedges = cv2.Canny(gray,low_threshold,higy_threshold) # 參數(shù):灰度圖,低閾值,高閾值。輸出為邊緣圖。
霍夫變換
rho = 2 theta = np.pi/180 threshold = 15 min_line_length = 50 max_line_gap = 20
# rho和theta是霍夫空間的距離和夾角# threshold是通過同一交點的曲線個數(shù)最小值# min_line_length是直線的最小像素長度# max_line_gap是線與線之間的最大像素長度。輸出為多條直線
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.a(chǎn)rray([]), min_line_length, max_line_gap)
請輸入評論內容...
請輸入評論/評論長度6~500個字
圖片新聞