1.顏色閾值+ 區(qū)域掩模
我們可以僅僅通過設(shè)置一些RGB通道閾值,來提取車道線。
以下的代碼設(shè)置了RGB通道閾值為220,大于220的像素將設(shè)置為黑色,這樣可以將測(cè)試圖片中的車道線提取出來
效果如下
我們發(fā)現(xiàn)符合閾值的像素既包括了車道線,也包含了其他非車道線部分。
顯然,一個(gè)成熟的自動(dòng)駕駛感知算法,是不可能使用這種方法的。僅僅依靠顏色,既不科學(xué)也不魯棒。
有一種改進(jìn)思路是利用圖像掩模的方法
假設(shè)拍攝圖像的前置攝像頭安裝在汽車上的固定位置,這樣車道線將始終出現(xiàn)在圖像的相同區(qū)域中。我們將設(shè)置了一個(gè)區(qū)域,認(rèn)為車道線處于該區(qū)域內(nèi)。
我們?cè)O(shè)置了一個(gè)三角形的區(qū)域,原則上你可以使用其他形狀
![圖
python代碼如下
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read in the image
image = mpimg.imread('test.jpg')
# Grab the x and y sizes and make two copies of the image
# With one copy we'll extract only the pixels that meet our selection,
# then we'll paint those pixels red in the original image to see our selection
# overlaid on the original.
ysize = image.shape[0]
xsize = image.shape[1]
color_select= np.copy(image)
line_image = np.copy(image)
# Define our color criteria
red_threshold = 220
green_threshold = 220
blue_threshold = 220
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
# Define a triangle region of interest (Note: if you run this code,
left_bottom = [0, ysize-1]
right_bottom = [xsize-1, ysize-1]
apex = [650, 400]
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)
# Mask pixels below the threshold
color_thresholds = (image[:,:,0] < rgb_threshold[0]) |
(image[:,:,1] < rgb_threshold[1]) |
(image[:,:,2] < rgb_threshold[2])
# Find the region inside the lines
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) &
(YY > (XX*fit_right[0] + fit_right[1])) &
(YY < (XX*fit_bottom[0] + fit_bottom[1]))
# Mask color selection
color_select[color_thresholds] = [0,0,0]
# Find where image is both colored right and in the region
line_image[~color_thresholds & region_thresholds] = [255,0,0]
# Display our two output images
plt.imshow(color_select)
plt.imshow(line_image)
# uncomment if plot does not display
plt.show()
-
RGB
+關(guān)注
關(guān)注
4文章
798瀏覽量
58510 -
檢測(cè)
+關(guān)注
關(guān)注
5文章
4488瀏覽量
91476 -
python
+關(guān)注
關(guān)注
56文章
4797瀏覽量
84690
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論