檢測車道線——2.選擇興趣區域 Region Masking


  通過簡單的顏色選擇,我們設法消除了圖像中除了車道線以外的幾乎所有內容。但是,在這一點上,自動提取確切的線條仍然非常棘手,因為我們仍然在周邊檢測到了一些不是線條線的其他物體。

  在這種情況下,我將假定拍攝圖像的前置攝像頭安裝在汽車的固定位置,這樣車道線總是會出現在圖像的同一區域。 所以在提取行車線的時候,只關注這個梯形區域內的圖像,可以避免其他區域的信息造成干擾。這個梯形區域如果選取地太大,則會引入更多無關信息(比如護欄,樹木等),如果梯形區域選取太小,則可能看不見行車線,所以這里需要權衡。接下來,我將通過添加一個標准來考慮這一點,僅在我們期望找到車道線的區域考慮用於顏色選擇的像素。

  看看下面的代碼。 變量left_bottom,right_bottom和apex代表了我想保留用於顏色選擇的三角形區域的頂點,同時掩蓋了其他所有內容。 在這里,我使用三角形面具來說明最簡單的情況,但稍后您將使用四邊形,原則上可以使用任何多邊形。numpy.polyfit(x,y,n)是用於多項式求過已知點的表達式,其中x為源數據點對應的橫坐標,可為行向量、矩陣,y為源數據點對應的縱坐標,可為行向量、矩陣,n為你要擬合的階數,一階直線擬合,二階拋物線擬合,並非階次越高越好,看擬合情況而定。 

 1  import matplotlib.pyplot as plt
 2  import matplotlib.image as mpimg
 3  import numpy as np
 4  
 5  # Read in the image and print some stats
 6  image = mpimg.imread('E:/spyder/a/a/test.jpg')
 7  print('This image is: ', type(image), 
 8           'with dimensions:', image.shape)
 9   # Pull out the x and y sizes and make a copy of the image
10  ysize = image.shape[0]
11  xsize = image.shape[1]
12  region_select = np.copy(image)
13  
14  # Define a triangle region of interest 
15  # Keep in mind the origin (x=0, y=0) is in the upper left in image processing
16  # Note: if you run this code, you'll find these are not sensible values!!
17  # But you'll get a chance to play with them soon in a quiz 
18  left_bottom = [0, 539]
19  right_bottom = [900, 300]
20  apex = [400, 0]
21  
22  # Fit lines (y=Ax+B) to identify the  3 sided region of interest
23  # np.polyfit() returns the coefficients [A, B] of the fit
24  fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)  
25  fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
26  fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)
27  
28  # Find the region inside the lines
29  XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
30  region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \
31                      (YY > (XX*fit_right[0] + fit_right[1])) & \
32                      (YY < (XX*fit_bottom[0] + fit_bottom[1]))
33  
34  # Color pixels red which are inside the region of interest
35  region_select[region_thresholds] = [255, 0, 0]
36  
37  # Display the image
38  plt.imshow(region_select)

若19~21的代碼改為如下:
19 left_bottom = [0, 540] 20 right_bottom = [900, 540] 21 apex = [400, 300]
則為:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM