Udacity 自駕車 project 1, 2
Project 1: Lance Lines Detection & Project 2: Advanced Lane Lines Detection
Project 1: Finding-Lane-Lines
Github: Project1_Finding-Lane-Lines
在第一個專案,我們使用了python+OpenCV來做道路線的偵測
這邊主要會用到OpenCV裡的兩個function: Canny + Hough Transform
- Canny edge detector: Canny的輸入為灰階圖像(grayscale之後),輸出將是稱為edges圖像。兩個threshold, low_threshold和high_threshold則是邊緣檢測的門檻值。
edges = cv2.Canny(gray, low_threshold, high_threshold)
As far as a ratio of
low_threshold
tohigh_threshold
, John Canny himself recommended a low to high ratio of 1:2 or 1:3.
透過Canny edge圖從左圖轉變成右圖
2. Hough Transform
再來是用Hough Transform來畫出圖裡的道路線段
透過Canny形成許多圖中的點,再以Hough Transform將照片中的各個直線上的pixels轉換成相交於同一個點的許多直線
# Define the Hough transform parameters
# Make a blank the same size as our image to draw on
rho = 1
theta = np.pi/180
threshold = 1
min_line_length = 10
max_line_gap = 1
line_image = np.copy(image)*0 #creating a blank to draw lines onlines = cv2.HoughLinesP(masked_edges, rho, theta, threshold,
np.array([]),min_line_length, max_line_gap)
左圖經過parameters optimized and region masked形成右圖
Pipeline五步驟程式碼:
最後結果以影片呈現:
Project 2: Advanced-Lane-Finding
Github: Project2_Advanced-Lane-Finding
第二個專案是更進階的OpenCV的應用:包含
- Camera Calibration, Image Distortion correction
- 更進階道路偵測(sobel), Color and Gradient threshold設定
- Perspective Transform(透視變換)
------------------------------------------------------------------------------
- Camera Calibration, Image Distortion correction: 攝影機校正
使用攝影機會帶來一些魚眼效果的問題(image distortion),於是我們就用下列方法來解決這些問題:
(1) Camera Calibration, Image Distortion correction:
解決畫面扭曲失真的問題,會造成我們這專案要偵測的道路扭曲,因此透過OpenCV的calibrateCamera, undistort來解決。
#Finding chessboard corners (for an 8x6 board):ret, corners = cv2.findChessboardCorners(gray, (8,6), None)#Drawing detected corners on an image:img = cv2.drawChessboardCorners(img, (8,6), corners, ret)#Camera calibration, given object points, image points, and the shape of the grayscale image:ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)#Undistorting a test image:dst = cv2.undistort(img, mtx, dist, None, mtx)
<解決了相機畫面失真扭曲問題>
2. 更進階道路偵測(sobel), Color and Gradient threshold設定
上個專案直接使用Canny edge detection來偵測,但會像圖一一樣偵測到很多非道路相關的線,因此我們需要用其他的技巧來找到車道線邊緣。
(1) 使用Sobel operator得到如下的結果:
針對gradient 的 magnitude & direction 做參數調整,得到更好的道路偵測結果並將兩者合併得到下圖(大):
(2) Color (HLS) and Gradient 門檻值設定:
(1) & (2) 結合: 順利找到明顯的車道線
Combined color and gradient thresholds for lane detection
3. Perspective Transform (透視變換):
再來是從下左圖來看,道路會有點彎曲甚至交集,但其實兩條路線是平行的,因此我們要用Perspective Transform來解決這個問題。
簡單來說就是將原本有點彎曲的道路轉成bird’s-eye view的形式,得到下兩張圖,可以看出不管是直線還是轉彎線,均能從Perspective Transform之後看出左右的道路線依然是平行的。
在操作完前面全部的步驟之後,可濾出清晰的道路線如下圖
最後用Sliding window來找出左右兩條線的pixel 再分別去Fit一個二次多項式後(丟進np.polyfit),我們可以找到一個圓弧,也就偵測出左右兩條曲線了
當我們找到道路線之後,在下一個Frame就不用再使用Sliding window來找左右兩條線了,直接在如下圖的line margin內尋找道路線能使pipeline運作更快
最後結果如下: